Java Basic code snippet based Question

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

java

public class Test {

    public static void main(String[] args) {

System.out.println(“Hello, World!”);

    }

}

a) Hello, World
b) Hello, World!
c) Compilation Error
d) Runtime Error

Answer: b) Hello, World!

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(x + y);

    }

}

a) 15
b) 5 10
c) 510
d) Compilation Error

Answer: a) 15

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

System.out.println(x++);

System.out.println(x);

    }

}

a) 5 5
b) 6 6
c) 5 6
d) Compilation Error

Answer: c) 5 6

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

System.out.println(++x);

    }

}

a) 5
b) 6
c) 5 6
d) Compilation Error

Answer: b) 6

5. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

System.out.println(x–);

System.out.println(x);

    }

}

a) 5 5
b) 4 4
c) 5 4
d) Compilation Error

Answer: c) 5 4

6. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

System.out.println(–x);

    }

}

a) 5
b) 4
c) 5 4
d) Compilation Error

Answer: b) 4

7. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        x += 10;

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) Compilation Error

Answer: c) 15

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

        x -= 5;

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) Compilation Error

Answer: a) 5

9. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        x *= 2;

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) Compilation Error

Answer: b) 10

10. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

        x /= 2;

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) Compilation Error

Answer: a) 5

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

        x %= 3;

System.out.println(x);

    }

}

a) 1
b) 2
c) 3
d) Compilation Error

Answer: b) 1

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

System.out.println(x > 5);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

System.out.println(x < 5);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: b) false

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

System.out.println(x == 10);

    }

}

a) true
b) false
c) 10
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

System.out.println(x != 10);

    }

}

a) true
b) false
c) 10
d) Compilation Error

Answer: b) false

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

System.out.println(x >= 10);

    }

}

a) true
b) false
c) 10
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

System.out.println(x <= 5);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: b) false

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(x > y);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: b) false

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(x < y);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 5;

System.out.println(x == y);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(x != y);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(x >= y);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: b) false

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

java

public class Test {

    public static void main(String[] args) {

        int x = 10;

        int y = 5;

System.out.println(x <= y);

    }

}

a) true
b) false
c) 10
d) Compilation Error

Answer: b) false

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 5;

System.out.println(x == y && x > 0);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

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

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(x < y || x > 0);

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

26. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        int y = 10;

System.out.println(!(x > y));

    }

}

a) true
b) false
c) 5
d) Compilation Error

Answer: a) true

27. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        if (x > 0) {

System.out.println(“Positive”);

        }

    }

}

a) Positive
b) Negative
c) 5
d) Compilation Error

Answer: a) Positive

28. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = -5;

        if (x > 0) {

System.out.println(“Positive”);

        } else {

System.out.println(“Negative”);

        }

    }

}

a) Positive
b) Negative
c) -5
d) Compilation Error

Answer: b) Negative

29. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        if (x > 0) {

System.out.println(“Positive”);

        } else {

System.out.println(“Negative”);

        }

    }

}

a) Positive
b) Negative
c) 5
d) Compilation Error

Answer: a) Positive

30. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        if (x > 0) {

System.out.println(“Positive”);

        } else if (x < 0) {

System.out.println(“Negative”);

        } else {

System.out.println(“Zero”);

        }

    }

}

a) Positive
b) Negative
c) Zero
d) Compilation Error

Answer: a) Positive

31. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = -5;

        if (x > 0) {

System.out.println(“Positive”);

        } else if (x < 0) {

System.out.println(“Negative”);

        } else {

System.out.println(“Zero”);

        }

    }

}

a) Positive
b) Negative
c) Zero
d) Compilation Error

Answer: b) Negative

32. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        if (x > 0) {

System.out.println(“Positive”);

        } else if (x < 0) {

System.out.println(“Negative”);

        } else {

System.out.println(“Zero”);

        }

    }

}

a) Positive
b) Negative
c) Zero
d) Compilation Error

