Interfaces And Abstract Classes(Java Program ) Questions and Answers

Question 1. Given the following piece of code:
public class School{
public abstract double numberOfStudent();
}
which of the following statements is true?
  1.    The keywords public and abstract cannot be used together.
  2.    The method numberOfStudent() in class School must have a body.
  3.    You must add a return statement in method numberOfStudent().
  4.    Class School must be defined abstract.
Explanation:-
Answer: Option D. -> Class School must be defined abstract.

Question 2. Which of the following declares an abstract method in an abstract Java class?
  1.    public abstract method();
  2.    public abstract void method();
  3.    public void abstract Method();
  4.    public void method() {}
  5.    public abstract void method() {}
Explanation:-
Answer: Option B. -> public abstract void method();

Question 3. Which of the following statements regarding abstract classes are true?
  1.    An abstract class can be extended.
  2.    A subclass of a non-abstract superclass can be abstract.
  3.    A subclass can override a concrete method in a superclass to declare it abstract.
  4.    An abstract class can be used as a data type.
  5.    All of the above
Explanation:-
Answer: Option E. -> All of the above

Question 4. Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct?
1. A a = new A();
2. A a = new B();
3. B b = new A();
4. B b = new B();
  1.    1 and 2
  2.    2 and 4
  3.    3 and 4
  4.    1 and 3
  5.    2 and 3
Explanation:-
Answer: Option B. -> 2 and 4

Question 5. Which of the following class definitions defines a legal abstract class?
  1.    class A { abstract void unfinished() { } }
  2.    class A { abstract void unfinished(); }
  3.    abstract class A { abstract void unfinished(); }
  4.    public class abstract A { abstract void unfinished(); }
Explanation:-
Answer: Option C. -> abstract class A { abstract void unfinished(); }

Question 6. What will be the output for the below code ?
public interface TestInf{
int i =10;
}
public class Test{
public static void main(String... args){
TestInf.i=12;
System.out.println(TestInf.i);
}
}
  1.    Compile with error
  2.    10
  3.    12
  4.    Runtime Exception
  5.    None of these
Explanation:-
Answer: Option A. -> Compile with error
All the variables declared in interface is implicitly static and final , therefore can't change the value.

Question 7. What is the output for the below code ?
interface A{
public void printValue();
}
1. public class Test{
2. public static void main (String[] args){
3. A a1 = new A(){
4. public void printValue(){
5. System.out.println("A");
6. }
7. };
8. a1.printValue();
9. }
10. }
  1.    Compilation fails due to an error on line 3
  2.    A
  3.    Compilation fails due to an error on line 8
  4.    null
  5.    None of these
Explanation:-
Answer: Option B. -> A
The 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.

Question 8. What will be the output?
1. public interface InfA{
2. protected String getName();
3. }
public class Test implements InfA{
public String getName(){
return "test-name";
}
public static void main (String[] args){
Test t = new Test();
System.out.println(t.getName());
}
}
  1.    test-name
  2.    Compilation fails due to an error on lines 2
  3.    Compilation fails due to an error on lines 1
  4.    Compilation succeed but Runtime Exception
  5.    None of these
Explanation:-
Answer: Option B. -> Compilation fails due to an error on lines 2
Illegal 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();
}
}
  1.    Program will compile and execute successfully and prints
  2.    Compilation error as class can not be declared as abstract.
  3.    Program compiles but leads to runtime exception.
  4.    Compilation error occurs as the abstract class TestAbstract contains a non-abstract method.
  5.    None of these
Explanation:-
Answer: Option A. -> Program will compile and execute successfully and prints
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.

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();
}
}
  1.    The code will lead to a compilation error as declaration of the display method has been provided in two interface.
  2.    The code will lead to a compilation error due to public modifier while declaring the display method.
  3.    The code will compile and execute successfully showing the output Welcome to Examveda.
  4.    The code will lead to a compilation error as the display method is not declared as abstract.
  5.    None of these
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.