Polymorphismandinheritance
Polymorphismandinheritance
class A {
public:
};
class B : public A {
public:
};
int main() {
ptr->display();
A. Class A
B. Class B
C. Compilation Error
D. Runtime Error
Answer: B. Class B
class A {
public:
};
class B : public A {
public:
};
int main() {
B b;
A. B A
B. A B
C. AB
D. Compilation Error
Answer: B. A B
7. Which keyword is used to prevent a function from being overridden in derived classes?
A. static
B. final
C. const
D. override
Answer: B. final
12. Which concept allows the same function name to behave differently for different classes?
A. Encapsulation
B. Polymorphism
C. Inheritance
D. Abstraction
Answer: B. Polymorphism
class Base {
public:
};
public:
void print() { cout << "Derived"; }
};
int main() {
bptr->print();
A. Base
B. Derived
C. Compilation Error
D. Runtime Error
Answer: A. Base
class Base {
public:
};
public:
};
int main() {
bptr->print();
A. Base
B. Derived
C. Compilation Error
D. Segmentation Fault
Answer: B. Derived
15. What happens when a derived class object is assigned to a base class pointer without virtual
functions?
A. Runtime error
B. Derived class method is called
C. Base class method is called
D. Nothing happens
Answer: C. Base class method is called
16. Which inheritance causes ambiguity if two base classes have the same function?
A. Single
B. Hierarchical
C. Multiple
D. Multilevel
Answer: C. Multiple
class A {
public:
};
class B : public A {
public:
};
class C : public B {
public:
};
int main() {
ptr->f();
}
A. A
B. B
C. C
D. Compilation Error
Answer: C. C
19. Which access modifier makes members accessible only within the class and friends?
A. private
B. protected
C. public
D. internal
Answer: A. private
21. Which of the following is true about base class pointer and derived class object?
A. Can't point
B. Only calls base class methods
C. Can access derived class methods if virtual
D. Causes compile-time error
Answer: C. Can access derived class methods if virtual
class A {
public:
};
class B : public A {
public:
};
int main() {
A a;
B b;
A *ptr = &a;
ptr->fun();
ptr = &b;
ptr->fun();
A. AB
B. BA
C. AA
D. BB
Answer: A. AB
class Base {
public:
cout << x;
};
public:
cout << x;
};
int main() {
b->show();
A. 10
B. 20
C. Compilation Error
D. Runtime Error
Answer: A. 10
30. What is the result of deleting a base class pointer pointing to a derived object, without virtual
destructor?
A. Memory is reclaimed properly
B. Only base destructor is called
C. Both destructors are called
D. Compile-time error
Answer: B. Only base destructor is called