Courseiva

1Z0-811 · domain

Java Basics and Syntax

Practise Oracle Java Foundations 1Z0-811 Java Basics and Syntax practice questions — original exam-style scenarios with answer choices, explanations, and analysis of common mistakes.

96 questions31 easy35 medium30 hard

Focused practice

Practice Java Basics and Syntax questions

Scored sessions drawing only from this domain — pick a length below.

Start 20-question practice test →

What this domain covers

What to know about Java Basics and Syntax

Java Basics and Syntax questions test whether you can apply the concept in context, not just recognise a definition.

How the topic appears in realistic exam-style scenarios.

Which detail in the question changes the correct answer.

How to eliminate plausible but wrong options.

How to connect the question back to the wider exam objective.

Watch out for

Common Java Basics and Syntax exam traps

  • Answering from memory before reading the full scenario.
  • Missing a constraint such as cost, availability, security, scope or command context.
  • Choosing a broad answer when the question asks for the most specific fix.
  • Ignoring why the wrong options are tempting.

Question index

All Java Basics and Syntax questions (96)

Click any question to see the full explanation, or start a practice session above.

1

A 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?

Medium
2

Which method overloading is valid?

Easy
3

A developer uses the following array initialization: int[] nums = new int[]{1, 2, 3}; Which of the following is true?

Hard
4

Arrange the steps to use the Scanner class to read user input in Java in the correct order.

Medium
5

Given the loop: for (int i=0; i<5; i++) { if (i==2) continue; System.out.print(i); } What is the output?

Hard
6

Given method: static void change(String s) { s = "new"; } What is output of: String name = "original"; change(name); System.out.println(name);

Hard
7

Which TWO of the following are valid Java identifiers?

Easy
8

Arrange the steps to declare and initialize a one-dimensional array in Java in the correct order.

Medium
9

What is the result of the expression 10 % 3?

Easy
10

Which statement about try-catch is true?

Hard
11

What is the output of this program?

Medium
12

Which TWO keywords are used for decision-making in Java? (Choose two.)

Easy
13

A subclass overrides a method from its superclass. Which annotation should be used to indicate the overriding intention?

Hard
14

A team decides to use a single Java source file for a small application. Which statement is true about the file structure?

Medium
15

Which TWO are valid Java identifiers? (Choose two.)

Easy
16

Which TWO statements about constructors are true? (Choose two.)

Medium
17

Which assignment requires an explicit cast to compile?

Medium
18

A 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?

Hard
19

What is the result of compiling and running this code?

Medium
20

Which TWO are valid ways to create a String object?

Easy
21

Refer to the exhibit. What is the output?

Easy
22

Which THREE are primitive data types in Java? (Choose three.)

Easy
23

A 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?

Easy
24

A method receives an int parameter and modifies its value inside the method. Does this change affect the caller's argument?

Medium
25

Which access modifier allows members to be accessed only by classes in the same package?

Medium
26

Which TWO of the following are valid Java identifiers? (Choose two.)

Easy
27

Refer to the exhibit. What is the likely cause of this error?

Easy
28

Which THREE are valid ways to declare and initialize an integer variable in Java? (Choose three.)

Hard
29

Given two String objects s1 = "Hello" and s2 = "Hello", what does the expression (s1 == s2) return?

Medium
30

Which is the correct way to declare an array of integers in Java?

Easy
31

Which THREE of the following are primitive data types in Java?

Hard
32

Which loop construct guarantees that the body executes at least once?

Medium
33

A method is declared as: public static void main(String[] args) { }. Which statement is true?

Medium
34

Which TWO of the following are legal ways to declare and initialize an array?

Medium
35

Which TWO statements are true about the main method?

Medium
36

A developer declares an integer variable inside a method but does not assign a value. What is the result of attempting to print the variable?

Easy
37

A 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?

Easy
38

Which access modifier makes a member visible only within its own class?

Medium
39

Given: String s = "Java"; s.concat(" Rocks"); System.out.println(s); What prints?

Easy
40

A 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?

Medium
41

The code does not compile. What is the error?

Medium
42

A method is expected to receive an integer and return its square. Which method signature is correct?

Easy
43

Given: 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?

Medium
44

You 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?

Hard
45

A 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?

Easy
46

A 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?

Medium
47

A class defines two methods with the same name but different parameter lists. This is known as:

Easy
48

A 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?

