C++ Study Guide: Variable Scope, Lifetime, Functions, Loops, and File I/O

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/28

flashcard set

Earn XP

Description and Tags

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.

29 Terms

1
New cards

What does variable scope refer to in C++?

The part of a program where a variable or identifier can be accessed.

2
New cards

Where can a local variable be accessed?

Only inside the function or block where it is declared.

3
New cards

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.

4
New cards

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.

5
New cards

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.

6
New cards

What is the lifetime of an automatic variable?

It is created when its block is entered and destroyed when the block ends.

7
New cards

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.

8
New cards

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.

9
New cards

Which operator is used to access a global variable when a local variable shares the same name?

The scope resolution operator ::.

10
New cards

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.

11
New cards

What does a void return type signify in a function declaration?

It means the function does not return a value.

12
New cards

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.

13
New cards

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.

14
New cards

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 xx to the power yy, and fabs(x) returns the absolute value of a decimal.

15
New cards

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.

16
New cards

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.

17
New cards

What keyword is used to declare a constant whose value cannot be changed after initialization?

The const keyword.

18
New cards

What is the distinction between the operators << and >> in C++?

<< sends information to output (cout), while >> receives information from input (cin).

19
New cards

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.

20
New cards

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.

21
New cards

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.

22
New cards

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 (11 time).

23
New cards

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++).

24
New cards

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.

25
New cards

How does execution proceed in a nested loop?

The inner loop completes all of its repetitions each time the outer loop runs once.

26
New cards

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.

27
New cards

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.

28
New cards

How is a failure to open a file tested using an ifstream object named inFile?

By using the condition if (!inFile).

29
New cards

What is the output of the code segment int x = 5; cout << x++; and why?

It outputs 55 because post-increment displays the initial value of 55 first before incrementing xx to 66.