50 Java Language MCQs with Answers
Question 1 |
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 |
What is the use of final keyword in Java?
![]() | When a class is made final, a subclass of it can not be created. |
![]() | When a method is final, it can not be overridden. |
![]() | When a variable is final, it can be assigned value only once. |
![]() | All of the above |
See final in Java.
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();
}
}
![]() | Base::show() called |
![]() | Derived::show() called |
![]() | Compiler Error |
![]() | Runtime Error |
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();
}
}
![]() | Base::show() called |
![]() | Derived::show() called |
![]() | Compiler Error |
Question 5 |
Which of the following is FALSE about arrays in Java?
![]() | A java array is always an object |
![]() | Length of array can be changed after creation of array |
![]() | Arrays in Java are always allocated on heap |
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 |
package main;
class T {
int t = 20;
}
class Main {
public static void main(String args[]) {
T t1 = new T();
System.out.println(t1.t);
}
}
![]() | 20 |
![]() | 0 |
![]() | Compiler Error |
Question 7 |
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);
}
}
![]() | 20 |
![]() | 40 |
![]() | Compiler Error |
Question 8 |
![]() | 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. |
Question 9 |
// 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]);
}
}
}
![]() | 10 20 30 40 50 |
![]() | Compiler Error |
![]() | 10 20 30 40 |
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.
![]() | 1, 3 and 4 |
![]() | 1, 2 and 4 |
![]() | 2, 3 and 4 |
![]() | 1, 2, 3 and 4 |
Answer: (A)
Explanation: The instance of an interface can't be created because it acts as an abstract class.
Quiz of this Question



