Showing 921–930 of 958 questions
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.
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
Correct Answer:
B. 'this' refers to the current object instance
EXPLANATION
'this' is a reference to the current object instance. It's used to distinguish instance variables from parameters and to call other constructors.
What is the output of: double d = 5.7; int i = (int) d; System.out.println(i);
EXPLANATION
Explicit casting from double to int truncates the decimal part. 5.7 becomes 5 (not rounded).
What will be the output: int a = 10; int b = 20; System.out.println(a > b ? "A" : "B");
EXPLANATION
Ternary operator: condition ? trueValue : falseValue. Since 10 > 20 is false, "B" is printed.
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
Correct Answer:
C. Both A and B
EXPLANATION
Both syntaxes are valid in Java for declaring 2D arrays. The brackets can be placed after the type or variable name.
What is the output of: System.out.println("5" + 3 + 2);
EXPLANATION
String concatenation: "5" + 3 becomes "53", then "53" + 2 becomes "532". Once a String is involved, + performs concatenation.
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".