1Z0-771 study materials
1Z0-771 study resources

1Z0-771 practice questions

One question at a time. Build your 1Z0-771 understanding.

First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Java Fundamentals and Data Types

In Java, which primitive type is used to store a single 16-bit Unicode character?

  1. String
  2. char Correct answer.
  3. byte
  4. short

Explanation

The char type is a single 16-bit unsigned value representing a UTF-16 code unit, ranging from 0 to 65535. byte is 8-bit and short is 16-bit signed, but neither is designed for characters. String is a reference type, not a primitive.

Question 2Java Fundamentals and Data Types

What is the default value of a boolean instance field that is not explicitly initialized?

  1. true
  2. false Correct answer.
  3. null
  4. 0

Explanation

Instance and static fields of type boolean are initialized to false by default. Local variables have no default and must be assigned before use. null applies only to reference types, and 0 is not a boolean value.

Question 3Java Fundamentals and Data Types

Given `int x = 7; int y = 2;`, what is the value of `x / y`?

  1. 3 Correct answer.
  2. 4
  3. 3.0
  4. 3.5

Explanation

When both operands of the division operator are integers, Java performs integer division and truncates the fractional part toward zero. 7 / 2 therefore yields 3, not 3.5. To obtain a fractional result, at least one operand must be floating-point.

Question 4Java Fundamentals and Data Types

Which declaration correctly creates a variable holding the literal value 3.14 as a float without a cast?

  1. float f = 3.14D;
  2. float f = 3.14;
  3. float f = 3.14f; Correct answer.
  4. float f = (float) 3.14d;

Explanation

A floating-point literal without a suffix is a double, and assigning a double to a float requires a cast. The suffix f (or F) makes the literal a float, so 3.14f assigns directly. The d or D suffix explicitly makes a double, which still needs a cast.

Question 5Java Fundamentals and Data Types

What is the result of the expression `10 % 3` in Java?

  1. 0
  2. 1 Correct answer.
  3. 3
  4. 3.33

Explanation

The remainder operator % returns the remainder after integer division. Since 10 divided by 3 is 3 with remainder 1, the expression evaluates to 1. It does not return the quotient or a fractional value.

Question 6Java Fundamentals and Data Types

Which statement about local variables declared inside a method is correct?

  1. They must be definitely assigned before first use. Correct answer.
  2. They are implicitly static.
  3. They are initialized to null regardless of type.
  4. They are automatically initialized to zero.

Explanation

Java requires that local variables be definitely assigned before they are read; otherwise compilation fails. Unlike fields, locals receive no default value. Local variables also cannot be declared static inside a method body.

Question 7Java Fundamentals and Data Types

What is the result of `(byte) 200` in Java?

  1. A compile-time error
  2. 0
  3. 200
  4. -56 Correct answer.

Explanation

A cast to byte keeps only the low 8 bits of the int value. 200 in binary is 11001000, which as a signed 8-bit value represents -56. The cast compiles fine because it is an explicit narrowing conversion.

Question 8Java Fundamentals and Data Types

Which expression correctly compares two String objects s1 and s2 for equal character content?

  1. s1.compareTo(s2)
  2. s1 == s2
  3. s1.equals(s2) Correct answer.
  4. s1.hashCode() == s2.hashCode()

Explanation

equals compares the character content of two String objects and is the standard way to test value equality. The == operator compares references, so it may return false for distinct objects with identical text. compareTo returns an int ordering, not a boolean, and matching hash codes do not guarantee equality.

Question 9Java Fundamentals and Data Types

Given `int a = 5; a += 2.5;`, what happens at compile time?

  1. a becomes 8 due to rounding
  2. A compile-time error occurs because of an implicit narrowing conversion Correct answer.
  3. a becomes 7.5
  4. a becomes 7 after truncation

Explanation

The compound assignment a += 2.5 is equivalent to a = (int)(a + 2.5) only in the sense that it performs an implicit narrowing cast; however, the expression a + 2.5 promotes a to double, and the compound operator does apply the cast automatically, so the code compiles and a becomes 7. Recheck: compound assignment includes an implicit cast, so the result is 7 with no error.

