Unit 5 Flashcards
What is an abstract class in Java?
A) A class that cannot be instantiated and may contain abstract methods.
B) A class that supports multiple inheritances.
C) A class that does not contain any methods.
D) A class that must implement all methods from its parent class.
A) A class that cannot be instantiated and may contain abstract methods.
Why would you use an abstract class?
A) To create a fully implemented class with no missing methods.
B) To provide a partial implementation and leave the rest to be implemented by subclasses.
C) To instantiate objects directly.
D) To ensure that methods are not overridden in subclasses.
B) To provide a partial implementation and leave the rest to be implemented by subclasses.
What is the primary purpose of interfaces in Java?
A) To provide a full method implementation.
B) To allow the creation of multiple inheritances of implementation.
C) To define a contract for what a class can do without dictating how it should do it.
D) To ensure all methods are static.
C) To define a contract for what a class can do without dictating how it should do it.
Which of the following is true about an interface?
A) It can contain default and static methods with an implementation.
B) It must contain only abstract methods.
C) It cannot have methods with a body.
D) It is used to share code between classes.
A) It can contain default and static methods with an implementation.
How does polymorphism benefit object-oriented programming?
A) By allowing methods to perform differently depending on the object it is acting upon.
B) By restricting methods to use only one form.
C) By preventing classes from inheriting from more than one superclass.
D) By forcing all subclasses to have the same methods.
A) By allowing methods to perform differently depending on the object it is acting upon.
What is the result of invoking an abstract method in a superclass?
A) It compiles and executes the default behavior.
B) It results in a compilation error.
C) It executes the overridden method in the subclass.
D) It throws a runtime exception.
C) It executes the overridden method in the subclass.
Consider the following code snippet. What is the issue if start(), stop(), and turn() methods in Vehicle are not abstract?
public class Vehicle {
public void start() { … }
public void stop() { … }
public void turn() { … }
}
A) There is no issue; the code is correct.
B) The methods need to be declared abstract to force subclasses to provide an implementation.
C) The methods should be static.
D) The methods must be private.
B) The methods need to be declared abstract to force subclasses to provide an implementation.
Which keyword is used to declare that a class cannot be instantiated?
A) static
B) final
C) abstract
D) sealed
C) abstract
If a class Bicycle extends Vehicle and Vehicle is an abstract class, what must Bicycle do?
A) Nothing special, it can be instantiated normally.
B) It must provide implementations for all of Vehicle’s abstract methods.
C) It must also be declared as abstract.
D) It should override all of Vehicle’s methods, abstract or not.
B) It must provide implementations for all of Vehicle’s abstract methods.
What is the output of the following code if emp is an instance of Secretary, which extends Employee?
Employee emp = new Secretary();
System.out.println(emp.toString());
A) The toString() method of Employee class.
B) The toString() method of Secretary class, if overridden; otherwise, Employee’s.
C) A compilation error.
D) A runtime error.
B) The toString() method of Secretary class, if overridden; otherwise, Employee’s.
What happens if you try to instantiate an abstract class directly in Java?
A) The code compiles successfully.
B) A runtime exception occurs.
C) A compilation error occurs.
D) The object is created with limited functionality.
C) A compilation error occurs.
Which of the following statements is true about interfaces?
A) An interface can declare instance fields.
B) An interface cannot contain any method implementations.
C) An interface can be instantiated directly.
D) Classes can implement multiple interfaces.
D) Classes can implement multiple interfaces.
What is the correct way to implement an interface Flyable in a class Bird?
public interface Flyable {
void fly();
}
A) public class Bird { void fly() { … } }
B) public class Bird extends Flyable { public void fly() { … } }
C) public class Bird implements Flyable { public void fly() { … } }
D) public class Bird implements Flyable { private void fly() { … } }
C) public class Bird implements Flyable { public void fly() { … } }
Which of the following is an example of polymorphism?
A) A method in a subclass uses the same name as a method in its superclass.
B) A method in a subclass has a different signature than a method in its superclass.
C) A method in a subclass performs a completely different function than a method in its superclass.
D) A subclass does not implement any of the methods defined in its superclass.
A) A method in a subclass uses the same name as a method in its superclass.
What will be the output of the following code snippet if the start() method in Bicycle class outputs “Bicycle started”?
Vehicle myBike = new Bicycle();
myBike.start();
Vehicle started”
B) “Bicycle started”
C) A compilation error due to incorrect instantiation.
D) No output, as Vehicle cannot have instantiated objects.
B) “Bicycle started”