1.
What is an abstract class?
Correct Answer
C. An abstract class is a class which has at least one abstract method which cannot be instantiated.
Explanation
An abstract class is a class that cannot be instantiated and is meant to serve as a base class for other classes. It may have abstract methods, which are methods without any implementation, and these methods must be implemented by the child classes that inherit from the abstract class. This allows the abstract class to define a common interface or behavior that the child classes must adhere to. Therefore, an abstract class is a class that has at least one abstract method and cannot be instantiated directly.
2.
What is an abstract method?
Correct Answer
C. A method which is not implemented. The implementation for this method needs to be done in a non abstract class which extends this class.
Explanation
An abstract method is a method that is declared in an abstract class but does not have any implementation. It serves as a placeholder for the method that needs to be implemented in a non-abstract class that extends the abstract class. This allows for the abstraction of common functionality in the abstract class while leaving the specific implementation details to the subclasses.
3.
Can an abstract class define both abstract methods and non-abstract methods?
Correct Answer
A. Yes - the child classes will inherit both
Explanation
Yes, an abstract class can define both abstract methods and non-abstract methods. The child classes that inherit from the abstract class will inherit both types of methods. Abstract methods are those that are declared in the abstract class but do not have an implementation. They must be implemented by the child classes. Non-abstract methods, on the other hand, have a complete implementation in the abstract class itself and can be directly used by the child classes without any modification.
4.
Does a subclass that extends an abstract class have to give implementation to all the abstract methods of the superclass?
Correct Answer
A. Not necessarily if the sub class is going to be declared abstract
Explanation
If a subclass extends an abstract class, it does not necessarily have to give implementation to all the abstract methods of the superclass. This is because the subclass itself can also be declared as abstract, which means it does not need to provide implementation for the abstract methods. Therefore, the correct answer is "not necessarily if the sub class is going to be declared abstract."
5.
What is an interface?
Correct Answer
B. A collection of abstract methods and constants
Explanation
An interface is a collection of abstract methods and constants. It is a way to define a contract for classes that implement it, specifying the methods that must be implemented and the constants that must be defined. By using interfaces, we can achieve abstraction and provide a common set of methods and constants that can be used by multiple classes. Classes implementing an interface must provide the implementation for all the abstract methods defined in the interface.
6.
What is Polymorphism in Java?
Correct Answer
D. The feature of deciding which overridden method will be used at the run time of a prgram
Explanation
Polymorphism in Java refers to the feature of deciding which overridden method will be used at the run time of a program. It allows objects of different classes to be treated as objects of a common superclass, enabling flexibility and code reusability. This feature is achieved through method overriding, where a subclass provides its own implementation of a method that is already defined in its superclass. At runtime, the JVM determines which version of the method to execute based on the actual type of the object being referred to.
7.
In order for the following code to be correct, what must be the type of the reference variable card?
____________________ card;
card=new Valentine("Joe",14);
card=new Holiday("Bob");
card=new Birthday("Emily",12);
Correct Answer
D. Card
Explanation
The reference variable "card" must be of type "Card" in order for the code to be correct. This is because the code assigns different types of objects (Valentine, Holiday, and Birthday) to the "card" variable. In order for this assignment to work, the variable must be able to hold objects of any subclass of the "Card" class.
8.
Dynamic Binding or Late Binding is:
Correct Answer
B. Because of overridden method and the use of the appropriate method during execution
Explanation
Dynamic Binding or Late Binding refers to the process of connecting a method with its parameters during runtime. This occurs when there is an overridden method, meaning that a subclass has defined a method with the same name and signature as a method in its superclass. During execution, the appropriate method is determined based on the actual object type rather than the reference type, allowing for polymorphism and the ability to invoke different implementations of the same method. This enables greater flexibility and extensibility in object-oriented programming.
9.
What determines what method is run in the following?Card crd=new Birthday("Lucinda",42);crd.greeting();
Correct Answer
B. The type of the object
Explanation
The type of the object determines what method is run in the given code. This is because the object is created using the "new" keyword with the type "Birthday", which is a subclass of the "Card" class. Since the object is of type "Birthday", the method "greeting()" in the "Birthday" class will be executed. This is known as dynamic method dispatch, where the method to be executed is determined at runtime based on the actual type of the object.
10.
Interfaces can contain _________________ and _____________________ and abstract classes can contain _____________________ , ______________________and at least ___ ____________________________________ __________________
Correct Answer
B. Abstract methods,constants,instance variables, implemented methods and one abstract method
Explanation
Interfaces can contain abstract methods, constants, instance variables, and implemented methods. Abstract classes can contain abstract methods, constants, variables, implemented methods, and at least one abstract method.