Week 2 - Gemini Flashcards
(27 cards)
What is the purpose of the const
keyword when declaring a variable?
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
What does an enum
(enumeration) allow you to do in C++?
It allows you to define a set of named integer constants
Explain the difference between the assignment operator (=
) and the equality comparison operator (==
). Why is accidentally using =
in an if
condition problematic?
“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.
What is the purpose of relational operators (<
, <=
, >
, >=
, ==
, !=
) in C++?
They are used to compare two values. The result of the comparison is a boolean value (true
or false
)[cite: 664
What do the logical operators &&
(AND), ||
(OR), and !
(NOT) do?
”&&
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
What is the conditional ternary operator (? :
) used for, and what is its basic structure?
“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
In an if
statement condition (e.g., if (expression)
), how are non-boolean values typically interpreted?
“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++
What are the three main types of scope mentioned in the notes?
“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
What is the role of the break
statement within a switch
structure?
“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
Describe the execution flow of a while
loop.
“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
Describe the three components in the parentheses of a for
loop: for (initialization; condition; increment)
.
“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
What is the effect of the break
statement inside a loop?
“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
What is the difference between a ‘value-returning’ function and a void
function?
“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
What is a function ‘definition’?
“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
What is ‘recursion’ in the context of functions?
“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
Explain the difference between ‘global’ and ‘local’ variables regarding their scope.
“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
What does declaring a local variable as static
achieve?
“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
How do you set the output field width for the next item printed using cout
, and which header is needed?
Use the setw(n)
manipulator (where n
is the width). The <iomanip>
header is needed.
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
?
Use the cout.precision(n)
member function or the setprecision(n)
manipulator (from <iomanip>
).
How do you achieve left-justified output within a specific field width using cout
?
Include <iomanip>
and use cout << setiosflags(ios::left);
before outputting the item with setw()
.
What header file is needed for file input/output operations?
”<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].
What class is typically used for reading data from a file?
“ifstream
.
“How do you open a file for writing using an ofstream
object (e.g.
Describe the core logic needed to calculate the factorial of a non-negative integer n
using a for
loop. (Sheet 1, Problem 3.1a)
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.
Describe the core logic needed to calculate the factorial of a non-negative integer n
using a while
loop. (Sheet 1, Problem 3.1b)
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
.