Q: Which two of the following are legal declarations for abstract classes and interfaces?
1. final abstract class Test {} 2. public static interface Test {} 3. final public class Test {} 4. protected abstract class Test {} 5. protected interface Test {} 6. abstract public class Test {}
A.1 and 2
B.2 and 4
C.3 and 5
D.3 and 6
Correct Option: DExplanation: 3 and 6
Q: Which of the following is a correct interface?
A.
interface A { void print() { } }
B.
abstract interface A { print(); }
C.
abstract interface A { abstract void print(); { }}
D.
interface A { void print(); }
Correct Option: DExplanation:
interface A { void print(); }
Q: Runnable is a _____ .
A.class
B. abstract class
C.interface
D. vaiable
Correct Option: CExplanation: interface
Q:
In Java, declaring a class abstract is useful
A.To prevent developers from further extending the class.
B.When it doesn't make sense to have objects of that class.
C.When default implementations of some methods are not desirable.
D. To force developers to extend the class not to use its capabilities.
Correct Option: BExplanation: When it doesn't make sense to have objects of that class.
Q: 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();
A.1 and 2
B.2 and 4
C.3 and 4
D.1 and 3
Correct Option: BExplanation: 2 and 4
Q: 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. interface Base2 implements Base {}
2. abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
3. abstract class Class2 implements Base {}
4. abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
5. abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}
A.1 and 2
B. 2 and 3
C.3 and 4
D.1 and 3
Correct Option: CExplanation: 3 and 4
Q: What will be the output?
interface A{
public void method();
}
class One{
public void method(){
System.out.println("Class One method");
}
}
class Two extends One implements A{
public void method(){
System.out.println("Class Two method");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method();
}
}
A.will print Class One method
B.will print Class Two method
C.compiles fine but print nothing
D.Compilation Error
Correct Option: BExplanation: will print Class Two method
Q: 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();
}
}
A.The code will lead to a compilation error as declaration of the display method has been provided in two interface.
B.The code will lead to a compilation error due to public modifier while declaring the display method.
C. The code will compile and execute successfully showing the output Welcome to Examveda.
D.The code will lead to a compilation error as the display method is not declared as abstract.
Correct Option: CExplanation: The code will compile and execute successfully showing the output Welcome to Examveda.
Q: Given the following piece of code:
public class School{
public abstract double numberOfStudent();
}
which of the following statements is true?
A.The keywords public and abstract cannot be used together.
B.The method numberOfStudent() in class School must have a body.
C.You must add a return statement in method numberOfStudent().
D.Class School must be defined abstract.
Correct Option: DExplanation: Class School must be defined abstract.