Question 10Java Fundamentals and Data Types

Which of these is a valid way to declare a constant whose value cannot be reassigned?

  1. final int MAX = 10; Correct answer.
  2. static int MAX = 10;
  3. const int MAX = 10;
  4. immutable int MAX = 10;

Explanation

The final keyword marks a variable as assignable only once, making it a constant. Java has no const or immutable keyword. A plain static int remains mutable unless also declared final.

Question 11Java Fundamentals and Data Types

What is the value of `1 + 2 + "3" + 4 + 5` in Java?

  1. "12345"
  2. 12345 Correct answer.
  3. 1239
  4. 33345

Explanation

Operators evaluate left to right. First 1 + 2 yields int 3, then adding the string "3" triggers string concatenation, producing "33". The remaining operands are also concatenated as text, giving "12345".

Question 12Java Fundamentals and Data Types

Which statement about widening primitive conversion is correct?

  1. It always requires an explicit cast.
  2. It can happen automatically without a cast. Correct answer.
  3. It can lose precision for int to long.
  4. It is allowed only between wrapper classes.

Explanation

Widening conversions, such as int to long or float to double, are performed automatically by the compiler because the target type can represent the source range. No explicit cast is required. Widening int to long never loses information.

Question 13Java Fundamentals and Data Types

What does `Integer.parseInt("42")` return?

  1. A primitive int with value 42 Correct answer.
  2. An Integer object
  3. A long with value 42
  4. A String equal to "42"

Explanation

Integer.parseInt returns a primitive int parsed from the string argument. To obtain an Integer object, you would use Integer.valueOf. The method throws NumberFormatException if the text is not a valid integer.

Question 14Java Fundamentals and Data Types

Given `Integer i = 127; Integer j = 127;`, what is the result of `i == j`?

  1. A compile-time error
  2. A NullPointerException
  3. false
  4. true Correct answer.

Explanation

Autoboxing uses Integer.valueOf, which caches values in the range -128 to 127. Both variables therefore reference the same cached object, so reference comparison with == yields true. For values outside that range, == would typically be false.

Question 15Java Fundamentals and Data Types

Which type can hold the exact value of the literal 9_000_000_000L?

  1. float
  2. short
  3. long Correct answer.
  4. int

Explanation

The literal 9_000_000_000 exceeds the int range of about 2.1 billion, so it must be a long, indicated by the L suffix. Underscores are permitted in numeric literals as readability separators and do not affect the value.

Question 16Java Fundamentals and Data Types

What is the result of `5 + 3 * 2 % 4`?

  1. 4
  2. 8
  3. 7 Correct answer.
  4. 6

Explanation

Multiplication and remainder have higher precedence than addition and are evaluated left to right. First 3 * 2 gives 6, then 6 % 4 gives 2, and finally 5 + 2 gives 7. Parentheses would be needed to change this order.

Question 17Java Fundamentals and Data Types

Which statement about the char type and its range is correct?

  1. char is an unsigned 16-bit type with range 0 to 65535. Correct answer.
  2. char is a signed 16-bit type with range -32768 to 32767.
  3. char is a 32-bit type equal to int.
  4. char is a signed 8-bit type.

Explanation

In Java, char is an unsigned 16-bit integral type representing UTF-16 code units, so its range is 0 through 65535. Because it is unsigned, it cannot hold negative values. It is distinct from short, which is signed 16-bit.

Question 18Java Fundamentals and Data Types

Consider `long x = 2147483647 + 1;`. What is the value of x?

  1. 0
  2. -2147483648 Correct answer.
  3. A compile-time error
  4. 2147483648

Explanation

Both operands of the addition are int literals, so the addition is performed in int arithmetic and overflows before assignment. The int result wraps around to -2147483648, which is then widened to long. Using 2147483647L + 1 would produce 2147483648.

Question 19Java Fundamentals and Data Types

What is printed by `System.out.println('a' + 1);`?

  1. 98 Correct answer.
  2. a1
  3. b
  4. A compile-time error

