50 Java Language MCQs with Answers

Last Updated : 23 Feb, 2022
Question 1
Output of following Java Program?
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();
    }
}
Tick
Derived::show() called
Cross
Base::show() called


Question 1-Explanation: 
In the above program, b is a reference of Base type and refers to an abject of Derived class. In Java, functions are virtual by default. So the run time polymorphism happens and derived fun() is called.
Question 2

What is the use of final keyword in Java?

Cross

When a class is made final, a subclass of it can not be created.

Cross

When a method is final, it can not be overridden.

Cross

When a variable is final, it can be assigned value only once.

Tick

All of the above



Question 2-Explanation: 
Question 3
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();
    }
}
Cross
Base::show() called
Cross
Derived::show() called
Tick
Compiler Error
Cross
Runtime Error


Question 3-Explanation: 
Final methods cannot be overridden. See the compiler error here.
Question 4

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();
    }
}
Tick
Base::show() called
Cross
Derived::show() called
Cross
Compiler Error


Question 4-Explanation: 
Like C++, when a function is static, runtime polymorphism doesn\'t happen.
Question 5

Which of the following is FALSE about arrays in Java?

Cross

A java array is always an object

Tick

Length of array can be changed after creation of array

Cross

Arrays in Java are always allocated on heap



Question 5-Explanation: 

In Java, arrays are objects, they have members like length. The length member is final and cannot be changed. All objects are allocated on heap in Java, so arrays are also allocated on heap.

Question 6
Predict the output?
package main;
class T {
  int t = 20;
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}
Tick
20
Cross
0
Cross
Compiler Error


Question 6-Explanation: 
In Java, member variables can assigned a value with declaration. In C++, only static const variables can be assigned like this.
Question 7
Predict the output of following Java program
class T {
  int t = 20;
  T() {
    t = 40;
  }
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}
Cross
20
Tick
40
Cross
Compiler Error


Question 7-Explanation: 
The values assigned inside constructor overwrite the values initialized with declaration.
Question 8
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 9
Predict the output?
// file name: Main.java
public class Main {
    public static void main(String args[]) {
       int arr[] = {10, 20, 30, 40, 50};
       for(int i=0; i < arr.length; i++)
       {
             System.out.print(" " + arr[i]);              
       }
    }
}
Tick
10 20 30 40 50
Cross
Compiler Error
Cross
10 20 30 40


Question 9-Explanation: 
It is a simple program where an array is first created then traversed. The important thing to note is, unlike C++, arrays are first class objects in Java. For example, in the following program, size of array is accessed using length which is a member of arr[] object.
Question 10

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 10-Explanation: 

Answer: (A) 

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

Quiz of this Question

There are 50 questions to complete.

Share your thoughts in the comments

Similar Reads