A friend function is defined outside the class but has the ability to access the _______ members of the class. 

  • static 
  • private 
  • public 
  • mutable
A friend function, although not a member of a class, can access its private and protected members. This provides an external function the capability to interact closely with the internals of the class.

Consider a scenario where you have a large dataset and you need to frequently erase and insert elements in the middle of the data. Which STL container should be avoided in order to prevent frequent reallocations and data movements? 

  • vector 
  • list 
  • deque 
  • set
The STL vector maintains its elements in a contiguous block of memory. Inserting or erasing elements in the middle requires shifting elements, which can be expensive, especially for large datasets. This might cause frequent reallocations and data movements, impacting performance.

Which type of loop is best suited when the number of iterations is known ahead of time? 

  • do-while loop 
  • while loop 
  • for loop 
  • recursive function
A for loop is best suited when the number of iterations is known ahead of time, as it allows for an initial statement (typically for initialization), a condition check, and an increment/decrement operation in a compact syntax.

How does C++ enforce encapsulation and abstraction in multi-level inheritance scenarios? 

  • By using abstract classes only. 
  • By private and protected access specifiers. 
  • Through use of virtual functions alone. 
  • By using friend functions.
In C++, encapsulation and abstraction in multi-level inheritance are primarily enforced using private and protected access specifiers. While private members are inaccessible outside the class, protected members can be accessed by derived classes, ensuring a controlled inheritance mechanism.

What is the main purpose of using protected access specifier in base class? 

  • To restrict all external access 
  • To allow all classes to access 
  • To enable inheritance only 
  • To share methods with friends
The protected access specifier in C++ is mainly used to ensure that members are inaccessible from outside the class (just like private members). However, the difference is that protected members can be accessed by derived classes. This allows for more secure inheritance, giving derived classes access without exposing to the external world.

Which of the following data types can store a non-integer number? 

  • char 
  • float 
  • int 
  • short
In C++, the float data type is used to store single-precision floating-point numbers, which can represent non-integer values (like 3.14). It provides a way to store decimal numbers with a floating decimal point.

Which of the following is a characteristic of an enum in C++? 

  • It can store multiple values at once 
  • It is used for dynamic memory allocation 
  • It consists of a set of named integer constants 
  • It defines object behavior
An enum in C++ consists of a set of named integer constants. It allows for creating a new data type where each value is represented by a unique name rather than a numeric value, enhancing the readability of the code. This means that an enum can only hold one of the values that you've defined.

How does encapsulation aid in reducing software development complexity? 

  • By allowing multiple inheritance. 
  • By segregating the program into separate modules. 
  • By promoting reusability of code. 
  • By bundling data and methods that operate on that data.
Encapsulation in C++ is the bundling together of data and the methods that operate on that data, restricting direct access to some of the object's components. This is a fundamental concept in object-oriented programming. By encapsulating data and functions into a single unit, we can hide the internal details and reduce complexity.

To achieve runtime polymorphism in C++, _______ are used. 

  • classes 
  • macros 
  • virtual functions 
  • inline functions
Runtime polymorphism in C++ is achieved through the use of virtual functions. These are functions in the base class that you can override in derived classes. The decision about which method to call (the one in the base class or the one in the derived class) is made at runtime, based on the actual type of the object being pointed to, rather than the type of the pointer. This allows for more dynamic behavior and is a core feature of object-oriented programming in C++.

What is the default access specifier for a base class in C++? 

  • private 
  • public 
  • protected 
  • internal
In C++, if you don't specify an access specifier for the members of a class, they are implicitly set to private. This means that they can't be accessed or viewed from objects of the class or any derived class unless friends.