Mahindra Placement Papers 2025 – Java Section- Set-1

Java Programming Questions and Solutions – Post 1

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 B implements Runnable {
    volatile int a;
    @Override
    public void run() {
        System.out.println(a++);
    }
}
public class A {
    public static void main(String args[]) {
        B classVar = new B();
        Thread t1 = new Thread(classVar);
        t1.start();
        Thread t2 = new Thread(classVar);
        t2.start();
    }
}

A. 0 1
B. 1 0
C. 0 0
D. Any of the above

Correct Answer: D. Any of the above

Explanation: Two threads share the same Runnable instance. Due to race conditions, the output can be 0 1, 1 0, or even 0 0 if the threads interleave in certain ways.

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

public class Test {
    public static void main(String[] args) {
        String s1 = new String("test");
        String s2 = new String("test");
        System.out.println(s1.equals(s2));
    }
}

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

Correct Answer: A. true

Explanation: The equals() method compares the content of the strings, not their references. Since both strings contain "test", the output is true.

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

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

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

Correct Answer: B. 0

Explanation: Arrays in Java are initialized with default values (0 for int). Since arr[1] is not assigned, it defaults to 0.

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

public class Test {
    public static void main(String[] args) throws Exception {
        char x = 65;
        System.out.println(x);
    }
}

A. 65
B. A
C. Compilation error
D. Runtime error

Correct Answer: B. A

Explanation: The decimal value 65 corresponds to the ASCII character ‘A’. When a char is printed using System.out.println(), it displays the character representation, not its numeric value. Therefore, the output is ‘A’.

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

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

A. Parent
B. Child
C. Compilation error
D. Runtime error

Correct Answer: B. Child

Explanation: Polymorphism ensures that the Child class’s show() method is called, printing Child.

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

public class Test {
    public static void main(String[] args) {
        try {
            System.out.println(5 / 0);
        } catch (ArithmeticException e) {
            System.out.println("Divide by zero");
        }
    }
}

A. 5
B. Divide by zero
C. Compilation error
D. Runtime error

Correct Answer: B. Divide by zero

Explanation: Division by zero throws an ArithmeticException, which is caught by the catch block, printing Divide by zero.

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

class B implements Runnable {
    AtomicInteger a = new AtomicInteger();
    @Override
    public void run() {
        System.out.println(a.getAndIncrement());
    }
}
public class A {
    public static void main(String args[]) {
        B classVar = new B();
        Thread t1 = new Thread(classVar);
        t1.start();
        Thread t2 = new Thread(classVar);
        t2.start();
    }
}

A. 0 1
B. 1 0
C. Both of the above
D. None of the above

Correct Answer: C. Both of the above

Explanation: AtomicInteger ensures atomic increments, so the output will be 0 1 or 1 0 depending on thread scheduling.

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

public class Test {
    static int value = 100;
    public static void main(String[] args) {
        Test t = null;
        System.out.println(t.value);
    }
}

A. 100
B. NullPointerException
C. Compilation error
D. 0

Correct Answer: A. 100

Explanation: value is a static variable, accessed via the class, not the instance. So, t.value resolves to Test.value, printing 100.

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

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

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

Correct Answer: B. false

Explanation: Integer values outside the range -128 to 127 are not cached, so x and y are different objects, making == return false.

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

import java.io.*;
class A {
    protected void process() {
        System.out.println("A");
    }
}
public class Test extends A {
    void process() {
        System.out.println("B");
    }
    public static void main(String[] args) throws Exception {
        new Test().process();
    }
}

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

Correct Answer: B. B

Explanation: The process() method in Test overrides the one in A, so B is printed.