Which of the following declaration will compile without errors?
a)

public abstract class Digit {
    public abstract void print();
}

b)

public class Digit {
    public abstract void print();
}

c)

public abstract class Digit {
    public abstract void print(){};
}

d)

public abstract class Digit {
    public void print();
}

e)

public class Digit {
    public void print(){};
}
  1. a, b

  2. b, c

  3. a, d

  4. a, e


Correct Option: D
Explanation:

To solve this question, the user needs to know about abstract classes and abstract methods in Java.

An abstract class is a class that cannot be instantiated on its own and is often used as a template for other classes. Abstract methods are methods that are declared but have no implementation in the abstract class.

Option a declares an abstract class Digit with an abstract method print(). This is a valid declaration of an abstract class.

Option b declares a class Digit with an abstract method print(). This is not a valid declaration because a non-abstract class cannot have an abstract method.

Option c declares an abstract class Digit with an abstract method print() that has an empty implementation. This is not valid because an abstract method cannot have an implementation in the abstract class.

Option d declares an abstract class Digit with a non-abstract method print(). This is not valid because an abstract class must have at least one abstract method.

Option e declares a non-abstract class Digit with a non-abstract method print(). This is a valid declaration of a non-abstract class.

Therefore, the correct answer is:

The Answer is: D

Find more quizzes: