• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses

50 Java Language MCQs with Answers

Question 1

Output of following Java Program?

Java
class Base {
    public void show() {
       System.out.println("Base::show() called");
    }
}
 
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
 
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
  • Derived::show() called

  • Base::show() called

Question 2

Which of the following is true about inheritance in Java. 1) In Java all classes inherit from the Object class directly or indirectly. The Object class is root of all classes. 2) Multiple inheritance is not allowed in Java. 3) Unlike C++, there is nothing like type of inheritance in Java where we can specify whether the inheritance is protected, public or private.
  • 1, 2 and 3
  • 1 and 2
  • 2 and 3
  • 1 and 3

Question 3

C
final class Complex {

    private final double re;
    private final double im;

    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }

    public String toString() {
        return \"(\" + re + \" + \" + im + \"i)\";
    }
}

class Main {
  public static void main(String args[])
  {
       Complex c = new Complex(10, 15);
       System.out.println(\"Complex number is \" + c);
  }         
}
  • Complex number is (10.0 + 15.0i)
  • Compiler Error
  • Complex number is SOME_GARBAGE
  • Complex number is Complex@8e2fb5
    Here 8e2fb5 is hash code of c

Question 4

Java
class Base {
    final public void show() {
       System.out.println("Base::show() called");
    }
}
 
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
 
class Main {
    public static void main(String[] args) {
        Base b = new Derived();;
        b.show();
    }
}
  • Base::show() called

  • Derived::show() called

  • Compiler Error

  • Runtime Error

Question 5

\Java\
class Base {
    public static void show() {
       System.out.println(\"Base::show() called\");
    }
}
 
class Derived extends Base {
    public static void show() {
       System.out.println(\"Derived::show() called\");
    }
}
 
class Main {
    public static void main(String[] args) {
        Base b = new Derived();
        b.show();
    }
}
  • Base::show() called
  • Derived::show() called
  • Compiler Error

Question 6

We can make a class abstract by
  • Declaring it abstract using the virtual keyword
  • Making at least one member function as virtual function
  • Making at least one member function as pure virtual function
  • Making all member function const

Question 7

Which of the following is used to make an Abstract class?
  • Making atleast one member function as pure virtual function
  • Making atleast one member function as virtual function
  • Declaring as Abstract class using virtual keyword
  • Declaring as Abstract class using static keyword

Question 8

Type IV JDBC driver is a driver
  • which is written in C++
  • which requires an intermediate layer
  • which communicates through Java sockets
  • which translates JDBC function calls into API not native to DBMS

Question 9

Predict the output of the following program. Java
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();
    }
}
 
  • a = 10
  • a = 20
  • Compilation error

Question 10

Which of the following is FALSE about abstract classes in Java
  • 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
  • Abstract classes can have constructors
  • A class can be made abstract without any abstract method
  • A class can inherit from multiple abstract classes.

There are 50 questions to complete.

Last Updated :
Take a part in the ongoing discussion