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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer