Java Abstract Class and Interface

Question 1
Which of the following is FALSE about abstract classes in Java
Cross
If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as abstract using 'abstract' keyword
Cross
Abstract classes can have constructors
Cross
A class can be made abstract without any abstract method
Tick
A class can inherit from multiple abstract classes.


Question 2

Which of the following is true about interfaces in java.

1) An interface can contain following type of members.
....public, static, final fields (i.e., constants) 
....default and static methods with bodies

2) An instance of interface can be created.

3) A class can implement multiple interfaces.

4) Many classes can implement the same interface.
Tick

1, 3 and 4

Cross

1, 2 and 4

Cross

2, 3 and 4

Cross

1, 2, 3 and 4



Question 2-Explanation: 

Answer: (A) 

Explanation: The instance of an interface can't be created because it acts as an abstract class.

Quiz of this Question

Question 3
Predict the output of the following program.
abstract class demo
{
    public int a;
    demo()
    {
        a = 10;
    }

    abstract public void set();
    
    abstract final public void get();

}

class Test extends demo
{

    public void set(int a)
    {
        this.a = a;
    }

    final public void get()
    {
        System.out.println("a = " + a);
    }

    public static void main(String[] args)
    {
        Test obj = new Test();
        obj.set(20);
        obj.get();
    }
}
 
Cross
a = 10
Cross
a = 20
Tick
Compilation error


Question 3-Explanation: 
Final method can’t be overridden. Thus, an abstract function can’t be final.
Question 4
Type IV JDBC driver is a driver
Cross
which is written in C++
Cross
which requires an intermediate layer
Tick
which communicates through Java sockets
Cross
which translates JDBC function calls into API not native to DBMS


Question 4-Explanation: 
JDBC type 4 driver,works directly by connecting to the database server through socket connections and converts JDBC calls to vendor-specific database protocols.These drivers don't require any intermediate layer. So, option (C) is correct.
Question 5
Which of the following statement(s) with regard to an abstract class in JAVA is/are TRUE ? I. An abstract class is one that is not used to create objects. II. An abstract class is designed only to act as a base class to be inherited by other classes.
Cross
Only I
Cross
Only II
Cross
Neither I nor II
Tick
Both I and II


Question 5-Explanation: 
Abstract data class is not used to create objects in Java and it is designed only to act as a base class to be inherited by other classes. Both Statement are correct. For more information Refer:Abstract Classes in Java Option (D) is correct.
Question 6
Which of the following is used to make an Abstract class?
Tick
Making atleast one member function as pure virtual function
Cross
Making atleast one member function as virtual function
Cross
Declaring as Abstract class using virtual keyword
Cross
Declaring as Abstract class using static keyword


Question 6-Explanation: 
Making atleast one member function as pure virtual function is the method to make abstract class. For more information on Abstract Class Refer:Pure Virtual Functions and Abstract Classes in C++ Option (A) is correct.
Question 7
We can make a class abstract by
Cross
Declaring it abstract using the virtual keyword
Cross
Making at least one member function as virtual function
Tick
Making at least one member function as pure virtual function
Cross
Making all member function const


Question 7-Explanation: 
An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. A pure virtual function can be declared by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration. Option (C) is correct.
There are 7 questions to complete.

  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads