Interfaces And Abstract Classes(Java Program ) Questions and Answers
Explanation:-
Answer: Option A. -> Compile with errorAll the variables declared in interface is implicitly static and final , therefore can't change the value.
Explanation:-
Answer: Option B. -> AThe A a1 reference variable refers not to an instance of interface A, but to an instance of an anonymous (unnamed) class. So there is no compilation error.
Explanation:-
Answer: Option B. -> Compilation fails due to an error on lines 2Illegal modifier for the interface method InfA.getName(); only public and abstracts are permitted.
Question 9. 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();
}
}
Explanation:-
Answer: Option A. -> Program will compile and execute successfully and printsThe 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.
Question 10. What happens if the following program is compiled and executed?
interface MyInterface{
void display();
}
interface MySubInterface extends MyInterface{
void display();
}
public class Test implements MySubInterface{
public void display(){
System.out.print("Welcome to Examveda.");
}
public static void main(String args[]){
Test t = new Test();
t.display();
}
}
interface MyInterface{
void display();
}
interface MySubInterface extends MyInterface{
void display();
}
public class Test implements MySubInterface{
public void display(){
System.out.print("Welcome to Examveda.");
}
public static void main(String args[]){
Test t = new Test();
t.display();
}
}
Explanation:-
Answer: Option C. -> The code will compile and execute successfully showing the output Welcome to Examveda.The program will compile and execute successfully as you can declare methods with same name in an interface and the method of either interface can be used, implying the option a is incorrect. The option B and D are incorrect as the methods of an interface are implicitly public and abstract.