Machine-translated by Google. Questions are authored in English.
Top

Polymorphism and Virtual Functions Questions and Answers

Answer: C

If the base class destructor is not virtual, the destructor in the derived class cannot be called when the object is accessed through a pointer to the base class.

When delete is applied to a base class pointer that points to a derived object, the destructor is chosen by the pointer's static type unless it is virtual. Only the base class destructor runs, so the derived part is not cleaned up, which can leak resources (strictly, the behaviour is undefined).

A non-virtual base destructor can still be called and still has a body. It is the derived class destructor, not the base one, that is skipped, so options A, B and D are wrong.

Please log in or register to report this question.

2.

The pointer to base classes are commonly used in design of container classes such as set, vector and lists. In this case which of the following solution exists ?

(i) yields homogeneous list
(ii) yields heterogeneous list
(iii) special type of secure variation of heterogeneous list
(iv) homogeneous and heterogeneous list

Exam Year(s)

MH SET - 2021, SET Exam - 2021,

Answer: C

The key's answer is (i), (ii), (iii) and (iv). I could confirm the first three points in general but not the paper's source for the full list, so this follows the key.

A container of pointers to a base class can hold objects of any publicly derived class, because a base pointer may point at a derived object. Such a list is therefore heterogeneous (ii). If every element is created as the same class, the same container is homogeneous (i), so one design covers both (iv). The key also counts (iii), a secured variation of the heterogeneous list, which I could not match to a standard C++ term.

The options naming only one kind of list leave out something the same container can do.

Please log in or register to report this question.

3.

class C {
public:
  void f(int a) {cout \(\lt\lt\) a;}
  void f(int a, int b) {cout \(\lt\lt\) a+b;};
  void f(int a, int b, int c) {cout \(\lt\lt\) a+b+c;}
};
int main( )
{
  C obj;
  obj.f(10);
  obj.f(10, 20);
  obj.f(10, 20, 30);
  return 0;
}

The above C++ source code exemplifies which of the following principle with respect to object-oriented programming ?

Exam Year(s)

MH SET - 2024, SET Exam - 2024,

Answer: D

The code exemplifies polymorphism.

Class C defines three member functions with the same name f but different parameter lists. The compiler selects the right one from the arguments at each call, which is function overloading, a form of compile-time (static) polymorphism. The program prints 10, then 30, then 60.

Nothing here hides implementation details behind an interface (abstraction) or bundles data with access control as the point of the example (encapsulation), and there is no base or derived class, so it is not inheritance.

Please log in or register to report this question.

4.

Which of the following term describes the ability of a class to provide common interface for different types of objects in object-oriented programming ?

Exam Year(s)

MH SET - 2025, SET Exam - 2025,

Answer: B

The term is polymorphism.

Polymorphism lets objects of different classes be used through one common interface: a call such as shape.draw() runs the right version for a circle or a square. It is achieved through inheritance with overridden methods, interfaces and overloading.

Encapsulation hides an object's data behind its methods. Inheritance lets a class acquire the members of another; it supports polymorphism but is not the ability itself. Abstraction exposes essential features while hiding detail.

Please log in or register to report this question.

Answer: C

By default C++ provides early binding for variables and early binding for functions.

Variables are bound to their types and storage at compile time. Function calls, including calls to member functions, are also resolved at compile time unless the function is declared virtual; only then does C++ use late (dynamic) binding through the virtual table.

So late binding is something the programmer asks for with virtual, not the default. Every option with late binding for functions or variables describes non-default behaviour.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Polymorphism and Virtual Functions Questions and Answers || Polymorphism and Virtual Functions MCQ Questions and Answers || Polymorphism and Virtual Functions GK Questions and Answers || Polymorphism and Virtual Functions GK MCQ Questions || Polymorphism and Virtual Functions Multiple Choice Questions and Answers || Polymorphism and Virtual Functions GK || GK on Polymorphism and Virtual Functions || Object Oriented Programming Questions and Answers || Object Oriented Programming MCQ Questions and Answers || Object Oriented Programming GK Questions and Answers || GK on Object Oriented Programming