1Z0-811 Java Language Fundamentals 2 — Questions and Answers
Question 1: What is the result of compiling and running the following code? int x = 5; System.out.println(x++ + ++x);
- 10
- 11
- 12 (Correct answer)
- 13
Correct answer: 12
x++ returns 5 (then x becomes 6), ++x increments to 7 and returns 7, so 5+7=12.
Question 2: Which of the following is a valid declaration of a multi-dimensional array in Java?
- int[] arr = new int[3][3];
- int[][] arr = new int[3][3]; (Correct answer)
- int arr[][] = new int[3,3];
- int[3][3] arr;
Correct answer: int[][] arr = new int[3][3];
int[][] arr = new int[3][3]; is the standard Java syntax for a 2D array declaration and initialization.
Question 3: Which keyword is used to prevent a variable from being reassigned after initialization?
- static
- const
- final (Correct answer)
- immutable
Correct answer: final
The 'final' keyword prevents reassignment of a variable once it has been initialized.
Question 4: What is the default value of an instance variable of type boolean in Java?
- true
- false (Correct answer)
- null
- 0
Correct answer: false
Instance variables of type boolean are automatically initialized to false if not explicitly set.
Question 5: Which of the following correctly describes operator precedence between * and +?
- + has higher precedence than *
- * has higher precedence than + (Correct answer)
- They have equal precedence
- Precedence depends on the operand types
Correct answer: * has higher precedence than +
Multiplication (*) has higher precedence than addition (+), so 2+3*4 evaluates to 14, not 20.
Question 6: What does the following code print? String s = "Hello"; s.concat(" World"); System.out.println(s);
- Hello World
- Hello (Correct answer)
- World
- Compilation error
Correct answer: Hello
Strings are immutable in Java; concat() returns a new String but the original reference 's' is unchanged.
Question 7: Which of the following is NOT a valid identifier in Java?
- _myVar
- $price
- 2ndItem (Correct answer)
- myVar2
Correct answer: 2ndItem
Java identifiers cannot begin with a digit; '2ndItem' is invalid because it starts with '2'.
What is the result of compiling and running the following code?
int x = 5;
System.out.println(x++ + ++x);