Object Oriented Programming — One Question at a Time

Object Oriented Programming — One Question at a Time

Practise 150 Object-Oriented Programming multiple-choice questions covering classes, objects, constructors, inheritance, polymorphism, C++ templates, exceptions and smart pointers. Work through one question at a time, check the correct answer with an explanation, and select Next Question to continue. This is self-paced practice without automatic scoring.

Before you practise: Read Object-Oriented Programming study material and examples. You can also browse all 1,576 interactive MCQs.

Practice Set 1 · Question 1 of 150

Question 1. In object-oriented programming, what is a class?

  1. A hardware register
  2. A compiler error
  3. A blueprint defining data and behaviour
  4. A single running process
Show Answer and Explanation

Correct answer: C. A blueprint defining data and behaviour

A class specifies an object’s attributes and operations.

Next Question →

Practice Set 1 · Question 2 of 150

Question 2. What is an object?

  1. An instance of a class
  2. A type of source-file extension
  3. Only a static method
  4. Only a function name
Show Answer and Explanation

Correct answer: A. An instance of a class

An object is an instance with its own identity and state.

Next Question →

Practice Set 1 · Question 3 of 150

Question 3. Which OOP principle bundles data and related methods together?

  1. Compilation
  2. Recursion
  3. Sorting
  4. Encapsulation
Show Answer and Explanation

Correct answer: D. Encapsulation

Encapsulation groups attributes and operations within a class.

Next Question →

Practice Set 1 · Question 4 of 150

Question 4. In C++, which access specifier usually makes a member accessible only inside its class and friends?

  1. global
  2. private
  3. public
  4. protected
Show Answer and Explanation

Correct answer: B. private

Private members are inaccessible to unrelated outside code; friends and class members can access them.

Next Question →

Practice Set 1 · Question 5 of 150

Question 5. A class Employee stores name and salary. Which is an example of an instance?

  1. The word ‘class’
  2. An Employee object for a specific worker
  3. The C++ compiler
  4. A programming paradigm
Show Answer and Explanation

Correct answer: B. An Employee object for a specific worker

A specific Employee object represents one entity defined by the Employee class.

Next Question →

Practice Set 1 · Question 6 of 150

Question 6. What is a constructor used for?

  1. Deleting all source files
  2. Ending a loop
  3. Compiling a program
  4. Initializing an object when it is created
Show Answer and Explanation

Correct answer: D. Initializing an object when it is created

Constructors establish initial object state as part of construction.

Next Question →

Practice Set 1 · Question 7 of 150

Question 7. In C++, a class destructor is named using which prefix?

  1. ~
  2. #
  3. %
  4. @
Show Answer and Explanation

Correct answer: A. ~

A C++ destructor has the class name prefixed by a tilde.

Next Question →

Practice Set 1 · Question 8 of 150

Question 8. What is true of a static data member in C++?

  1. It always belongs separately to each object
  2. It cannot store numbers
  3. It is associated with the class rather than each instance
  4. It is available only during compilation
Show Answer and Explanation

Correct answer: C. It is associated with the class rather than each instance

A static data member has one class-associated entity, unlike per-object non-static members.

Next Question →

Practice Set 1 · Question 9 of 150

Question 9. In a C++ const member function, what is generally disallowed?

  1. Reading data members
  2. Calling other const functions
  3. Returning values
  4. Changing non-mutable data members
Show Answer and Explanation

Correct answer: D. Changing non-mutable data members

Const member functions normally cannot modify non-mutable state of the object.

Next Question →

Practice Set 1 · Question 10 of 150

Question 10. When is a local C++ object’s destructor normally called?

  1. Only when the computer shuts down
  2. When its scope ends
  3. At the beginning of source compilation
  4. After each arithmetic operation
Show Answer and Explanation

Correct answer: B. When its scope ends

For an automatic local object, destruction normally occurs on exiting its scope.

Continue to Practice Set 2

Practice Set 2 · Question 11 of 150

Question 11. What distinguishes function overloading in C++?

  1. Only using recursion
  2. Only a different return type
  3. Same name with different parameter lists
  4. Only a different comment
Show Answer and Explanation

Correct answer: C. Same name with different parameter lists

Overloads share a function name but differ in parameter signatures; return type alone is insufficient.

Next Question →

Practice Set 2 · Question 12 of 150

Question 12. What relationship is typically represented by composition?

  1. A strong whole–part ownership relationship
  2. A random numeric comparison
  3. A source-code comment
  4. Only a function call
Show Answer and Explanation

Correct answer: A. A strong whole–part ownership relationship

Composition models parts whose lifetimes are controlled by their whole in typical OO design.

Next Question →

Practice Set 2 · Question 13 of 150

Question 13. Which relation is best described as ‘is-a’?

  1. Inheritance
  2. File input
  3. Aggregation
  4. Composition
Show Answer and Explanation

Correct answer: A. Inheritance

Inheritance represents a subtype relationship when designed consistently with substitutability.

Next Question →

Practice Set 2 · Question 14 of 150

Question 14. Which example most naturally illustrates aggregation?

  1. A function computing 2+2
  2. An integer division
  3. A department referencing teachers who may exist independently
  4. A constructor assigning x = 0
Show Answer and Explanation

Correct answer: C. A department referencing teachers who may exist independently

Aggregation commonly describes a non-owning whole–part relationship where components can exist separately.

Next Question →

Practice Set 2 · Question 15 of 150

Question 15. In C++, what can operator overloading do?

  1. Alter precedence of existing operators
  2. Define operator behaviour for user-defined types
  3. Create a completely new operator symbol
  4. Change every built-in integer addition
Show Answer and Explanation

Correct answer: B. Define operator behaviour for user-defined types

Operator overloads support user-defined types but cannot create new operators or change precedence.

Next Question →

Practice Set 2 · Question 16 of 150

Question 16. Which access specifier lets derived classes access a C++ base-class member but normally restricts unrelated code?

  1. namespace
  2. private
  3. public
  4. protected
Show Answer and Explanation

Correct answer: D. protected

Protected members can be used by class members, friends and appropriate derived-class code.

Next Question →

Practice Set 2 · Question 17 of 150

Question 17. What is runtime polymorphism in C++ usually achieved through?

  1. Only arrays
  2. Preprocessor comments
  3. Virtual functions
  4. Only global variables
Show Answer and Explanation

Correct answer: C. Virtual functions

Virtual functions allow a call through a base reference or pointer to dispatch to a derived override.

Next Question →

Practice Set 2 · Question 18 of 150

Question 18. What is the purpose of a pure virtual function in C++?

  1. Guarantee every object is deleted
  2. Ban all inheritance
  3. Change arithmetic precedence
  4. Require a concrete derived class to provide an implementation, subject to inherited overrides
Show Answer and Explanation

Correct answer: D. Require a concrete derived class to provide an implementation, subject to inherited overrides

A pure virtual declaration makes its class abstract; concrete derived classes need a final overrider.

Next Question →

Practice Set 2 · Question 19 of 150

