Java Coding MCQ Questions and Answers

Welcome to Java Coding MCQ Questions. Here, we present 20 MCQs (Multiple-Choice Questions) with code snippets to test your understanding of Java coding and logical stills. Let's get started!

1. What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        System.out.println("Java" + 1 + 2 + 3);
    }
}
a) Java6
b) Java123
c) 6Java
d) Java1236

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

public class Test {
    public static void main(String[] args) {
        int a = 5, b = 7;
        System.out.println(a + b + " is the result");
    }
}
a) 12 is the result
b) 5 and 7 is the result
c) 57 is the result
d) 5+7 is the result

3. What does this Java code print?

public class App {
    public static void main(String[] args) {
        int x = 10;
        int y = ++x;
        System.out.println(x + " " + y);
    }
}
a) 10 10
b) 10 11
c) 11 10
d) 11 11

4. What is the result of running the following Java code?

public class Example {
    public static void main(String[] args) {
        boolean flag = (10 > 9);
        System.out.println(flag);
    }
}
a) false
b) true
c) 0
d) 1

5. Analyze the following code snippet. What will it output?

public class MyClass {
    public static void main(String[] args) {
        int i = 5;
        int j = i++;
        System.out.println(i + " " + j);
    }
}
a) 5 5
b) 6 6
c) 6 5
d) 5 6

6. What does this Java code print?

public class Counter {
    public static void main(String[] args) {
        int count = 0;
        count += 1;
        System.out.println(count);
    }
}
a) 0
b) 1
c) Error
d) None of the above

7. What will the following Java program output?

public class Main {
    public static void main(String[] args) {
        int x = 5;
        System.out.println(x > 3 || x < 4);
    }
}
a) true
b) false
c) Compilation error
d) Runtime error

8. What is printed by this Java code?

public class Test {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        System.out.println(numbers[1]);
    }
}
a) 1
b) 2
c) 3
d) Error

9. What will this code snippet output?

public class LoopTest {
    public static void main(String[] args) {
        for(int i = 0; i < 3; i++) {
            System.out.print(i + " ");
        }
    }
}
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) 0 1

10. What does the following Java program output?

public class Test {
    public static void main(String[] args) {
        int x = 5;
        int y = x;
        System.out.println(x == y);
    }
}
a) true
b) false
c) 5
d) None of the above

11. Consider the Java program below. What will be its output?

public class PrintTest {
    public static void main(String[] args) {
        char letter = 'A';
        letter++;
        System.out.println(letter);
    }
}
a) A
b) B
c) C
d) Error

12. What will the following Java code output?

public class Example {
    public static void main(String[] args) {
        System.out.println(4 * 5 / 2);
    }
}
a) 10
b) 20
c) 6
d) 0

13. What is the output of this Java code snippet?

public class Test {
    public static void main(String[] args) {
        System.out.println('A' + 1);
    }
}
a) A1
b) B
c) 66
d) 65

14. What does the following code snippet output?

public class JavaTest {
    public static void main(String[] args) {
        double pi = 3.14;
        double radius = 10;
        double area = pi * radius * radius;
        System.out.println(area);
    }
}
a) 314.0
b) 31.4
c) 3.14
d) 314

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

public class Test {
    public static void main(String[] args) {
        int x = 3;
        int y = x * (x + 5) / 2;
        System.out.println(y);
    }
}
a) 12
b) 15
c) 18
d) 21

16. What is the output of this Java code?

public class Compute {
    public static void main(String[] args) {
        int result = 2;
        result *= 3 + 5;
        System.out.println(result);
    }
}
a) 10
b) 16
c) 24
d) 26

17. What does the following Java code print?

public class SwitchTest {
    public static void main(String[] args) {
        int day = 2;
        switch (day) {
            case 1: System.out.println("Monday"); break;
            case 2: System.out.println("Tuesday"); break;
            default: System.out.println("Sunday");
        }
    }
}
a) Monday
b) Tuesday
c) Sunday
d) No output

18. What will the following Java code output?

public class Hello {
    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Sum is: " + (a + b));
    }
}
a) Sum is: 10
b) Sum is: 20
c) Sum is: 30
d) Sum is: 1020

19. What is the output of the following Java program?

public class Test {
    public static void main(String[] args) {
        int num = 50;
        num = num-- - --num;
        System.out.println(num);
    }
}
a) 0
b) 1
c) -49
d) 49

20. Analyze the following Java code snippet. What will it output?

public class Calculation {
    public static void main(String[] args) {
        int i = 3;
        int j = ++i * 10;
        System.out.println(j);
    }
}
a) 30
b) 40
c) 50
d) 60

Comments