What is the default access specifier for class members in C++?
Answer: B
In C++, the default access specifier for class members is 'private'. Only for structs, the default is 'public'.
Q.2Easy
Which keyword is used to create a derived class in C++?
Answer: C
In C++, the colon (:) operator is used to specify inheritance. Syntax: class Derived : public Base {}
Q.3Easy
What is the purpose of a virtual function in C++?
Answer: B
Virtual functions enable runtime polymorphism by allowing derived classes to override base class methods. The correct method is called based on the object type at runtime.
Q.4Easy
Which access specifier allows access from derived classes but not from outside?
Answer: C
The 'protected' access specifier allows access from within the class and derived classes, but not from outside the class hierarchy.
Q.5Easy
What is the purpose of 'this' pointer in C++?
Answer: B
The 'this' pointer is an implicit pointer that points to the current object. It's used to access member variables and functions of the current object.
Advertisement
Q.6Easy
What happens when you try to access a private member of a class from outside?
Answer: B
Accessing private members from outside the class results in a compilation error. The compiler enforces access control at compile time.
Q.7Easy
In C++, which keyword is used to prevent a class from being inherited?
Answer: A
The 'final' keyword (introduced in C++11) prevents a class from being used as a base class for further inheritance.
Q.8Easy
Which of the following best describes polymorphism in C++?
Answer: B
Polymorphism allows objects to take on multiple forms through compile-time (overloading) and runtime (overriding) mechanisms, enabling flexible and extensible code design.
Q.9Easy
In C++, what is the correct syntax for a pure virtual function?
Answer: A
A pure virtual function in C++ is declared with the 'virtual' keyword followed by '= 0'. This makes the function purely virtual, requiring derived classes to implement it.
Q.10Easy
Which access specifier allows a derived class to access members of the base class?
Answer: B
The 'protected' access specifier allows derived classes to access members of the base class while keeping them hidden from the outside world.
Q.11Easy
In C++, what is the correct way to implement an interface-like behavior?
Answer: B
In C++, an interface-like behavior is achieved using an abstract class containing only pure virtual functions. Derived classes must implement these functions, enforcing a contract.
Q.12Easy
Which of the following is a key principle of Object-Oriented Programming that bundles data and methods together?
Answer: A
Encapsulation is the bundling of data (attributes) and methods (functions) into a single unit called a class, hiding internal details from the outside world.
Q.13Easy
In C++, what is the default access specifier for members of a class?
Answer: B
In C++, the default access specifier for class members is private, meaning they are not accessible outside the class by default.
Q.14Easy
Which of the following correctly describes a pure virtual function in C++?
Answer: B
A pure virtual function is declared with '= 0' and has no implementation in the base class. Derived classes must override it to create a concrete class.
Q.15Easy
In C++, an abstract class is a class that contains at least one _______.
Answer: C
An abstract class must contain at least one pure virtual function, making it impossible to instantiate objects of that class directly.
Q.16Easy
What does the 'this' pointer represent in C++?
Answer: B
'this' is a constant pointer that points to the current object instance, allowing members to reference themselves.
Q.17Easy
Which of the following best defines abstraction in OOP?
Answer: A
Abstraction is the process of hiding complex implementation details and exposing only the necessary interface to users.
Q.18Easy
What is the correct syntax for inheriting from multiple base classes in C++?
Answer: C
Multiple inheritance in C++ uses the syntax 'class Derived : public Base1, public Base2 { };' with appropriate access specifiers.
Q.19Easy
What is the access specifier for class members that are accessible only within the same class?
Answer: C
Private members are accessible only within the same class. Public members are accessible from anywhere, and protected members are accessible in derived classes.
Q.20Easy
In C++, when a derived class object is created, which constructor is called first?
Answer: B
The base class constructor is always called before the derived class constructor to ensure proper initialization of inherited members.