Showing 251–260 of 270 questions
Consider the code: int x = 5; x += 3; What is the value of x?
EXPLANATION
The compound assignment operator += means x = x + 3. So 5 + 3 = 8.
What will be the output of: System.out.println(10 / 3);
A
3.33
B
3
C
3.333333
D
Compilation Error
EXPLANATION
Both operands are integers, so integer division is performed. Result is 3 (not 3.33). To get decimal, at least one operand must be float/double.
What is the purpose of the 'instanceof' operator?
A
To create new instances
B
To check if an object is an instance of a class
C
To compare object values
D
To delete object instances
Correct Answer:
B. To check if an object is an instance of a class
EXPLANATION
The 'instanceof' operator is used to check if an object is an instance of a specific class or implements a specific interface.
What does the 'super' keyword do in Java?
A
Refers to the current object
B
Refers to the parent class
C
Creates a new instance
D
Marks a method as superior
Correct Answer:
B. Refers to the parent class
EXPLANATION
The 'super' keyword is used to refer to parent class members and invoke parent class constructors.
Which of the following is a valid variable declaration?
A
int 2num;
B
int num-2;
C
int num$2;
D
int num.2;
Correct Answer:
C. int num$2;
EXPLANATION
Variable names can contain letters, digits, underscores, and dollar signs, but cannot start with a digit or contain hyphens or dots.
What is the correct way to declare a constant in Java?
A
const int MAX = 100;
B
static int MAX = 100;
C
public static final int MAX = 100;
D
final static int MAX = 100;
Correct Answer:
C. public static final int MAX = 100;
EXPLANATION
'const' is not a Java keyword. The correct convention for constants is public static final. Option D also works but C follows the standard convention.
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
Correct Answer:
B. Package names use dot notation for hierarchy
EXPLANATION
Package names follow a hierarchical structure using dots (e.g., com.example.app). Package statement must be the first non-comment statement.
Which keyword is used to prevent method overriding in Java?
A
abstract
B
final
C
static
D
synchronized
EXPLANATION
The 'final' keyword prevents a method from being overridden by subclasses.
Which of the following is NOT a valid Java identifier?
A
_variable123
B
$myVar
C
123variable
D
my_Var$
Correct Answer:
C. 123variable
EXPLANATION
Java identifiers cannot start with a digit. Valid identifiers must start with a letter, underscore, or dollar sign.
What will be the output of: System.out.println(Math.max(5, 10) - Math.min(3, 8));
EXPLANATION
Math.max(5, 10) = 10, Math.min(3, 8) = 3. So 10 - 3 = 7.