Interfaces And Abstract Classes - Study Mode
[#221] interface Test{
int p = 10
//line 1
public int q = 20
//line 2
public static int r = 30
//line 3
public static final int s = 40
//line 4
} Which of the above line will give compilation error?
Correct Answer
1
[#222] What will happen after compiling this program code? abstract class MyClass{ //line 1
private int a, b
public void call(int a, int b){
this.a = a
this.b = b
System.out.print(a+b)
}
}
public class Test{
public static void main(String args[]){
MyClass m = new MyClass()
//line 2
m.call(12,25)
}
}
Correct Answer
(C) Compilation error due to line 2
Explanation
Solution: Abstract class is not concrete class which means object cannot be created for abstract class, its requires extending it and then create the object of extended class.
[#223] 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()
}
}
Correct Answer
(B) A
Explanation
Solution: 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.
[#224] What will be the output? public interface InfA{
protected String getName()
}
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())
}
}
Correct Answer
(B) Compilation fails due to an error on lines 2
Explanation
Solution: Illegal modifier for the interface method InfA.getName()
only public and abstracts are permitted.
[#225] 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)
}
}
Correct Answer
(A) Compile with error
Explanation
Solution: All the variables declared in interface is implicitly static and final , therefore can't change the value.