Cpp Test 2
C++ Test 2 – Functions, Pointers and OOP (50 Marks)
- One question will appear on the screen at a time.
- Use Next and Previous buttons to move between questions.
- Use the right-side question palette to jump to any question.
- Use Mark for Review for questions you want to revisit.
- The quiz auto-submits when time is over.
Question Palette
Yellow = Review
White = Not Answered
Answer key with explanations
Attempt the test and submit it first, then use this key to revise. Each answer has a short explanation so you understand why it is correct.
Show all 50 answers
- A class is
Answer: A. Blueprint for objects – Class defines data and functions for objects.
- An object is
Answer: A. Instance of a class – Object is created from a class.
- Which access specifier hides members from outside class?
Answer: B. private – private members cannot be accessed directly outside class.
- Default access in a C++ class is
Answer: B. private – Class members are private by default.
- Default access in a C++ struct is
Answer: C. public – Struct members are public by default.
- Constructor name is same as
Answer: B. Class name – Constructor has same name as class.
- Constructor return type is
Answer: C. No return type – Constructor has no return type.
- Destructor name begins with
Answer: B. ~ – Destructor name is ~ClassName.
- Which pointer refers to current object?
Answer: B. this – this points to current object.
- Static data member belongs to
Answer: A. Class – Static member is shared at class level.
- Friend function can access
Answer: B. Private members if declared friend – friend function can access private/protected members.
- Inheritance supports
Answer: A. Code reuse – Inheritance reuses and extends base class features.
- The class inherited from is called
Answer: B. Base class – Base class is parent class.
- The inheriting class is called
Answer: A. Derived class – Derived class inherits from base class.
- Which inheritance mode keeps public members public?
Answer: C. public – Public inheritance keeps public members public.
- Polymorphism means
Answer: A. Many forms – Polymorphism means one interface with many forms.
- Run-time polymorphism is achieved using
Answer: A. Virtual functions – virtual functions support dynamic binding.
- Pure virtual function syntax includes
Answer: A. = 0 – Pure virtual function ends with = 0.
- A class with pure virtual function is
Answer: A. Abstract class – It becomes an abstract class.
- Operator overloading is used for
Answer: A. User-defined meaning of operators – Operator overloading defines operator behavior for classes.
- Which operator cannot be overloaded?
Answer: C. :: – Scope resolution operator :: cannot be overloaded.
- Templates enable
Answer: A. Generic programming – Templates work with different data types generically.
- Which keyword starts a function template?
Answer: A. template – template starts template declaration.
- Exception handling uses
Answer: A. try, throw, catch – try-throw-catch handles exceptions.
- Which keyword throws an exception?
Answer: B. throw – throw raises an exception.
- Which keyword handles an exception?
Answer: A. catch – catch handles thrown exception.
- STL stands for
Answer: A. Standard Template Library – STL means Standard Template Library.
- Which STL container is dynamic array?
Answer: A. vector – vector is dynamic array.
- Which vector function adds element at end?
Answer: A. push_back – push_back appends element.
- Which STL container stores unique sorted values?
Answer: A. set – set stores unique sorted keys.
- Which STL container stores key-value pairs?
Answer: A. map – map stores key-value pairs.
- Which data structure follows LIFO?
Answer: A. Stack – Stack follows Last In First Out.
- Which data structure follows FIFO?
Answer: B. Queue – Queue follows First In First Out.
- Which function gets top element of stack?
Answer: C. top() – stack uses top().
- Which algorithm sorts a vector?
Answer: A. sort() – sort(begin,end) sorts a range.
- Which header contains sort()?
Answer: A. <algorithm> – sort is declared in <algorithm>.
- Which iterator points to first element?
Answer: A. begin() – begin() returns iterator to first element.
- Which iterator points after the last element?
Answer: B. end() – end() points one past last element.
- Range-based for loop syntax uses
Answer: A. for(auto x : container) – C++ range-for uses colon.
- auto keyword is used to
Answer: A. Let compiler infer type – auto deduces type from initializer.
- lambda expression is
Answer: A. Unnamed function – Lambda is an anonymous function object.
- Smart pointers are available in
Answer: A. <memory> – <memory> provides smart pointers.
- unique_ptr means
Answer: A. Single ownership – unique_ptr has exclusive ownership.
- shared_ptr means
Answer: A. Shared ownership – shared_ptr shares ownership through reference counting.
- RAII is mainly about
Answer: A. Resource management through object lifetime – RAII ties resource release to object destruction.
- binary_search requires range to be
Answer: A. Sorted – binary_search expects sorted range.
- Which function reverses a range?
Answer: A. reverse() – reverse(begin,end) reverses order.
- Which program design is best for a student management project?
Answer: A. Use classes and file handling – Classes and files make project organized.
- Encapsulation means
Answer: A. Wrapping data and methods together with controlled access – Encapsulation protects data inside class.
- Abstraction means
Answer: A. Showing essential details and hiding internal complexity – Abstraction hides implementation details.