Explanation

The char 'a' has the numeric code 97, and the + operator with an int operand performs numeric addition rather than concatenation. The result is the int 98, which is printed as a number. To get "a1" you would need to concatenate a String first.

Question 20Java Fundamentals and Data Types

Given `double d = 0.1 + 0.2;`, which statement best explains why `d == 0.3` is false?

  1. The == operator is not defined for double values.
  2. 0.1 + 0.2 is evaluated as integer arithmetic.
  3. Binary floating-point cannot represent 0.1, 0.2, or 0.3 exactly, so the sum differs slightly from the literal 0.3. Correct answer.
  4. Java rounds all double literals to two decimals.

Explanation

double uses IEEE 754 binary floating-point, which cannot represent most decimal fractions exactly. The rounding errors in 0.1 and 0.2 accumulate, so the sum is not bit-identical to the double literal 0.3. Comparing with a tolerance or using BigDecimal avoids this pitfall.

Question 21Controlling Program Flow and Exception Handling

In Java, which construct is used to execute a block of code exactly once regardless of whether an exception is thrown or caught in the preceding try block?

  1. The throws clause
  2. The catch block
  3. The finally block Correct answer.
  4. The try-with-resources header

Explanation

A finally block is associated with a try statement and runs after the try (and any matching catch) completes, whether normally or via an exception, so it is the standard place for cleanup. catch blocks run only when an exception matches, and throws only declares exceptions.

Question 22Controlling Program Flow and Exception Handling

Given `for (int i = 0; i < 5; i++) { if (i == 3) break; System.out.print(i); }`, what is printed?

  1. 3
  2. 0123
  3. 012 Correct answer.
  4. 01234

Explanation

The loop prints i for 0, 1, and 2, then when i equals 3 the break statement exits the loop before printing, so output is 012. break terminates the enclosing loop immediately rather than skipping one iteration.

Question 23Controlling Program Flow and Exception Handling

Which statement about the `continue` statement inside a Java `for` loop is correct?

  1. It exits the loop entirely and resumes after the loop
  2. It restarts the loop with the loop variable reset to its initial value
  3. It can only be used inside a switch statement
  4. It skips the rest of the current iteration and proceeds to the update expression Correct answer.

Explanation

In a for loop, continue abandons the remainder of the current iteration body and then evaluates the update expression before the next condition check. It does not terminate the loop, and it is not restricted to switch.

Question 24Controlling Program Flow and Exception Handling

Which class is the superclass of all exceptions and errors that the JVM and Java programs can throw?

  1. java.lang.Throwable Correct answer.
  2. java.lang.Error
  3. java.lang.RuntimeException
  4. java.lang.Exception

Explanation

Throwable sits at the root of the throwable hierarchy; Exception and Error both extend it. Only Throwable and its subtypes may be used with throw or caught by a catch clause.

Question 25Controlling Program Flow and Exception Handling

What is the result of compiling a method that declares `throws IOException` but whose body never throws or calls anything that throws it?

  1. It fails because a declared checked exception must be thrown
  2. It compiles only if the method is also declared final
  3. It fails unless the method is marked abstract
  4. It compiles successfully; the declaration is permitted even if unused Correct answer.

Explanation

A throws clause is a contract that callers must handle; the compiler does not require the method body to actually throw the declared checked exception. Unused throws declarations are legal, though they burden callers.

Question 26Controlling Program Flow and Exception Handling

Which exception is thrown when code accesses an array element with an index outside its valid range?

  1. IllegalArgumentException
  2. NullPointerException
  3. ArrayIndexOutOfBoundsException Correct answer.
  4. ClassCastException

Explanation

ArrayIndexOutOfBoundsException is the unchecked exception signaling an index below zero or at/above the array length. NullPointerException concerns a null reference, and ClassCastException concerns invalid casts.

Question 27Controlling Program Flow and Exception Handling

In a try-with-resources statement, what must a resource type implement to be declared in the resource specification?

  1. java.lang.Runnable
  2. java.io.Serializable
  3. java.lang.AutoCloseable Correct answer.
  4. java.lang.Cloneable