Question 19. If Dog publicly derives from Animal, which is generally an acceptable substitution?

  1. Convert Dog into an integer
  2. Use an Animal interface to operate on a Dog
  3. Use any Animal automatically as a Dog
  4. Treat every class as unrelated
Show Answer and Explanation

Correct answer: B. Use an Animal interface to operate on a Dog

Public inheritance intends substitutability: a Dog is usable through a suitable Animal interface.

Next Question →

Practice Set 2 · Question 20 of 150

Question 20. What does an abstract class in C++ prohibit?

  1. Directly creating an instance of that abstract class
  2. Declaring data members
  3. Declaring methods
  4. Being used as a base class
Show Answer and Explanation

Correct answer: A. Directly creating an instance of that abstract class

An abstract class cannot be directly instantiated, though it can serve as a base.

Continue to Practice Set 3

Practice Set 3 · Question 21 of 150

Question 21. What is a C++ function template?

  1. A file permission
  2. A function pattern that can generate functions for suitable types
  3. A database schema
  4. A storage device
Show Answer and Explanation

Correct answer: B. A function pattern that can generate functions for suitable types

Function templates describe generic operations instantiated for types satisfying their requirements.

Next Question →

Practice Set 3 · Question 22 of 150

Question 22. Which standard C++ container provides contiguous dynamic storage of elements?

  1. std::vector
  2. std::list
  3. std::set
  4. std::map
Show Answer and Explanation

Correct answer: A. std::vector

std::vector stores elements contiguously and can resize dynamically.

Next Question →

Practice Set 3 · Question 23 of 150

Question 23. For a std::vector<int> v = {2,4,6}, what does v.size() return?

  1. 6
  2. 2
  3. 4
  4. 3
Show Answer and Explanation

Correct answer: D. 3

There are three elements, so size() returns 3.

Next Question →

Practice Set 3 · Question 24 of 150

Question 24. What is the main idea of generic programming?

  1. Always use global variables
  2. Store source code only in a database
  3. Write reusable algorithms parameterized by types or operations
  4. Avoid all type checking
Show Answer and Explanation

Correct answer: C. Write reusable algorithms parameterized by types or operations

Generic code expresses an algorithm for a family of compatible types.

Next Question →

Practice Set 3 · Question 25 of 150

Question 25. What is the role of a try block in C++ exception handling?

  1. Prevent all runtime errors
  2. Write a file without I/O
  3. Declare an inheritance relationship
  4. Mark code whose exceptions may be handled by matching handlers
Show Answer and Explanation

Correct answer: D. Mark code whose exceptions may be handled by matching handlers

A try block establishes a region with associated exception handlers.

Next Question →

Practice Set 3 · Question 26 of 150

Question 26. Which keyword explicitly signals an exception in C++?

  1. catch
  2. class
  3. throw
  4. const
Show Answer and Explanation

Correct answer: C. throw

The throw expression initiates exception handling.

Next Question →

Practice Set 3 · Question 27 of 150

Question 27. What does serialization generally mean?

  1. Converting an object’s relevant state to a storable or transferable representation
  2. Removing every class from a program
  3. Sorting a vector automatically
  4. Checking whether a number is prime
Show Answer and Explanation

Correct answer: A. Converting an object’s relevant state to a storable or transferable representation

Serialization encodes state for storage or transfer; deserialization reconstructs it appropriately.

Next Question →

Practice Set 3 · Question 28 of 150

Question 28. Why should input from a serialized file be validated before reconstructing an object?

  1. Every file is always secure
  2. A corrupted or untrusted representation may contain invalid data
  3. Validation changes the compiler version
  4. There are no possible parsing errors
Show Answer and Explanation

Correct answer: B. A corrupted or untrusted representation may contain invalid data

Validation guards against malformed, incompatible and potentially malicious input.

Next Question →

Practice Set 3 · Question 29 of 150

Question 29. What is the main purpose of a catch block?

  1. Handle an exception of a matching type
  2. Automatically compile another file
  3. Define a template
  4. Construct an array
Show Answer and Explanation

Correct answer: A. Handle an exception of a matching type

A catch clause processes an exception after it is thrown and matched.

Next Question →

Practice Set 3 · Question 30 of 150

Question 30. Which principle best supports robust exception handling?

  1. Ignore all errors
  2. Assume inputs can never fail
  3. Use exceptions for every loop iteration
  4. Maintain valid program state when an operation fails
Show Answer and Explanation

Correct answer: D. Maintain valid program state when an operation fails

Well-designed code preserves invariants and releases resources when failures occur.

Continue to Practice Set 4

Practice Set 4 · Question 31 of 150

Question 31. A C++ struct differs from a class by default in which way?

  1. A class cannot have constructors
  2. A struct cannot inherit
  3. Default member and base access is public for struct
  4. A struct cannot have methods
Show Answer and Explanation

Correct answer: C. Default member and base access is public for struct

In C++, struct and class support largely the same features, but their default access differs.

Next Question →

Practice Set 4 · Question 32 of 150

Question 32. In C++, what is printed by: class C {public: int x=4;}; C a; C b=a; b.x=9; std::cout<<a.x; ?

  1. 9
  2. 4
  3. 13
  4. Undefined
Show Answer and Explanation

Correct answer: B. 4

The implicitly generated copy constructor copies the integer member. Modifying b.x does not change a.x, so the program prints 4 (assuming the required iostream header is included).

Next Question →

Practice Set 4 · Question 33 of 150

Question 33. Which initialization invokes a C++ copy constructor?

  1. delete p;
  2. sizeof(Widget);
  3. Widget b(a);
  4. Widget* p=nullptr;
Show Answer and Explanation

Correct answer: C. Widget b(a);

Constructing b from an existing Widget a selects the copy constructor when applicable.

Next Question →

Practice Set 4 · Question 34 of 150

Question 34. For a member object owned by another C++ object, which is destroyed first when the outer object is destroyed?

  1. The outer destructor body, then its member objects
  2. Neither ever
  3. The compiler
  4. Its members before its destructor body
Show Answer and Explanation

Correct answer: A. The outer destructor body, then its member objects

The outer destructor body runs before destruction of its non-static member subobjects.

Next Question →

Practice Set 4 · Question 35 of 150

Question 35. Which feature permits initialization of a const data member in C++?

  1. Assignment in the constructor body
  2. A loop after construction
  3. A destructor
  4. Constructor member-initializer list
Show Answer and Explanation

Correct answer: D. Constructor member-initializer list

Const data members must be initialized, for example using a member-initializer.

Next Question →

Practice Set 4 · Question 36 of 150

Question 36. If class D publicly inherits B, how are public B members generally accessed through D?

  1. Become private automatically
  2. Remain public
  3. Become inaccessible
  4. Become static
Show Answer and Explanation

Correct answer: B. Remain public

Public inheritance preserves public accessibility for inherited public members.

Next Question →

Practice Set 4 · Question 37 of 150

Question 37. A protected base member under private inheritance becomes what access in the derived class?

  1. protected
  2. private
  3. global
  4. public
Show Answer and Explanation

Correct answer: B. private

Private inheritance makes accessible inherited public/protected base members private within the derived class.

Next Question →

Practice Set 4 · Question 38 of 150