Answer: c) Zero

33. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 5;

        switch (x) {

            case 1:

System.out.println(“One”);

                break;

            case 2:

System.out.println(“Two”);

                break;

            case 3:

System.out.println(“Three”);

                break;

            case 4:

System.out.println(“Four”);

                break;

            case 5:

System.out.println(“Five”);

                break;

            default:

System.out.println(“Invalid”);

        }

    }

}

a) One
b) Two
c) Three
d) Five

Answer: d) Five

34. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 3;

        switch (x) {

            case 1:

System.out.println(“One”);

                break;

            case 2:

System.out.println(“Two”);

                break;

            case 3:

System.out.println(“Three”);

                break;

            default:

System.out.println(“Invalid”);

        }

    }

}

a) One
b) Two
c) Three
d) Invalid

Answer: c) Three

35. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 6;

        switch (x) {

            case 1:

System.out.println(“One”);

                break;

            case 2:

System.out.println(“Two”);

                break;

            case 3:

System.out.println(“Three”);

                break;

            default:

System.out.println(“Invalid”);

        }

    }

}

a) One
b) Two
c) Three
d) Invalid

Answer: d) Invalid

36. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 2;

        int y = 0;

        switch (x) {

            case 1:

                y = 1;

                break;

            case 2:

                y = 2;

                break;

            case 3:

                y = 3;

                break;

            default:

                y = 4;

        }

System.out.println(y);

    }

}

a) 0
b) 1
c) 2
d) 3

Answer: c) 2

37. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 4;

        int y = 0;

        switch (x) {

            case 1:

                y = 1;

                break;

            case 2:

                y = 2;

                break;

            case 3:

                y = 3;

                break;

            default:

                y = 4;

        }

System.out.println(y);

    }

}

a) 0
b) 1
c) 2
d) 4

Answer: d) 4

38. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 2;

        switch (x) {

            case 1:

System.out.println(“One”);

                break;

            case 2:

System.out.println(“Two”);

            case 3:

System.out.println(“Three”);

                break;

            default:

System.out.println(“Invalid”);

        }

    }

}

a) One
b) Two Three
c) Two
d) Invalid

Answer: b) Two Three

39. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        for (int i = 0; i< 5; i++) {

            x += i;

        }

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) 20

Answer: b) 10

40. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        for (int i = 1; i<= 5; i++) {

            x += i;

        }

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) 20

Answer: c) 15

41. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 1;

        for (int i = 1; i<= 3; i++) {

            x *= i;

        }

System.out.println(x);

    }

}

a) 3
b) 6
c) 9
d) 1

Answer: b) 6

42. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        int i = 0;

        while (i< 5) {

            x += i;

i++;

        }

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) 20

Answer: b) 10

43. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        int i = 1;

        while (i<= 5) {

            x += i;

i++;

        }

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) 20

Answer: c) 15

44. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 1;

        int i = 1;

        while (i<= 3) {

            x *= i;

i++;

        }

System.out.println(x);

    }

}

a) 3
b) 6
c) 9
d) 1

Answer: b) 6

45. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        int i = 0;

        do {

            x += i;

i++;

        } while (i< 5);

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) 20

Answer: b) 10

46. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        int i = 1;

        do {

            x += i;

i++;

        } while (i<= 5);

System.out.println(x);

    }

}

a) 5
b) 10
c) 15
d) 20

Answer: c) 15

47. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 1;

        int i = 1;

        do {

            x *= i;

i++;

        } while (i<= 3);

System.out.println(x);

    }

}

a) 3
b) 6
c) 9
d) 1

Answer: b) 6

48. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        for (int i = 1; i<= 5; i++) {

            if (i % 2 == 0) {

                continue;

            }

            x += i;

        }

System.out.println(x);

    }

}

a) 9
b) 10
c) 15
d) 5

Answer: a) 9

49. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 0;

        for (int i = 1; i<= 5; i++) {

            if (i == 3) {

                break;

            }

            x += i;

        }

System.out.println(x);

    }

}

a) 3
b) 6
c) 10
d) 5

Answer: a) 3

50. What will be the output of the following code?

java

public class Test {

    public static void main(String[] args) {

        int x = 1;

        for (int i = 1; i<= 3; i++) {

            x *= i;

        }

System.out.println(x);

    }

}

a) 1
b) 6
c) 9
d) 3

Answer: b) 6

51. What will be the output of the following code?

java

class A {

    void display() {

System.out.println(“A”);

    }

}

class B extends A {

    void display() {

System.out.println(“B”);

    }

    public static void main(String[] args) {

        B obj = new B();

obj.display();

    }

}

a) A
b) B
c) AB
d) BA

Answer: b) B

52. What will be the output of the following code?

java

class A {

A() {

System.out.println(“A”);

    }

}

class B extends A {

B() {

System.out.println(“B”);

    }

    public static void main(String[] args) {

        B obj = new B();

    }

}

a) A
b) B
c) AB
d) BA

Answer: c) AB

53. What will be the output of the following code?

java

class A {

    static void display() {

System.out.println(“A”);

    }

}

class B extends A {

    static void display() {

System.out.println(“B”);

    }

    public static void main(String[] args) {

        A obj = new B();

obj.display();

    }

}

a) A
b) B
c) AB
d) BA

Answer: a) A

54. What will be the output of the following code?

java

class A {

    void display() {

System.out.println(“A”);

    }

}

class B extends A {

    void display() {

System.out.println(“B”);

    }

}

class C extends B {

    void display() {

System.out.println(“C”);

    }

    public static void main(String[] args) {

        C obj = new C();

obj.display();

    }

}

a) A
b) B
c) C
d) ABC

Answer: c) C

55. What will be the output of the following code?

java

class A {

A() {

System.out.println(“A”);

    }

}

class B extends A {

B() {

System.out.println(“B”);

    }

}

class C extends B {

C() {

System.out.println(“C”);

    }

    public static void main(String[] args) {

        C obj = new C();

    }

}

a) A
b) AB
c) ABC
d) CBA

Answer: c) ABC

56. What will be the output of the following code?

java

class A {

    static void display() {

System.out.println(“A”);

    }

}

class B extends A {

    static void display() {

System.out.println(“B”);

    }

}

class C extends B {

    static void display() {

System.out.println(“C”);

    }

    public static void main(String[] args) {

        A obj = new C();

obj.display();

    }

}

a) A
b) B
c) C
d) Compilation Error

Answer: a) A

57. What will be the output of the following code?

java

interface A {

    void display();

}

class B implements A {

    public void display() {

System.out.println(“B”);

    }

    public static void main(String[] args) {

        B obj = new B();

obj.display();

    }

}

a) A
b) B
c) AB
d) Compilation Error

Answer: b) B

58. What will be the output of the following code?

java

interface A {

    void display();

}

interface B extends A {

    void show();

}

class C implements B {

    public void display() {

System.out.println(“Display”);

    }

    public void show() {

System.out.println(“Show”);

    }

    public static void main(String[] args) {

        C obj = new C();

obj.display();

obj.show();

    }

}

a) Display
b) Show
c) Display Show
d) Compilation Error

Answer: c) Display Show

59. What will be the output of the following code?

java

interface A {

    default void display() {

System.out.println(“A”);

    }

}

class B implements A {

    public static void main(String[] args) {

        B obj = new B();

obj.display();

    }

}

a) A
b) Compilation Error
c) Runtime Error
d) None of the above

Answer: a) A

60. What will be the output of the following code?

java

interface A {

    default void display() {

System.out.println(“A”);

    }

}

interface B {

    default void display() {

System.out.println(“B”);

    }

}

class C implements A, B {

    public void display() {

A.super.display();

    }

    public static void main(String[] args) {

        C obj = new C();

obj.display();

    }

}

