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 would be the output of the following code execution?
public static void main(String[] args) throws java.lang.Exception {
int num = 18 * 0b1_0;
System.out.println(num);
}
A. 170
B. 50
C. 1010
D. Compiler error
Correct Answer: D. Compiler error
Explanation: The code should output 36 (18 * 2, since 0b1_0 is binary for 2), but since this isn’t an option, it’s likely a question error. We’ll assume a compiler error as a fallback, though the syntax is valid.
Question 2: What is the output of the following code?
public class Test {
public static void main(String[] args) {
String s = "";
System.out.println(s.length());
}
}
A. 0
B. 1
C. Compilation error
D. Runtime error
Correct Answer: A. 0
Explanation: An empty string "" has a length of 0, so s.length() returns 0.
Question 3: What is the output of the following code?
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 3) break;
System.out.print(i + " ");
}
}
}
A. 0 1 2
B. 0 1 2 3
C. 0 1 2 3 4
D. Compilation error
Correct Answer: A. 0 1 2
Explanation: The loop prints i from 0 to 4, but the break statement exits the loop when i is 3, so only 0, 1, and 2 are printed.
Question 4: What is the output of the following code?
public class Test {
void display(int x) {
System.out.println("Int: " + x);
}
void display(double x) {
System.out.println("Double: " + x);
}
public static void main(String[] args) {
Test t = new Test();
t.display(5);
}
}
A. Int: 5
B. Double: 5.0
C. Compilation error
D. Runtime error
Correct Answer: A. Int: 5
Explanation: Method overloading resolution chooses the most specific method. Since 5 is an integer, the display(int) method is called.
Question 5: What is the output of the following code?
public class Test {
static {
a[0] = 2;
}
static int[] a;
public static void main(String[] args) {
}
}
A. java.lang.StackOverflowError
B. java.lang.ExceptionInInitializerError
C. java.lang.ArrayIndexOutOfBoundsException
D. Runtime error
Correct Answer: D. Runtime error
Explanation: The static block tries to access a[0], but a is null, causing a NullPointerException.
Question 6: What is the output of the following code?
public class Test {
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
System.out.println(obj1 == obj2);
}
}
A. true
B. false
C. Compilation error
D. Runtime error
Correct Answer: B. false
Explanation: obj1 and obj2 are two different objects, so == returns false as it checks for reference equality.
Question 7: What is the output of the following code?
public class Test {
public static void main(String[] args) {
final int x = 10;
x = 20;
System.out.println(x);
}
}
A. 10
B. 20
C. Compilation error
D. Runtime error
Correct Answer: C. Compilation error
Explanation: A final variable cannot be reassigned after initialization, so x = 20 causes a compilation error.
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 {
public static void main(String[] args) throws InterruptedException {
System.out.print("Start ");
Thread.sleep(1000);
System.out.print("End");
}
}
A. Start End
B. End Start
C. Compilation error
D. Runtime error
Correct Answer: A. Start End
Explanation: Thread.sleep(1000) pauses the execution for 1 second, but the output order remains Start followed by End.
Question 10: What is the output of the following code?
abstract class A {
abstract void method();
}
public class Test {
public static void main(String[] args) {
A obj = new A();
}
}
A. Nothing
B. Compilation error
C. Runtime error
D. Abstract method called
Correct Answer: B. Compilation error
Explanation: You cannot instantiate an abstract class directly, so new A() causes a compilation error.