Question 38. Which mechanism addresses repeated virtual base subobjects in diamond inheritance?

  1. method overloading
  2. operator precedence
  3. file serialization
  4. virtual inheritance
Show Answer and Explanation

Correct answer: D. virtual inheritance

Virtual inheritance makes specified common virtual base subobjects shared.

Next Question →

Practice Set 4 · Question 39 of 150

Question 39. What happens if a derived class has a function with the same name as a base-class overload set?

  1. The derived declaration can hide the base overload set
  2. All are deleted
  3. The program must use virtual inheritance
  4. Base overloads are always automatically visible
Show Answer and Explanation

Correct answer: A. The derived declaration can hide the base overload set

Name hiding applies unless the base overloads are reintroduced, such as with using B::f.

Next Question →

Practice Set 4 · Question 40 of 150

Question 40. Which keyword explicitly disallows further overriding of a virtual function in C++?

  1. friend
  2. static
  3. final
  4. mutable
Show Answer and Explanation

Correct answer: C. final

The final specifier prohibits overriding in later derived classes.

Continue to Practice Set 5

Practice Set 5 · Question 41 of 150

Question 41. For virtual void f() and a base reference bound to a derived object, which implementation is invoked?

  1. Neither
  2. The first declared method alphabetically
  3. Always the base
  4. The final overrider for the object’s dynamic type
Show Answer and Explanation

Correct answer: D. The final overrider for the object’s dynamic type

Virtual dispatch selects the final overrider according to the dynamic object type.

Next Question →

Practice Set 5 · Question 42 of 150

Question 42. What does override help the C++ compiler verify?

  1. That a template is always optimized
  2. That a function really overrides a virtual base function
  3. That a loop terminates
  4. That data is encrypted
Show Answer and Explanation

Correct answer: B. That a function really overrides a virtual base function

override requests a compile-time check that the function matches a virtual base declaration.

Next Question →

Practice Set 5 · Question 43 of 150

Question 43. Why should a C++ base class intended for polymorphic deletion normally have a virtual destructor?

  1. To avoid constructors
  2. To speed up file reads
  3. To enable safe deletion via a base pointer
  4. To make all members public
Show Answer and Explanation

Correct answer: C. To enable safe deletion via a base pointer

When an object of a derived class is deleted through a pointer to its base type, a virtual base destructor normally ensures the derived and base destructors are invoked correctly. This question assumes ordinary delete through a base pointer.

Next Question →

Practice Set 5 · Question 44 of 150

Question 44. In C++, what is object slicing?

  1. Copying a derived object into a base object, losing derived-specific state
  2. Allocating an array
  3. Writing an object to file
  4. Splitting a string
Show Answer and Explanation

Correct answer: A. Copying a derived object into a base object, losing derived-specific state

A base-by-value copy retains only the base subobject.

Next Question →

Practice Set 5 · Question 45 of 150

Question 45. Can a C++ abstract class define non-pure member functions?

  1. Yes
  2. Only static functions
  3. Only constructors
  4. No
Show Answer and Explanation

Correct answer: A. Yes

Abstract classes may have fully implemented methods alongside pure virtual ones.

Next Question →

Practice Set 5 · Question 46 of 150

Question 46. Which declarations are valid C++ function overloads?

  1. Only functions with different names
  2. int f(int) and double f(int)
  3. int f(int) and int f(double)
  4. int f(int) twice
Show Answer and Explanation

Correct answer: C. int f(int) and int f(double)

Parameter type differences permit overloading; changing only the return type does not.

Next Question →

Practice Set 5 · Question 47 of 150

Question 47. What is the purpose of an explicit single-argument constructor?

  1. Force inheritance
  2. Prevent unintended implicit converting construction
  3. Make an object immutable
  4. Allow only heap allocation
Show Answer and Explanation

Correct answer: B. Prevent unintended implicit converting construction

explicit inhibits implicit conversions through that constructor.

Next Question →

Practice Set 5 · Question 48 of 150

Question 48. What does std::vector::push_back normally do?

  1. Erase the first element
  2. Sort the vector
  3. Return its capacity
  4. Append an element
Show Answer and Explanation

Correct answer: D. Append an element

push_back adds an element at the end.

Next Question →

Practice Set 5 · Question 49 of 150

Question 49. For std::vector<int> v{8,3,5}; what is v.at(1)?

  1. An exception
  2. 8
  3. 3
  4. 5
Show Answer and Explanation

Correct answer: C. 3

at(1) returns the second element and checks bounds.

Next Question →

Practice Set 5 · Question 50 of 150

Question 50. What can happen when std::vector reallocates due to growth?

  1. The vector becomes a set
  2. Every element becomes zero
  3. Its size must become zero
  4. References and iterators to existing elements can be invalidated
Show Answer and Explanation

Correct answer: D. References and iterators to existing elements can be invalidated

Moving storage to a larger allocation can invalidate existing pointers, references and iterators.

Continue to Practice Set 6

Practice Set 6 · Question 51 of 150

Question 51. What does a catch(const std::exception& e) handler avoid compared with catch by value?

  1. All possible errors
  2. Exception object slicing
  3. All compiler diagnostics
  4. Every memory allocation
Show Answer and Explanation

Correct answer: B. Exception object slicing

Catching by reference preserves polymorphism and avoids copying the exception object.

Next Question →

Practice Set 6 · Question 52 of 150

Question 52. What is the role of noexcept on a function declaration?

  1. State that the function is non-throwing under its exception specification
  2. Turn the function into a constructor
  3. Encrypt arguments
  4. Make all calls constexpr
Show Answer and Explanation

Correct answer: A. State that the function is non-throwing under its exception specification

If an exception escapes a noexcept function, std::terminate is invoked.

Next Question →

Practice Set 6 · Question 53 of 150

Question 53. Which data should normally be included in serialized objects?

  1. Uninitialised memory bytes
  2. Relevant persistent object state
  3. Raw process-specific pointer addresses
  4. Open operating-system file handles
Show Answer and Explanation

Correct answer: B. Relevant persistent object state

Persistent representations should encode meaningful data rather than transient process addresses or handles.

Next Question →

Practice Set 6 · Question 54 of 150

Question 54. In C++, what does this print? int x=4; int& r=x; r=9; std::cout<<x;

  1. 9
  2. 13
  3. Compilation error
  4. 4
Show Answer and Explanation

Correct answer: A. 9

r is a reference to x; assigning through r changes x to 9.

Next Question →

Practice Set 6 · Question 55 of 150

Question 55. A class owns a dynamically allocated buffer. Which special member is used when an existing object is assigned from another existing object by copy?

  1. Default constructor
  2. Conversion constructor
  3. Destructor
  4. Copy assignment operator
Show Answer and Explanation

Correct answer: D. Copy assignment operator

An assignment expression such as a = b on two existing objects invokes the copy assignment operator when available.

Next Question →

Practice Set 6 · Question 56 of 150

Question 56. What is printed? class A {public: static int f(){return 6;}}; std::cout<<A::f();

  1. Compilation error
  2. 0
  3. 6
  4. A
Show Answer and Explanation

Correct answer: C. 6

