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

    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.

  2. 2

    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.

  3. 3

    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.

  4. 4

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

  5. 5

    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.

  6. 6

    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.

  7. 7

    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.

  8. 8

    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.

  9. 9

    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 details

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

  10. 10

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

    Show answer details

    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.

Create an account to continue.