Which of the following is NOT a type of polymorphism in C++?
Answer: C
Memory Polymorphism is not a recognized type of polymorphism in C++. The main types are compile-time (static), runtime (dynamic), and parametric polymorphism.
Q.22Medium
What will be the output of the following code?
class A { public: virtual void func() { cout << "A"; } };
class B : public A { public: void func() { cout << "B"; } };
A *ptr = new B();
ptr->func();
delete ptr;
Answer: B
Since func() is virtual and ptr points to a B object, B::func() is called due to dynamic dispatch, printing 'B'.
Q.23Medium
In C++, what is a friend function?
Answer: B
A friend function is declared using the 'friend' keyword and can access private and protected members of the class, breaking encapsulation when necessary.
Q.24Medium
In C++11 and later, what does the 'override' keyword ensure when used with virtual functions?
Answer: B
The 'override' keyword (C++11) provides compile-time checking to ensure that a function actually overrides a virtual function from the base class.
Q.25Medium
Which of the following statements about constructors in C++ is FALSE?
Answer: B
Constructors do not have a return type, not even void. They are special member functions that automatically initialize objects when created.
Advertisement
Q.26Medium
In C++, what is the default access specifier for class members?
Answer: C
In C++, the default access specifier for class members is private. For structs, it is public.
Q.27Medium
What is the output of the following code?
class Base { public: void show() { cout << "Base"; } };
class Derived : public Base { public: void show() { cout << "Derived"; } };
int main() { Base *b = new Derived(); b->show(); }
Answer: A
Without the virtual keyword, static binding occurs. The function called is determined by the pointer type (Base*), not the actual object type. Output is 'Base'.
Q.28Medium
Which statement about 'this' pointer is correct?
Answer: B
The 'this' pointer is an implicit pointer that points to the current object. It is automatically available in non-static member functions.
Q.29Medium
What is the purpose of using static members in a class?
Answer: B
Static members belong to the class, not to individual objects. All objects of the class share the same static member, making it useful for counters, constants, etc.
Q.30Medium
Which of the following is an example of compile-time polymorphism?
Answer: B
Function overloading is resolved at compile time. Virtual functions and dynamic casting are examples of runtime polymorphism.
Q.31Medium
What is the main difference between composition and inheritance?
Answer: A
Composition represents a 'has-a' relationship where a class contains objects of other classes. Inheritance represents an 'is-a' relationship where a class extends another class.
Q.32Medium
Which of the following correctly uses pure virtual functions?
Answer: B
Abstract classes with pure virtual functions cannot be instantiated. Derived classes must override pure virtual functions. Option B correctly shows this pattern.
Q.33Medium
What will be the output of the following code?
class Base { public: virtual void display() { cout << "Base"; } };
class Derived : public Base { public: void display() { cout << "Derived"; } };
int main() { Base *ptr = new Derived(); ptr->display(); }
Answer: B
This demonstrates runtime polymorphism. The virtual function is called based on the actual object type (Derived), not the pointer type (Base), so 'Derived' is printed.
Q.34Medium
Which of the following statements about abstract classes in C++ is true?
Answer: B
An abstract class in C++ must have at least one pure virtual function. It cannot be instantiated directly, but it can have constructors and other member functions.
Q.35Medium
What is the purpose of the 'const' keyword when used with member functions?
Answer: B
A const member function cannot modify any member variables of the object. It guarantees that the function will not alter the state of the object.
Q.36Medium
In the context of operator overloading, which operator cannot be overloaded in C++?
Answer: B
The scope resolution operator (::), member access operator (.), pointer-to-member operator (.*), and ternary operator (?:) cannot be overloaded in C++.
Q.37Medium
What is the output of the following code?
class A { public: int x = 5; };
class B : public A { public: int x = 10; };
int main() { B obj; cout << obj.x; }
Answer: B
The derived class B has its own member variable 'x' with value 10, which shadows the base class member. obj.x refers to B's x, which is 10.
Q.38Medium
Which of the following correctly describes function binding in C++?
Answer: C
Virtual functions use dynamic (runtime) binding where the function to be called is determined based on the actual object type at runtime. Non-virtual functions use static (compile-time) binding.
Q.39Medium
What will be printed by the following code?
class Test { int x; public: Test() { x = 0; } ~Test() { cout << "Destructor"; } };
int main() { Test t1, t2; return 0; }
Answer: B
Two objects t1 and t2 are created. When the program exits, both objects are destroyed in the reverse order of creation (t2 then t1), calling the destructor twice.
Q.40Medium
What is the primary advantage of using virtual functions in a base class?
Answer: B
Virtual functions enable runtime polymorphism, allowing the correct derived class method to be called through a base class pointer or reference, facilitating flexible and extensible code design.