Skip to content

1Z0-811 Java Foundations Associate Practice Questions

Prepare for 1Z0-811 with more than an answer.

288 questions in the full set20 sample questionsUpdated Jan 23, 2026
Exam fee
$95 USD
Level
Foundations/Associate
Valid for
Does not expire
Domains covered on the exam 13
  1. What Is Java?5%
  2. Java Basics10%
  3. Basic Java Elements8%
  4. Working with Java Data Types10%
  5. Working with Java Operators10%
  6. Working with the String Class8%
  7. Working with the Random and Math Classes5%
  8. Using Decision Statements10%
  9. Using Looping Statements10%
  10. Debugging and Exception Handling8%
  11. Arrays and ArrayLists10%
  12. Classes and Constructors10%
  13. Java Methods8%
  1. 1

    Which of the following are valid Java reserved words? (Select THREE)

    Show answer details

    Correct answer: 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.

  2. 2

    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 details

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

    What is the primary role of the Java Virtual Machine (JVM)?

    Show answer details

    Correct answer: C

    The JVM is the core component of the Java Runtime Environment (JRE). Its primary function is to load, verify, and execute Java bytecode. It acts as an abstract computing machine that enables a Java program to run on any platform that has a JVM, thus providing platform independence ("Write Once, Run Anywhere"). The compiler (javac) handles the compilation from source code to bytecode.

  4. 4

    The java.lang package is automatically imported into every Java program.

    Show answer details

    Correct answer: A

    The java.lang package contains fundamental classes that are essential to the Java language, such as Object, String, System, Math, and the wrapper classes (Integer, Double, etc.). Because these classes are used so frequently, the Java compiler automatically imports the entire java.lang package into every source file, so you do not need to write import java.lang.*; yourself.

  5. 5

    A developer needs to store a list of student grades. The number of students is not known in advance and can change during runtime. The developer must be able to add new grades, remove grades, and check how many grades have been entered. Which data structure is the most appropriate choice for this scenario?

    Show answer details

    Correct answer: B

    The key requirement is that the number of elements is unknown and can change. ArrayList is a dynamic, resizable collection class from the Java Collections Framework. It allows elements to be added and removed easily, and its size can grow or shrink as needed. A standard array has a fixed size that must be declared at creation, making it unsuitable for this scenario.

  6. 6

    Which of the following variable declarations will cause a compilation error?

    Show answer details

    Correct answer: D

    This declaration attempts to assign a double literal (1_000_000.0) to an int variable (value). This is a narrowing conversion that would result in a loss of precision, so the Java compiler flags it as an error. Underscores are legal in numeric literals for readability, but the type mismatch is the issue. The other options are valid: _count is a valid identifier, 99.99d is a valid double literal, and a final variable can be declared and initialized on separate lines (as long as it's only initialized once).

  7. 7

    A developer needs to convert a String containing a number into its int primitive equivalent. If the string is not a valid number, the program should catch the error and assign a default value of -1. Given the following logic flow, which exception should be handled in the catch block?

    flowchart TD Start([Start]) --> Input{Read String input} Input --> Process{Attempt to parse String to int} Process -->|Success| Assign[Assign parsed int] Process -->|Failure| Handle[Handle Error] Handle --> AssignDefault[Assign default value -1] Assign --> End([End]) AssignDefault --> End

    Show answer details

    Correct answer: D

    The Integer.parseInt() method is used to convert a String to an int. If the String does not contain a parsable integer (e.g., it contains letters or is empty), this method throws a NumberFormatException. Therefore, the catch block must be designed to handle this specific unchecked exception.

  8. 8

    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 details

    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.

  9. 9

    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 details

    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.

  10. 10

    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 details

    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.

Create an account to continue.