Q. What is the output for the below code?
interface A{
public void printValue();
}

public class Test{
public static void main (String[] args){
A a1 = new A(){
public void printValue(){
System.out.println("A");
}
};
a1.printValue();
}
}

  • (A) Compilation fails due to an error on line 3
  • (B) A
  • (C) Compilation fails due to an error on line 8
  • (D) Compilation fails due to an error on line 8 D. null
πŸ’¬ Discuss
βœ… Correct Answer: (B) A
Explanation: 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.
Explanation by: Vijay Sangwan
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
158
Total Visits
πŸ“½οΈ
2 y ago
Published
πŸŽ–οΈ
Vijay Sangwan
Publisher
πŸ“ˆ
92%
Success Rate