1Z0-811 Sample Questions

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.

  1. 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 is 5 / 2) evaluates to 2, truncating the decimal part. Only after this integer result is calculated is it assigned to the double variable z. The integer 2 is then implicitly cast to a double, becoming 2.0 for the assignment. To get 2.5, at least one of the operands would need to be a double (e.g., (double) x / y).

  2. 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 getUserCategory that takes an integer level and returns a String. The logic is as follows:

    • If level is 1, return "Beginner".
    • If level is 2, return "Intermediate".
    • If level is 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 switch statement is ideal when comparing a single variable against multiple discrete constant values. The logic required fits this pattern perfectly. Using a switch with case labels for 1, 2, and 3, and a default case for all other values, results in code that is cleaner, more readable, and often more efficient than a long if-else-if chain.

  3. 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 break statement is used to terminate the enclosing loop (in this case, the for loop) immediately. When id is -1, the break statement will be executed, and control will transfer to the first statement after the loop. This fulfills the requirement to stop processing. continue would only skip the current iteration, and return would exit the entire method.

  4. Question 4IntermediateSelect 2

    Arrays and ArrayLists · Compare an array and an ArrayList

    A new developer is working with arrays and ArrayLists for the first time. They need to understand the fundamental differences between the two. Which of the following statements are true regarding arrays and ArrayLists in Java? (Select TWO)

    Show answer & explanation

    Correct answers: A, C

  5. Question 5Advanced

    Classes and Constructors · Create a new class including a main method

    Case Study:

    A small startup is developing a UserProfile class 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 private to 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 final for username and email ensures they are set only once via the constructor and cannot be changed, fulfilling the immutability requirement. Making all fields private enforces 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 for displayName and biography correctly implements the business logic that only these two fields can be modified after creation.

  6. 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-catch structure with multiple catch blocks, the blocks are checked in order. NumberFormatException is a subclass of Exception. Because the first catch block catches the more general Exception class, it will catch all exceptions, including NumberFormatException. Consequently, the second, more specific catch block 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

  7. 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. s1 refers to an object in the string constant pool. s2 is explicitly created as a new object on the heap. Since they are different objects in memory, this is false.
    • s1 == s3: Both s1 and s3 are assigned the same string literal. Java's string pool optimization ensures that they both point to the same object in memory. Therefore, this is true.
    • s1.equals(s2): The equals() method compares the actual character sequences of the strings. Since both strings contain "Java", this is true.
  8. 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 last is null to prevent a NullPointerException. The && operator uses short-circuit evaluation, so !last.isEmpty() is only checked if last is 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.

  9. 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, and goto are all Java reserved words. default is used in switch statements and interfaces. transient is a modifier used in serialization. goto is a reserved word that is not used in the language. main is a method name by convention, not a keyword. String is a class name, not a keyword.

  10. 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:

    1. a / b is 10 / 3, which is 3 due to integer division.
    2. The first part of the && is 3 == 3, which is true.
    3. Because the first part is true, the second part c * b++ > 10 must be evaluated.
    4. c * b++ uses the post-increment operator. The original value of b (which is 3) is used in the multiplication: 4 * 3 equals 12. After this evaluation, b is incremented to 4.
    5. The comparison becomes 12 > 10, which is true.
    6. The final result is true && true, which is true.
    7. The final value of b is 4. So the output is true, 4.

Ready for the real thing?

The full 1Z0-811 simulator has every exam-style question, timed mode, and instant scoring.