1/37
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Describe the basic structure of a C++ program.
A basic C++ program consists of: 1) #include directives, 2) using namespace std;, 3) int main(), 4) variable declarations, 5) executable statements, 6) return 0;, and 7) the closing brace }. Execution begins inside main() and ends when return 0 executes.
What is the purpose of #include
The #include directive tells the compiler to include a library before compilation. The iostream library provides the definitions for cin, cout, endl, and other standard input/output features.
Why is using namespace std; commonly used?
It tells the compiler to use names from the standard namespace without requiring std:: before every object. Without it you would write std::cout, std::cin, and std::endl.
What is the main() function?
main() is the entry point of every C++ program. Program execution always begins inside main(). It is declared as int main() because it returns an integer value to the operating system.
Why is return 0; used at the end of main()?
return 0; tells the operating system that the program finished successfully. Although modern C++ allows omitting it, many instructors and compilers still expect it.
What is a variable?
A variable is a named memory location that stores a value which may change while a program executes. Variables must be declared before they are used.
What are the rules for naming variables?
A variable must begin with a letter or underscore, may contain letters, numbers, and underscores, cannot contain spaces or special symbols, and cannot use C++ keywords such as int, return, or if.
Compare the common C++ data types.
int stores whole numbers, double stores decimal numbers, char stores a single character inside single quotes, bool stores true or false, and string stores multiple characters inside double quotes.
What is the difference between declaring and initializing a variable?
Declaring creates the variable. Initializing gives it its first value. Example: int age; declares a variable, while int age = 18; both declares and initializes it.
Why should variables always be initialized?
Using an uninitialized variable produces garbage values and results in a logic error that can be difficult to locate because the compiler usually does not detect it.
What is an assignment statement?
An assignment statement stores a value into a variable using the = operator. Example: total = price * quantity; calculates the expression first and stores the result into total.
Explain the difference between = and ==.
The = operator assigns a value to a variable. The == operator compares two values for equality and returns either true or false. Accidentally using = inside an if statement is a common programming mistake.
What are keywords in C++?
Keywords are reserved words that already have a meaning in the language, such as int, if, else, while, return, double, and switch. They cannot be used as variable names.
Explain how cin works.
cin is the standard input stream. It uses the extraction operator (>>) to read values from the keyboard and store them into variables. Example: cin >> age;
Explain how cout works.
cout is the standard output stream. It uses the insertion operator (<<) to display text, variables, expressions, and manipulators to the screen.
What do the << and >> operators mean?
The << operator inserts data into an output stream, while the >> operator extracts data from an input stream. Think of << as sending information out and >> as bringing information in.
What is the purpose of endl and \n?
Both move output to a new line. \n inserts a newline character directly into the output, while endl also flushes the output buffer. For most programs, \n is slightly faster.
What are escape sequences?
Escape sequences begin with a backslash and represent special characters. Common ones include \n for a new line, \t for a tab, \ for a backslash, and \" for a quotation mark.
Explain how multiple values can be read with cin.
Multiple values can be read using a single cin statement. Example: cin >> x >> y >> z; The user enters the values separated by spaces or by pressing Enter.
Explain cascading output with cout.
Several pieces of output can be combined into one statement using multiple << operators. Example: cout << "Area = " << area << "\n"; This improves readability.
What is the difference between integer and floating-point division?
If both operands are integers, the decimal portion is discarded. Example: 5 / 3 equals 1. If at least one operand is a double, the result contains decimals. Example: 5.0 / 3 equals 1.66667.
What determines the type of an arithmetic expression?
If all operands are integers, the result is an integer. If any operand is a double, the entire expression is evaluated as a double.
What is the modulus operator (%)?
The modulus operator returns the remainder after integer division. Example: 17 % 5 equals 2. It only works with integer operands.
List the arithmetic operators in C++.
The arithmetic operators are + for addition, - for subtraction, * for multiplication, / for division, and % for remainder.
Explain operator precedence in arithmetic expressions.
C++ follows PEMDAS. Parentheses are evaluated first, then multiplication, division, and modulus, followed by addition and subtraction. Operators of equal precedence are evaluated from left to right.
What is type casting?
Type casting converts one data type into another. It is commonly used to force integer values to behave as doubles before division.
Why is static_cast
static_cast
What happens if integer division is assigned to a double?
The integer division occurs first, losing the decimal portion, and then the result is converted to a double. Example: double x = 5 / 2; stores 2.0 instead of 2.5.
What are the three major categories of programming errors?
Syntax errors violate C++ grammar and are detected by the compiler. Run-time errors occur while the program executes. Logic errors allow the program to run but produce incorrect results.
What is a syntax error?
A syntax error is a violation of C++ grammar, such as a missing semicolon, unmatched braces, misspelled keyword, or incorrect punctuation.
What is a run-time error?
A run-time error occurs while the program is executing. Examples include dividing by zero, opening a nonexistent file, or using invalid input.
What is a logic error?
A logic error occurs when the program compiles and runs but produces incorrect results because the algorithm is wrong.
Explain the roles of the compiler and linker.
The compiler translates source code into object code. The linker combines object code with precompiled libraries to create the executable program.
What is source code?
Source code is the original C++ program written by the programmer before compilation.
What is object code?
Object code is the machine-language version produced by the compiler before the linker creates the executable.
Why is indentation important in C++?
The compiler ignores most whitespace, but proper indentation greatly improves readability, debugging, and maintenance of programs.
Why should variable declarations appear near the beginning of a function?
Declaring variables near the beginning makes programs easier to read, easier to maintain, and helps prevent accidentally using undeclared variables.
What are good programming practices taught in early C++?
Use meaningful variable names, initialize variables, indent code consistently, use comments when appropriate, avoid magic numbers, separate problem solving from coding, and test programs thoroughly using multiple inputs.