Unit 5 Flashcards

1
Q

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) A class that cannot be instantiated and may contain abstract methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A

B) To provide a partial implementation and leave the rest to be implemented by subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A

C) To define a contract for what a class can do without dictating how it should do it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A) It can contain default and static methods with an implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A) By allowing methods to perform differently depending on the object it is acting upon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

C) It executes the overridden method in the subclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A

B) The methods need to be declared abstract to force subclasses to provide an implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which keyword is used to declare that a class cannot be instantiated?

A) static
B) final
C) abstract
D) sealed

A

C) abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

B) It must provide implementations for all of Vehicle’s abstract methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

B) The toString() method of Secretary class, if overridden; otherwise, Employee’s.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

C) A compilation error occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

D) Classes can implement multiple interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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() { … } }

A

C) public class Bird implements Flyable { public void fly() { … } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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) A method in a subclass uses the same name as a method in its superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

B) “Bicycle started”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which statement is true about the relationship between an inner class and its enclosing class in Java?

A) An inner class cannot access the members of its enclosing class.
B) A static inner class has access to the instance variables of its enclosing class.
C) Non-static inner classes have access to all members, including private ones, of the enclosing class.
D) An inner class must be instantiated with a new instance of the enclosing class every time.

A

C) Non-static inner classes have access to all members, including private ones, of the enclosing class.

17
Q

What is an anonymous object in Java and how is it typically used?

A) An object that is instantiated and given a specific name for later reference.
B) An object instantiated without being assigned to any variable, often used for immediate method calling.
C) An object with hidden identity to enhance security features within Java applications.
D) An object that is created without any class definition.

A

B) An object instantiated without being assigned to any variable, often used for immediate method calling.

18
Q

Which of the following is a capability of interfaces in Java?

A) Interfaces cannot contain any methods with a body.
B) Interfaces allow both abstract methods and default methods with implementations.
C) Interfaces can only contain static methods that are final.
D) Interfaces strictly do not allow any method implementations, similar to abstract classes.

A

B) Interfaces allow both abstract methods and default methods with implementations.

19
Q

If a class Bicycle extends an abstract class Vehicle, what must Bicycle specifically do according to Java programming principles?

A) Bicycle must declare itself as abstract.
B) Bicycle must implement all of Vehicle’s abstract methods.
C) Bicycle can ignore the abstract methods in Vehicle if it does not use them.
D) Bicycle needs to redefine all the constructors of Vehicle.

A

B) Bicycle must implement all of Vehicle’s abstract methods.

20
Q

What is the purpose of an empty interface, also known as a marker interface, in Java?

A) To create a template for classes to follow, ensuring they implement specific methods.
B) To serve as markers to inform the compiler about certain capabilities of a class like serialization.
C) To allow classes to inherit from multiple sources without any method definitions.
D) To enforce method overloading in implementing classes without imposing structural constraints.

A

B) To serve as markers to inform the compiler about certain capabilities of a class like serialization.