Week 2 - Gemini Flashcards

(27 cards)

1
Q

What is the purpose of the const keyword when declaring a variable?

A

It specifies that the variable’s value cannot be altered after initialization during the program’s execution[cite: 5]. It’s used for values that should not change

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

What does an enum (enumeration) allow you to do in C++?

A

It allows you to define a set of named integer constants

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

Explain the difference between the assignment operator (=) and the equality comparison operator (==). Why is accidentally using = in an if condition problematic?

A

“The assignment operator (=) assigns the value on the right to the variable on the left[cite: 13, 460]. The equality operator (==) checks if the values on both sides are equal, returning true or false[cite: 665, 461]. Using = in an if condition (e.g., if (i = j)) assigns the value of j to i and then checks if that assigned value is non-zero (true). This leads to incorrect logic, as it doesn’t compare the original values[cite: 68].
“What is the purpose of compound assignment operators (e.g.

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

What is the purpose of relational operators (<, <=, >, >=, ==, !=) in C++?

A

They are used to compare two values. The result of the comparison is a boolean value (true or false)[cite: 664

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

What do the logical operators && (AND), || (OR), and ! (NOT) do?

A

&& returns true if both operands are true[cite: 32, 728]. || returns true if at least one operand is true[cite: 34, 728]. ! reverses the logical state of its operand (true becomes false, false becomes true)[cite: 36, 37].
“Which logical operator (&& or ||) has higher precedence? What should you do if unsure about evaluation order?”,”&& (AND) has higher precedence than || (OR)[cite: 41

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

What is the conditional ternary operator (? :) used for, and what is its basic structure?

A

“It provides a compact way to assign a value to a variable based on a condition. Structure: variable = (condition) ? value_if_true : value_if_false;[cite: 45, 46, 694].
“What is the fundamental purpose of flow control statements like if

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

In an if statement condition (e.g., if (expression)), how are non-boolean values typically interpreted?

A

“The value 0 is interpreted as false, and any non-zero value is interpreted as true[cite: 65].
“What is a ‘block’ of code in C++

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

What are the three main types of scope mentioned in the notes?

A

“Block scope (within {}), function scope (within a function), and global scope (accessible everywhere)[cite: 685].
“Describe the purpose and basic syntax of a switch statement.”,”It allows making multiple choices based on the value of a single expression (usually an integer or char). Syntax: switch (expression) { case constant1: // statements break; case constant2: // statements break; ... default: // statements }[cite: 69

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

What is the role of the break statement within a switch structure?

A

“It terminates the execution of the switch statement. Without break, execution ‘falls through’ to the next case’s statements[cite: 71, 73, 1606].
“What happens if none of the case constants in a switch statement match the expression’s value?”,”If a default: label exists

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

Describe the execution flow of a while loop.

A

“1. The condition is evaluated. 2. If the condition is true, the statement(s) inside the loop body are executed. 3. Control returns to step 1. If the condition is false, the loop terminates, and execution continues after the loop[cite: 84, 85, 86, 751].
“Describe the execution flow of a do-while loop and how it differs from a while loop.”,”1. The statement(s) inside the loop body are executed first. 2. Then

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

Describe the three components in the parentheses of a for loop: for (initialization; condition; increment).

A

“1. initialization: Executed once before the loop begins (e.g., int i = 0). 2. condition: Evaluated before each iteration. If true, the loop body executes; if false, the loop terminates (e.g., i < 10). 3. increment: Executed after each iteration (e.g., i++)[cite: 94, 95, 741, 742, 744].
“What is the effect of the continue statement inside a loop?”,”It immediately skips the rest of the current iteration’s loop body and proceeds to the next iteration (evaluating the loop’s condition again)[cite: 102

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

What is the effect of the break statement inside a loop?

A

“It immediately terminates the entire loop, and program execution continues with the statement immediately following the loop[cite: 99, 100, 1571, 1581].
“What does the exit() function do

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

What is the difference between a ‘value-returning’ function and a void function?

A

“A value-returning function computes and returns a single value of a specific type (e.g., double, int) using a return value; statement[cite: 113, 115, 118, 128]. A void function performs a task but does not return a value; its return type is specified as void[cite: 113, 136].
“What is a function ‘declaration’ (or ‘prototype’) and why is it needed?”,”A function declaration tells the compiler the function’s name

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

What is a function ‘definition’?

A

“The function definition provides the actual code (the function body, enclosed in {}) that executes when the function is called[cite: 1717]. It includes the function header (return type, name, parameters) and the implementation. Example: double compoundInterest(...) { /* code */ return interest; }[cite: 135].
“Explain ‘function overloading’. What distinguishes overloaded functions?”,”Function overloading allows multiple functions to share the same name

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

What is ‘recursion’ in the context of functions?

A

“Recursion is a technique where a function calls itself within its own definition[cite: 142, 2363].
“What are the two essential components of a correctly defined recursive function?”,”1. Base Case(s): A condition under which the function does not call itself

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

Explain the difference between ‘global’ and ‘local’ variables regarding their scope.

A

“Global variables are declared outside any function and can be accessed from anywhere in the program[cite: 167]. Local variables are declared inside a function or block and can only be accessed within that specific scope[cite: 167, 168].
“What happens to a normal local variable’s value between function calls?”,”A normal local variable is created and potentially initialized each time the function is called

17
Q

What does declaring a local variable as static achieve?

A

“A static local variable is initialized only the first time the function is called. It retains its value between subsequent calls to the function. A single instance exists for all calls[cite: 170, 171, 2310].
“When passing parameters to a function

18
Q

How do you set the output field width for the next item printed using cout, and which header is needed?

A

Use the setw(n) manipulator (where n is the width). The <iomanip> header is needed.

19
Q

How do you set the precision (number of digits after the decimal point for fixed or total significant digits for scientific) for floating-point output using cout?

A

Use the cout.precision(n) member function or the setprecision(n) manipulator (from <iomanip>).

20
Q

How do you achieve left-justified output within a specific field width using cout?

A

Include <iomanip> and use cout << setiosflags(ios::left); before outputting the item with setw().

21
Q

What header file is needed for file input/output operations?

A

<fstream>[cite: 1643].
“What class is typically used for writing data to a file? Give an example of creating an object.”,”ofstream. Example: ofstream myOutputFile;[cite: 1643].

22
Q

What class is typically used for reading data from a file?

A

ifstream.
“How do you open a file for writing using an ofstream object (e.g.

23
Q

Describe the core logic needed to calculate the factorial of a non-negative integer n using a for loop. (Sheet 1, Problem 3.1a)

A

Initialize a result variable (e.g., long long factorial = 1;). Use a for loop that iterates from 1 up to n (e.g., for (int i = 1; i <= n; ++i)). Inside the loop, multiply the result by the loop counter (factorial *= i;). Handle the base case n=0 where factorial is 1 separately or by loop design.

24
Q

Describe the core logic needed to calculate the factorial of a non-negative integer n using a while loop. (Sheet 1, Problem 3.1b)

A

Initialize result (long long factorial = 1;) and a counter (int i = n;). Use a while loop (while (i > 0)). Inside the loop, multiply the result by the counter (factorial *= i;) and decrement the counter (i--;). Handle n=0.

25
Describe the core logic needed to calculate the factorial of a non-negative integer `n` using a `do-while` loop. (Sheet 1, Problem 3.1c)
Initialize result (`long long factorial = 1;`) and counter (`int i = n;`). Use a `do-while` loop. Inside the `do` block, multiply result by counter (`factorial *= i;`) and decrement counter (`i--;`). The condition is `while (i > 0);`. Requires careful handling of `n=0` before the loop or inside.
26
Describe the core logic to calculate $x^n$ (where `x` is a number and `n` is a positive integer) using a `for` loop. (Sheet 1, Problem 3.2a)
Initialize result (e.g., `double result = 1.0;`). Use a `for` loop that iterates `n` times (e.g., `for (int i = 0; i < n; ++i)`). Inside the loop, multiply the result by `x` (`result *= x;`). Handle `n=0` (result is 1).
27
Which C++ library function calculates powers like $x^n$, and which header file is needed? (Sheet 1, Problem 3.2b)
"The `pow(x, n)` function. Header file: ``[cite: 110, 111, 462].