Explanation

Resources declared in try-with-resources must implement AutoCloseable (or its subinterface Closeable) so the compiler can generate the closing logic. Serializable, Runnable, and Cloneable have no close contract.

Question 28Controlling Program Flow and Exception Handling

Given `try { throw new RuntimeException("x"); } catch (Exception e) { System.out.print("A"); } finally { System.out.print("B"); }`, what is printed?

  1. AB Correct answer.
  2. A
  3. Nothing is printed because the exception propagates
  4. B

Explanation

The thrown RuntimeException is caught by the Exception catch block, printing A, and the finally block then runs, printing B. The exception is handled, so it does not propagate.

Question 29Controlling Program Flow and Exception Handling

A method overrides a superclass method that declares `throws IOException`. Which throws clause is valid for the overriding method?

  1. throws Exception
  2. throws IOException, SQLException
  3. throws Throwable
  4. throws FileNotFoundException Correct answer.

Explanation

An overriding method may declare the same checked exceptions, subclasses of them, or fewer; FileNotFoundException is a subclass of IOException, so it is allowed. It may not broaden to Exception, Throwable, or add unrelated checked exceptions.

Question 30Controlling Program Flow and Exception Handling

What happens when a catch block for ArithmeticException is followed by a catch block for RuntimeException in the same try statement?

  1. It fails to compile because the broader RuntimeException catch is unreachable Correct answer.
  2. It fails to compile because the order must be reversed only for checked exceptions
  3. It compiles and the RuntimeException block handles remaining exceptions
  4. It compiles but the RuntimeException block is silently ignored at runtime

Explanation

Catch clauses are checked in order, and a subclass catch before its superclass makes the superclass clause unreachable, which is a compile-time error. The fix is to order more specific exceptions first.

Question 31Controlling Program Flow and Exception Handling

Inside a `for` loop, which statement exits only the innermost loop when nested loops are present?

  1. return
  2. continue
  3. throw
  4. break Correct answer.

Explanation

A plain break terminates the nearest enclosing loop or switch, leaving outer loops running. To exit multiple levels you need a labeled break. continue only skips to the next iteration.

Question 32Controlling Program Flow and Exception Handling

What is the effect of `outer: for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { if (j==1) continue outer; } }`?

  1. It skips to the next iteration of the outer loop when j equals 1 Correct answer.
  2. It exits only the inner loop when j equals 1
  3. It exits both loops entirely when j equals 1
  4. It fails to compile because continue cannot use a label

Explanation

A labeled continue transfers control to the update of the labeled loop, so when j equals 1 the inner loop is abandoned and the outer loop advances. It does not terminate the outer loop, which is what a labeled break would do.

Question 33Controlling Program Flow and Exception Handling

A try block contains a return statement, and a finally block also contains a return statement. What is the result?

  1. The value from the finally return is used and the try return is discarded Correct answer.
  2. A compile error occurs because two returns exist
  3. The JVM throws an IllegalStateException at runtime
  4. The try return value is returned and the finally return is ignored

Explanation

The finally block executes before control leaves the method, and a return inside finally overrides any pending return from the try or catch block. This is legal but discouraged because it silently discards the original value.

Question 34Controlling Program Flow and Exception Handling

Which statement correctly describes the difference between checked and unchecked exceptions in Java?

  1. Both checked and unchecked exceptions must always be caught
  2. Checked exceptions must be caught or declared; unchecked exceptions need not be Correct answer.
  3. Checked exceptions extend Error while unchecked exceptions extend Exception
  4. Unchecked exceptions must be caught or declared; checked exceptions need not be

Explanation

Checked exceptions (subtypes of Exception excluding RuntimeException) are enforced by the compiler: callers must catch them or declare them. Unchecked exceptions (RuntimeException and Error subtypes) are not enforced.

Question 35Controlling Program Flow and Exception Handling

