Question 1
What happens if an abstract class does not have any abstract methods?
It will not compile.
The class can still be abstract.
Java will automatically provide an abstract method.
It becomes a concrete class.
Question 2
What will be the output of this code?
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
}
}
Animal makes a sound
Dog barks
Compilation error
Runtime error
Question 3
Which of the following statements about inheritance is false?
Java supports single inheritance.
Java allows multiple class inheritance using extends.
Interfaces can be used to achieve multiple inheritance.
The super keyword can be used to invoke the parent class constructor.
Question 4
Which of the following statements about abstract classes is correct?
Abstract classes cannot have constructors.
Abstract classes cannot have static methods.
An object of an abstract class cannot be instantiated.
Abstract classes cannot have final methods.
Question 5
What will be the output of this program?
abstract class Parent {
abstract void show();
}
class Child extends Parent {
void show() {
System.out.println("Child class method");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}
Compilation Error
Runtime Error
Child class method
No Output
Question 6
Which of the following is true about interfaces in java.
1) An interface can contain following type of members.
....public, static, final fields (i.e., constants)
....default and static methods with bodies
2) An instance of interface can be created.
3) A class can implement multiple interfaces.
4) Many classes can implement the same interface.
1, 3 and 4
1, 2 and 4
2, 3 and 4
1, 2, 3 and 4
Question 7
Which of the following is FALSE about abstract classes in Java
If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as abstract using \'abstract\' keyword
Abstract classes can have constructors
A class can be made abstract without any abstract method
A class can inherit from multiple abstract classes.
Question 8
Predict the output of the following program.
abstract class demo
{
public int a;
demo()
{
a = 10;
}
abstract public void set();
abstract final public void get();
}
class Test extends demo
{
public void set(int a)
{
this.a = a;
}
final public void get()
{
System.out.println("a = " + a);
}
public static void main(String[] args)
{
Test obj = new Test();
obj.set(20);
obj.get();
}
}
a = 10
a = 20
Compilation error
Question 9
Which of the following is true about an abstract class in Java?
An abstract class can be instantiated directly.
An abstract class can contain both abstract and non-abstract methods.
All methods in an abstract class must be abstract.
An abstract class cannot have a constructor.
Question 10
In Java, we can make a class abstract by
Declaring it abstract using the abstract
keyword.
Making at least one method final
.
Declaring all methods as static
.
Declaring at least one method as abstract
.
There are 50 questions to complete.