Test Series - oops

Test Number 31/78

Q: 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
Solution: 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.
Q: 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
Solution: This is a property of abstract class. It can define main() function inside it. There is no restriction on its definition and implementation.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Abstract classes can ____________________ instances.
A. Never have
B. Always have
C. Have array of
D. Have pointer of
Solution: 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.
Q: 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
Solution: 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.
Q: Which among the following is an important use of abstract classes?
A. Header files
B. Class Libraries
C. Class definitions
D. Class inheritance
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10