Mahindra Placement Papers 2025 – Java Section- Set-2

Java Programming Questions and Solutions – Post 2

These Java Questions will help you to prepare the Java Section of the Mahindra placement Papers. If you have done BE/BTech from CS or IT, These type of questions you can get in your GET CS IT exam of Mahindra.

Question 1: What is the output of the following code?

class A {
    int z;
    static void func() {
        System.out.println(z++);
    }
    public static void main(String args[]) {
        func();
    }
}

A. 0
B. Junk value
C. Compilation error
D. Runtime error

Correct Answer: C. Compilation error

Explanation: The method func() is static and cannot access the instance variable z without an instance of the class A.

Question 2: What is the output of the following code?

public class Test {
    public static void main(String[] args) {
        int x = 3;
        System.out.println(x++ + x);
    }
}

A. 6
B. 7
C. 8
D. Compilation error

Correct Answer: B. 7

Explanation: x++ uses 3 and increments x to 4. The expression becomes 3 + 4, which equals 7.

Question 3: What is the output of the following code?

public class Test {
    public static void main(String[] args) {
        String s = null;
        System.out.println("Value: " + s);
    }
}

A. Value: null
B. Compilation error
C. Runtime error
D. Value:

Correct Answer: A. Value: null

Explanation: Concatenating a null string with another string results in the string "Value: null".

Question 4: What is the output of the following code?

public class Test {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println(arr[3]);
    }
}

A. 0
B. 3
C. Compilation error
D. Runtime error

Correct Answer: D. Runtime error

Explanation: Accessing arr[3] in an array of size 3 (indices 0 to 2) causes an ArrayIndexOutOfBoundsException.

Question 5: What is the output of the following code?

public class Test {
    public static void main(String[] args) throws Exception {
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(str1 == str2);
    }
}

A. true
B. false
C. Compilation error
D. Runtime error

Correct Answer: A. true

Explanation: String literals are interned in Java, so str1 and str2 point to the same object in the string pool, making == return true.

Question 6: What is the output of the following code?

class Parent {
    int x = 10;
}
public class Child extends Parent {
    int x = 20;
    public static void main(String[] args) {
        Parent obj = new Child();
        System.out.println(obj.x);
    }
}

A. 10
B. 20
C. Compilation error
D. Runtime error

Correct Answer: A. 10

Explanation: Variables are not polymorphic. The type of obj is Parent, so obj.x accesses Parent’s x, which is 10.

Question 7: What is the output of the following code?

public class Test implements Runnable {
    public void run() {
        System.out.println("Thread running");
    }
    public static void main(String[] args) {
        Test t = new Test();
        Thread th = new Thread(t);
        th.run();
    }
}

A. Thread running
B. Nothing
C. Compilation error
D. Runtime error

Correct Answer: A. Thread running

Explanation: Calling th.run() directly executes the run() method in the main thread, printing Thread running. Note that th.start() would start a new thread, but run() does not.

Question 8: What is the output of the following code?

class A {
    int x = 1;
}
public class Test extends A {
    public static void main(String args[]) {
        Test obj1 = new Test();
        A obj2 = new A();
        obj1 = (Test)obj2;
        System.out.println(obj1.x);
    }
}

A. 1
B. Compilation error
C. Runtime error
D. None of the above

Correct Answer: C. Runtime error

Explanation: The downcast from A to Test causes a ClassCastException because obj2 is not an instance of Test.

Question 9: What is the output of the following code?

public class Test {
    static int x;
    static {
        x = 5;
    }
    public static void main(String[] args) {
        System.out.println(x);
    }
}

A. 0
B. 5
C. Compilation error
D. Runtime error

Correct Answer: B. 5

Explanation: The static block initializes x to 5 before the main method runs, so 5 is printed.

Question 10: What is the output of the following code?

public class Test {
    public static void main(String[] args) {
        try {
            int x = Integer.parseInt("abc");
        } catch (NumberFormatException e) {
            System.out.println("Number error");
        } catch (Exception e) {
            System.out.println("General error");
        }
    }
}

A. Number error
B. General error
C. Compilation error
D. Runtime error

Correct Answer: A. Number error

Explanation: Integer.parseInt("abc") throws a NumberFormatException, which is caught by the first catch block, printing Number error.