a) A
b) B
c) Compilation Error
d) Runtime Error

Answer: a) A

61. What will be the output of the following code?

java

class Test {

    static void display() {

System.out.println(“Static method”);

    }

    void show() {

System.out.println(“Instance method”);

    }

    public static void main(String[] args) {

        Test obj = new Test();

obj.display();

obj.show();

    }

}

a) Static method Instance method
b) Instance method Static method
c) Compilation Error
d) Runtime Error

Answer: a) Static method Instance method

62. What will be the output of the following code?

java

class Test {

    static void display() {

System.out.println(“Static method”);

    }

    void show() {

System.out.println(“Instance method”);

    }

    public static void main(String[] args) {

Test.display();

    }

}

a) Static method
b) Instance method
c) Compilation Error
d) Runtime Error

Answer: a) Static method

63. What will be the output of the following code?

java

class Test {

    static void display() {

System.out.println(“Static method”);

    }

    void show() {

System.out.println(“Instance method”);

    }

    public static void main(String[] args) {

        Test obj = new Test();

obj.show();

    }

}

a) Static method
b) Instance method
c) Compilation Error
d) Runtime Error

Answer: b) Instance method

64. What will be the output of the following code?

java

class Test {

    static {

System.out.println(“Static block”);

    }

    {

System.out.println(“Instance block”);

    }

Test() {

System.out.println(“Constructor”);

    }

    public static void main(String[] args) {

        new Test();

    }

}

a) Static block
b) Instance block
c) Constructor
d) Static block Instance block Constructor

Answer: d) Static block Instance block Constructor

65. What will be the output of the following code?

java

class Test {

    static {

System.out.println(“Static block”);

    }

    {

System.out.println(“Instance block”);

    }

Test() {

System.out.println(“Constructor”);

    }

    public static void main(String[] args) {

        new Test();

        new Test();

    }

}

a) Static block Instance block Constructor Instance block Constructor
b) Instance block Constructor Instance block Constructor
c) Static block Instance block Constructor
d) Instance block Constructor

Answer: a) Static block Instance block Constructor Instance block Constructor

66. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    public static void main(String[] args) {

        Test obj = new Test();

System.out.println(obj.x);

System.out.println(obj.y);

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

67. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    public static void main(String[] args) {

System.out.println(Test.x);

System.out.println(new Test().y);

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

68. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    void display() {

System.out.println(x);

System.out.println(y);

    }

    public static void main(String[] args) {

        new Test().display();

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

69. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    void display() {

System.out.println(x);

System.out.println(y);

    }

    public static void main(String[] args) {

        Test obj1 = new Test();

        Test obj2 = new Test();

        obj1.display();

        obj2.display();

    }

}

a) 10 20 10 20
b) 20 10 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20 10 20

70. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

71. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

Test.display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

72. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

        new Test().display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

73. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    void display() {

System.out.println(x);

System.out.println(y);

    }

    public static void main(String[] args) {

        new Test().display();

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

74. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    void display() {

System.out.println(x);

System.out.println(y);

    }

    public static void main(String[] args) {

        Test obj = new Test();

obj.display();

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

75. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

Test.display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

76. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

        Test obj = new Test();

obj.display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

77. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

        new Test().display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

78. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    static void display() {

System.out.println(x);

    }

    public static void main(String[] args) {

        Test obj = new Test();

obj.display();

    }

}

a) 10
b) 20
c) Compilation Error
d) Runtime Error

Answer: a) 10

79. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    void display() {

System.out.println(x);

System.out.println(y);

    }

    public static void main(String[] args) {

        Test obj = new Test();

obj.display();

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

80. What will be the output of the following code?

java

class Test {

    static int x = 10;

    int y = 20;

    void display() {

System.out.println(x);

System.out.println(y);

    }

    public static void main(String[] args) {

        new Test().display();

    }

}

a) 10 20
b) 20 10
c) Compilation Error
d) Runtime Error

Answer: a) 10 20

What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now