Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
Which of the following is NOT a primitive data type in Java?
Answer: C
String is a non-primitive (reference) data type in Java. Primitive types include int, boolean, double, float, char, long, short, byte.
Q.2Medium
What is the size of 'char' data type in Java?
Answer: B
The 'char' data type in Java is 2 bytes (16 bits) because it uses Unicode character encoding.
Q.3Medium
Which of the following will compile without error? public static void main(String args[]) { int x = 10; final int y = 20; y = 30; }
Answer: B
Once a final variable is assigned, it cannot be reassigned. The line 'y = 30;' will cause a compilation error.
Q.4Medium
What is the output of: System.out.println(310);
Answer: B
When both operands are integers, the division result is an integer. 310 = 3 (integer division, not floating-point).
Q.5Medium
Which operator has the highest precedence in Java?
Answer: C
Parentheses () and array access [] have the highest precedence in Java. Multiplication, division, and modulo come next, followed by addition and subtraction.
Q.6Medium
What is the output of: System.out.println(5 > 3 ? 'A' : 'B');
Answer: A
The ternary operator (?:) evaluates the condition (5 > 3, which is true) and returns the first value 'A'.
Q.7Medium
Which statement is correct about Java variables?
Answer: A
Java variable names are case-sensitive. 'age' and 'Age' are different variables. Names cannot start with digits, cannot contain spaces, and special characters are not allowed.
Q.8Medium
Identify the output: System.out.println("Java".length());
Answer: A
The length() method returns the number of characters in the string "Java", which is 4 characters.
Q.9Medium
Which of the following is the correct way to declare and initialize an array in Java?
Answer: B
The correct syntax is 'datatype[] arrayName = {elements};' or 'datatype[] arrayName = new datatype[size];'
Q.10Medium
What will be the output: System.out.println('A' + 'B');
Answer: B
Characters are treated as their ASCII values in arithmetic operations. 'A' (65) + 'B' (66) = 131.
Q.11Medium
What is the output of: int x = 5; System.out.println(++x + x++);
Answer: B
++x increments x to 6 and uses it, then x++ uses 6 and increments to 7. So 6 + 6 = 12.
Q.12Medium
Which of the following correctly represents a long literal in Java?
Answer: D
Both 'L' and 'l' suffixes are valid for long literals in Java, though 'L' is preferred as it's less confusing with '1'.
Q.13Medium
What is the result of: System.out.println(true && false || true);
Answer: A
&& has higher precedence than ||. So: (true && false) || true = false || true = true.
Q.14Medium
Which of the following statements will compile successfully?
Answer: D
Option A fails (300 exceeds byte range). Option B works with explicit casting. Option C works (char to int conversion is implicit).
Q.15Medium
What is the output of: System.out.println(10 * 52 * 2);
Answer: A
Operators *, / have same precedence and are evaluated left to right: ((10 * 2) / 5) * 2 = (520) * 2 = 4 * 2 = 8.
Q.16Medium
What is the output of: System.out.println("5" + 3 + 2);
Answer: B
String concatenation: "5" + 3 becomes "53", then "53" + 2 becomes "532". Once a String is involved, + performs concatenation.
Q.17Medium
Which of the following correctly initializes a 2D array in Java?
Answer: C
Both syntaxes are valid in Java for declaring 2D arrays. The brackets can be placed after the type or variable name.
Q.18Medium
What will be the output: int a = 10; int b = 20; System.out.println(a > b ? "A" : "B");
Answer: B
Ternary operator: condition ? trueValue : falseValue. Since 10 > 20 is false, "B" is printed.
Q.19Medium
What is the output of: double d = 5.7; int i = (int) d; System.out.println(i);
Answer: C
Explicit casting from double to int truncates the decimal part. 5.7 becomes 5 (not rounded).
Q.20Medium
Which statement about Java's 'this' keyword is correct?
Answer: B
'this' is a reference to the current object instance. It's used to distinguish instance variables from parameters and to call other constructors.