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();
    }
}
A
Derived::show() called
B
Base::show() called
Java Inheritance    50 Java Language MCQs with Answers    
Discuss it


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?

A

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

B

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

C

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

D

All of the above

Java final keyword    50 Java Language MCQs with Answers    
Discuss it


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();
    }
}
A
Base::show() called
B
Derived::show() called
C
Compiler Error
D
Runtime Error
Java Inheritance    50 Java Language MCQs with Answers    
Discuss it


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();
    }
}
A
Base::show() called
B
Derived::show() called
C
Compiler Error
Java Inheritance    50 Java Language MCQs with Answers    
Discuss it


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?

A

A java array is always an object

B

Length of array can be changed after creation of array

C

Arrays in Java are always allocated on heap

Java Arrays    50 Java Language MCQs with Answers    
Discuss it


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);
   }
}
A
20
B
0
C
Compiler Error
Java Constructors    50 Java Language MCQs with Answers    
Discuss it


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);
   }
}
A
20
B
40
C
Compiler Error
Java Constructors    50 Java Language MCQs with Answers    
Discuss it


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
A
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
B
Abstract classes can have constructors
C
A class can be made abstract without any abstract method
D
A class can inherit from multiple abstract classes.
Java Abstract Class and Interface    50 Java Language MCQs with Answers    
Discuss it


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]);              
       }
    }
}
A
10 20 30 40 50
B
Compiler Error
C
10 20 30 40
Java Arrays    50 Java Language MCQs with Answers    
Discuss it


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.
A

1, 3 and 4

B

1, 2 and 4

C

2, 3 and 4

D

1, 2, 3 and 4

Java Abstract Class and Interface    50 Java Language MCQs with Answers    
Discuss it


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.