Unit4 Flashcards
-
What is the primary purpose of inheritance in object-oriented programming?
- a) To allow new objects to be made from existing ones.
- b) To allow classes to derive from multiple base classes.
- c) To provide a way for classes to obtain all the properties and behaviors of another class.
- d) To restrict access to methods in the superclass.
Correct Answer: c) To provide a way for classes to obtain all the properties and behaviors of another class.
-
Which statement is true about polymorphism?
- a) It allows methods to perform differently in different instances.
- b) It restricts methods from being overridden.
- c) It only applies to static methods.
- d) It prevents classes from inheriting features from their parent classes.
Correct Answer: a) It allows methods to perform differently in different instances.
-
What does the
extends
keyword signify in a Java class definition?- a) The class is being enhanced with additional methods.
- b) The class is providing new implementations for base class methods.
- c) The class is inheriting from a superclass.
- d) The class cannot be instantiated.
Correct Answer: c) The class is inheriting from a superclass.
*Consider the following classes:
javaCopy code
public class Fruit {
public void flavor() { System.out.println(“Fruit flavor”); }
}
public class Orange extends Fruit {
public void flavor() { System.out.println(“Orange flavor”); }
}
What is this an example of?**
- a) Overloading
- b) Overriding
- c) Dynamic Binding
- d) Static Binding
Correct Answer: b) Overriding
-
Which of the following is a correct example of polymorphism?
- a)
Fruit myFruit = new Orange();
- b)
Orange myOrange = new Fruit();
- c)
Fruit myFruit = new Fruit();
- d)
Orange myOrange = new Orange();
- a)
Correct Answer: a) Fruit myFruit = new Orange();
-
What is early binding?
- a) The type of binding where method calls are resolved at runtime.
- b) The type of binding where method calls are resolved as soon as the class is loaded.
- c) The type of binding where method calls are resolved at compile time.
- d) The type of binding that occurs only in interpreted languages.
Correct Answer: c) The type of binding where method calls are resolved at compile time.
-
What is the result of the
super
keyword in an overridden method?- a) It calls the overridden method in the superclass.
- b) It prevents the method from being overridden.
- c) It deletes the method from the superclass.
- d) It renames the method in the superclass.
Correct Answer: a) It calls the overridden method in the superclass.
-
How does Java implement multiple inheritances?
- a) Through direct extension of multiple classes.
- b) Through interfaces.
- c) Java does not support multiple inheritances.
- d) Through virtual classes.
Correct Answer: b) Through interfaces. (Note: Java does not support multiple inheritances through classes but allows a class to implement multiple interfaces.)
-
Which of these is a use of downcasting?
- a) To access methods that the reference type doesn’t have.
- b) To restrict the functionality of an object.
- c) To convert primitive types into objects.
- d) To prevent exceptions in method calls.
Correct Answer: a) To access methods that the reference type doesn’t have.
-
What is displayed when a subclass object calls
super.methodName()
and this method is overridden in the subclass?- a) The method in the superclass is called.
- b) The method in the subclass is called.
- c) A runtime error occurs.
- d) A compile-time error occurs.
Correct Answer: a) The method in the superclass is called.