• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

50 Java Language MCQs with Answers

Question 41

Consider the following Java code fragment:
1   public class While
2   {
3    public void loop()
4      {
5      int x = 0;
6       while(1)
7         {
8         System.out.println("x plus one is" +(x+1));
9        }
10     }
11  }
  • There is syntax error in line no. 1
  • There is syntax errors in line nos. 1 & 6
  • There is syntax error in line no. 8
  • There is syntax error in line no. 6

Question 42

What is the result of the following program?
program side-effect (input, output);
    var x, result: integer;
    function f (var x:integer):integer;
    begin
        x:x+1;f:=x;
    end;
begin
    x:=5;
    result:=f(x)*f(x);
    writeln(result);
end;
  • 5
  • 25
  • 36
  • 42

Question 43

Predict the output of the following program. Java
class Test
{
    public void demo(String str)
    {
        String[] arr = str.split(\";\");
        for (String s : arr)
        {
            System.out.println(s);
        }
    }

    public static void main(String[] args)
    {
        char array[] = {\'a\', \'b\', \' \', \'c\', \'d\', \';\', \'e\', \'f\', \' \', 
                        \'g\', \'h\', \';\', \'i\', \'j\', \' \', \'k\', \'l\'};
        String str = new String(array);
        Test obj = new Test();
        obj.demo(str);
    }
}
  • ab cd
    ef gh
    ij kl
  • ab
    cd;ef
    gh;ij
    kl
  • Compilation error

Question 44

Java
class Test {
public static void swap(Integer i, Integer j) {
      Integer temp = new Integer(i);
      i = j;
      j = temp;
   }
   public static void main(String[] args) {
      Integer i = new Integer(10);
      Integer j = new Integer(20);
      swap(i, j);
      System.out.println(\"i = \" + i + \", j = \" + j);
   }
}
  • i = 10, j = 20
  • i = 20, j = 10
  • i = 10, j = 10
  • i = 20, j = 20

Question 45

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

Question 46

Output of following Java program Java
class Main {
 public static void main(String args[]){
   final int i;
   i = 20;
   System.out.println(i);
 }
}
  • 20
  • Compiler Error
  • 0
  • Garbage value

Question 47

Java
class Main {
 public static void main(String args[]){
    final int i;
    i = 20;
    i = 30;
    System.out.println(i);
 }
}
  • 30
  • Compiler Error
  • Garbage value
  • 0

Question 48

Java
class Base {
  public final 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
  • Compiler Error
  • Exception

Question 49

Predict the output of following Java program Java
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 50

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

There are 50 questions to complete.

Last Updated :
Take a part in the ongoing discussion