1Z0-811 Java Foundations Associate Practice Questions
Prepare for 1Z0-811 with more than an answer.
- Exam fee
- $95 USD
- Level
- Foundations/Associate
- Valid for
- Does not expire
Domains covered on the exam 13
- What Is Java?5%
- Java Basics10%
- Basic Java Elements8%
- Working with Java Data Types10%
- Working with Java Operators10%
- Working with the String Class8%
- Working with the Random and Math Classes5%
- Using Decision Statements10%
- Using Looping Statements10%
- Debugging and Exception Handling8%
- Arrays and ArrayLists10%
- Classes and Constructors10%
- Java Methods8%
- 1
Which of the following are valid Java reserved words? (Select THREE)
Show answer details
Correct answer: 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. - 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:
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.
- 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
The
java.langpackage is automatically imported into every Java program.Show answer details
Correct answer: A
The
java.langpackage contains fundamental classes that are essential to the Java language, such asObject,String,System,Math, and the wrapper classes (Integer,Double, etc.). Because these classes are used so frequently, the Java compiler automatically imports the entirejava.langpackage into every source file, so you do not need to writeimport java.lang.*;yourself. - 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.
ArrayListis 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
Which of the following variable declarations will cause a compilation error?
Show answer details
Correct answer: D
This declaration attempts to assign a
doubleliteral (1_000_000.0) to anintvariable (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:_countis a valid identifier,99.99dis a valid double literal, and afinalvariable can be declared and initialized on separate lines (as long as it's only initialized once). - 7
A developer needs to convert a
Stringcontaining a number into itsintprimitive 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 thecatchblock?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 --> EndShow answer details
Correct answer: D
The
Integer.parseInt()method is used to convert aStringto anint. If theStringdoes not contain a parsable integer (e.g., it contains letters or is empty), this method throws aNumberFormatException. Therefore, thecatchblock must be designed to handle this specific unchecked exception. - 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 variablesprincipal,rate, andtimeare all of typedouble. Which statement accurately describes the use of theMathclass in this context?Show answer details
Correct answer: B
The
Mathclass in thejava.langpackage 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, likeMath.pow(), without needing to create an object (e.g.,new Math()). In fact, theMathclass has a private constructor, so it cannot be instantiated. - 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
statickeyword 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
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) tobound(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.
