Showing 261–270 of 270 questions
What will be the output: String s = "Hello"; System.out.println(s.charAt(1));
EXPLANATION
charAt() uses 0-based indexing. Index 1 refers to the second character, which is 'e' in "Hello".
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 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 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.
What will be the output of: System.out.println(5 / 2);
EXPLANATION
Integer division in Java results in an integer. 5/2 = 2 (remainder is discarded). To get 2.5, at least one operand must be float/double.
What will be the output of: boolean flag = true; System.out.println(!flag);
A
true
B
false
C
!true
D
Compilation error
EXPLANATION
The logical NOT operator (!) inverts the boolean value. !true becomes false.
What will be the output of the following code? int x = 5; x += 3; System.out.println(x);
EXPLANATION
The += operator adds the right operand to the left operand. x += 3 is equivalent to x = x + 3, so 5 + 3 = 8.
Which keyword is used to create a constant variable in Java?
A
const
B
constant
C
final
D
immutable
EXPLANATION
The 'final' keyword is used to declare constants in Java. Once assigned, the value cannot be changed.
Which of the following is the correct syntax to declare a variable in Java?
A
int x = 10;
B
int x: 10;
C
x int = 10;
D
int: x = 10;
Correct Answer:
A. int x = 10;
EXPLANATION
Java uses the syntax 'datatype variableName = value;' for variable declaration.