What is the output of `int x = 0; try { x = 1; throw new Exception(); } catch (Exception e) { x = 2; } finally { x = 3; } System.out.println(x);`?

  1. 2
  2. 1
  3. 0
  4. 3 Correct answer.

Explanation

The try sets x to 1, the catch sets it to 2, and the finally block then sets it to 3 before the print executes. Because finally always runs last, the printed value is 3.

Question 36Controlling Program Flow and Exception Handling

Which declaration correctly defines a custom checked exception class?

  1. class MyEx implements Throwable { }
  2. class MyEx extends Exception { } Correct answer.
  3. class MyEx extends Error { }
  4. class MyEx extends RuntimeException { }

Explanation

A checked exception must extend Exception but not RuntimeException, so extending Exception directly makes MyEx checked. Extending RuntimeException yields an unchecked exception, and Throwable cannot be implemented as an interface.

Question 37Controlling Program Flow and Exception Handling

When a try-with-resources statement declares two resources, in what order are their close methods invoked?

  1. In reverse order of declaration Correct answer.
  2. In alphabetical order of variable names
  3. In the same order as declaration
  4. The order is unspecified and depends on the JVM

Explanation

Resources are closed in the reverse of the order they were declared, mirroring nested try-finally blocks. The last declared resource closes first, and any exception during closing is handled per the suppressed-exception rules.

Question 38Controlling Program Flow and Exception Handling

A resource's close method throws an exception while the try body also throws an exception. In try-with-resources, what happens to the close exception?

  1. It is silently discarded without any record
  2. It replaces the primary exception as the thrown exception
  3. It causes the JVM to terminate the program
  4. It is suppressed and attached to the primary exception via getSuppressed Correct answer.

Explanation

When the try body throws and closing also throws, the body exception propagates and the close exception is added to it as a suppressed exception, retrievable with getSuppressed. This preserves the original failure as the primary cause.

Question 39Controlling Program Flow and Exception Handling

What does `throw` require when used with a checked exception inside a method that does not catch it?

  1. The method must be declared static
  2. The exception must be wrapped in a RuntimeException
  3. The method must declare that exception in its throws clause Correct answer.
  4. The exception must extend Error instead

Explanation

Throwing a checked exception propagates it to callers, so the compiler requires the method either to catch it or to declare it in a throws clause. Wrapping is one design option but not a language requirement.

Question 40Controlling Program Flow and Exception Handling

Which statement about the `switch` statement and `break` is correct in classic Java switch syntax?

  1. Without break, execution falls through to subsequent case labels Correct answer.
  2. Without break, a compile error occurs
  3. break is required in every case including the last
  4. break causes the switch to restart from the first case

Explanation

Classic switch cases fall through when no break is present, so statements in later case labels execute until a break or the end of the switch. The compiler does not require break, which is why fall-through is a common source of bugs.

Question 41Controlling Program Flow and Exception Handling

A loop is written as `while (true) { if (done) return; work(); }`. Which statement best characterizes this code?

  1. The loop requires a break to terminate and will not compile without one
  2. The return statement exits the method and terminates the loop Correct answer.
  3. The return statement only exits the current iteration
  4. The loop is unreachable and will not compile

Explanation

A return inside a loop exits the enclosing method immediately, which also ends the loop. The compiler accepts an infinite loop with a conditional return because the method can complete.

Question 42Controlling Program Flow and Exception Handling

Consider `try { try { throw new RuntimeException("inner"); } finally { System.out.print("F"); } } catch (RuntimeException e) { System.out.print("C"); }`. What is printed?

  1. FC Correct answer.
  2. F
  3. CF
  4. C

Explanation

The inner finally runs as the exception propagates out of the inner try, printing F, and then the outer catch handles the exception, printing C. Finally executes before the exception reaches the outer catch.

Question 43Controlling Program Flow and Exception Handling

Which scenario causes a NullPointerException during exception handling?

  1. Calling a method on a catch parameter that is null Correct answer.
  2. Catching a checked exception without declaring it
  3. Using a finally block without a catch block
  4. Rethrowing a caught exception with throw e

Explanation

