What will be the output: System.out.println('A' + 'B');
A AB B 131 C Error D 2
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++);
A 11 B 12 C 10 D 13
++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?
A long num = 1000000; B long num = 1000000L; C long num = 1000000l; D Both B and C
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);
A 5.5 B 5.50 C Error D 5
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);
A true B false C Error D null
&& has higher precedence than ||. So: (true && false) || true = false || true = true.
Which of the following statements will compile successfully?
A byte b = 300; B byte b = (byte) 300; C int i = 'a'; D Both B and C
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 * 5 2 * 2);
A 8 B 4 C 16 D 2
Operators *, / have same precedence and are evaluated left to right: ((10 * 2) / 5) * 2 = (5 20 ) * 2 = 4 * 2 = 8.
Which of the following is the correct syntax for a multi-line comment in Java?
A // This is a comment B /* This is a comment */ C <!-- This is a comment --> D # This is a comment
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));
A H B e C l D o
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);
A 10 B 532 C Error D 7
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?
A int[][] arr = new int[3][4]; B int[] arr[] = new int[3][4]; C Both A and B D Neither A nor B
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");
A A B B C Error D AB
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);
A 6 B 5.7 C 5 D Error
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?
A 'this' refers to the parent class B 'this' refers to the current object instance C 'this' is a static reference D 'this' cannot be used in constructors
'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));
A 2 B 5 C 7 D 15
Math.max(5, 10) = 10, Math.min(3, 8) = 3. So 10 - 3 = 7.
Which of the following is NOT a valid Java identifier?
A _variable123 B $myVar C 123variable D my_Var$
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?
A abstract B final C static D synchronized
The 'final' keyword prevents a method from being overridden by subclasses.
Which of the following statements about Java packages is correct?
A Package names must be in uppercase B Package names use dot notation for hierarchy C A single Java file can belong to multiple packages D Package statement must appear after import statements
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?
A Both check string content B == checks reference, .equals() checks content C == checks content, .equals() checks reference D They are identical in functionality
== compares object references (memory location), while .equals() compares the actual string content.
Which access modifier allows access within the same package and subclasses?
A public B private C protected D default
protected allows access within the same package and to subclasses even outside the package.