The static member function can be called using the class name and returns 6.

Next Question →

Practice Set 6 · Question 57 of 150

Question 57. What is printed? struct B {virtual int f(){return 1;}}; struct D:B {int f() override {return 5;}}; D d; B& b=d; std::cout<<b.f();

  1. 0
  2. Compilation error
  3. 1
  4. 5
Show Answer and Explanation

Correct answer: D. 5

Virtual dispatch uses D::f because the reference refers to a D object.

Next Question →

Practice Set 6 · Question 58 of 150

Question 58. What is printed? struct C {int n=3; int get() const {return n;}}; const C c; std::cout<<c.get();

  1. Undefined
  2. 0
  3. 3
  4. Compiler error
Show Answer and Explanation

Correct answer: C. 3

A const member function can read the member of a const object.

Next Question →

Practice Set 6 · Question 59 of 150

Question 59. What is printed? std::vector<int> v{1,2}; v.push_back(3); std::cout<<v.size();

  1. 3
  2. 4
  3. 0
  4. 2
Show Answer and Explanation

Correct answer: A. 3

push_back adds a third element.

Next Question →

Practice Set 6 · Question 60 of 150

Question 60. What is printed? struct X {X(int v):n(v){} int n;}; X a(8); std::cout<<a.n;

  1. 1
  2. 8
  3. Undefined
  4. 0
Show Answer and Explanation

Correct answer: B. 8

The constructor member initializer assigns n the value 8.

Continue to Practice Set 7

Practice Set 7 · Question 61 of 150

Question 61. What does this print? int n=3; auto f=[n](){return n+2;}; n=10; std::cout<<f();

  1. 5
  2. 12
  3. 10
  4. Compilation error
Show Answer and Explanation

Correct answer: A. 5

Capturing n by value copies 3 at lambda creation, so f returns 5.

Next Question →

Practice Set 7 · Question 62 of 150

Question 62. Which C++ rule applies to the order of non-static data-member initialization?

  1. Alphabetical member names
  2. Random order
  3. Order written in constructor initializer list
  4. Declaration order in the class
Show Answer and Explanation

Correct answer: D. Declaration order in the class

Non-static members are initialized in their declaration order, independent of initializer-list order.

Next Question →

Practice Set 7 · Question 63 of 150

Question 63. What does the C++ keyword delete mean in Widget(const Widget&)=delete?

  1. Enable garbage collection
  2. Delete every Widget object
  3. Disallow use of that copy-constructor overload
  4. Make the function virtual
Show Answer and Explanation

Correct answer: C. Disallow use of that copy-constructor overload

A deleted special member cannot be selected for a valid call.

Next Question →

Practice Set 7 · Question 64 of 150

Question 64. Which class is automatically noncopyable because it owns a std::unique_ptr member (unless custom copy support is supplied)?

  1. A class with a bool member
  2. A class with a unique_ptr member
  3. A class with an int member
  4. An empty struct
Show Answer and Explanation

Correct answer: B. A class with a unique_ptr member

unique_ptr is move-only, causing the implicit copy constructor of an owning class to be deleted.

Next Question →

Practice Set 7 · Question 65 of 150

Question 65. What is the main benefit of std::unique_ptr?

  1. It forces allocation on the stack
  2. It permits arbitrary shared ownership
  3. It expresses exclusive ownership and automatic deletion
  4. It stores only integers
Show Answer and Explanation

Correct answer: C. It expresses exclusive ownership and automatic deletion

A unique_ptr owns a dynamically allocated object and frees it when ownership ends.

Next Question →

Practice Set 7 · Question 66 of 150

Question 66. Which pair is required for overriding a C++ virtual function?

  1. Same name and a compatible parameter signature
  2. Same name with unrelated parameter types
  3. Only a different return type
  4. Same source-file line
Show Answer and Explanation

Correct answer: A. Same name and a compatible parameter signature

An override must match the relevant virtual signature, including cv/ref qualifiers, with allowable return types.

Next Question →

Practice Set 7 · Question 67 of 150

Question 67. A derived object is accessed through a base pointer. Calling a non-virtual method normally selects which implementation?

  1. Always the most-derived type
  2. A randomly chosen implementation
  3. A template specialization
  4. Based on static type of the expression
Show Answer and Explanation

Correct answer: D. Based on static type of the expression

Non-virtual member calls are resolved based on the expression’s static type.

Next Question →

Practice Set 7 · Question 68 of 150

Question 68. In C++, which keyword specifies that a virtual function cannot be overridden further?

  1. override
  2. final
  3. private
  4. friend
Show Answer and Explanation

Correct answer: B. final

final prevents additional overrides of the specified virtual function.

Next Question →

Practice Set 7 · Question 69 of 150

Question 69. Which class design best models a Car containing and owning its Engine value member?

  1. Only a function overload
  2. Composition
  3. Public inheritance
  4. An unrelated namespace
Show Answer and Explanation

Correct answer: B. Composition

A directly contained Engine subobject has lifetime tied to its Car, a standard composition model.

Next Question →

Practice Set 7 · Question 70 of 150

Question 70. When might aggregation be preferable to composition?

  1. When ownership must be exclusive
  2. When every part must be destroyed with the whole
  3. When compilation is forbidden
  4. When an associated object has an independent lifetime
Show Answer and Explanation

Correct answer: D. When an associated object has an independent lifetime

Aggregation is suitable for non-owning relationships between independently existing objects.

Continue to Practice Set 8

Practice Set 8 · Question 71 of 150

Question 71. Which declaration is a simple C++ function template?

  1. template<class T> T id(T x){return x;}
  2. int id(int); int id(int);
  3. class T = int;
  4. virtual template<class T> void f();
Show Answer and Explanation

Correct answer: A. template<class T> T id(T x){return x;}

The template parameter T makes id generic over suitable argument types.

Next Question →

Practice Set 8 · Question 72 of 150

Question 72. For std::vector<int> v{10,20,30}, what does v.at(3) do?

  1. Returns 10
  2. Returns 30
  3. Throws std::out_of_range
  4. Appends a new element
Show Answer and Explanation

Correct answer: C. Throws std::out_of_range

The valid indices are 0, 1 and 2. The bounds-checked at(3) throws std::out_of_range.

Next Question →

Practice Set 8 · Question 73 of 150

Question 73. Which C++ construct automatically releases owned resources if an exception unwinds the stack?

  1. Unmanaged raw allocations
  2. Ignoring every exception
  3. Only global arrays
  4. Objects whose destructors follow RAII
Show Answer and Explanation

Correct answer: D. Objects whose destructors follow RAII

Stack unwinding destroys automatic objects, allowing RAII cleanup.

Next Question →

Practice Set 8 · Question 74 of 150

Question 74. What happens if an exception escapes a noexcept function?

  1. It is silently ignored
  2. std::terminate is called
  3. The function retries automatically
  4. The compiler inserts a catch
Show Answer and Explanation

Correct answer: B. std::terminate is called

Violating a non-throwing exception specification causes termination.

Next Question →

Practice Set 8 · Question 75 of 150

