Sample questions
Oracle Java Foundations 1Z0-811 practice questions
Arrange the steps to compile and run a Java program from the command line in the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Arrange the steps to handle an exception using try-catch-finally in Java in the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Arrange the steps to use the Scanner class to read user input in Java in the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Arrange the steps to create an object from a class in Java in the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Arrange the steps to use a for loop to iterate over an array in Java in the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Arrange the steps to overload a method in Java in the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
A developer writes: boolean b = !true && false; What is the value of b?
Trap 1: Compilation error: invalid operator.
Valid boolean expression.
Trap 2: NullPointerException
No objects involved.
Trap 3: true
Both operands are false.
- A
Compilation error: invalid operator.
Why wrong: Valid boolean expression.
- B
false
!true -> false; false && false -> false.
- C
NullPointerException
Why wrong: No objects involved.
- D
true
Why wrong: Both operands are false.
A developer writes: int x; System.out.println(x); What is the result?
Trap 1: null
Incorrect. null is for reference types.
Trap 2: Runtime error
Incorrect. The error is at compile time.
Trap 3: 0
Incorrect. Local variables do not get default values.
- A
null
Why wrong: Incorrect. null is for reference types.
- B
Compilation fails
Correct. The variable x is not initialized.
- C
Runtime error
Why wrong: Incorrect. The error is at compile time.
- D
0
Why wrong: Incorrect. Local variables do not get default values.
A junior developer wrote the following code to compare two strings entered by a user: if (username == "admin") { grantAccess(); } else { denyAccess(); }. The code always denies access even when the user enters 'admin'. What is the most likely cause, and how should the code be fixed?
Trap 1: The input string has leading or trailing spaces
Possible but less likely; not the most likely cause.
Trap 2: The variable username is null, causing a NullPointerException that…
NullPointerException would crash, not just deny.
Trap 3: The strings are compared using == which is case-sensitive
Case-sensitivity is not the issue.
- A
The input string has leading or trailing spaces. Use username.trim().equals("admin") instead.
Why wrong: Possible but less likely; not the most likely cause.
- B
The variable username is null, causing a NullPointerException that is caught and denied access. Add a null check.
Why wrong: NullPointerException would crash, not just deny.
- C
The strings are compared using == which is case-sensitive. Use username.equalsIgnoreCase("admin") instead.
Why wrong: Case-sensitivity is not the issue.
- D
The strings are compared using == which checks reference equality. Use username.equals("admin") instead.
equals compares content.
Given: int a = 10; int b = 20; boolean flag = a++ > 10 && ++b > 20; What are the values of a and b after execution?
Trap 1: a=10, b=21
Incorrect: a is incremented to 11, and b is not incremented.
Trap 2: a=10, b=20
Incorrect: a is incremented to 11 due to post-increment.
Trap 3: a=11, b=21
Incorrect: due to short-circuit, b is not incremented.
- A
a=10, b=21
Why wrong: Incorrect: a is incremented to 11, and b is not incremented.
- B
a=10, b=20
Why wrong: Incorrect: a is incremented to 11 due to post-increment.
- C
a=11, b=21
Why wrong: Incorrect: due to short-circuit, b is not incremented.
- D
a=11, b=20
Correct: a becomes 11, b remains 20 because the right side of && is not evaluated.
What is the value of the expression (10 > 5) && (3 < 2)?
Trap 1: 1
Boolean expression returns boolean.
Trap 2: true
Both conditions must be true.
Trap 3: 0
Boolean expression returns boolean.
- A
1
Why wrong: Boolean expression returns boolean.
- B
true
Why wrong: Both conditions must be true.
- C
false
The second condition is false.
- D
0
Why wrong: Boolean expression returns boolean.
Which primitive data type should be used to store a single character?
Trap 1: int
int is for integers.
Trap 2: byte
byte is for small integers.
Trap 3: String
String is a class, not primitive.
- A
int
Why wrong: int is for integers.
- B
byte
Why wrong: byte is for small integers.
- C
char
char is a 16-bit Unicode character.
- D
String
Why wrong: String is a class, not primitive.
A developer writes: for(int i=0; i<10; i++) { if(i%2==0) continue; System.out.print(i); }. What is the output?
Trap 1: 0123456789
Ignores continue.
Trap 2: 02468
Prints even numbers, but continue skips even numbers.
Trap 3: 123456789
Includes 0 which is even.
- A
0123456789
Why wrong: Ignores continue.
- B
13579
Correctly prints odd numbers.
- C
02468
Why wrong: Prints even numbers, but continue skips even numbers.
- D
123456789
Why wrong: Includes 0 which is even.
What is the result of: System.out.println(10 + 20 + "30");
Trap 1: 102030
Incorrect.
Trap 2: 30
Incorrect.
Trap 3: Compilation fails
Incorrect.
- A
102030
Why wrong: Incorrect.
- B
3030
Correct because addition then concatenation.
- C
30
Why wrong: Incorrect.
- D
Compilation fails
Why wrong: Incorrect.
What is the output of the following code? int[] a = {1,2,3}; int[] b = a; b[0] = 99; System.out.println(a[0]);
Trap 1: 0
Default value not applicable.
Trap 2: 1
The array is shared, so a[0] becomes 99.
Trap 3: Compilation error
Code compiles fine.
- A
0
Why wrong: Default value not applicable.
- B
1
Why wrong: The array is shared, so a[0] becomes 99.
- C
99
a and b refer to same array.
- D
Compilation error
Why wrong: Code compiles fine.
Which TWO statements are true about interfaces in Java?
Trap 1: Interfaces can be instantiated.
Interfaces cannot be instantiated.
Trap 2: Interfaces can contain constructors.
Interfaces cannot have constructors.
Trap 3: Interfaces cannot have default methods.
Default methods are allowed since Java 8.
- A
Interfaces can contain private methods.
Since Java 9, interfaces can have private methods for code reuse.
- B
Interfaces can be instantiated.
Why wrong: Interfaces cannot be instantiated.
- C
Interfaces can contain constructors.
Why wrong: Interfaces cannot have constructors.
- D
All interface variables are implicitly public static final.
Interface fields are constants.
- E
Interfaces cannot have default methods.
Why wrong: Default methods are allowed since Java 8.
Given: abstract class Shape { abstract void draw(); } class Circle extends Shape { void draw() {} } Which is true?
Trap 1: Shape cannot have a constructor
Abstract classes can have constructors, invoked via super().
Trap 2: Shape s = new Shape(); is valid
Cannot instantiate abstract classes.
Trap 3: draw() must be public in Circle
The overriding method can have the same or broader access; not necessarily public.
- A
Shape cannot have a constructor
Why wrong: Abstract classes can have constructors, invoked via super().
- B
Circle must override draw()
Circle provides implementation; it compiles fine.
- C
Shape s = new Shape(); is valid
Why wrong: Cannot instantiate abstract classes.
- D
draw() must be public in Circle
Why wrong: The overriding method can have the same or broader access; not necessarily public.
A method throws a checked exception. Which of the following is the correct way to handle it in the calling method?
Trap 1: Ignore the exception since it is checked.
Checked exceptions must be handled or declared.
Trap 2: Add a throws clause to the calling method.
This does not handle the exception; it propagates it.
Trap 3: Use a finally block without catch.
Finally does not handle exceptions.
- A
Ignore the exception since it is checked.
Why wrong: Checked exceptions must be handled or declared.
- B
Add a throws clause to the calling method.
Why wrong: This does not handle the exception; it propagates it.
- C
Enclose the call in a try-catch block.
Catching the exception handles it.
- D
Use a finally block without catch.
Why wrong: Finally does not handle exceptions.
Given: int a = 9; int b = 2; double c = a / b; System.out.println(c); What is the output?
Trap 1: 4.5
Would be true if division was double / int, but both operands are int.
Trap 2: 4.0
Duplicate of B, but only one correct answer.
Trap 3: 4
This is the integer result, but the variable is double, so output includes decimal.
- A
4.5
Why wrong: Would be true if division was double / int, but both operands are int.
- B
4.0
Correct. Integer division gives 4, then cast to double implicitly.
- C
4.0
Why wrong: Duplicate of B, but only one correct answer.
- D
4
Why wrong: This is the integer result, but the variable is double, so output includes decimal.
Which of the following correctly uses the ternary operator to set int max to the larger of two ints x and y?
Trap 1: int max = x > y ? y : x;
This returns the smaller.
Trap 2: int max = if (x > y) x else y;
Not valid Java syntax.
Trap 3: int max = (x > y) ? x, y;
Comma not allowed.
- A
int max = x > y ? x : y;
Correct syntax.
- B
int max = x > y ? y : x;
Why wrong: This returns the smaller.
- C
int max = if (x > y) x else y;
Why wrong: Not valid Java syntax.
- D
int max = (x > y) ? x, y;
Why wrong: Comma not allowed.
Match each access modifier to its visibility level.
Drag a concept onto its matching description — or click a concept then click the description.
Accessible from anywhere
Accessible within same package and subclasses
Accessible only within same package
Accessible only within same class
Match each Java operator to its description.
Drag a concept onto its matching description — or click a concept then click the description.
Increment by 1
Modulo (remainder) operator
Logical AND (short-circuit)
Checks if an object is of a certain type
Ternary conditional operator
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.