Showing 931–940 of 958 questions
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
D
# This is a comment
Correct Answer:
B. /* This is a comment */
EXPLANATION
Multi-line comments in Java use /* */ syntax. Single-line comments use //. Other syntaxes are for different languages.
What is the output of: System.out.println(10 * 2 / 5 * 2);
EXPLANATION
Operators *, / have same precedence and are evaluated left to right: ((10 * 2) / 5) * 2 = (20 / 5) * 2 = 4 * 2 = 8.
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
Correct Answer:
D. Both B and C
EXPLANATION
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 result of: System.out.println(true && false || true);
A
true
B
false
C
Error
D
null
EXPLANATION
&& has higher precedence than ||. So: (true && false) || true = false || true = true.
What will be printed? float f = 5.5f; System.out.println(f);
EXPLANATION
The 'f' suffix indicates a float literal. The output will be 5.5 (trailing zeros are not shown in standard printing).
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
Correct Answer:
D. Both B and C
EXPLANATION
Both 'L' and 'l' suffixes are valid for long literals in Java, though 'L' is preferred as it's less confusing with '1'.
What is the output of: int x = 5; System.out.println(++x + x++);
EXPLANATION
++x increments x to 6 and uses it, then x++ uses 6 and increments to 7. So 6 + 6 = 12.
What will be the output: System.out.println('A' + 'B');
EXPLANATION
Characters are treated as their ASCII values in arithmetic operations. 'A' (65) + 'B' (66) = 131.
Which of the following is a valid variable name in Java?
A
123abc
B
_myVar
C
my-Var
D
class
Correct Answer:
B. _myVar
EXPLANATION
Variable names must start with a letter, underscore (_), or dollar sign ($). They cannot start with digits, contain hyphens, or be reserved keywords.
Which keyword is used to prevent a class from being inherited?
A
static
B
abstract
C
final
D
private
EXPLANATION
The 'final' keyword prevents a class from being extended/inherited. A final class cannot have subclasses.