1/28
Comprehensive practice flashcards covering C++ fundamentals, including variable scope and lifetime, functions, parameter passing, math and character libraries, control structures, loops, and file stream I/O.
What does variable scope refer to in C++?
The part of a program where a variable or identifier can be accessed.
Where can a local variable be accessed?
Only inside the function or block where it is declared.
What is the scope and lifetime of a global variable?
It can be accessed by functions within its accessible program scope, and it exists throughout program execution.
What defines a block in C++, and what is the scope of a variable declared inside it?
A block is a section of code enclosed in curly braces { }, and a variable declared inside it can only be accessed within that block.
What is the fundamental difference between variable scope and variable lifetime?
Scope refers to where a variable can be used or accessed, whereas lifetime refers to how long a variable exists in memory.
What is the lifetime of an automatic variable?
It is created when its block is entered and destroyed when the block ends.
How does a static local variable behave regarding lifetime and initialization?
It is initialized only once, exists throughout program execution, and retains its value between function calls.
What is name hiding in C++?
A situation where a local variable has the same name as a global variable, causing the local variable to take priority inside its scope.
Which operator is used to access a global variable when a local variable shares the same name?
The scope resolution operator ::.
How do pass-by-value and pass-by-reference differ in parameter syntax and effect on the original variable?
Pass-by-value passes a copy with no & in the parameter, meaning modifications do not change the original variable; pass-by-reference uses & to reference the original variable directly, meaning modifications can change the original variable.
What does a void return type signify in a function declaration?
It means the function does not return a value.
In C++ function vocabulary, what is the difference between a prototype and a definition?
A prototype declares the function's name, return type, and parameters before it is used, while a definition contains the actual instructions for the function.
What is the difference between a parameter and an argument?
A parameter is the variable listed in the function definition, while an argument is the actual value passed to the function during a call.
What are the purposes of the <cmath> functions sqrt(x), pow(x, y), and fabs(x)?
sqrt(x) calculates the square root, pow(x, y) raises x to the power y, and fabs(x) returns the absolute value of a decimal.
What are the functions of toupper(ch), tolower(ch), isdigit(ch), and isalpha(ch) from the <cctype> library?
toupper(ch) converts a lowercase letter to uppercase, tolower(ch) converts an uppercase letter to lowercase, isdigit(ch) checks whether a character is a digit, and isalpha(ch) checks whether a character is a letter.
What kind of data does each basic C++ data type store: int, double, float, char, string, and bool?
int stores whole numbers, double and float store decimal numbers, char stores one character, string stores text, and bool stores true or false.
What keyword is used to declare a constant whose value cannot be changed after initialization?
The const keyword.
What is the distinction between the operators << and >> in C++?
<< sends information to output (cout), while >> receives information from input (cin).
What is the difference between = and == in C++?
= is the assignment operator that assigns a value to a variable, whereas == is an equality operator that checks whether two values are equal.
How do the logical operators &&, ||, and ! function in C++?
&& (AND) requires both conditions to be true, || (OR) requires at least one condition to be true, and ! (NOT) reverses a condition.
What happens if a break statement is omitted from a case inside a switch statement?
Execution continues sequentially into the following case or cases, a behavior known as fall-through.
When is the condition checked in a do-while loop, and how does this affect its minimum executions?
The condition is checked after the body runs, guaranteeing that the loop always executes at least once (1 time).
What are the three parts specified inside the parentheses of a for loop header?
Initialization (e.g., int i = 1), condition (e.g., i <= 5), and update (e.g., i++).
What is the difference between break and continue when used inside a loop?
break terminates and exits the entire loop, while continue skips the remainder of the current iteration and advances to the next iteration.
How does execution proceed in a nested loop?
The inner loop completes all of its repetitions each time the outer loop runs once.
What is a common programming error that leads to an infinite loop?
Forgetting to update the loop counter variable, preventing the loop condition from ever becoming false.
In the <fstream> library, what are the respective roles of ifstream, ofstream, and fstream?
ifstream reads information from a file, ofstream writes information to a file, and fstream can both read from and write to a file.
How is a failure to open a file tested using an ifstream object named inFile?
By using the condition if (!inFile).
What is the output of the code segment int x = 5; cout << x++; and why?
It outputs 5 because post-increment displays the initial value of 5 first before incrementing x to 6.