Question 75. Which handler is generally preferable for standard exceptions?

  1. catch(std::string*)
  2. catch(std::exception e)
  3. catch(const std::exception& e)
  4. catch(int*)
Show Answer and Explanation

Correct answer: C. catch(const std::exception& e)

Catching by const reference avoids slicing and unnecessary copying.

Next Question →

Practice Set 8 · Question 76 of 150

Question 76. Why is serializing a raw pointer’s numerical address generally unsuitable for persistent data?

  1. The address is not a stable meaningful identity across processes or runs
  2. Pointers never have addresses
  3. Files cannot hold digits
  4. All pointer values are zero
Show Answer and Explanation

Correct answer: A. The address is not a stable meaningful identity across processes or runs

An in-process address typically cannot be used to reconstruct an object later.

Next Question →

Practice Set 8 · Question 77 of 150

Question 77. For repeated insertion at the back and indexed random access, which STL container is often a good starting choice?

  1. std::vector
  2. std::forward_list
  3. std::stack
  4. std::priority_queue
Show Answer and Explanation

Correct answer: A. std::vector

vector supports amortized constant-time push_back and constant-time index access.

Next Question →

Practice Set 8 · Question 78 of 150

Question 78. Why should a class expose a small stable public interface?

  1. To make data necessarily global
  2. To avoid constructors
  3. To reduce coupling to its implementation details
  4. To forbid testing
Show Answer and Explanation

Correct answer: C. To reduce coupling to its implementation details

An interface limits dependencies on private representation, simplifying maintenance.

Next Question →

Practice Set 8 · Question 79 of 150

Question 79. Given struct Box {int n;}; Box a{3}; Box b=a; b.n+=4; what is a.n?

  1. Undefined
  2. 3
  3. 4
  4. 7
Show Answer and Explanation

Correct answer: B. 3

Copying a Box copies its integer member. Updating b does not change a.

Next Question →

Practice Set 8 · Question 80 of 150

Question 80. A C++ object contains a std::string member. What handles that member’s cleanup when the object is destroyed?

  1. A compiler warning
  2. Deleting the source file
  3. Manual free() in every caller
  4. The string member’s destructor
Show Answer and Explanation

Correct answer: D. The string member’s destructor

Member subobjects are destroyed automatically when their containing object is destroyed.

Continue to Practice Set 9

Practice Set 9 · Question 81 of 150

Question 81. Given struct Counter {inline static int n=0; Counter(){++n;}}; Counter a,b,c; what is Counter::n?

  1. 1
  2. 2
  3. 3
  4. 0
Show Answer and Explanation

Correct answer: C. 3

Three constructor calls increment the shared static member to three.

Next Question →

Practice Set 9 · Question 82 of 150

Question 82. Given int x=2; int& y=x; ++y; what is x?

  1. 0
  2. Undefined
  3. 2
  4. 3
Show Answer and Explanation

Correct answer: D. 3

y aliases x; incrementing y increments x.

Next Question →

Practice Set 9 · Question 83 of 150

Question 83. What is the danger of returning a reference to a non-static local integer from a C++ function?

  1. A safe persistent reference
  2. A dangling reference after return
  3. A required deep copy
  4. Automatic heap allocation
Show Answer and Explanation

Correct answer: B. A dangling reference after return

The local integer has automatic storage duration and its lifetime ends when the function returns. The returned reference dangles; using it afterward has undefined behaviour.

Next Question →

Practice Set 9 · Question 84 of 150

Question 84. A Library stores non-owning references to existing Book objects. Which relationship is most natural?

  1. Aggregation
  2. Public inheritance from Book
  3. Operator overloading
  4. Exclusive composition
Show Answer and Explanation

Correct answer: A. Aggregation

Independent object lifetimes suggest aggregation.

Next Question →

Practice Set 9 · Question 85 of 150

Question 85. A House owns Room objects stored as direct data members. Which relationship is illustrated?

  1. No ownership
  2. Composition
  3. Virtual dispatch
  4. Generic programming
Show Answer and Explanation

Correct answer: B. Composition

Direct member objects have lifetimes tied to their containing object.

Next Question →

Practice Set 9 · Question 86 of 150

Question 86. Why can publicly deriving Square from a mutable Rectangle with independent width and height setters be problematic?

  1. A square may violate the rectangle interface’s behavioural expectations
  2. It prevents compilation in every case
  3. It makes all members static
  4. It always improves substitution
Show Answer and Explanation

Correct answer: A. A square may violate the rectangle interface’s behavioural expectations

A square requiring equal sides may not satisfy a rectangle interface allowing independent dimensions.

Next Question →

Practice Set 9 · Question 87 of 150

Question 87. Given void f(int); void f(double); which overload is selected by f(2.5)?

  1. Both
  2. Necessarily ambiguous
  3. f(int)
  4. f(double)
Show Answer and Explanation

Correct answer: D. f(double)

The floating-point literal is a double, giving an exact match.

Next Question →

Practice Set 9 · Question 88 of 150

Question 88. Given struct B {virtual int f() const {return 1;}}; struct D:B {int f() const override {return 4;}}; D d; B* p=&d; what is p->f()?

  1. Undefined
  2. 1
  3. 4
  4. 5
Show Answer and Explanation

Correct answer: C. 4

Virtual dispatch selects the derived override.

Next Question →

Practice Set 9 · Question 89 of 150

Question 89. A derived method g(int) hides its base’s g(double). What can reintroduce the base overload set?

  1. static Base::g;
  2. friend Base::g;
  3. delete Base::g;
  4. using Base::g;
Show Answer and Explanation

Correct answer: D. using Base::g;

A using declaration can expose the base overload set.

Next Question →

Practice Set 9 · Question 90 of 150

Question 90. Given std::vector<int> v{5,10,15}; what is v.front()+v.back()?

  1. 30
  2. 15
  3. 20
  4. 25
Show Answer and Explanation

Correct answer: C. 20

The first and last elements are 5 and 15, whose sum is 20.

Continue to Practice Set 10

Practice Set 10 · Question 91 of 150

Question 91. Which category of iterators is required by std::sort?

  1. Random-access iterators
  2. Only output iterators
  3. Only input iterators
  4. No iterators
Show Answer and Explanation

Correct answer: A. Random-access iterators

std::sort requires random-access iterators.

Next Question →

Practice Set 10 · Question 92 of 150

Question 92. What must be true for a template expression a < b to compile for a substituted type?

  1. No instantiation can occur
  2. The comparison expression must be valid
  3. Every type must be a pointer
  4. The type must be int
Show Answer and Explanation

Correct answer: B. The comparison expression must be valid

Template requirements depend on the operations used on the substituted types.

Next Question →

Practice Set 10 · Question 93 of 150

Question 93. When a constructor throws, what happens to its fully constructed member subobjects?

  1. Their destructors run
  2. They always leak
  3. The complete object’s destructor body runs
  4. They survive indefinitely
Show Answer and Explanation

Correct answer: A. Their destructors run

Successfully constructed members are destroyed during exception unwinding.

Next Question →

Practice Set 10 · Question 94 of 150

