COMMON EXAM TRICK QUESTIONS
These are VERY REAL professor traps.
Trick #1: “Why are data members private?”
Wrong answer:
“Because that’s how C++ works.”
Correct answer:
To enforce encapsulation, protect object integrity, and prevent invalid states.
Trick #2: “Why not make everything public?”
Correct answer:
Public data allows external code to violate class invariants and bypass validation.
Use the word invariants — professors LOVE it.
Trick #3: “Why use getters instead of public variables?”
Correct answer:
Getters allow controlled access and future changes without breaking code.
Trick #4: “What is a read-only member function?”
Correct answer:
A function that computes and returns a value without modifying object state.
Bonus points if you say:
It is typically marked
const.
Trick #5: “When should you use a struct instead of a class?”
Correct answer:
When grouping related data without enforcing rules or encapsulation.
Trick #6: “Why is a constructor important?”
Correct answer:
It ensures objects begin life in a valid and predictable state.
Trick #7: “What happens if you don’t initialize data members?”
Correct answer:
They contain undefined (garbage) values, leading to unpredictable behavior.
Trick #8: “Why separate .h and .cpp files?”
Correct answer:
To separate interface from implementation and improve maintainability.
Trick #9: “Why make helper functions private?”
Correct answer:
They are implementation details and not part of the public interface.
Trick #10: “Why use object composition?”
Correct answer:
To build complex behavior from simpler, reusable objects.
FINAL CONFIDENCE CHECK
If you can:
Explain why data is private
Explain what constructors do
Spot bad class design
Explain encapsulation
Design a class from scratch
You are READY for C++ Programming Fundamentals 2.