Showing 951–958 of 958 questions
Which operator has the highest precedence in Java?
A
* / %
B
+ -
C
() []
D
== !=
EXPLANATION
Parentheses () and array access [] have the highest precedence in Java. Multiplication, division, and modulo come next, followed by addition and subtraction.
What is the output of: System.out.println(10 / 3);
A
3.33
B
3
C
3.0
D
3.333333
EXPLANATION
When both operands are integers, the division result is an integer. 10 / 3 = 3 (integer division, not floating-point).
Which of the following will compile without error? public static void main(String args[]) { int x = 10; final int y = 20; y = 30; }
A
Yes, it will compile
B
No, compilation error on y = 30
C
No, compilation error on final declaration
D
No, compilation error on main method
Correct Answer:
B. No, compilation error on y = 30
EXPLANATION
Once a final variable is assigned, it cannot be reassigned. The line 'y = 30;' will cause a compilation error.
What is the size of 'char' data type in Java?
A
1 byte
B
2 bytes
C
4 bytes
D
8 bytes
Correct Answer:
B. 2 bytes
EXPLANATION
The 'char' data type in Java is 2 bytes (16 bits) because it uses Unicode character encoding.
Which of the following is NOT a primitive data type in Java?
A
int
B
boolean
C
String
D
double
Correct Answer:
C. String
EXPLANATION
String is a non-primitive (reference) data type in Java. Primitive types include int, boolean, double, float, char, long, short, byte.
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.