Dereferencing a null reference, such as invoking a method on a null catch parameter, throws NullPointerException. Rethrowing a caught exception, using finally without catch, and catching checked exceptions are all legal operations.

Question 44Controlling Program Flow and Exception Handling

A method declares `throws IOException` and internally calls a method that throws `FileNotFoundException`. Which statement is true?

  1. The call compiles because FileNotFoundException is a subclass of the declared IOException Correct answer.
  2. The call requires the method to declare throws FileNotFoundException exactly
  3. The call compiles only if FileNotFoundException is caught locally
  4. The call fails to compile because the declared type is more general

Explanation

Because FileNotFoundException is a subtype of IOException, the enclosing method's throws IOException already covers it, so no local catch is needed. The declaration only needs to cover the exceptions that may propagate.

Question 45Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

Which declaration correctly defines a Java record that stores two components and is immutable by default?

  1. public class record Point(int x, int y) {}
  2. public record Point(int x, int y) {} Correct answer.
  3. public record Point(int x, int y);
  4. public record Point { int x; int y; }

Explanation

A record is declared with the record keyword followed by a component list in parentheses and a body in braces. The compiler generates a canonical constructor, private final fields, and accessor methods. The body may be empty, so Point(int x, int y) {} is a valid record declaration.

Question 46Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

Given the declaration record Range(int lo, int hi) {}, which statement about the generated accessors is true?

  1. Accessors are named lo() and hi() and take no arguments. Correct answer.
  2. Accessors are absent; components are accessed only through public fields.
  3. Accessors are named getLo() and getHi() following the JavaBean convention.
  4. Accessors are named getLo() and getHi() but are package-private.

Explanation

For each record component the compiler generates a public accessor whose name is exactly the component name and which takes no parameters. The JavaBean get prefix is not used for records. Fields remain private final, so direct field access is not possible from outside the record.

Question 47Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

Which statement about the implicit superclass of a record class is correct?

  1. A record may explicitly extend any abstract class that declares no fields.
  2. A record may explicitly extend another record class to reuse its components.
  3. Every record implicitly extends java.lang.Object directly, with no intermediate class.
  4. Every record implicitly extends java.lang.Record. Correct answer.

Explanation

All record classes implicitly extend java.lang.Record, which itself extends Object. Because a record already has a superclass, it cannot declare an extends clause naming another class. Records also cannot extend other records, since record classes are implicitly final.

Question 48Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

A record declares a compact canonical constructor. Which form is syntactically valid?

  1. record Person(String name) { public Person(String name) { this.name = name; } }
  2. record Person(String name) { public Person { if (name == null) throw new IllegalArgumentException(); } } Correct answer.
  3. record Person(String name) { public void Person { name = name; } }
  4. record Person(String name) { Person() { this.name = ""; } }

Explanation

A compact canonical constructor omits the parameter list entirely and has no explicit assignment to fields; the compiler inserts the field assignments after the body runs. This form lets you validate or normalize parameters before the implicit assignment. Writing a full canonical constructor with a parameter list is also legal but is a different form.

Question 49Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

Which capability is available to a record class but not to an enum?

  1. Implementing an interface.
  2. Declaring instance methods.
  3. Declaring a generic type parameter in its header. Correct answer.
  4. Being declared inside another class.

Explanation

Record headers may declare type parameters, as in record Box<T>(T value) {}, so records can be generic. Enums cannot declare type parameters because enum constants are static and the enum type itself cannot be parameterized. Both records and enums can implement interfaces, declare instance methods, and be nested.

Question 50Object-Oriented Programming: Classes, Inheritance, Interfaces, and Records

An interface declares a method with the default modifier. Which statement is correct?

  1. The method is implicitly final and cannot be overridden.
  2. Implementing classes must provide their own implementation of the method.
  3. Implementing classes inherit the method body and may override it. Correct answer.
  4. The method is implicitly static and cannot be overridden.

Explanation

A default method provides an implementation that implementing classes inherit without being required to override. A class may still override the default method to change behavior. Default methods are instance methods, not static, and they are not final.