Java tricky interview questions

Abhishek vt
5 min readMay 24, 2024

--

Greetings everyone! Today, we’re exploring tricky interview questions concerning the core java. These resources are specifically designed to aid individuals preparing for interviews. Whether you’re navigating technical interviews or seeking to enhance your understanding, this series aims to be a valuable asset in your journey toward success.

  1. What is the output of the following code snippet?
 public static void main(String[] args) {

int a = 5;
int b = 10;
System.out.println(a b);

}

Answer : 16

2. What is the result of this code execution?

public static void main(String[] args) {

String str = null;
if (str instanceof String) {
System.out.println("True");
} else {
System.out.println("False");
}

}

Answer : False

3. What is the output of the following code?

 public static void main(String[] args) {

int x = 10;
int y = (x > 5) ? (x < 10 ? 1 : 2) : 3;
System.out.println(y);

}

Answer : 2

4. What is the output of the following code?

int a = 10;
System.out.println(a = (a = 5) * (a / 5));

Answer : 15

5. What will be the result of this code?

public static void main(String[] args) {

String s1 = "Java";
String s2 = "Ja" "va";
System.out.println(s1 == s2);


}

Answer : True

6. What will be the output?

 public static void main(String[] args) {

int[] arr = new int[5];
System.out.println(arr[0]);

}

Answer : 0

7. What is the output?

public static void main(String[] args) {

int i = 0;
int j = i i;
System.out.println(j);


}

Answer : 2

8. What will the following code print?

public static void main(String[] args) {

System.out.println("Hello" == new String("Hello"));

}

Answer : False

9. What is the result of the following?

int a = 5;
int b = 10;
a ^= b ^= a ^= b;
System.out.println(a "-" b);

Answer : 0–5

10. What will this code output?

public static void main(String[] args) {

System.out.println(10 20 "30" 40 50);

}

Answer : 30304050

11. What is the output?

 public static void main(String[] args) {

String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1.equals(s2) && s1 == s2);

}

Answer : False

12. What will this code print?

 public static void main(String[] args) {

Integer a = 127;
Integer b = 127;
System.out.println(a == b);

}

Answer : True

13. What will the following code produce?

 public static void main(String[] args) {

for (int i = 0; i < 3; i ) {
switch (i) {
case 0:
System.out.print("A");
case 1:
System.out.print("B");
break;
case 2:
System.out.print("C");
}
}

}

Answer: ABBC

14. What is the result of this code?

 public static void main(String[] args) {

String a = "apple";
String b = "apple";
a = a.replace("p", "b");
System.out.println(a b);

}

Answer : abbleapple

15. What is the output?

 public static void main(String[] args) {

boolean a = true;
boolean b = false;
boolean c = a || b && !a;
System.out.println(c);

}

Answer : true

16. What is the result?

 public static void main(String[] args) {

int x = 2;
int y = 0;
for (; y < 10; y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y " ");
}

}

Answer : 1 3 5 7 9

17. What will this code output?

public static void main(String[] args) {

try {
int a = 10 / 0;
System.out.println("This will not be printed");
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block executed");
}

}

Answer :

Exception caught

Finally block executed

18. What is the result?

 public static void main(String[] args) {

int a = 5;
int b = 10;
if ((a = 3) == b) {
System.out.println(a);
} else {
System.out.println(a b);
}

}

Answer : 13

19. What will this code print?

public static void main(String[] args) {

System.out.println(Math.min(Double.MIN_VALUE, 0.0d));

}

Answer : 0.0

20. What is the output of this code?

public static void main(String[] args) {

int a = 0;
System.out.println(a == a);

}

Answer : false

21. What will this code produce?

public static void main(String[] args) {

String s1 = new String("xyz");
String s2 = "xyz";
System.out.println(s1.intern() == s2);

}

Answer : True

22. What is the output of the following?

 public static void main(String[] args) {

List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.set(1, "C");
list.add("D");
System.out.println(list);

}

Answer : [A, C, D]

23 . What will be the result of this code?

 public static void main(String[] args) {

int[] arr = { 1, 2, 3, 4, 5 };
int sum = 0;
for (int i = 0; i < arr.length; i ) {
sum = (arr[i] % 2 == 0) ? arr[i] : 0;
}
System.out.println(sum);

}

Answer : 6

24. What is the output of this code?

 public static void main(String[] args) {

int x = 10;
int y = 20;
int z = x = y -= x = y;
System.out.println(z);

}

Answer : 0

25. What is the output of this code?

 public static void main(String[] args) {

int[] nums = { 1, 2, 3, 4, 5 };
for (int num : nums) {
if (num % 2 == 0) {
continue;
}
System.out.print(num " ");
}
}

Answer : 1 3 5

--

--

Abhishek vt

👨‍💻Java & Blockchain Developer 🌐 | Trainer 📚 | Interview Coach 🎥 | Sharing tutorials and tips on Java ☕️ & Blockchain tech | www.abhishekvt.com