Test Series - java

Test Number 65/64

Q: An abstract class in Java can be created using the keyword ____.
A. final
B. interface
C. abstract
D. static
Solution: You should precede the keyword "abstract" before the keyword "class".
Q: To create an Abstract class, the keyword "class" is also required. State TRUE or FALSE.
A. TRUE
B. FALSE
C. -
D. -
Solution: True. You create a class using the keyword CLASS and make it abstract using the keyword ABSTRACT.


abstract class ClassA{ }
Q: Can you create an object from an abstract class in Java?
A. Yes
B. No
C. -
D. -
Solution: No. You can not instantiate or create an object from an abstract class.
Q: Which is the opposite of an Abstract Class?
A. Interface
B. concrete class
C. -
D. -
Solution: The Concrete Class is the opposite of an Abstract Class. Concrete class defines all the methods completely as opposed to abstract classes that leave implementation to the developers.
Q: Choose a correct implementation of an Abstract class in the below Java code?
A. abstract class ClassA { }
B. abstract class ClassB { abstract void method(); }
C. abstract class ClassC { void method() { System.out.print("Hello Abstract Class"); } }
D. All the above
Solution: All the above. An abstract class need not contain a single abstract method also.
Q: An abstract class in Java usually contains one or more abstract ____.
A. constructors
B. methods
C. variables
D. None
Solution: Developers usually write abstract classes with one or more abstract methods to be implemented after inheriting the class.
Q: Does the below Java code with abstract method compile?
class Puppy
{
  abstract void showName();
}
A. NO
B. YES
C. -
D. -
Solution: No. An abstract method can be defined only by an abstract class.
Q: What is the output of the below Java program with an abstract class?
abstract class Coffee
{
  Coffee()
  {
    System.out.println("Inside Constructor of Coffee..");
  }
}
class ColdCoffee extends Coffee
{
  ColdCoffee()
  {
    System.out.println("Inside Constructor of ColdCoffee..");
  }
}
public class AbstractClassTesting
{
  public static void main(String[] args)
  {
    ColdCoffee cf = new ColdCoffee();
  }
}
A.  Compiler error
B. Inside Constructor of Coffee.. Inside Constructor of ColdCoffee..
C. Inside Constructor of ColdCoffee..
D. Inside Constructor of Coffee..
Solution: An abstract class can contain constructors. The order of invoking of constructors is from the Superclass to Subclass.
Q: What is the output of the below Java program with an abstract class?
final abstract class Bell
{  }
class DoorBell extends Bell
{
  DoorBell()
  {
    System.out.println("DoorBell ringing..");
  }
}
public class AbstractClassTesting2
{
  public static void main(String[] args)
  {
    Bell bell = new DoorBell();
  }
}
A. DoorBell ringing..
B. No output
C. Compiler error
D. None of the above
Solution: You can not use the keyword FINAL before an abstract class. The classes declared with the "final" keyword can not be subclassed or inherited. So, the FINAL keyword is banned for use with an abstract class. So, the compiler throws an error.
Q: What is the output of the below Java program with an abstract class?
abstract class MathLibrary
{
  static final float PI = 3.1415f;
}

public class AbstractClassTesting3
{
  public static void main(String[] args)
  {
    System.out.println(MathLibrary.PI);
  }
}
A. No output
B. Compiler error
C. 3.1415
D. None of the above
Solution: The output will be 3.1415. You can define and use a static final variable inside an abstract class. A static final variable is nothing but a constant. It is common to access a static variable with a DOT operator preceded by the Class Name.

You Have Score    /10