Java Foundations Associate Free Sample Questions

Create a free account to browse all 20 sample questions. The full practice test includes 288 questions. Use the simulator for timed and flashcard mode.

Try Simulator

1Z0-811 Sample Questions

  1. Question 1

    Q1

    A financial application calculates compound interest. During a code review, the following line is flagged: double total = principal * Math.pow(1 + rate, time);. The variables principal, rate, and time are all of type double. Which statement accurately describes the use of the Math class in this context?

    Show answer & explanation

    Correct answer: B

    The Math class in the java.lang package consists of static methods and constants. Static members belong to the class itself, not to any specific instance. Therefore, they are called using the class name, like Math.pow(), without needing to create an object (e.g., new Math()). In fact, the Math class has a private constructor, so it cannot be instantiated.

  2. Question 2

    Q2

    A developer needs to create a utility method that calculates the area of a circle. The method should be accessible without creating an instance of the class it resides in. The method signature is intended to be: public ___ double calculateArea(double radius). Which keyword must be placed in the blank to meet this requirement?

    Show answer & explanation

    Correct answer: C

    The static keyword declares a method that belongs to the class itself rather than to a specific instance of the class. This allows the method to be called using the class name (e.g., UtilityClass.calculateArea(5.0)) without needing to create an object first. This is the standard approach for creating utility methods.

  3. Question 3

    Q3

    A developer is writing a program to simulate a dice roll, which should produce a random integer between 1 and 6 (inclusive). They have written the following code snippet:

    import java.util.Random;
    
    // ... inside a method
    Random rand = new Random();
    int result = // ... code to generate number ...
    

    Which line of code correctly generates the required random number?

    Show answer & explanation

    Correct answer: C

    The method rand.nextInt(bound) generates a random integer from 0 (inclusive) to bound (exclusive). To get a number from 1 to 6, we need a range of 6 possible numbers (1, 2, 3, 4, 5, 6). rand.nextInt(6) generates a number from 0 to 5. Adding 1 to this result shifts the range to 1 to 6, which correctly simulates a standard six-sided die roll.

  4. Question 4

    Q4Multiple answers

    A class DataProcessor is designed with multiple methods to handle different data types. Examine the following method signatures within the class:

    1. public void process(int data)
    2. public void process(int data, boolean flag)
    3. public int process(double data)
    4. public void process(int value)

    Which two method signatures will cause a compilation error due to invalid method overloading? (Select TWO)

    Show answer & explanation

    Correct answers: A, D

    Method overloading requires that each method with the same name has a unique parameter list (either the number of parameters or the types of parameters). The method signature is defined by the method name and its parameter types. The return type and parameter names are not part of the signature. Methods 1 (process(int data)) and 4 (process(int value)) have the exact same signature: process(int). This duplicate signature will cause a compilation error. Methods 2 and 3 have unique parameter lists and are valid overloads.

  5. Question 5

    Q5

    True or False: A single .java source file can contain multiple public class declarations.

    Show answer & explanation

    Correct answer: B

    A single Java source file (.java) can have at most one public class. If a public class exists, the filename must match the class name exactly (e.g., class MyClass must be in MyClass.java). A file can contain multiple non-public (package-private) classes, but only one can be declared public.

  6. Question 6

    Q6

    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).

  7. Question 7

    Q7

    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.

  8. Question 8

    Q8

    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.

  9. Question 9

    Q9Multiple answers

    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

  10. Question 10

    Q10

    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.

Register free to unlock 10 more sample questions

Create a free account to continue with the rest of the 1Z0-811 sample set.

Lifetime One

Own this practice test forever.

$79.99
$75.99
one-time
  • Full access to 288 questions
  • Study, Timed & Flashcard Modes
  • All past and future versions i
  • Detailed Explanations
  • Study Tracking & Past Attempts
  • Brainy AI Assistant
  • Lifetime updates

Two

Any 2 exams per month.

$20.00/exam
$39.99
/month
  • 2 active exam slots
  • Study, Timed & Flashcard Modes
  • All past and future versions i
  • Detailed Explanations
  • Study Tracking & Past Attempts
  • 1,000 Brainy AI Credits
  • Cancel anytime

Premium Twelve

Any 12 exams over 3 months.

$15.00/exam
$179.99
/3 months
  • 4 active exam slots
  • Study, Timed & Flashcard Modes
  • All past and future versions i
  • Detailed Explanations
  • Study Tracking & Past Attempts
  • 15,000 Brainy AI Credits
  • Dedicated support
  • Friend seat included — full access

Trusted by professionals at

NvidiaSupabaseGitHubOpenAITursoClerkClaude AIAmazon