Challenge your understanding of C++ object-oriented programming concepts as applied in backend development scenarios. This quiz assesses essential principles like inheritance, polymorphism, encapsulation, and class design with practical examples from typical C++ backend projects.
In a backend project, you design a base class called APIHandler and a derived class UserAPIHandler. Which C++ feature allows UserAPIHandler to use public methods inherited from APIHandler?
Explanation: Inheritance lets the UserAPIHandler class automatically use the public methods and properties defined in APIHandler. Abstraction refers to hiding complex details, not to reuse or sharing of methods. Overloading means having methods with the same name but different parameters, which is unrelated here. Composition is a different approach where objects are combined rather than inherited.
In a class representing a database record, which access modifier ensures that data members can only be changed by member functions, not directly?
Explanation: Private members can only be accessed or modified by member functions of the same class, ensuring encapsulation of sensitive data. Public allows unrestricted access which is insecure for data. Protected gives access to derived classes, not external code. External is not a valid C++ access modifier.
Which C++ OOP concept lets you call a base class pointer function and execute the derived class's overridden method in a microservice scenario?
Explanation: Polymorphism enables base class pointers to call methods that may be overridden in derived classes, selecting the correct version at runtime. Delegation refers to handing responsibility from one object to another, which is different. Defaulting and static linking are unrelated to runtime method selection.
When designing a User class for backend authentication, which C++ principle ensures that the password can only be modified through a setter method?
Explanation: Encapsulation hides the internal representation by keeping data private and exposing setter methods, which is essential for security. Abstraction focuses on simplifying interfaces, while polymorphism and multiple inheritance do not directly address data access control.
When creating a Logger object to record backend events, what is the primary reason for defining a constructor in the Logger class?
Explanation: A constructor sets up initial values or acquires resources when an object is created. Creating multiple copies relates to copy constructors. Operator overloading and inheritance are not primary reasons for the basic constructor's existence.
To allow custom plugins in a backend system, you define a virtual function called execute(). What does declaring it as virtual allow?
Explanation: Making a function virtual allows derived classes (like plugin implementations) to override its behavior. Private access restricts visibility; virtual has no effect on access level. Preventing inheritance is not related, and functions are not duplicated by making them virtual.
You need multiple parsers for different data formats. What kind of class should the base DataParser be if it just declares an interface with pure virtual functions?
Explanation: A class with at least one pure virtual function is abstract and cannot be instantiated, making it ideal for defining interfaces. Final and sealed are used to prevent further subclassing. Partial classes are not a feature in C++.
In a method for a backend Session class, you reference member variables using 'this->'. What does the 'this' pointer represent?
Explanation: The 'this' pointer refers to the object invoking the method, enabling access to its members. It does not point to the parent class, previous object, or the global namespace.
Why might you overload constructors in a FileHandler class for backend resource management?
Explanation: Constructor overloading provides flexibility for initialization with different parameters. It does not control inheritance, provide multiple destructors (C++ only allows one), or directly disable copying.
In a backend application, why would you use a static member variable in a Config class?
Explanation: A static member belongs to the class, not any specific instance, making it ideal for shared data like configuration. It does not affect deletion, is unrelated to polymorphism, and does not directly enforce encapsulation.
Which C++ keyword allows an external function to access private data of a class, such as a utility function for serialization?
Explanation: The 'friend' keyword permits external functions or other classes to access private and protected members of a class. The other options are not valid C++ keywords.
When a backend object like a DBConnection is deleted, what is the main purpose of its destructor?
Explanation: Destructors are responsible for cleaning up, such as freeing memory or closing files. Hiding implementation details and operator overriding are not the jobs of destructors, while multiple inheritance is unrelated.
If a backend OrderService contains an instance of Logger rather than inheriting from it, what OOP relationship is this?
Explanation: Composition means a class contains objects of another class, such as OrderService containing a Logger. Inheritance means extending another class. Aggregation is similar but often implies a weaker relationship, while polymorphism refers to method overriding.
In a backend application, if the base Repository class marks a method as protected, what does this restrict?
Explanation: Protected members are accessible within the class itself and its subclasses, not from outside. Public access is more open, unrelated classes cannot access protected members, and friend functions can access protected members if declared.
How can operator overloading be helpful in designing a QueryBuilder class for a backend project?
Explanation: Operator overloading allows objects to interact using custom definitions of operators like + or <<, which can make building query chains expressive and readable. It does not affect global variables, does more than just type safety, and is unrelated to inheritance hierarchies.
In a struct representing a backend response, how can you ensure all members have default values upon object creation?
Explanation: Member initializers or a default constructor are used to assign default values when an object is created. Mere public access does not initialize values, virtual methods concern behavior, and abstract structs are not a C++ concept.