What will be the output: System.out.println('A' + 'B');
Characters are treated as their ASCII values in arithmetic operations. 'A' (65) + 'B' (66) = 131.
What is the output of: int x = 5; System.out.println(++x + x++);
++x increments x to 6 and uses it, then x++ uses 6 and increments to 7. So 6 + 6 = 12.
Which of the following correctly represents a long literal in Java?
Both 'L' and 'l' suffixes are valid for long literals in Java, though 'L' is preferred as it's less confusing with '1'.
What will be printed? float f = 5.5f; System.out.println(f);
The 'f' suffix indicates a float literal. The output will be 5.5 (trailing zeros are not shown in standard printing).
What is the result of: System.out.println(true && false || true);
&& has higher precedence than ||. So: (true && false) || true = false || true = true.
Advertisement
Which of the following statements will compile successfully?
Option A fails (300 exceeds byte range). Option B works with explicit casting. Option C works (char to int conversion is implicit).
What is the output of: System.out.println(10 * 52 * 2);
Operators *, / have same precedence and are evaluated left to right: ((10 * 2) / 5) * 2 = (520) * 2 = 4 * 2 = 8.
Which of the following is the correct syntax for a multi-line comment in Java?
Multi-line comments in Java use /* */ syntax. Single-line comments use //. Other syntaxes are for different languages.
What will be the output: String s = "Hello"; System.out.println(s.charAt(1));
charAt() uses 0-based indexing. Index 1 refers to the second character, which is 'e' in "Hello".
What is the output of: System.out.println("5" + 3 + 2);
String concatenation: "5" + 3 becomes "53", then "53" + 2 becomes "532". Once a String is involved, + performs concatenation.
Which of the following correctly initializes a 2D array in Java?
Both syntaxes are valid in Java for declaring 2D arrays. The brackets can be placed after the type or variable name.
What will be the output: int a = 10; int b = 20; System.out.println(a > b ? "A" : "B");
Ternary operator: condition ? trueValue : falseValue. Since 10 > 20 is false, "B" is printed.
What is the output of: double d = 5.7; int i = (int) d; System.out.println(i);
Explicit casting from double to int truncates the decimal part. 5.7 becomes 5 (not rounded).
Which statement about Java's 'this' keyword is correct?
'this' is a reference to the current object instance. It's used to distinguish instance variables from parameters and to call other constructors.
What will be the output of: System.out.println(Math.max(5, 10) - Math.min(3, 8));
Math.max(5, 10) = 10, Math.min(3, 8) = 3. So 10 - 3 = 7.
Which of the following is NOT a valid Java identifier?
Java identifiers cannot start with a digit. Valid identifiers must start with a letter, underscore, or dollar sign.
Which keyword is used to prevent method overriding in Java?
The 'final' keyword prevents a method from being overridden by subclasses.
Which of the following statements about Java packages is correct?
Package names follow a hierarchical structure using dots (e.g., com.example.app). Package statement must be the first non-comment statement.
What is the difference between == and .equals() for String comparison?
== compares object references (memory location), while .equals() compares the actual string content.
Which access modifier allows access within the same package and subclasses?
protected allows access within the same package and to subclasses even outside the package.