Week 1 - Gemini Flashcards

(86 cards)

1
Q

What is the purpose of the #include<iostream> directive?

A

It tells the compiler and linker that the program will need to be linked to a library of routines that handle input from the keyboard and output to the screen (e.g.

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

What is the using namespace std; directive used for?

A

The C++ standard divides names (like cin and cout) into subcollections called namespaces. This directive indicates that the program will use names defined in the std namespace (where iostream defines cout and cin). [cite: 67, 68, 69]

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

What is the significance of the int main() function in a C++ program?

A

The main() function is the entry point of every C++ program; it’s the function called when the program is run. The execution of all C++ programs begins with the main function. [cite: 70, 71]

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

What do the curly braces {} signify in the main() function (and other functions)?

A

The open brace { indicates the beginning of the function’s definition, and the closing brace } indicates its end. Everything between these braces is the function’s body. [cite: 72, 73]

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

What does the return 0; statement in the main() function typically indicate?

A

It indicates that the program worked as expected without any errors during its execution. [cite: 74]

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

What is cout in C++ and how is it used with the << operator?

A

cout identifies the standard character output device (usually the computer screen). The insertion operator << indicates that what follows is inserted into cout. [cite: 77, 79]

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

How are statements separated in C++?

A

Statements are separated with an ending semicolon (;). [cite: 80]

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

What does it mean that C++ is a ‘statically typed language’?

A

When you use a variable in C++, you must specify the type of data that will be stored in that variable, and once chosen, this type cannot be changed. [cite: 81, 82, 83]

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

Why is it important to specify the data type of a variable in C++?

A

Data is stored in the computer as strings of 1s and 0s. Specifying the type tells the computer how to interpret these 1s and 0s (e.g., as binary numbers for integers, or character codes for chars). [cite: 85, 86]

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

Define ‘bit’.

A

A single binary digit. [cite: 88]

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

Define ‘byte’.

A

8 binary digits. [cite: 89]

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

How many bytes are in a kilobyte (in the context of computer memory)?

A

$2^{10}$ or 1024 bytes. [cite: 89]

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

How many bytes are in a megabyte (in the context of computer memory)?

A

$2^{20}$ or 1048576 bytes. [cite: 89]

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

What is hexadecimal notation?

A

A base-16 number system using characters $0,1,2,3,…,9,A,B,…,F$. [cite: 90]

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

What is a ‘variable’ in C++?

A

A symbolic name for storing information in memory. The value stored can be changed. Variables must be declared before use. [cite: 91, 93]

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

What is the basic syntax for declaring a variable in C++?

A

Type_of_data Name_Variable; [cite: 94]

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

List the built-in C++ data type for Boolean values and its keyword.

A

Boolean, bool [cite: 95, 97]

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

List the built-in C++ data type for characters and its keyword.

A

Character, char [cite: 95]

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

List the built-in C++ data type for integers and its keyword.

A

Integer, int [cite: 95]

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

List the built-in C++ data types for floating-point numbers and their keywords.

A

Floating point, float; Double floating point, double [cite: 95]

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

What values can a bool variable hold?

A

true or false. [cite: 97]

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

How is a char data type stored in memory?

A

As a number between 0 and 255, taking up exactly one byte. [cite: 100]

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

What is the ASCII code?

A

It associates an integer value for each symbol in the character set, allowing arithmetic operations on characters. For example, '9' - '0' evaluates to 9 because their ASCII values are 57 and 48 respectively. [cite: 102, 103]

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

How does declaring a variable as char versus int affect input and output?

A

It makes an important difference to the type of input the program expects and the format of the output it produces. [cite: 104]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Provide an example C++ code snippet to read a character and print its ASCII integer representation.
`int number; char character; cout<<"Type in a character: \\n"; cin>>character; number=(int)character; cout<<"The character is "<(character)`) [cite: 105, 106]
26
Name some data-type specifiers for integers in C++ other than `int`.
`signed`, `unsigned`, `short`, `long`, `long long`. [cite: 108]
27
What does the `unsigned` specifier mean for an integer type?
The variable is non-negative. This uses one less bit of memory that would otherwise be used for the sign. [cite: 111, 112]
28
What is the `sizeof()` operator used for in C++?
To get the size (in bytes) of various data types. [cite: 113]
29
Provide an example C++ code snippet to print the size of the `int` data type.
`cout << "Size of int : " << sizeof(int) << endl;` [cite: 114]
30
What is 'casting' in C++?
Converting a value from one data type to another. [cite: 121]
31
Why is casting sometimes necessary, for example, in division?
When dividing two `int` values, C++ performs integer division (e.g., $7/2$ is 3). To get a floating-point result (e.g., 3.5), one or both numbers must be treated as a floating-point type. If variables are used, casting is needed. [cite: 119]
32
What is the C++ syntax for casting a variable `numerator` to `double` using `static_cast`?
`static_cast(numerator)` [cite: 122]
33
What is the C-style cast syntax to convert `14.35` to an `int`?
`(int)(14.35)` (Note: `static_cast(14.35)` is preferred in modern C++) [cite: 125]
34
What is the result of `double c = a/b;` if `int a = 3;` and `int b = 5;`? Why?
The result is 0. Because `a/b` is an integer division (3/5 = 0), and then 0 is cast to `double`. [cite: 127, 128, 130]
35
How can you get the correct floating-point result (0.6) for `double c = a/b;` if `int a = 3;` and `int b = 5;`?
`double c = (static_cast(a))/b;` [cite: 131]
36
What happens when casting a `double` to an `int`?
Information (the decimal part) is potentially lost. [cite: 132]
37
What happens when casting a `bool` to an `int`?
`true` converts to 1, and `false` converts to 0. [cite: 135]
38
What happens when casting an `int` to a `bool`?
Non-zero integers convert to `true`, and 0 converts to `false`. Information can be lost. [cite: 135]
39
What happens when casting a `char` to an `int`?
Converts the character to the integer number used to represent that character on the system (e.g., its ASCII value). [cite: 136]
40
How can you format `cout` to display floating-point numbers in fixed notation with a specific number of decimal places (e.g., 2)?
`cout.setf(ios::fixed); cout.precision(2);` [cite: 138, 139]
41
How can you format `cout` to display floating-point numbers in scientific notation?
`cout.setf(ios::scientific);` [cite: 142]
42
What is the `typedef` keyword used for in C++?
To assign an alternative name to a data type. For example, `typedef int Integer;` allows `Integer` to be used as a synonym for `int`. [cite: 144, 145]
43
How can you initialize a variable at the same time as declaring it? Give an example.
`double PI = 3.1415926535;` [cite: 153]
44
What is the `const` keyword used for in C++? Give an example.
To specify that a variable's value cannot be altered during program execution. Example: `const double PI = 3.1415926535;` [cite: 154, 155]
45
What is an 'enumeration' (`enum`) in C++? Give an example.
It allows declaration of a set of named integer constants. Example: `enum {MON, TUES, WED, THURS, FRI, SAT, SUN};` This makes MON=0, TUES=1, etc. by default. [cite: 156, 157, 158]
46
How can you assign specific integer values to members of an `enum` list?
`enum{ MON=1, TUES, WED, THURS, FRI, SAT=-1, SUN};` Uninitialized members take the value of the previous member plus one. [cite: 159, 160]
47
What is the assignment operator in C++ and what does it do?
The `=` operator. It assigns a value to a variable. E.g., `x = 5;` or `x = y;`. [cite: 162, 163]
48
If `x = y;` is executed, and then the value of `y` changes, does the value of `x` also change?
No, the change in `y` will not affect the new value taken by `x`. [cite: 165]
49
What is the equivalent of `y = 2 + (x = 5);` as separate statements?
`x = 5; y = 2 + x;` [cite: 166]
50
List the basic arithmetic operators in C++.
`+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (modulo). [cite: 167]
51
What does the modulo operator (`%`) do?
It gives the remainder of a division of two values. [cite: 167]
52
What are compound assignment operators? Give an example.
Operators that modify the current value of a variable by performing an operation and then assigning the result to the first operand. Example: `y += x;` is equivalent to `y = y + x;`. Others include `-=`, `*=`, `/=`, `%=`. [cite: 168, 169, 170, 171]
53
What do the increment (`++`) and decrement (`--`) operators do?
They increase or reduce the value stored in a variable by one. [cite: 172]
54
What is the difference between prefix (`++x`) and postfix (`x++`) forms of the increment operator when used in an expression?
In prefix form, the increment is done *before* the rest of the expression is evaluated. In postfix form, the increment is done *after* the complete expression is evaluated. [cite: 174, 175, 176]
55
If `x = 3; y = ++x;`, what are the values of `x` and `y`?
`x` contains 4, `y` contains 4. [cite: 177, 178]
56
If `x = 3; y = x++;`, what are the values of `x` and `y`?
`x` contains 4, `y` contains 3. [cite: 177, 179]
57
List the relational/comparison operators in C++.
`==` (equal to), `!=` (not equal to), `>` (greater than), `>=` (greater than or equal to), `<` (less than), `<=` (less than or equal to). [cite: 180]
58
What are the logical operators in C++?
`&&` (logical AND), `||` (logical OR), `!` (logical NOT). [cite: 180]
59
Explain the `&&` (logical AND) operator.
If both operands are non-zero (true), the condition becomes true. E.g., If A is 1 (true) and B is 0 (false), (A && B) is false. [cite: 181, 182]
60
Explain the `||` (logical OR) operator.
If any of the two operands is non-zero (true), the condition becomes true. E.g., If A is 1 (true) and B is 0 (false), (A || B) is true. [cite: 183, 184]
61
Explain the `!` (logical NOT) operator.
It reverses the logical state of its operand. If a condition is true, `!` makes it false. E.g., If A is 1 (true) and B is 0 (false), !(A && B) is true (since A&&B is false). !(A||B) is false. [cite: 185, 186, 187]
62
What is the precedence of `&&` versus `||`? Which one is evaluated first in an unparenthesized expression?
`&&` has higher precedence than `||` (similar to `*` and `+`). If in doubt, use parentheses. [cite: 191, 192]
63
What is the conditional ternary operator (`? :`) and its syntax?
It evaluates an expression, returning one value if true and another if false. Syntax: `condition ? result1 : result2`. [cite: 195, 196]
64
How can you use the conditional ternary operator to find the maximum of two integers `a` and `b` and store it in `max_val`?
`int max_val = ((a > b) ? a : b);` [cite: 197]
65
What does the bitwise operator `<<` do? Give an example.
Shifts all bits in a number to the left by a given amount. `x << 3` shifts all bits in `x` three places to the left. [cite: 200, 201]
66
What does the bitwise operator `>>` do? Give an example.
Shifts all bits in a number to the right by a given amount. `x >> 3` shifts all bits in `x` three places to the right. [cite: 202, 203]
67
What does the bitwise AND operator `&` do?
The n-th bit of `a&b` is 1 if the corresponding bit of `a` is 1 AND the corresponding bit of `b` is 1. [cite: 204, 205]
68
What does the bitwise OR operator `|` do?
The n-th bit of `a|b` is 1 if the corresponding bit of `a` is 1 OR the corresponding bit of `b` is 1. [cite: 206]
69
How do you write a C++ program to print the lines: 'You are a student.' and 'You are learning C++.' on separate lines? (Sheet 1, Prelim Q1)
`#include\nusing namespace std;\nint main() {\n cout << "You are a student.\\n";\n cout << "You are learning C++.\\n";\n return 0;\n}` (The exercise output implies separate `cout` statements or `endl`) [cite: 3]
70
How do you declare two integer variables and one float variable, assign 10, 15, and 12.6 to them respectively, and print their values? (Sheet 1, Prelim Q2)
`#include\nusing namespace std;\nint main() {\n int intVar1 = 10;\n int intVar2 = 15;\n float floatVar = 12.6f; // 'f' suffix for float literal is good practice\n cout << intVar1 << endl;\n cout << intVar2 << endl;\n cout << floatVar << endl;\n return 0;\n}` [cite: 4, 5]
71
How do you prompt the user to input 3 integer values and then print these values in forward and reversed order? (Sheet 1, Prelim Q3)
`#include\nusing namespace std;\nint main() {\n int n1, n2, n3;\n cout << "Please enter your 3 numbers: ";\n cin >> n1 >> n2 >> n3;\n cout << "Your numbers forward:\\n" << n1 << "\\n" << n2 << "\\n" << n3 << "\\n";\n cout << "Your numbers reversed:\\n" << n3 << "\\n" << n2 << "\\n" << n1 << "\\n";\n return 0;\n}` [cite: 6, 7]
72
How do you declare 3 integer variables, assign 10 and 15 to the first two, assign their product to the third, and print all three? (Sheet 1, Prelim Q4)
`#include\nusing namespace std;\nint main() {\n int val1 = 10;\n int val2 = 15;\n int product = val1 * val2;\n cout << val1 << endl;\n cout << val2 << endl;\n cout << product << endl;\n return 0;\n}` [cite: 8, 10]
73
How do you write a C++ program to print the number of bytes used by five different basic data types (e.g., char, int, float, double, bool)? (Sheet 1, Basic Data Types Q1)
`#include\nusing namespace std;\nint main() {\n cout << "Size of char: " << sizeof(char) << " bytes\\n";\n cout << "Size of int: " << sizeof(int) << " bytes\\n";\n cout << "Size of float: " << sizeof(float) << " bytes\\n";\n cout << "Size of double: " << sizeof(double) << " bytes\\n";\n cout << "Size of bool: " << sizeof(bool) << " bytes\\n";\n // Try other combinations like short, long, unsigned int\n cout << "Size of short int: " << sizeof(short int) << " bytes\\n";\n cout << "Size of long int: " << sizeof(long int) << " bytes\\n";\n cout << "Size of unsigned int: " << sizeof(unsigned int) << " bytes\\n";\n return 0;\n}` [cite: 12, 13, 114]
74
How do you cast `char` values to `int` to find the ASCII codes for characters like 'a', 'z', 'A', 'Z', '0', '9'? (Sheet 1, Basic Data Types Q2)
`#include\nusing namespace std;\nint main() {\n char c1 = 'a', c2 = 'z', c3 = 'A', c4 = 'Z', c5 = '0', c6 = '9';\n cout << "ASCII for 'a': " << static_cast(c1) << endl;\n cout << "ASCII for 'z': " << static_cast(c2) << endl;\n cout << "ASCII for 'A': " << static_cast(c3) << endl;\n cout << "ASCII for 'Z': " << static_cast(c4) << endl;\n cout << "ASCII for '0': " << static_cast(c5) << endl;\n cout << "ASCII for '9': " << static_cast(c6) << endl;\n return 0;\n}` (Based on problem description and lecture content [cite: 102, 105, 122, 136]) [cite: 13]
75
Outline the logic for a program that reads a character and displays if it's a lowercase letter (and its uppercase), an uppercase letter (and its lowercase), or not a letter. (Sheet 1, Basic Data Types Q3)
1. Read a character `ch`.\n2. Check if `ch >= 'a' && ch <= 'z'` (lowercase).\n If true, print `ch` and `static_cast(ch - 'a' + 'A')`.\n3. Else, check if `ch >= 'A' && ch <= 'Z'` (uppercase).\n If true, print `ch` and `static_cast(ch - 'A' + 'a')`.\n4. Else, print `ch` is not a letter. (This uses knowledge of ASCII values being contiguous for letters) [cite: 14, 102, 103]
76
What is a compiled language?
Compiled languages are translated to the target machine's native language by a program called a compiler, resulting in very fast code. C++ is an example. [cite: 38, 39]
77
What is an interpreted language?
Interpreted languages (e.g., R, Matlab, Python) are read by a program called an interpreter and are executed by that program. They are usually much slower than compiled programs. [cite: 39, 40]
78
What is the general structure of the first 'Hello world!' C++ program?
`#include\nusing namespace std;\nint main()\n{\n cout << "Hello world!";\n return 0;\n}` [cite: 62, 63]
79
Can you do arithmetic with `char` types in C++? Explain.
Yes, because `char` types are stored as numbers (ASCII codes). For example, `'9' - '0'` would result in the integer 9. [cite: 102, 103]
80
What is meant by initializing a variable?
Assigning an initial value to a variable at the time of its declaration or before its first use. [cite: 152, 153]
81
What is the difference between `cout.setf(ios::fixed);` and `cout.setf(ios::scientific);`?
`ios::fixed` forces output of floating-point numbers in fixed decimal format (e.g., 123.45). `ios::scientific` forces output in scientific notation (e.g., $1.2345e+02$). [cite: 138, 142]
82
What is the typical range of values you can store in an `int` variable?
Guaranteed to store values between $-2^{31}$ and $2^{31}-1$. [cite: 110]
83
What is the typical range of values you can store in a `long long` variable?
Guaranteed to store values between $-2^{63}$ and $2^{63}-1$. [cite: 110]
84
What are comments in C++ and how are they denoted?
Comments are notes for human readers that are ignored by the compiler. Single-line comments start with `//`. Multi-line comments start with `/*` and end with `*/`.
85
What does the `` header file provide?
It provides standard mathematical functions like `sqrt()` (square root), `pow()` (power), trigonometric functions, etc.
86
What does `cin` represent in C++?
It represents the standard input stream, typically the keyboard. It's used with the extraction operator `>>` to read input.