Interfaces And Abstract Classes(Computer Science > 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 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 4. 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 5. 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 6. Which of the following is a correct interface?
  1.    interface A { void print() { } }
  2.    abstract interface A { print(); }
  3.    abstract interface A { abstract void print(); { }}
  4.    interface A { void print(); }
Explanation:-
Answer: Option D. -> interface A { void print(); }

Question 7. Determine output of the following code.
interface A { }
class C { }
class D extends C { }
class B extends D implements A { }
public class Test extends Thread{
public static void main(String[] args){
B b = new B();
if (b instanceof A)
System.out.println("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
  1.    Nothing.
  2.    b is an instance of A.
  3.    b is an instance of C.
  4.    b is an instance of A followed by b is an instance of C.
Explanation:-
Answer: Option D. -> b is an instance of A followed by b is an instance of C.

Question 8. Given the following piece of code:
public interface Guard{
void doYourJob();
}
abstract public class Dog implements Guard{ }
which of the following statements is correct?
  1.    This code will not compile, because method doYourJob() in interface Guard must be defined abstract.
  2.    This code will not compile, because class Dog must implement method doYourJob() from interface Guard.
  3.    This code will not compile, because in the declaration of class Dog we must use the keyword extends instead of implements.
  4.    This code will compile without any errors.
Explanation:-
Answer: Option D. -> This code will compile without any errors.

Question 9. In Java, declaring a class abstract is useful
  1.    To prevent developers from further extending the class.
  2.    When it doesn't make sense to have objects of that class.
  3.    When default implementations of some methods are not desirable.
  4.    To force developers to extend the class not to use its capabilities.
  5.    When it makes sense to have objects of that class.
Explanation:-
Answer: Option B. -> When it doesn't make sense to have objects of that class.

Question 10. 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();
}
}
  1.    will print Class One method
  2.    will print Class Two method
  3.    compiles fine but print nothing
  4.    Compilation Error
  5.    None of these
Explanation:-
Answer: Option B. -> will print Class Two method