Showing 941–950 of 958 questions
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.
Consider: int x = 0; System.out.println(x == 0 && x != 1); What is the output?
A
true
B
false
C
Compilation error
D
1
EXPLANATION
Both conditions are true: x == 0 (true) and x != 1 (true). The && (AND) operator returns true when both operands are true.
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.
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];'
What is the scope of a local variable in Java?
A
Throughout the class
B
Throughout the method or block where it is declared
C
Throughout the package
D
Throughout the entire program
Correct Answer:
B. Throughout the method or block where it is declared
EXPLANATION
Local variables have scope limited to the method or code block in which they are declared. They cannot be accessed outside that block.
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.
What will be printed? System.out.println(10 + 20 + "Java");
A
30Java
B
10 + 20 + Java
C
1020Java
D
Compilation error
Correct Answer:
A. 30Java
EXPLANATION
String concatenation works left to right. 10 + 20 = 30 (integer addition), then 30 + "Java" = "30Java" (string concatenation).
Consider the code: int a = 5; int b = a++; System.out.println(a + " " + b); What is the output?
EXPLANATION
The post-increment operator (a++) returns the value before incrementing. So b = 5, then a becomes 6. Output: 6 5
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.
What is the output of: System.out.println(5 > 3 ? 'A' : 'B');
A
A
B
B
C
true
D
Compilation error
EXPLANATION
The ternary operator (?:) evaluates the condition (5 > 3, which is true) and returns the first value 'A'.