Java Find Output Program Questions and Answers

Here are 25 MCQs Java code snippets related to various concepts, including OOP, loops, arrays, data types, threading, exceptions, strings, and other Java specifics. 

Each question includes a code snippet and multiple-choice options for the output. After you complete the test, you can check your score, the correct answer, and an explanation.

1. What will be the output of the following Java code?

class Base {
    Base() {
        System.out.print("Base ");
    }
}
class Derived extends Base {
    Derived() {
        System.out.print("Derived ");
    }
}
public class Main {
    public static void main(String[] args) {
        new Derived();
    }
}
a) Base Derived
b) Derived Base
c) Derived
d) Base

2. What will be the output of the following Java code?

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[4]);
a) 1
b) 2
c) 4
d) 5

3. What will be the output of the following Java code?

for(int i = 0; i < 5; i++) {
    if(i % 2 == 0) continue;
    System.out.print(i + " ");
}
a) 1 3
b) 0 2 4
c) 1 3 5
d) 0 1 2 3 4

4. What will be the output of the following Java code?

String s = "Java";
s.concat(" Programming");
System.out.println(s);
a) Java
b) Java Programming
c) Programming
d) Null

5. What will be the output of the following Java code involving exception handling?

try {
    System.out.print("Hello ");
    throw new RuntimeException();
    } catch(Exception e) {
        System.out.print("World");
    }
a) Hello
b) World
c) Hello World
d) Runtime Exception

6. What will be the output of the following Java code involving threading?

class MyThread extends Thread {
    public void run() {
        System.out.print("Thread running ");
    }
}
public class Test {
    public static void main(String[] args) {
        new MyThread().start();
        System.out.print("Main ");
    }
}
a) Thread running Main
b) Main Thread running
c) Main
d) Thread running

7. What will be the output of the following Java code using a double data type?

double a = 1.0/0.0;
System.out.println(a);
a) Infinity
b) Exception
c) 0
d) Undefined

8. What will be the output of the following Java code?

System.out.println("1" + new Integer(2) + 3);
a) 123
b) 6
c) 33
d) 15

9. What will be the output of the following Java code with bitwise operators?

int a = 6;  // 110 in binary
int b = 4;  // 100 in binary
System.out.println(a & b);
a) 2
b) 4
c) 6
d) 0

10. What will be the output of the following Java code using the modulo operator?

System.out.println(10 % 4);
a) 0
b) 1
c) 2
d) 4

11. What will be the output of the following Java code?

int i = 0;
while (i < 5) {
    i++;
    if (i == 3) continue;
    System.out.print(i + " ");
}
a) 1 2 4 5
b) 1 2 3 4 5
c) 2 3 4 5
d) 1 2 3 4

12. What will be the output of the following Java code?

double num = 1.0 / 2.0;
System.out.println(num);
a) 0.5
b) 0
c) 1
d) 0.50

13. What will be the output of the following Java code?

char letter = 'A' + 1;
System.out.println(letter);
a) A
b) B
c) 66
d) None of the above

14. What will be the output of the following Java code?

System.out.println(11 % 6);
a) 5
b) 6
c) 1
d) 11

15. What will be the output of the following Java code?

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun && isFishTasty);
a) true
b) false
c) Compilation error
d) None of the above

16. What will be the output of the following Java code?

int x = 10;
int y = ++x;
System.out.println(x + ", " + y);
a) 10, 11
b) 11, 11
c) 11, 10
d) 10, 10

17. What will be the output of the following Java code?

byte b = 126;
b += 1;
b += 1;
System.out.println(b);
a) 128
b) -128
c) 127
d) -126

18. What will be the output of the following Java code?

double pi = 3.14159;
double radius = 10.0;
System.out.println(pi * radius * radius);
a) 314.159
b) 314.16
c) 315
d) 314

19. What will be the output of the following Java code?

System.out.println("1" + 2 + 3);
a) "123"
b) "6"
c) "15"
d) None of the above

20. What will be the output of the following Java code?

int a = 6;  // 110 in binary
int b = 4;  // 100 in binary
System.out.println(a | b);
a) 2
b) 4
c) 6
d) 10

21. What will be the output of the following Java code?

int[] arr = {7, 8, 9};
System.out.println(arr[arr[1]]);
a) 7
b) 8
c) 9
d) Throws an exception

22. What will be the output of the following Java code?

double[] arr = {1.1, 2.2, 3.3};
arr[0] = arr[2];
System.out.println(arr[0]);
a) 1.1
b) 2.2
c) 3.3
d) None of the above

23. What will be the output of the following Java code?

boolean[] arr = new boolean[5];
System.out.println(arr[3]);
a) true
b) false
c) null
d) Throws an exception

24. What will be the output of the following Java code?

class Example {
    int x;  // Default value 0
    static int y;  // Default value 0
    void method() {
        int z = 5;  // Local variable must be initialized
        System.out.println(x + y + z);
    }
}
public class Test {
    public static void main(String[] args) {
        new Example().method();
    }
}
a) 0
b) 5
c) 10
d) Compilation error

25. What will be the output of the following Java code?

int[] numbers = new int[5];
for(int i = 0; i < numbers.length; i++) {
    numbers[i] = i * i;
}
System.out.println(numbers[2]);
a) 1
b) 2
c) 4
d) 8

Comments