Hard
49

A 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?

Hard
50

A 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?

Medium
51

Given methods: void print(Integer i) { System.out.println("Integer"); } void print(int i) { System.out.println("int"); } What is output of print(10);?

Medium
52

Which THREE are valid Java identifiers?

Easy
53

Refer to the exhibit. What is the result of attempting to compile and run the code?

Medium
54

Consider: for(int i=0;i<10;i++) { int x = i; } System.out.println(x); What is the result?

Hard
55

A package named com.example.util is declared in a file. Where should the file be placed in the directory structure?

Easy
56

A parent class has a static method display() and an instance method show(). A child class attempts to override both. What is the outcome?

Hard
57

Given 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?

Hard
58

Which loop construct guarantees that the loop body executes at least once?

Easy
59

Given: int[] arr = {10,20,30}; System.out.println(arr[3]); What is the result?

Hard
60

A developer wants to iterate over an array of integers named 'numbers'. Which loop declaration will correctly access each element?

Easy
61

Which THREE statements about the final keyword in Java are true?

Hard
62

A developer writes the following code: int x = 5; int y = x++; What are the values of x and y after execution?

Easy
63

Refer to the exhibit. What is the output?

Easy
64

Which THREE are valid ways to declare and initialize a two-dimensional int array in Java?

Medium
65

Refer to the exhibit. What is the output?

Hard
66

What likely caused this compilation error?

Hard
67

Match each Java collection interface to its characteristics.

Medium
68

A company requires a method that accepts an integer and returns true if the integer is even, otherwise false. Which implementation best follows Java conventions?

Medium
69

Which THREE of the following are primitive data types in Java?

Hard
70

Refer to the exhibit. What happens when you compile this code?

Medium
71

What is the value printed?

Hard
72

Refer to the exhibit. What is the most likely cause?

Medium
73

A 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?

Hard
74

Which TWO are valid identifiers in Java? (Choose two.)

Medium
75

Match each access modifier to its visibility level.

Medium
76

Refer to the exhibit. What is the output?

Hard
77

Which primitive data type should be used to store a single character?

Easy
78

You 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?

Medium
79

Given the following code: class A { A() { System.out.print("A "); } } class B extends A { B() { System.out.print("B "); } } class C extends B { C() { System.out.print("C"); } public static void main(String[] args) { new C(); } } What is the output when running the main method?

Hard
80

What is the result of: Integer a = null; int b = (a != null) ? a : 0; System.out.println(b);

Hard
81

A developer writes: int x = 5; int y = x++ + ++x; What is the value of y after execution?

Hard
82

A developer writes: if (x = 5) { System.out.println("x is 5"); } What is the result?

Medium
83

Which TWO keywords are used to control access to class members? (Choose two.)

Medium
84

What is the result of: int[] arr = new int[5]; System.out.println(arr[5]);

Easy
85

Which data type should be used to store a single character like 'A'?

Easy
86

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

Hard
87

Given the code: int[] arr = {1,2,3}; for(int x : arr) { if(x==2) continue; System.out.print(x); } What is the output?

Hard
88

Which THREE are primitive data types in Java? (Choose three.)

Hard
89

A developer writes the following code: int x = 5; System.out.println(x++); What is the output?

Easy
90

What is the result of the following code? String s1 = "Hello"; String s2 = " World"; String s3 = s1 + s2; System.out.println(s3);

Medium
91

Given int[] arr = {1,2,3}; which correctly creates a new array with length 5 and copies the contents of arr?

Hard
92

What is the scope of a variable declared inside a for loop?

Medium
93

What is the output? ```java public class Test { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; System.out.println(s1.equals(s2)); } } ```

Easy
94

What is the value of the expression (10 > 5) && (3 < 2)?

Easy
95

A developer writes: int x; System.out.println(x); What is the result?

Easy
96

A developer uses a switch statement with a String variable. Which is true about this usage?

Hard

Frequently asked questions

What does the Java Basics and Syntax domain cover on the 1Z0-811 exam?
Java Basics and Syntax questions test whether you can apply the concept in context, not just recognise a definition.
How many questions are in this domain?
This page lists all 96 Java Basics and Syntax questions in the 1Z0-811 question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
What is the best way to practise this domain?
Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
Can I practise only Java Basics and Syntax questions?
Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.
Oracle Java Foundations 1Z0-811 Java Basics and Syntax Practice Questions