The Wayback Machine - https://web.archive.org/web/20250119053842/https://www.geeksforgeeks.org/quizzes/50-java-language-mcqs-with-answers-2/
  • Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech

50 Java Language MCQs with Answers

Last Updated :
Discuss
Comments

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

In Java, we can make a class abstract by

  • Declaring it abstract using the abstract keyword.

  • Making at least one method final.

  • Declaring all methods as static.

  • Declaring at least one method as abstract.

Question 7

Which of the following is true about an abstract class in Java?

  • An abstract class can be instantiated directly.

  • An abstract class can contain both abstract and non-abstract methods.

  • All methods in an abstract class must be abstract.

  • An abstract class cannot have a constructor.

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.

Master JAVA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!

There are 50 questions to complete.

Take a part in the ongoing discussion