Tricky Java Programming Questions
Java Program Output
Question 1: Unicode Character
Java Code
public class A {
public static void main(String args[]) {
//\u000d System.out.println("hello");
}
}
Output
hello
The Unicode character \u000d
represents a carriage return. In some contexts, this might not be visible, but in this case, it does not cause an error and the "hello" is printed.
Question 2: Arithmetic Operators
Java Code
public class Test {
public static void main(String args[]) {
int i = 20 + +9 - -12 + +4 - -13 + +19;
System.out.println(i); // Output: 77
}
}
Question 3: String and StringBuilder Comparison
Java Code
String s1 = "Java";
String s2 = "Java";
StringBuilder sb1 = new StringBuilder();
sb1.append("Ja").append("va");
System.out.println(s1 == s2); // true
System.out.println(s1.equals(s2)); // true
System.out.println(sb1.toString() == s1); // false
System.out.println(sb1.toString().equals(s1)); // true
Question 4: try-catch-finally
Java Code
public class Demo {
public static void main(String args[]) {
System.out.print("a");
try {
System.out.print("b");
throw new IllegalArgumentException();
} catch (RuntimeException e) {
System.out.print("c");
} finally {
System.out.print("d");
}
System.out.print("e"); // Output: abcd
}
}
Question 5: Variable Initialization
Java Code
public class _C {
private static int $;
public static void main(String main[]) {
String a_b; //Variable not initialized
System.out.print($); // Output: 0
System.out.print(a_b); // Output: null
}
}
Question 6: ArrayList and Array Manipulation
Java Code
int[] array = {6, 9, 8};
List<Integer> list = new ArrayList<>();
list.add(array[0]);
list.add(array[2]);
list.set(1, array[1]);
list.remove(0);
System.out.println(list); // Output: [9]
Question 7: Character Array Comparison
Java Code
public class Actors {
public static void main(String arg[]) {
char[] ca = {0x4e, 'N', 78}; //Note: \u004e should be 'N'
System.out.println((ca[0] == ca[1]) + " " + (ca[0] == ca[2])); // Output: true true
}
}
Question 8: Invalid switch Statement
Java Code
public class SwitchExample {
public static void main(String args[]) {
int x = 80;
switch (x) {
case x > 70: //Invalid Case
System.out.println("True");
break;
case 65 < x && x <= 70: //Invalid Case
System.out.println("False");
break;
default:
System.out.println("Default"); // Output: Default
}
}
}
Question 9: String Concatenation
Java Code
public class StringConcat {
public static void main(String args[]) {
String str = "ONE" + 1 + 2 + "TWO" + "THREE" + 3 + 4 + "FOUR" + "FIVE" + 5;
System.out.println(str); // Output: ONE12TWOTHREE34FOURFIVE5
}
}
Question 10: Math.min()
Java Code
import java.lang.Double;
public class Demo {
public static void main(String args[]) {
System.out.println(Math.min(Double.MIN_VALUE, 0.0d)); // Output: -4.9E-324
}
}
Question 11: Long Integer Calculation
Java Code
public class LongExample {
public static void main(String[] args) {
long longWithL = 1000 * 60 * 60 * 24 * 365L;
long longWithoutL = 1000 * 60 * 60 * 24 * 365;
System.out.println(longWithL + " and " + longWithoutL); // Output: 31536000000 and 2147483647
}
}
Question 12: Static Block and Variable Calculation
Java Code
public class Demo {
static int x = 1111;
static {
x = x-- - --x;
}
{
x = x++ + ++x;
}
public static void main(String args[]) {
System.out.println(x); // Output: 2
}
}