Abstract Class MCQs
What is an abstract class in Java?
A. A class that cannot be instantiated and may contain abstract methods
B. A class that can be instantiated and contains only abstract methods
C. A class with only static methods
D. A class that can be instantiated and contains no methods
Which of the following is true about abstract methods in an abstract class?
A. Abstract methods must be implemented by subclasses
B. Abstract methods can have a body
C. Abstract methods can only be static
D. Abstract methods can be private
Can an abstract class have a constructor?
A. Yes, an abstract class can have a constructor
B. No, an abstract class cannot have a constructor
C. Yes, but only if it is private
D. Yes, but only if the constructor is static
What will be the output of the following code?
java
Copy code
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Woof");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.eat();
a.makeSound();
}
}
A. Eating Woof
B. Woof Eating
C. Compilation error
D. Eating
Can an abstract class have fields (instance variables)?
A. Yes, an abstract class can have fields
B. No, an abstract class cannot have fields
C. Yes, but only static fields
D. No, an abstract class can only have methods
What happens if a subclass does not implement all abstract methods of an abstract superclass?
A. The subclass must also be declared abstract
B. The subclass will compile successfully
C. The subclass will throw a runtime exception
D. The subclass can be instantiated, but only with default implementations
Which keyword is used to declare an abstract class in Java?
A. abstract
B. abstractclass
C. class
D. super
What will be the output of the following code?
java
Copy code
abstract class Shape {
abstract void draw();
void display() {
System.out.println("Displaying");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.display();
s.draw();
}
}
A. Displaying Drawing Circle
B. Drawing Circle Displaying
C. Displaying
D. Compilation error
Can an abstract class implement an interface?
A. Yes, an abstract class can implement an interface
B. No, an abstract class cannot implement an interface
C. Yes, but only if the interface methods are abstract
D. Yes, but only if the abstract class does not have abstract methods
What is the output of the following code?
java
Copy code
abstract class Vehicle {
abstract void start();
void stop() {
System.out.println("Vehicle stopped");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car started");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
v.stop();
}
}
A. Car started Vehicle stopped
B. Vehicle stopped Car started
C. Car started
D. Compilation error
Can an abstract class be declared final?
A. Yes, an abstract class can be declared final
B. No, an abstract class cannot be final
C. Yes, but only if it does not contain any abstract methods
D. No, an abstract class can be final only if it has no methods
What will be the output of the following code?
java
Copy code
abstract class Animal {
void sleep() {
System.out.println("Sleeping");
}
abstract void makeSound();
}
class Cat extends Animal {
void makeSound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Cat();
a.makeSound();
a.sleep();
}
}
A. Meow Sleeping
B. Sleeping Meow
C. Meow
D. Sleeping
What is the purpose of an abstract class?
A. To provide a base for other classes to extend and enforce a contract
B. To create a concrete class that can be instantiated
C. To contain only static methods
D. To prevent inheritance
Can an abstract class be extended by another abstract class?
A. Yes, an abstract class can be extended by another abstract class
B. No, an abstract class cannot be extended by another abstract class
C. Yes, but only if the subclass implements all abstract methods
D. No, an abstract class can only be extended by concrete classes
What is the output of the following code?
java
Copy code
abstract class Base {
abstract void method();
void display() {
System.out.println("Base display");
}
}
class Derived extends Base {
void method() {
System.out.println("Derived method");
}
}
public class Main {
public static void main(String[] args) {
Base b = new Derived();
b.display();
}
}
A. Derived method
B. Base display
C. Derived method Base display
D. Compilation error
Which of the following is true about abstract classes?
A. An abstract class can have both abstract and concrete methods
B. An abstract class can only have abstract methods
C. An abstract class can only have static methods
D. An abstract class can only have constructors
What will be the result of the following code?
java
Copy code
abstract class Vehicle {
abstract void drive();
void honk() {
System.out.println("Honking");
}
}
class Bike extends Vehicle {
void drive() {
System.out.println("Bike driving");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Bike();
v.drive();
v.honk();
}
}
A. Bike driving Honking
B. Honking Bike driving
C. Bike driving
D. Honking
Can an abstract class be used as a type for variables?
A. Yes, an abstract class can be used as a type for variables
B. No, an abstract class cannot be used as a type for variables
C. Yes, but only if the abstract class is instantiated
D. No, abstract classes cannot be assigned to variables
What is the result of the following code?
java
Copy code
abstract class Instrument {
abstract void play();
void tune() {
System.out.println("Tuning");
}
}
class Piano extends Instrument {
void play() {
System.out.println("Playing Piano");
}
}
public class Main {
public static void main(String[] args) {
Instrument i = new Piano();
i.tune();
i.play();
}
}
A. Tuning Playing Piano
B. Playing Piano Tuning
C. Tuning
D. Playing Piano
Which of the following statements is false regarding abstract classes?
A. An abstract class can be instantiated directly
B. An abstract class can have constructors
C. An abstract class can have instance variables
D. An abstract class can extend another abstract class
Answers:
A. A class that cannot be instantiated and may contain abstract methods
A. Abstract methods must be implemented by subclasses
A. Yes, an abstract class can have a constructor
A. Eating Woof
A. Yes, an abstract class can have fields
A. The subclass must also be declared abstract
A. abstract
A. Displaying Drawing Circle
A. Yes, an abstract class can implement an interface
A. Car started Vehicle stopped
B. No, an abstract class cannot be final
A. Meow Sleeping
A. To provide a base for other classes to extend and enforce a contract
A. Yes, an abstract class can be extended by another abstract class
B. Base display
A. An abstract class can have both abstract and concrete methods
A. Bike driving Honking
A. Yes, an abstract class can be used as a type for variables
A. Tuning Playing Piano
A. An abstract class can be instantiated directly
No comments:
Post a Comment