Question 94. Which C++ smart pointer offers reference-counted shared ownership?

  1. std::weak_ptr alone
  2. Non-owning raw pointer
  3. std::unique_ptr
  4. std::shared_ptr
Show Answer and Explanation

Correct answer: D. std::shared_ptr

shared_ptr supports shared ownership through reference counting.

Next Question →

Practice Set 10 · Question 95 of 150

Question 95. What is std::weak_ptr commonly useful for?

  1. Replacing every constructor
  2. Creating a strong ownership cycle
  3. Observing a shared object without owning it
  4. Exclusive destruction
Show Answer and Explanation

Correct answer: C. Observing a shared object without owning it

weak_ptr allows non-owning access and can help avoid reference cycles.

Next Question →

Practice Set 10 · Question 96 of 150

Question 96. What does the basic exception-safety guarantee promise?

  1. Success always
  2. Valid invariants and no leaked resources
  3. Absolutely unchanged values
  4. No thrown exceptions
Show Answer and Explanation

Correct answer: B. Valid invariants and no leaked resources

An operation can modify state but must preserve invariants and avoid leaks.

Next Question →

Practice Set 10 · Question 97 of 150

Question 97. Which condition is essential for a correct equality operator on value objects?

  1. Multiple inheritance
  2. A virtual destructor in every class
  3. Reflexivity, symmetry and transitivity
  4. Automatic dynamic allocation
Show Answer and Explanation

Correct answer: C. Reflexivity, symmetry and transitivity

Equality should behave as an equivalence relation, including symmetry and transitivity.

Next Question →

Practice Set 10 · Question 98 of 150

Question 98. Which C++ feature permits a class to inherit from two direct base classes?

  1. Multiple inheritance
  2. Function hiding
  3. Serialization
  4. Single inheritance
Show Answer and Explanation

Correct answer: A. Multiple inheritance

A derived class can list two or more direct base classes.

Next Question →

Practice Set 10 · Question 99 of 150

Question 99. What does virtual inheritance help resolve in a diamond hierarchy?

  1. Every compiler warning
  2. Overloaded operators
  3. Invalid input
  4. Duplicate common base subobjects
Show Answer and Explanation

Correct answer: D. Duplicate common base subobjects

A virtual base is shared within the most-derived object rather than duplicated along multiple paths.

Next Question →

Practice Set 10 · Question 100 of 150

Question 100. Which principle says derived objects should uphold the behavioural expectations of their base type?

  1. Greedy choice property
  2. Liskov substitution principle
  3. FIFO principle
  4. Hashing principle
Show Answer and Explanation

Correct answer: B. Liskov substitution principle

Substitution requires preserving the base abstraction’s contract.

Continue to Practice Set 11

Practice Set 11 · Question 101 of 150

Question 101. Which type is suitable for a non-owning link that breaks a shared_ptr ownership cycle?

  1. std::string
  2. std::weak_ptr
  3. std::unique_ptr
  4. std::vector
Show Answer and Explanation

Correct answer: B. std::weak_ptr

weak_ptr observes a shared object without contributing to shared ownership.

Next Question →

Practice Set 11 · Question 102 of 150

Question 102. Why is shallow copying an owning raw pointer dangerous?

  1. It always duplicates the allocation
  2. It changes every integer to zero
  3. It disables destructors
  4. It can make multiple objects delete the same resource
Show Answer and Explanation

Correct answer: D. It can make multiple objects delete the same resource

Two objects can end up believing that they exclusively own the same allocation.

Next Question →

Practice Set 11 · Question 103 of 150

Question 103. What is the Rule of Zero in C++ class design?

  1. Prefer RAII members that avoid writing custom special member functions
  2. Declare no class members
  3. Use only static functions
  4. Avoid standard containers
Show Answer and Explanation

Correct answer: A. Prefer RAII members that avoid writing custom special member functions

Resource-managing member types let the compiler-generated operations often suffice.

Next Question →

Practice Set 11 · Question 104 of 150

Question 104. What is a class template?

  1. A compiler warning
  2. A serialized file
  3. A pattern for defining a family of classes
  4. A temporary runtime object only
Show Answer and Explanation

Correct answer: C. A pattern for defining a family of classes

Template arguments determine particular instantiations of the class pattern.

Next Question →

Practice Set 11 · Question 105 of 150

Question 105. Which issue can make raw binary serialization nonportable?

  1. The existence of constructors
  2. Comments
  3. The use of local variables
  4. Differences in byte order and object representation
Show Answer and Explanation

Correct answer: D. Differences in byte order and object representation

Endianness, padding and type representation may differ across systems.

Next Question →

Practice Set 11 · Question 106 of 150

Question 106. Which exception-safety guarantee promises that a failed operation leaves observable state unchanged?

  1. Basic guarantee
  2. Strong guarantee
  3. Only memory safety
  4. No guarantee
Show Answer and Explanation

Correct answer: B. Strong guarantee

The strong guarantee gives rollback-like behaviour on an operation’s failure.

Next Question →

Practice Set 11 · Question 107 of 150

Question 107. How can polymorphic object slicing be avoided?

  1. Delete all virtual methods
  2. Disable inheritance
  3. Use references or pointers to base interfaces
  4. Copy derived objects by base value
Show Answer and Explanation

Correct answer: C. Use references or pointers to base interfaces

References and pointers preserve the object’s dynamic type.

Next Question →

Practice Set 11 · Question 108 of 150

Question 108. Which design choice most directly prevents a cyclic ownership leak between two std::shared_ptr-managed objects?

  1. Using std::weak_ptr for one non-owning link
  2. Calling push_back on both objects
  3. Using a larger vector capacity
  4. Making every field public
Show Answer and Explanation

Correct answer: A. Using std::weak_ptr for one non-owning link

If both objects strongly own one another, their reference counts may never reach zero. Replacing an appropriate strong link with weak_ptr breaks the ownership cycle.

Next Question →

Practice Set 11 · Question 109 of 150

Question 109. What does the C++ access specifier public permit?

  1. Access only from the class itself
  2. Access only through a friend
  3. No access from any function
  4. Access from permitted external code
Show Answer and Explanation

Correct answer: D. Access from permitted external code

Public class members are generally accessible through valid expressions from outside the class.

Next Question →

Practice Set 11 · Question 110 of 150

Question 110. Which C++ expression creates an automatic local object of class Book?

  1. Book b;
  2. Book* b = nullptr;
  3. delete b;
  4. sizeof(Book);
Show Answer and Explanation

Correct answer: A. Book b;

A declaration such as Book b creates a local object when Book is default-constructible.

Next Question →

Practice Set 12 · Question 111 of 150

Question 111. What is an object’s invariant?

  1. A property that is always false
  2. A property expected to hold in its valid states
  3. The size of its source file
  4. Its compiler version
Show Answer and Explanation

Correct answer: B. A property expected to hold in its valid states

Class invariants describe relationships that valid object states preserve.

Next Question →

Practice Set 12 · Question 112 of 150

Question 112. Why might a member be declared private?

  1. To require global variables
  2. To make the member an independent function
  3. To protect representation details behind an interface
  4. To forbid all constructors
Show Answer and Explanation

Correct answer: C. To protect representation details behind an interface

