1Z0-811 Sample Questions & Answers
Digs into a wide spread of topics tied at the top: data types, operators, decisions, looping, arrays and ArrayLists, and class construction, plus Java basics, debugging, exceptions, method design, and the Random and Math classes alongside String methods.
Launch the full 1Z0-811 simulator →Showing 10 of 20 free samples.
- Question 1Intermediate
Working with Java Data Types · Cast a value from one data type to another including automatic and manual promotion
What is the output of the following code snippet?
int x = 5; int y = 2; double z = x / y; System.out.println(z);Show answer & explanation
Correct answer: B
In Java, when an arithmetic operation is performed on two integers, the result is also an integer. This is known as integer division. The expression
x / y(which is5 / 2) evaluates to2, truncating the decimal part. Only after this integer result is calculated is it assigned to thedoublevariablez. The integer2is then implicitly cast to adouble, becoming2.0for the assignment. To get2.5, at least one of the operands would need to be adouble(e.g.,(double) x / y). - Question 2Beginner
Using Decision Statements · Use the switch statement
A developer is implementing a system that categorizes user levels. They need to write a method
getUserCategorythat takes an integerleveland returns aString. The logic is as follows:- If
levelis 1, return "Beginner". - If
levelis 2, return "Intermediate". - If
levelis 3, return "Advanced". - For any other level, return "Expert".
Which control flow statement is most suitable and readable for implementing this logic?
Show answer & explanation
Correct answer: C
A
switchstatement is ideal when comparing a single variable against multiple discrete constant values. The logic required fits this pattern perfectly. Using aswitchwithcaselabels for 1, 2, and 3, and adefaultcase for all other values, results in code that is cleaner, more readable, and often more efficient than a longif-else-ifchain. - If
- Question 3Beginner
Using Looping Statements · Develop code that uses break and continue statements
A program processes a list of task IDs stored in an
ArrayList. During processing, if a task ID of -1 is encountered, the program should immediately stop processing and exit the loop. Which code snippet correctly implements this requirement?ArrayList taskIds = new ArrayList (); // ... taskIds is populated ... for (int id : taskIds) { if (id == -1) { // What goes here? } System.out.println("Processing task: " + id); }Show answer & explanation
Correct answer: D
The
breakstatement is used to terminate the enclosing loop (in this case, theforloop) immediately. Whenidis -1, thebreakstatement will be executed, and control will transfer to the first statement after the loop. This fulfills the requirement to stop processing.continuewould only skip the current iteration, andreturnwould exit the entire method. - Question 4IntermediateSelect 2
Arrays and ArrayLists · Compare an array and an ArrayList
A new developer is working with arrays and
ArrayListsfor the first time. They need to understand the fundamental differences between the two. Which of the following statements are true regarding arrays andArrayListsin Java? (Select TWO)Show answer & explanation
Correct answers: A, C
- Question 5Advanced
Classes and Constructors · Create a new class including a main method
Case Study:
A small startup is developing a
UserProfileclass for its new social media application. The requirements state that every user profile must be created with a unique username and an email address, which cannot be changed after creation. A user's display name is also required at creation but can be modified later. Finally, a user's biography is optional and can be set or updated at any time.The lead developer has mandated the following design principles:
- All instance variables (fields) must be
privateto ensure encapsulation. - The class must provide constructors to enforce the creation rules.
- Methods should be provided to access and modify data where allowed.
Which class implementation best meets all the specified requirements?
Show answer & explanation
Correct answer: C
This design correctly implements all requirements. Using
private finalfor username and email ensures they are set only once via the constructor and cannot be changed, fulfilling the immutability requirement. Making all fieldsprivateenforces encapsulation. The single constructor taking all required fields ensures a valid object state upon creation. Providing getters for all fields allows data to be read, while providing setters (mutators) only fordisplayNameandbiographycorrectly implements the business logic that only these two fields can be modified after creation. - All instance variables (fields) must be
- Question 6Advanced
Debugging and Exception Handling · Use try and catch blocks
Examine the following code that processes user input:
public void processInput(String input) { try { int value = Integer.parseInt(input); System.out.println("Value: " + value); } catch (Exception ex) { System.out.println("General Error"); } catch (NumberFormatException nfe) { System.out.println("Invalid Number"); } }What is the problem with this exception handling code?
Show answer & explanation
Correct answer: C
In a
try-catchstructure with multiplecatchblocks, the blocks are checked in order.NumberFormatExceptionis a subclass ofException. Because the firstcatchblock catches the more generalExceptionclass, it will catch all exceptions, includingNumberFormatException. Consequently, the second, more specificcatchblock is unreachable, which the Java compiler flags as a compilation error. To fix this, the more specific exception (NumberFormatException) must be caught before the more general one (Exception).graph TD A[Exception] --> B[RuntimeException] B --> C[IllegalArgumentException] C --> D[NumberFormatException] style A fill:#f9f,stroke:#333,stroke-width:2px style D fill:#ccf,stroke:#333,stroke-width:2px - Question 7Advanced
Using Decision Statements · Compare how == differs between primitives and objects
What is the result of executing the following code snippet?
String s1 = "Java"; String s2 = new String("Java"); String s3 = "Java"; System.out.print((s1 == s2) + ", "); System.out.print((s1 == s3) + ", "); System.out.print(s1.equals(s2));Show answer & explanation
Correct answer: B
s1 == s2: This compares object references.s1refers to an object in the string constant pool.s2is explicitly created as a new object on the heap. Since they are different objects in memory, this isfalse.s1 == s3: Boths1ands3are assigned the same string literal. Java's string pool optimization ensures that they both point to the same object in memory. Therefore, this istrue.s1.equals(s2): Theequals()method compares the actual character sequences of the strings. Since both strings contain "Java", this istrue.
- Question 8Intermediate
Working with the String Class · Develop code that uses methods from the String class
A developer is writing a method to format a user's name. The method should take a first and last name and return a formatted string like "Last, First". However, if the last name is null or empty, it should just return the first name. Which of the following implementations correctly handles this logic?
Show answer & explanation
Correct answer: B
This implementation is the most robust. It first checks if
lastisnullto prevent aNullPointerException. The&&operator uses short-circuit evaluation, so!last.isEmpty()is only checked iflastis not null. This correctly handles both null and empty strings for the last name, returning the formatted string only when the last name is valid. - Question 9BeginnerSelect 3
Basic Java Elements · Use Java reserved words
Which of the following are valid Java reserved words? (Select THREE)
Show answer & explanation
Correct answers: A, D, E
default,transient, andgotoare all Java reserved words.defaultis used in switch statements and interfaces.transientis a modifier used in serialization.gotois a reserved word that is not used in the language.mainis a method name by convention, not a keyword.Stringis a class name, not a keyword. - Question 10Advanced
Working with Java Operators · Describe the operator precedence and use of parenthesis
Given the following code:
int a = 10; int b = 3; int c = 4; boolean result = a / b == 3 && c * b++ > 10; System.out.println(result + ", " + b);What is the output?
Show answer & explanation
Correct answer: D
Let's break down the evaluation:
a / bis10 / 3, which is3due to integer division.- The first part of the
&&is3 == 3, which istrue. - Because the first part is true, the second part
c * b++ > 10must be evaluated. c * b++uses the post-increment operator. The original value ofb(which is 3) is used in the multiplication:4 * 3equals12. After this evaluation,bis incremented to 4.- The comparison becomes
12 > 10, which istrue. - The final result is
true && true, which istrue. - The final value of
bis 4. So the output istrue, 4.
Ready for the real thing?
The full 1Z0-811 simulator has every exam-style question, timed mode, and instant scoring.