Week 3 - Gemini Flashcards

(24 cards)

1
Q

Explain the concept of a ‘block’ in C++ and its relation to variable scope.

A

A block is a sequence of statements enclosed in curly braces {}. Variables declared inside a block have ‘block scope’, meaning they only exist and are accessible within that block (and any nested blocks). They are created upon entering the block and destroyed upon exiting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between ‘function scope’ and ‘block scope’?

A

Function scope typically refers to labels (like for goto, though goto is discouraged), which are visible throughout the function. Block scope applies to variables declared within any {} block, limiting their existence and visibility to that block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe a situation where using a switch statement might be clearer or more efficient than a long chain of if-else if statements.

A

A switch statement is often clearer when you need to perform different actions based on the specific value of a single integer or character expression, especially when there are many possible constant values to check against. It directly jumps to the matching case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is the break statement crucial in most switch case blocks? What happens if it’s omitted?

A

break exits the switch statement after a case block is executed. If omitted, execution ‘falls through’ to the statements in the next case block, which is usually unintended behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When is a do-while loop guaranteed to execute its body at least once, unlike a while or for loop?

A

Because the condition in a do-while loop (do { statements } while (condition);) is checked after the loop body executes, ensuring the body runs at least one time, even if the condition is initially false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Contrast the typical use cases for for loops versus while loops.

A

for loops are often preferred when the number of iterations is known beforehand (e.g., iterating 0 to N-1, processing all elements of an array). while loops are generally better when the loop should continue as long as a condition holds true, but the number of iterations isn’t known in advance (e.g., reading data until end-of-file, repeating until user enters a specific value).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the difference in behavior between break and continue when used inside a loop.

A

break immediately terminates the entire loop, and execution resumes after the loop. continue skips the remainder of the current iteration only and proceeds to the next iteration’s condition check (and increment in a for loop).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you create a loop that runs indefinitely until a specific condition inside the loop triggers an exit?

A

You can use while(true) to create an infinite loop, and then use an if statement inside the loop to check for the exit condition, executing a break statement when that condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of ‘procedural abstraction’ when designing functions?

A

It means focusing on what a function does (its purpose and interface - parameters and return type) without needing to worry about the internal details of how it does it. This allows for modular design and makes code easier to use and maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the difference between a function parameter declared as int x (pass-by-value) and int& x (pass-by-reference).

A

With int x (pass-by-value), the function receives a copy of the argument’s value; changes inside the function don’t affect the original. With int& x (pass-by-reference), the function receives a reference (alias) to the original argument; changes inside the function do affect the original variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why might pass-by-reference (&) be preferred over pass-by-value for function parameters in certain situations?

A
  1. To allow the function to modify the original argument. 2. To avoid the overhead of copying large data structures (like complex objects or large arrays) when the function only needs to read them (though const& is better for read-only access).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What constitutes a function’s ‘signature’ in C++?

A

The function’s name combined with the number and types of its parameters. The return type is NOT part of the signature for overloading purposes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does the compiler resolve calls to overloaded functions (functions with the same name but different parameters)?

A

The compiler determines which version of the function to call based on the number and types of the arguments provided in the function call, matching them to the function signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you call an overloaded function with argument types that don’t exactly match any signature, what might the compiler try to do?

A

The compiler may attempt to perform implicit type conversions (casting) on the arguments to find a suitable match. If no unique match can be found after considering conversions, it will result in a compilation error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the key difference in the lifetime and initialization of a regular local variable versus a static local variable within a function?

A

A regular local variable is created and initialized every time the function is called and destroyed when the function exits. A static local variable is initialized only the first time the function is called and retains its value between calls, existing for the entire duration of the program’s execution after its first initialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe the process of splitting a C++ program into header (.h) and implementation (.cpp) files for a function. What goes where?

A

The header file (.h) contains the function declaration (prototype) and any necessary includes, type definitions, or constants related to the function’s interface. The implementation file (.cpp) contains the function definition (the actual code/body) and includes its own header file. The file calling the function includes the header file.

17
Q

What is the purpose of the <iomanip> library? Name two manipulators it provides.

A

It provides tools for formatting input and output streams. Examples: setw(n) to set the field width, setprecision(n) to set floating-point precision, setiosflags() to set formatting flags like ios::left or ios::fixed.

18
Q

What is the purpose of the <fstream> library?

A

It provides classes for file stream operations, allowing programs to read from and write to files. Key classes include ifstream (input), ofstream (output), and fstream (input/output).

19
Q

Explain the role of ofstream, ifstream, and the open() and close() methods in file handling.

A

ofstream is used to create output file streams (for writing). ifstream is used to create input file streams (for reading). The open(filename, mode) method associates the stream object with a physical file and specifies the mode (e.g., ios::out, ios::in, ios::app). The close() method disconnects the stream from the file.

20
Q

How can a program detect when it has reached the end of a file while reading?

A

Using the eof() member function of the ifstream object (e.g., inputFile.eof()). It returns true if a read operation has attempted to read past the end of the file.

21
Q

What is the difference between opening a file with ios::out versus ios::app?

A

ios::out (default for ofstream) opens the file for writing. If the file exists, its contents are discarded (truncated). If it doesn’t exist, it’s created. ios::app opens the file for appending; output is added to the end of the file without deleting existing content. If the file doesn’t exist, it’s created.

22
Q

What control structure is suitable for mapping a specific integer input (like a finger number 1-5) to a specific string output (like ‘Thumb’)? (Week 3 Problem 1)

A

A switch statement is ideal here, checking the integer input against case values 1 through 5 and outputting the corresponding name, with a default case for invalid input. An if-else if chain could also be used.

23
Q

How would you structure a program that repeatedly calculates the square of a user-entered number until the user decides to exit? (Week 3 Problem 3)

A

Use a loop that continues indefinitely (e.g., while(true) or for(;;)). Inside the loop: 1. Prompt the user for a number. 2. Read the number. 3. Calculate and display the square. 4. Ask the user if they want to continue (e.g., enter ‘y’ or ‘n’). 5. Read the user’s choice. 6. If the choice indicates exit (e.g., ‘n’), use break to exit the loop.

24
Q

What key components are needed to sum all odd integers within a range specified by the user (e.g., from start to end)? (Week 3 Problem 4)

A
  1. Input: Read start and end values from the user. 2. Initialization: Declare a sum variable and initialize it to 0 (int sum = 0;). 3. Loop: Use a for loop to iterate through numbers from start to end (for (int i = start; i <= end; ++i)). 4. Condition: Inside the loop, use an if statement to check if the current number i is odd (if (i % 2 != 0)). 5. Accumulation: If the number is odd, add it to the sum (sum += i;). 6. Output: After the loop, display the final sum.