Private access enables controlled use of internal representation.

Next Question →

Practice Set 12 · Question 113 of 150

Question 113. Which principle asks classes to have focused responsibilities?

  1. FIFO principle
  2. Pigeonhole principle
  3. Binary search rule
  4. Single responsibility principle
Show Answer and Explanation

Correct answer: D. Single responsibility principle

Focused responsibilities help reduce unrelated reasons for a class to change.

Next Question →

Practice Set 12 · Question 114 of 150

Question 114. Why are virtual functions useful?

  1. They enable runtime dispatch through base interfaces
  2. They disable all inheritance
  3. They convert classes to templates
  4. They automatically prevent memory allocation
Show Answer and Explanation

Correct answer: A. They enable runtime dispatch through base interfaces

Virtual dispatch selects the final overrider based on a polymorphic object’s dynamic type.

Next Question →

Practice Set 12 · Question 115 of 150

Question 115. What is the purpose of a C++ initializer list in a constructor?

  1. Declare an unrelated namespace
  2. Initialize bases and data members
  3. Sort all arrays
  4. Catch all exceptions
Show Answer and Explanation

Correct answer: B. Initialize bases and data members

Member-initializer lists initialize subobjects before the constructor body.

Next Question →

Practice Set 12 · Question 116 of 150

Question 116. Which expression normally accesses a member via a pointer to an object?

  1. p::value
  2. p#value
  3. p->value
  4. p.value when p is a pointer
Show Answer and Explanation

Correct answer: C. p->value

The arrow operator accesses a member through a pointer.

Next Question →

Practice Set 12 · Question 117 of 150

Question 117. What does a C++ reference variable generally provide?

  1. A standalone file on disk
  2. An untyped memory allocator
  3. A compiler command
  4. An alias to another object
Show Answer and Explanation

Correct answer: D. An alias to another object

A reference denotes an existing object or function according to its type.

Next Question →

Practice Set 12 · Question 118 of 150

Question 118. What does the override specifier prevent?

  1. Accidental failure to match a base virtual function
  2. All calls to the function
  3. Any use of a constructor
  4. Every runtime exception
Show Answer and Explanation

Correct answer: A. Accidental failure to match a base virtual function

The compiler checks that an override-designated function overrides a base virtual function.

Next Question →

Practice Set 12 · Question 119 of 150

Question 119. What is dynamic binding?

  1. Fixing all variables at compile time
  2. Selecting an overridden operation at runtime
  3. Linking object files only
  4. Sorting dynamic arrays
Show Answer and Explanation

Correct answer: B. Selecting an overridden operation at runtime

Dynamic dispatch resolves virtual calls according to the runtime object type.

Next Question →

Practice Set 12 · Question 120 of 150

Question 120. Which operation cannot be overloaded in C++?

  1. The equality operator ==
  2. The subscript operator []
  3. The scope resolution operator ::
  4. The addition operator + for user-defined types
Show Answer and Explanation

Correct answer: C. The scope resolution operator ::

C++ does not allow overloading the scope-resolution operator.

Next Question →

Practice Set 13 · Question 121 of 150

Question 121. What does a default constructor allow when accessible and applicable?

  1. Mandatory deletion of an object
  2. Only copy initialization
  3. Creation of an abstract class instance
  4. Construction without explicit arguments
Show Answer and Explanation

Correct answer: D. Construction without explicit arguments

A default constructor can be invoked with no supplied arguments.

Next Question →

Practice Set 13 · Question 122 of 150

Question 122. When should a destructor release resources?

  1. When the object owns resources that need cleanup
  2. Whenever a local variable is read
  3. Only when a loop ends
  4. After every assignment
Show Answer and Explanation

Correct answer: A. When the object owns resources that need cleanup

RAII ties the release of owned resources to object lifetime.

Next Question →

Practice Set 13 · Question 123 of 150

Question 123. What can cause a memory leak with raw owning pointers?

  1. Calling a pure function
  2. Losing an allocated pointer without releasing its allocation
  3. Using a const reference
  4. Creating an automatic int
Show Answer and Explanation

Correct answer: B. Losing an allocated pointer without releasing its allocation

Unreleased allocations can remain inaccessible and consume memory.

Next Question →

Practice Set 13 · Question 124 of 150

Question 124. What is the C++ rule of five concerned with?

  1. Five integer types
  2. Five standard algorithms
  3. Copying, moving and destruction for resource-owning classes
  4. Five kinds of loops
Show Answer and Explanation

Correct answer: C. Copying, moving and destruction for resource-owning classes

The rule of five covers the destructor and copy/move construction and assignment.

Next Question →

Practice Set 13 · Question 125 of 150

Question 125. What is move semantics intended to support?

  1. Copying every byte twice
  2. Preventing all assignments
  3. Converting every pointer to a reference
  4. Transferring resources from a source object when permitted
Show Answer and Explanation

Correct answer: D. Transferring resources from a source object when permitted

Move operations can transfer ownership instead of expensive copying.

Next Question →

Practice Set 13 · Question 126 of 150

Question 126. Which expression constructs a dynamically allocated Widget in C++?

  1. new Widget()
  2. delete Widget()
  3. Widget::new()
  4. sizeof Widget()
Show Answer and Explanation

Correct answer: A. new Widget()

A new-expression allocates storage and constructs an object.

Next Question →

Practice Set 13 · Question 127 of 150

Question 127. What is the preferred owner for one dynamically allocated object in modern C++?

  1. A reference to destroyed memory
  2. std::unique_ptr
  3. A non-owning raw pointer only
  4. An unrelated global int
Show Answer and Explanation

Correct answer: B. std::unique_ptr

unique_ptr expresses exclusive ownership with automatic destruction.

Next Question →

Practice Set 13 · Question 128 of 150

Question 128. What is an interface in object-oriented design?

  1. A file extension
  2. A sorting order only
  3. A set of operations exposed to clients
  4. The amount of RAM installed
Show Answer and Explanation

Correct answer: C. A set of operations exposed to clients

An interface defines available interactions without requiring knowledge of implementation details.

Next Question →

Practice Set 13 · Question 129 of 150

Question 129. What is a key benefit of composition over unnecessary inheritance?

  1. It guarantees zero memory use
  2. It forbids multiple objects
  3. It makes virtual functions illegal
  4. It can reuse behavior without creating an is-a relationship
Show Answer and Explanation

Correct answer: D. It can reuse behavior without creating an is-a relationship

Composition models has-a relationships and can reduce coupling.

Next Question →

Practice Set 13 · Question 130 of 150

Question 130. What is dependency injection?

  1. Providing collaborators to an object from outside
  2. Deleting all inherited members
  3. Changing every variable to static
  4. A syntax error
Show Answer and Explanation

Correct answer: A. Providing collaborators to an object from outside

External collaborators make dependencies explicit and easier to substitute in tests.

Next Question →

Practice Set 14 · Question 131 of 150

Question 131. What does const correctness help express?

  1. That every object is global
  2. Which operations should not modify an object
  3. That the program cannot throw
  4. That all members are public
Show Answer and Explanation

Correct answer: B. Which operations should not modify an object

