Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Abstract Class”.
1. Which among the following best describes abstract classes?
a) If a class has more than one virtual function, it’s abstract class
b) If a class have only one pure virtual function, it’s abstract class
c) If a class has at least one pure virtual function, it’s abstract class
d) If a class has all the pure virtual functions only, then it’s abstract class
Answer: c
Clarification: The condition for a class to be called abstract class is that it must have at least one pure virtual function. The keyword abstract must be used while defining abstract class in java.
2. Can abstract class have main() function defined inside it?
a) Yes, depending on return type of main()
b) Yes, always
c) No, main must not be defined inside abstract class
d) No, because main() is not abstract function
Answer: b
Clarification: This is a property of abstract class. It can define main() function inside it. There is no restriction on its definition and implementation.
3. If there is an abstract method in a class then, ________________
a) Class must be abstract class
b) Class may or may not be abstract class
c) Class is generic
d) Class must be public
Answer: a
Clarification: It is a rule that if a class have even one abstract method, it must be an abstract class. If this rule was not made, the abstract methods would have got skipped to get defined in some places which are undesirable with the idea of abstract class.
4. If a class is extending/inheriting another abstract class having abstract method, then _______________________
a) Either implementation of method or making class abstract is mandatory
b) Implementation of the method in derived class is mandatory
c) Making the derived class also abstract is mandatory
d) It’s not mandatory to implement the abstract method of parent class
Answer: a
Clarification: Either of the two things must be done, either implementation or declaration of class as abstract. This is done to ensure that the method intended to be defined by other classes gets defined at every possible class.
5. Abstract class A has 4 virtual functions. Abstract class B defines only 2 of those member functions as it extends class A. Class C extends class B and implements the other two member functions of class A. Choose the correct option below.
a) Program won’t run as all the methods are not defined by B
b) Program won’t run as C is not inheriting A directly
c) Program won’t run as multiple inheritance is used
d) Program runs correctly
Answer: d
Clarification: The program runs correctly. This is because even class B is abstract so it’s not mandatory to define all the virtual functions. Class C is not abstract but all the virtual functions have been implemented will that class.
6. Abstract classes can ____________________ instances.
a) Never have
b) Always have
c) Have array of
d) Have pointer of
Answer: a
Clarification: When an abstract class is defined, it won’t be having the implementation of at least one function. This will restrict the class to have any constructor. When the class doesn’t have constructor, there won’t be any instance of that class.
7. We ___________________ to an abstract class.
a) Can create pointers
b) Can create references
c) Can create pointers or references
d) Can’t create any reference, pointer or instance
Answer: c
Clarification: Even though there can’t be any instance of abstract class. We can always create pointer or reference to abstract class. The member functions which have some implementation inside abstract itself can be used with these references.
8. Which among the following is an important use of abstract classes?
a) Header files
b) Class Libraries
c) Class definitions
d) Class inheritance
Answer: b
Clarification: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers. This helps us to get some already implemented codes and functions that might have not been provided by the programming language itself.
9. Use of pointers or reference to an abstract class gives rise to which among the following feature?
a) Static Polymorphism
b) Runtime polymorphism
c) Compile time Polymorphism
d) Polymorphism within methods
Answer: b
Clarification: The runtime polymorphism is supported by reference and pointer to an abstract class. This relies upon base class pointer and reference to select the proper virtual function.
10. The abstract classes in java can _________________
a) Implement constructors
b) Can’t implement constructor
c) Can implement only unimplemented methods
d) Can’t implement any type of constructor
Answer: a
Clarification: The abstract classes in java can define a constructor. Even though instance can’t be created. But in this way, only during constructor chaining, constructor can be called. When instance of concrete implementation class is created, it’s known as constructor chaining.
11. Abstract class can’t be final in java.
a) True
b) False
Answer: a
Clarification: If an abstract class is made final in java, it will stop the abstract class from being extended. And if the class is not getting extended, there won’t be another class to implement the virtual functions. Due to this contradicting fact, it can’t be final in java.
12. Can abstract classes have static methods (Java)?
a) Yes, always
b) Yes, but depends on code
c) No, never
d) No, static members can’t have different values
Answer: a
Clarification: There is no restriction on declaring static methods. The only condition is that the virtual functions must have some definition in the program.
13. It is _________________________ to have an abstract method.
a) Not mandatory for an static class
b) Not mandatory for a derived class
c) Not mandatory for an abstract class
d) Not mandatory for parent class
Answer: c
Clarification: Derived, parent and static classes can’t have abstract method (We can’t say what type of these classes is). And for abstract class it’s not mandatory to have abstract method. But if any abstract method is there inside a class, then class must be abstract type.
14. How many abstract classes can a single program contain?
a) At most 1
b) At least 1
c) At most 127
d) As many as required
Answer: d
Clarification: There is no restriction on the number of abstract classes that can be defined inside a single program. The programs can use as many abstract classes as required. But the functions with no body must be implemented.
15. Is it necessary that all the abstract methods must be defined from an abstract class?
a) Yes, depending on code
b) Yes, always
c) No, never
d) No, if function is not used, no definition is required
Answer: b
Clarification: That is the rule of programming language that each function declared, must have some definition. There can’t be some abstract method that remains undefined. Even if it’s there, it would result in compile time error.