Showing 461–470 of 476 questions
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 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.
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 the correct way to declare and initialize an array in Java?
A
int array[5] = {1, 2, 3, 4, 5};
B
int[] array = {1, 2, 3, 4, 5};
C
int array = [1, 2, 3, 4, 5];
D
array[5] int = {1, 2, 3, 4, 5};
Correct Answer:
B. int[] array = {1, 2, 3, 4, 5};
EXPLANATION
The correct syntax is 'datatype[] arrayName = {elements};' or 'datatype[] arrayName = new datatype[size];'
Identify the output: System.out.println("Java".length());
A
4
B
5
C
Compilation error
D
null
EXPLANATION
The length() method returns the number of characters in the string "Java", which is 4 characters.
Which statement is correct about Java variables?
A
Variable names are case-sensitive
B
Variable names can start with a digit
C
Variable names can contain spaces
D
Variable names can use special characters like @, #
Correct Answer:
A. Variable names are case-sensitive
EXPLANATION
Java variable names are case-sensitive. 'age' and 'Age' are different variables. Names cannot start with digits, cannot contain spaces, and special characters are not allowed.