What will be the output when the following program is compiled and executed?
abstract class TestAbstract{
String my_name;
String myName(){
my_name = "Examveda";
return my_name;
}
abstract void display();
}
public class Test extends TestAbstract{
void display(){
String n = myName();
System.out.print("My name is "+ n);
}
public static void main(String args[]){
Test t = new Test();
t.display();
}
}
abstract class TestAbstract{
String my_name;
String myName(){
my_name = "Examveda";
return my_name;
}
abstract void display();
}
public class Test extends TestAbstract{
void display(){
String n = myName();
System.out.print("My name is "+ n);
}
public static void main(String args[]){
Test t = new Test();
t.display();
}
}
A. Program will compile and execute successfully and prints
B. Compilation error as class can not be declared as abstract.
C. Program compiles but leads to runtime exception.
D. Compilation error occurs as the abstract class TestAbstract contains a non-abstract method.
E. None of these
Answer: Option A
Solution(By Examveda Team)
The options B, C and D are incorrect options as in Java we can declare an abstract class comprising of abstract and non-abstract methods that will not lead to any compilation error. Therefore, option A is the correct answer implying that the 't' instance of Test class invokes the display method, which is implemented in the Test class. The display method invokes myName() method declared int the TestAbstract class and prints the name.
A. A contract specifying a set of methods that a class must implement
B. A class that cannot be instantiated
C. A class that contains only static methods
D. A subclass of the Object class
In Java, can a class implement multiple interfaces?
A. Interfaces cannot be implemented by classes
B. Only abstract classes can implement interfaces
C. No, a class can only implement one interface
D. Yes, a class can implement multiple interfaces
What is the purpose of an abstract class in Java?
A. To declare abstract methods
B. To provide a base class with common methods and fields
C. To make a class final
D. To declare abstract methods
Join The Discussion