Covers Java program structure and object-oriented concepts, data types and operators, the String, Random and Math classes, decision and loop statements, debugging, exceptions, and methods.
20 free sample questions288 in the full practice test
Working with Random and Math Classes · Use the Math class
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?
Answer and 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.
Q2Question 2
Beginner
Java Methods · Describe a static method and demonstrate its use within a program
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?
Answer and 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.
Q3Question 3
Intermediate
Working with Random and Math Classes · Use the Random class
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?
Answer and 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.
Q4Question 4
IntermediateMultiple answers
Java Methods · Create overloaded methods
A class DataProcessor is designed with multiple methods to handle different data types. Examine the following method signatures within the class:
public void process(int data)
public void process(int data, boolean flag)
public int process(double data)
public void process(int value)
Which two method signatures will cause a compilation error due to invalid method overloading? (Select TWO)
Answer and 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.
Q5Question 5
Beginner
Java Basics · Describe the components of a basic Java program
True or False: A single .java source file can contain multiple public class declarations.
Answer and 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.
Q6Question 6
Intermediate
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);
Answer and 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).
Q7Question 7
Beginner
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?
Answer and 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.
Q8Question 8
Beginner
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);
}
Answer and 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.
Q9Question 9
IntermediateMultiple answers
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)
Answer and explanation
Correct answers: A, C
Q10Question 10
Advanced
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?
Answer and 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 for 10 more questions
Or unlock all 288 1Z0-811 questions with explanations, timed mode and flashcards.