Const qualifiers communicate and help enforce nonmodifying access.

Next Question →

Practice Set 14 · Question 132 of 150

Question 132. When an exception is thrown, what is stack unwinding?

  1. Sorting the call stack
  2. Deleting every source file
  3. Destroying completed automatic objects as scopes are exited
  4. Restarting compilation
Show Answer and Explanation

Correct answer: C. Destroying completed automatic objects as scopes are exited

Unwinding leaves active scopes while destructors perform cleanup.

Next Question →

Practice Set 14 · Question 133 of 150

Question 133. Which standard container offers key-value mappings with ordered keys?

  1. std::vector
  2. std::stack
  3. std::queue
  4. std::map
Show Answer and Explanation

Correct answer: D. std::map

std::map maintains an ordered associative mapping.

Next Question →

Practice Set 14 · Question 134 of 150

Question 134. Which standard container supports average constant-time key lookup under typical hashing assumptions?

  1. std::unordered_map
  2. std::list
  3. std::forward_list
  4. std::priority_queue
Show Answer and Explanation

Correct answer: A. std::unordered_map

Hash-based unordered maps have average constant-time search under suitable hashing.

Next Question →

Practice Set 14 · Question 135 of 150

Question 135. What is a template specialization?

  1. A virtual base
  2. A customized template definition for particular arguments
  3. An automatically deleted constructor
  4. An exception handler
Show Answer and Explanation

Correct answer: B. A customized template definition for particular arguments

Specialization can provide behavior for selected template arguments.

Next Question →

Practice Set 14 · Question 136 of 150

Question 136. What is a lambda expression in modern C++?

  1. A class access specifier
  2. An operating-system process
  3. A way to define a function object inline
  4. A file opening mode
Show Answer and Explanation

Correct answer: C. A way to define a function object inline

A lambda creates a callable closure object.

Next Question →

Practice Set 14 · Question 137 of 150

Question 137. What is a virtual destructor particularly important for?

  1. Calling only static member functions
  2. Allocating local integers
  3. Sorting a vector
  4. Deleting derived objects through base pointers
Show Answer and Explanation

Correct answer: D. Deleting derived objects through base pointers

A virtual base destructor ensures complete polymorphic destruction.

Next Question →

Practice Set 14 · Question 138 of 150

Question 138. What is a copy constructor expected to initialize?

  1. A new object using another object
  2. The base class’s source code
  3. A file only
  4. An already existing object via assignment
Show Answer and Explanation

Correct answer: A. A new object using another object

Copy construction creates a distinct object from a source object.

Next Question →

Practice Set 14 · Question 139 of 150

Question 139. What does operator= typically do for an existing object?

  1. Create a fresh type
  2. Assign a new state to the existing object
  3. Construct a completely unrelated file
  4. Always destroy the program
Show Answer and Explanation

Correct answer: B. Assign a new state to the existing object

The assignment operator updates an already constructed object.

Next Question →

Practice Set 14 · Question 140 of 150

Question 140. What is the purpose of unit testing a class?

  1. Make every method virtual
  2. Eliminate all future bugs
  3. Verify individual methods and object behavior
  4. Set monitor brightness
Show Answer and Explanation

Correct answer: C. Verify individual methods and object behavior

Unit tests exercise defined class behavior against expectations.

Next Question →

Practice Set 15 · Question 141 of 150

Question 141. Which SOLID guideline favors adding behavior without editing stable existing code?

  1. Least-recently-used principle
  2. Handshaking lemma
  3. Binary heap property
  4. Open-closed principle
Show Answer and Explanation

Correct answer: D. Open-closed principle

The open-closed principle encourages extension while limiting modification.

Next Question →

Practice Set 15 · Question 142 of 150

Question 142. What is a covariant return type permitted in certain C++ overrides?

  1. A pointer or reference to a more-derived class type
  2. An arbitrary unrelated integer
  3. A return type always changed to void
  4. A parameter pack
Show Answer and Explanation

Correct answer: A. A pointer or reference to a more-derived class type

C++ allows certain covariant class pointer/reference return types for virtual overrides.

Next Question →

Practice Set 15 · Question 143 of 150

Question 143. Can constructors in C++ themselves be virtual?

  1. Only as templates
  2. No
  3. Yes, always
  4. Only when private
Show Answer and Explanation

Correct answer: B. No

Construction establishes dynamic type, and C++ constructors cannot be virtual.

Next Question →

Practice Set 15 · Question 144 of 150

Question 144. What does the nullptr literal represent in C++?

  1. A string terminator object
  2. A valid object address in every case
  3. A null pointer value
  4. An integer with value one
Show Answer and Explanation

Correct answer: C. A null pointer value

nullptr is the dedicated null pointer literal.

Next Question →

Practice Set 15 · Question 145 of 150

Question 145. What is the benefit of using an enum class over an unscoped enum?

  1. Automatic deep copying of all objects
  2. Elimination of all casting
  3. A guarantee of memory-free code
  4. Stronger scoping and type safety
Show Answer and Explanation

Correct answer: D. Stronger scoping and type safety

Scoped enumerations limit unintended name and implicit conversion conflicts.

Next Question →

Practice Set 15 · Question 146 of 150

Question 146. What is an object’s dynamic type in a polymorphic hierarchy?

  1. The type of the actual most-derived object
  2. Always the declared pointer type
  3. The compiler version
  4. The data-member count
Show Answer and Explanation

Correct answer: A. The type of the actual most-derived object

Virtual dispatch follows the dynamic type of the object, not merely the pointer’s static type.

Next Question →

Practice Set 15 · Question 147 of 150

Question 147. Which design pattern creates objects through an interface rather than exposing construction details?

  1. Binary search
  2. Factory method
  3. Insertion sort
  4. Pigeonhole principle
Show Answer and Explanation

Correct answer: B. Factory method

A factory method delegates object creation through an abstraction.

Next Question →

Practice Set 15 · Question 148 of 150

Question 148. Why use a virtual function’s base interface rather than downcasting routinely?

  1. To ensure every pointer is null
  2. To force code duplication
  3. To reduce dependence on concrete derived types
  4. To disable inheritance
Show Answer and Explanation

Correct answer: C. To reduce dependence on concrete derived types

Working through a base abstraction improves substitutability and decoupling.

Next Question →

Practice Set 15 · Question 149 of 150

Question 149. What does std::make_unique<T>() typically return?

  1. A borrowed raw array
  2. A std::weak_ptr<T>
  3. An int containing the object size
  4. A std::unique_ptr<T> owning a newly created T
Show Answer and Explanation

Correct answer: D. A std::unique_ptr<T> owning a newly created T

make_unique constructs an object and returns an exclusive-ownership smart pointer.

Next Question →

Practice Set 15 · Question 150 of 150

Question 150. What does a const reference parameter often help avoid?

  1. Unnecessary copying of the passed object
  2. All function calls
  3. All access checking
  4. Every possible compiler error
Show Answer and Explanation

Correct answer: A. Unnecessary copying of the passed object

Passing by const reference allows read-only access without copying large objects.

Restart from Question 1

Return to the full course materials and worked examples