C++ Quiz Practice - Modules 5-8
Questions with Detailed Solutions
by jsecco
& Visual Explanations
Memory Model:
[title=Question 1: Array Initialization & Bounds]
1 int numbers [4] = {10 , 20 , 30}; BEFORE:
5 10 15 20
What is the value of numbers[4]?
(a) 10
(b) Undefined/garbage value
(c) 30 reset(values,4)
(d) 50
(e) 0 AFTER:
0 0 0 values[2]=0
0
Why Changes Persist:
• Arrays always passed by reference
■ Answer: (b) Undefined/garbage value • No & needed for array parameters
• reset() modifies the original array
• All elements set to 0 in loop
• values[2] becomes 0
Visual Memory Layout:
[title=Question 3: Static Member Variables]
OUT OF
VALID VALID VALID VALID BOUNDS
10 20 30 0 ???
1 class Book {
2 private :
[0] [1] [2] [3] [4]
3 string _title ;
4 static int _total_books ;
Key Points: 5 public :
• Array size 4 ⇒ valid indices: 0, 1, 2, 3 6 Book () : _title ( " Unknown " ) {
• Partial init {10,20,30} ⇒ [3]=0 7 _total_books ++;
• numbers[4] is OUT OF BOUNDS 8 }
• Accessing beyond bounds = undefined behavior 9 Book ( string t ) : _title ( t ) {
• Common mistake: size n ̸= index n 10 _total_books ++;
11 }
12 static int get_total () {
13 return _total_books ;
14 }
15 };
16
17 int Book :: _total_books = 0;
18
19 // Code :
20 Book b1 ( " 1984 " ) ;
[title=Question 2: Pass-by-Reference with Arrays] 21 Book b2 ( " Brave New World " ) ;
22 Book b3 ;
1 void reset ( int arr [] , size_t size ) {
23 cout << Book :: get_total () ;
2 for ( size_t i =0; i < size ; i ++) {
3 arr [ i ] = 0; Output? (a) 1 (b) 3 (c) 2 (d) 0 (e) Error
4 }
5 }
6 ■ Answer: (b) 3
7 int main () {
8 int values [4] = {5 , 10 , 15 , 20};
9 reset ( values , 4) ; Static Variable Trace:
10 cout << values [2]; Line Action _total_books
Init _total_books = 0 0
11 } b1("1984") Constructor: ++ 1
b2("BNW") Constructor: ++ 2
What is the output? b3 Default constructor: ++ 3
(a) 5 (b)] 0 (c)] 20 (d)] Compile error (e)] 15 Output get_total() returns 3
Key Concept: Static variables are shared by all objects
_total_books = 3
b1 b2 b3
■ Answer: (b) 0
[title=Question 4: Private Member Access]
1 class Student {
2 private :
3 int _id ;
4 public :
5 Student ( int id ) : _id ( id ) {}
6 int get_id () { return _id ; }
7 };
8
9 int main () {
10 Student s1 (12345) ;
11 cout << s1 . _id ;
12 }
Result?
(a) Outputs 0 (b) Runtime error (c) Outputs _id
(d) Outputs 12345 (e) Compile-time error
■ Answer: (e) Compile-time error
Access Control Diagram:
private: BLOCKED
main()
int _id;
public: ALLOWED
get_id()
Why Compile Error:
• _id is private
• Cannot access from outside class
• Compiler catches this immediately
• Not a runtime error!
• Solution: s1.get_id() (public method)
[title=Question 5: For Loop Execution]
1 int result = 0;
2 for ( int i =1; i <=5; i ++) {
3 result += i ;
4 }
Value of result?
(a) 15 (b) 10 (c) 5 (d) 16 (e) 20
■ Answer: (a) 15
Execution Trace:
Iter i Before After Condition
1 1 0 0+1 = 1 1<=5 ✓
2 2 1 1+2 = 3 2<=5 ✓
3 3 3 3+3 = 6 3<=5 ✓
4 4 6 6 + 4 = 10 4<=5 ✓
5 5 10 10 + 5 = 15 5<=5 ✓
- 6 15 - 6<=5 × EXIT
Formula Verification:
P5 n(n+1) 5×6
i=1 i= 2 = 2 = 15 ✓
Loop Flow:
1. i=1 (init once)
2. Check 1<=5? YES ⇒ execute
3. Update i++
4. Repeat until 6<=5 = FALSE
Value (Copy) Visualization:
[title=Question 6: Pass-by-Reference (Objects)]
1 class Car { main() detail_car()
2 public : COPY
my_car
3 string _color ; color="red"
Car c
UNCHANGED
COPY
4 Car ( string c ) : _color ( c ) {}
5 void repaint ( string c ) { _color = c ;}
6 }; copy color="shiny black"
7
8 void detail_car ( Car & c ) { // Note &
9 c . repaint ( " shiny black " ) ; Comparison with Q6:
10 } Q6 (Reference) Q7 (Value)
11 Syntax Car& c Car c
Behavior Alias (same object) Copy (new object)
12 int main () { Changes Persist Lost
13 Car my_car ( " red " ) ; Result "shiny black" "red"
14 detail_car ( my_car ) ;
15 cout << my_car . _color ;
16 }
Output?
(a) "shiny black" (b) "_color" (c) Compile error
(d) "red" (e) "" [title=Question 8: Do-While Loop]
1 int count = 5;
2 do {
■ Answer: (a) "shiny black" 3 count - -;
4 } while ( count > 0) ;
How many times does body execute?
Reference Visualization: (a) 0 (b) 5 (c) Infinitely (d) 6 (e) 4
main() detail_car()
reference (&)
my_car Car& c
color="red" SAME object ■ Answer: (b) 5
color = "shiny black"
Do-While Flow:
START
Why It Changes: count=5
• Car& c = pass-by-reference (&) execute
• c is an alias for my_car count–
• Not a copy - same object! yes count>0?
no
EXIT
• Changes to c affect my_car
• Original object modified
Trace Table:
Iter count before count after Condition
1 5 4 4>0 ✓ continue
2 4 3 3>0 ✓ continue
[title=Question 7: Pass-by-Value (Objects)] 3
4
3
2
2
1
2>0 ✓ continue
1>0 ✓ continue
5 1 0 0>0 × EXIT
1 class Car {
2 public : Key: Do-while checks AFTER body ⇒ runs 1+ times
3 string _color ;
4 Car ( string c ) : _color ( c ) {}
5 void repaint ( string c ) { _color = c ;}
6 };
7
8 void detail_car ( Car c ) { // NO & [title=Question 9: Loop with Step ̸= 1]
9 c . repaint ( " shiny black " ) ;
10 } 1 int data [6] = {2 , 4 , 6 , 8 , 10 , 12};
11
2 for ( int i =0; i <6; i +=2) {
12 int main () { 3 data [ i ] = data [ i ] / 2;
13 Car my_car ( " red " ) ; 4 }
14 detail_car ( my_car ) ; 5 cout << data [0] << " "
15 cout << my_car . _color ; 6 << data [1] << " "
16 } 7 << data [2];
Output? Output?
(a) "_color" (b) "red" (c) Compile error (a) 2 2 3 (b) Runtime error (c) 1 2 3
(d) "shiny black" (e) "" (d) 2 4 6 (e) 1 4 3
■ Answer: (b) "red" ■ Answer: (e) 1 4 3
Step-by-Step Execution:
[title=Question 10: Course Requirements] True or false: For course projects, you can earn full credit
BEFORE:
even if you do not include a main driver with sample program runs copied into sample_runs.txt.
2 4 6 8 10 12 (a) True
[0] [1] [2] [3] [4] [5] (b) False
MODIFIED:
1 4 3 8 5 12
■ Answer: (b) False
Iteration Details: Requirements Checklist:
i Action Condition □ Main driver program
0 data[0]=2/2=1 0<6 ✓
2 data[2]=6/2=3 2<6 ✓ □ Sample runs in sample_runs.txt
4 data[4]=10/2=5 4<6 ✓ □ Both required for full credit
6 - 6<6 × EXIT
□ Follow all project specifications
Output: data[0]=1, data[1]=4, data[2]=3 ⇒ 1 4 3 Remember: Always read and follow project requirements carefully!
KEY TAKEAWAYS
Arrays:
• Size n ⇒ indices 0 to n − 1 (not n!)
• Always passed by reference (no & needed)
• Partial init fills remaining with 0
• Out of bounds = undefined behavior
Functions:
• NO & = pass-by-value (copy, no change)
• & = pass-by-reference (alias, changes persist)
• Arrays always by reference
Classes:
• Private ⇒ compile error if accessed directly
• Use public methods to access private members
• Static: shared by all objects, init outside class
Loops:
• For/While: check BEFORE (0+ times)
• Do-While: check AFTER (1+ times)
• Use < not <= to avoid off-by-one
• Make trace table for complex loops
Common Mistakes:
• arr[size] is out of bounds!
• Forgetting & when need to modify
• Direct private member access
• Forgetting static variable initialization outside class
• Confusing compile-time vs runtime errors
Quiz Practice by jsecco • Study these patterns • Good luck! • Page 2/2