Practice 1Z0-811 Java Basics and Syntax questions with full explanations on every answer.
Start practicing
Java Basics and Syntax — choose a session length
Free · No account required
Click any question to see the full explanation and answer options, or start a focused practice session above.
A developer writes the following code: int x = 5; System.out.println(x++); What is the output?
2A team decides to use a single Java source file for a small application. Which statement is true about the file structure?
3Given the code: String s1 = "Java"; String s2 = new String("Java"); if (s1 == s2) { System.out.print("Equal"); } else { System.out.print("Not Equal"); } What is the output?
4Which primitive data type should be used to store a single character?
5A method is declared as: public static void main(String[] args) { }. Which statement is true?
6Which code snippet correctly creates a two-dimensional array with 3 rows and 4 columns?
7What is the value of the expression (10 > 5) && (3 < 2)?
8Which loop construct guarantees that the body executes at least once?
9Given the code: int[] arr = {1,2,3}; for(int x : arr) { if(x==2) continue; System.out.print(x); } What is the output?
10Which access modifier makes a member visible only within its own class?
11What is the result of the expression 10 % 3?
12Which TWO are valid identifiers in Java? (Choose two.)
13Which THREE are valid ways to declare and initialize an integer variable in Java? (Choose three.)
14Which TWO keywords are used for decision-making in Java? (Choose two.)
15Which THREE statements about the main method are correct? (Choose three.)
16What is the output of this program?
17What is the value printed?
18What is the output?
19You are developing a Java application for a library management system. The system must track the number of books in each genre. You need to store genre names (String) and their counts (int). The data will be accessed frequently and modified rarely. Which Java data structure should you use to store this mapping efficiently, while ensuring that genre names are unique?
20You are writing a program that processes a list of transactions. Each transaction has a timestamp (long), amount (double), and type (String). The program needs to iterate through the transactions in the order they were added. Which collection should you use to maintain insertion order and allow fast iteration?
21Arrange the steps to declare and initialize a one-dimensional array in Java in the correct order.
22Arrange the steps to use the Scanner class to read user input in Java in the correct order.
23Match each access modifier to its visibility level.
24Match each Java collection interface to its characteristics.
25A developer writes code to calculate the average of two integers: int a = 5; int b = 10; int avg = a / b;. Which change ensures the average is correctly calculated as a double?
26A class defines a static variable initialized at declaration: static int count = 10;. A static method attempts to modify it: count = 20;. Which statement is true?
27A developer uses a switch statement with a String variable. Which is true about this usage?
28Which loop construct guarantees that the loop body executes at least once?
29A method receives an int parameter and modifies its value inside the method. Does this change affect the caller's argument?
30Given int[] arr = {1,2,3}; which correctly creates a new array with length 5 and copies the contents of arr?
31A class defines two methods with the same name but different parameter lists. This is known as:
32Which access modifier allows members to be accessed only by classes in the same package?
33A subclass overrides a method from its superclass. Which annotation should be used to indicate the overriding intention?
34Which TWO are valid Java identifiers? (Choose two.)
35Which TWO statements about constructors are true? (Choose two.)
36Which THREE are primitive data types in Java? (Choose three.)
37Refer to the exhibit. What is the output?
38Refer to the exhibit. What is the result of attempting to compile and run the code?
39Refer to the exhibit. What is the output?
40A developer writes: int x; System.out.println(x); What is the result?
41Given: String s = "Java"; s.concat(" Rocks"); System.out.println(s); What prints?
42What is the result of: int[] arr = new int[5]; System.out.println(arr[5]);
43Given: int day = 3; switch(day) { case 1: System.out.print("A"); case 2: System.out.print("B"); case 3: System.out.print("C"); case 4: System.out.print("D"); } What prints?
44Given methods: void print(Integer i) { System.out.println("Integer"); } void print(int i) { System.out.println("int"); } What is output of print(10);?
45Which assignment requires an explicit cast to compile?
46Consider: for(int i=0;i<10;i++) { int x = i; } System.out.println(x); What is the result?
47Given method: static void change(String s) { s = "new"; } What is output of: String name = "original"; change(name); System.out.println(name);
48What is the result of: Integer a = null; int b = (a != null) ? a : 0; System.out.println(b);
49Which TWO of the following are valid Java identifiers?
50Which TWO of the following are legal ways to declare and initialize an array?
51Which THREE of the following are primitive data types in Java?
52Refer to the exhibit. What is the likely cause of this error?
53Refer to the exhibit. What happens when you compile this code?
54Refer to the exhibit. What is the output?
55A developer needs to declare a constant for the value of PI (3.14159) in a class. Which declaration follows Java best practices?
56A method is expected to receive an integer and return its square. Which method signature is correct?
57A class has a static variable counter initialized to 0. Two threads increment counter 1000 times each using counter++. What is a possible final value of counter?
58Which is the correct way to declare an array of integers in Java?
59A developer writes: if (x = 5) { System.out.println("x is 5"); } What is the result?
60Which TWO statements are true about the main method?
61Which THREE of the following are primitive data types in Java?
62Which TWO are valid ways to create a String object?
63Refer to the exhibit. What is the output?
64Refer to the exhibit. What is the most likely cause?
65Refer to the exhibit. What is the output when running the main method?
66A developer writes the following code to compare two strings: String s1 = "Java"; String s2 = new String("Java"); if (s1 == s2) { System.out.println("Equal"); } else { System.out.println("Not equal"); } What is the output?
67A package named com.example.util is declared in a file. Where should the file be placed in the directory structure?
68A developer uses the following array initialization: int[] nums = new int[]{1, 2, 3}; Which of the following is true?
69A method is declared as: public void setAge(int age) { this.age = age; } Which statement is correct about this code assuming the class has an instance variable age?
70A developer writes the following code: int x = 5; int y = x++; What are the values of x and y after execution?
71A company requires a method that accepts an integer and returns true if the integer is even, otherwise false. Which implementation best follows Java conventions?
72Given the loop: for (int i=0; i<5; i++) { if (i==2) continue; System.out.print(i); } What is the output?
73Which data type should be used to store a single character like 'A'?
74What is the result of the following code? String s1 = "Hello"; String s2 = " World"; String s3 = s1 + s2; System.out.println(s3);
75Given: int[] arr = {10,20,30}; System.out.println(arr[3]); What is the result?
76Which method overloading is valid?
77What is the scope of a variable declared inside a for loop?
78Which statement about try-catch is true?
79Which TWO keywords are used to control access to class members? (Choose two.)
80Which THREE are primitive data types in Java? (Choose three.)
81Which THREE are valid Java identifiers? (Choose three.)
82The code does not compile. What is the error?
83What likely caused this compilation error?
84A developer is working on a Java application that processes user input. The application reads an integer from the command line using args[0], converts it to an int, and uses it in a loop. When testing, the application throws a NumberFormatException when the user provides an alphabetic string. The developer needs to handle this exception gracefully by prompting the user to enter a valid number and retrying. However, the developer must avoid infinite loops. The current code uses a while loop with a flag. Which approach ensures the code handles the exception, provides feedback, and terminates if the user enters 'quit'? The environment is a standard Java SE 11 application. The developer wants a robust solution without using external libraries.
85A developer declares an integer variable inside a method but does not assign a value. What is the result of attempting to print the variable?
86Given two String objects s1 = "Hello" and s2 = "Hello", what does the expression (s1 == s2) return?
87A developer writes: int x = 5; int y = x++ + ++x; What is the value of y after execution?
88A developer wants to iterate over an array of integers named 'numbers'. Which loop declaration will correctly access each element?
89A class has two methods: void setValue(int a) and void setValue(double a). Which call will result in a compilation error?
90A parent class has a static method display() and an instance method show(). A child class attempts to override both. What is the outcome?
91Which TWO are valid ways to declare and initialize a two-dimensional int array in Java?
92Which THREE are valid Java identifiers?
93Which THREE statements about the final keyword in Java are true?
94A financial trading application processes high-volume transactions. The system uses a multithreaded architecture where multiple threads update a shared Account object's balance field. Recently, intermittently incorrect balance calculations have been reported. Developers suspect a race condition on the balance field. The Account class is defined as follows: public class Account { private double balance = 0.0; public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } public double getBalance() { return balance; } } Threads are created using ExecutorService with a fixed thread pool. The issue occurs only under heavy load. Which course of action should the development team take to resolve the issue while maintaining performance?
95A junior developer wrote the following code to calculate the sum of an array: int[] numbers = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 1; i <= numbers.length; i++) { sum += numbers[i]; } System.out.println("Sum: " + sum); The developer expects the output to be 15, but the program throws an exception. What is the root cause and the correct fix?
96A team is developing a library management system. They have a base class 'Item' with a method 'getTitle()' that returns the title. They also have a subclass 'Book' that overrides 'getTitle()'. In some places, they have a method that accepts an Item reference but actually receives a Book object. They want to call a method specific to Book, such as 'getISBN()', that is not in Item. They attempt to cast the parameter: ((Book) item).getISBN(). However, occasionally the program crashes with ClassCastException. What is the best practice to avoid this exception?
97A web application uses a HashMap to cache user session data. The keys are String objects representing session IDs, and values are Session objects. After some time, the cache memory usage grows excessively, causing OutOfMemoryError. The cache is never cleared. The team decides to implement a cache eviction policy. Which approach is best suited for this scenario in terms of Java standard library?
98A mobile app backend uses Java streams to process user data. The code snippet filters users who are active and older than 18, then collects them into a list: List<User> result = users.stream() .filter(u -> u.isActive()) .filter(u -> u.getAge() > 18) .collect(Collectors.toList()); Performance metrics show that this stream operation is slow when the user list has millions of entries. The team wants to improve performance without changing the business logic. Which change would most likely improve performance?
99A developer is writing a program to find the maximum value in an array of integers. The code is: int[] nums = {10, 20, 5, 30, 15}; int max = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; } } System.out.println(max); The output is 30, which is correct for this array. However, the developer is concerned that if all numbers are negative, the output would be 0 instead of the highest negative number. Which modification ensures the algorithm works correctly for all integer arrays?
100You are tasked with debugging a Java application that processes temperature sensor readings. The application reads an array of double values representing temperatures in Celsius, and should output the number of readings that exceed a threshold of 30.0°C. The current implementation is: public class TemperatureAnalyzer { public static void main(String[] args) { double[] readings = {25.5, 32.1, 30.0, 28.9, 35.7}; int count = 0; for (int i = 0; i < readings.length; i++) { if (readings[i] > 30.0) { count++; } } System.out.println("Count: " + count); } } However, the output is "Count: 3" instead of the expected 2 (since 30.0 is not above the threshold). The developer believes the issue is that the condition should be '>=' to include 30.0, but the specification says strictly above 30.0. After further investigation, you discover that the problem is due to floating-point imprecision when comparing with the literal 30.0. Which of the following changes would correctly fix the comparison to reliably count readings strictly greater than 30.0, considering floating-point representation?
101Which TWO of the following are valid Java identifiers? (Choose two.)
102What is the result of compiling and running this code?
103A developer is building a Java application for a bank that processes account transactions. The application reads transaction amounts from a CSV file provided by a third party. The amounts are in string format, e.g., '1,234.56' or '$5,000.00'. The developer uses Integer.parseInt() to convert these strings to integers for processing. However, many transactions fail with NumberFormatException. The developer adds a try-catch block to catch the exception and log the error. But still, the application fails to process valid transactions because the parsing does not handle the formatting. The development team is considering several approaches to fix this issue. The goal is to parse the string into an integer representing the whole dollar amount (ignoring cents and formatting). Which course of action should the developer take?
The Java Basics and Syntax domain covers the key concepts tested in this area of the 1Z0-811 exam blueprint published by Oracle. Courseiva provides free domain-focused practice, mock exams, missed-question review, and readiness tracking across all 1Z0-811 domains — no account required.
The Courseiva 1Z0-811 question bank contains 103 questions in the Java Basics and Syntax domain. Click any question to see the full explanation and answer breakdown.
Start with a 10-question focused session to identify your baseline accuracy in this domain. Read every explanation — even for questions you answer correctly — to understand the reasoning. Once you score consistently above 80%, move to a 20–30 question session to confirm depth before moving to the next domain.
Yes — the session launcher on this page draws questions exclusively from the Java Basics and Syntax domain. Choose 10, 20, 30, or 50 questions for a focused session, or click individual questions to review them one by one.
Save your results, see per-domain analytics, and get readiness scores — free, for every certification.
Sign Up FreeFree forever · Every certification included