Java SE 11 Developer study materials
Java SE 11 Developer study resources

Java SE 11 Developer practice questions

One question at a time. Build your Java SE 11 Developer 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 1Working with Java data types

Which declaration correctly defines a constant whose value cannot be reassigned after initialization?

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

Explanation

The final modifier makes a variable assignable only once, so after initialization the compiler rejects any further assignment. Java has no const keyword, and static or volatile affect storage and visibility, not reassignability.

Question 2Working with Java data types

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

  1. 3.0 of type int
  2. 3 of type int Correct answer.
  3. 4 of type int
  4. 3.5 of type double

Explanation

When both operands of / are integral, Java performs integer division, truncating the fractional part toward zero. The result type is int, so 7/2 yields 3, not 3.5 and not a rounded 4.

Question 3Working with Java data types

Which statement about the range of the primitive type `byte` is correct?

  1. It ranges from -127 to 128.
  2. It ranges from 0 to 255.
  3. It ranges from -32768 to 32767.
  4. It ranges from -128 to 127. Correct answer.

Explanation

A byte is a signed 8-bit two's-complement integer, giving a range of -128 through 127. The asymmetric range occurs because one bit encodes the sign.

Question 4Working with Java data types

What is the result of the expression `'a' + 1` in Java?

  1. A compile-time error
  2. The String "a1"
  3. The char 'b'
  4. The int 98 Correct answer.

Explanation

The char type is an integral type, so binary numeric promotion applies to 'a' + 1 and both operands become int. The result is the int 98, which only becomes 'b' if explicitly cast back to char.

Question 5Working with Java data types

Which line compiles without error when assigned to a variable of type `float`?

  1. float f = 3.14d;
  2. float f = 3.14f; Correct answer.
  3. float f = 3.14;
  4. float f = 3.14Fd;

Explanation

A floating-point literal without a suffix is a double, which cannot be assigned to float without a cast. The f or F suffix makes the literal a float, so 3.14f assigns directly.

Question 6Working with Java data types

Given `var list = new java.util.ArrayList<>();` what is the inferred static type of `list`?

  1. ArrayList (raw type)
  2. ArrayList<Object> Correct answer.
  3. The declaration fails to compile
  4. Object

Explanation

With var, the compiler infers the type from the initializer. The diamond operator infers Object for the type argument here, so the static type becomes ArrayList<Object>, not a raw ArrayList.

Question 7Working with Java data types

Which assignment requires an explicit cast to compile?

  1. long l = 100;
  2. int i = 'x';
  3. byte b = 200; Correct answer.
  4. double d = 5;

Explanation

The literal 200 is an int constant outside byte's range of -128 to 127, so an explicit cast is required. Widening conversions such as char to int, int to long, and int to double happen implicitly.

Question 8Working with Java data types

What does the expression `10 % 3` evaluate to, and what does `-10 % 3` evaluate to?

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

Explanation

The remainder operator returns the leftover after integer division, and its sign follows the dividend. Thus 10 % 3 is 1, while -10 % 3 is -1.

Question 9Working with Java data types

Which statement about `String` objects is correct?

  1. Two Strings with equal content always fail == comparison.
  2. Strings are primitive types like char.
  3. String literals with the same characters share the same interned object. Correct answer.
  4. Strings are mutable, so calling a method can change their content.

Explanation

String literals are automatically interned, so identical literals refer to the same object and compare equal with ==. Strings are immutable reference types, and == compares references, which may or may not be equal.

Question 10Working with Java data types

What is printed by the following code? StringBuilder sb = new StringBuilder("abc"); sb.append("def").reverse(); System.out.println(sb);

  1. abcdef
  2. fedcba Correct answer.
  3. defabc
  4. abc

Explanation

append returns the same StringBuilder, allowing chained calls, and reverse mutates it in place. After appending, the content is abcdef, which reverses to fedcba.

Question 11Working with Java data types

Which declaration creates an array of 5 int elements initialized to zero?

  1. int[] a = new int(5);
  2. int[] a = {5};
  3. int[] a = new int[5]; Correct answer.
  4. int a[5];

Explanation

The new int[5] syntax allocates an array of length 5, and int elements default to 0. Array creation uses brackets for the length, not parentheses.

Question 12Working with Java data types

What is printed by the following code? int[][] grid = new int[3][]; System.out.println(grid[0]);

  1. null Correct answer.
  2. A NullPointerException is thrown
  3. A compile-time error occurs
  4. 0

Explanation

new int[3][] creates an array of three int[] references but does not allocate the inner arrays. Each element is therefore null, and printing grid[0] prints null.

Question 13Working with Java data types

Which statement correctly describes default initialization of instance fields?

  1. Instance fields must always be assigned before use.
  2. Object-typed fields default to an empty object.
  3. Instance fields of type boolean default to false. Correct answer.
  4. Local variables and instance fields both default to zero.

Explanation

Instance fields receive default values, and for boolean that default is false. Local variables are not defaulted and must be assigned before use, and reference fields default to null.

Question 14Working with Java data types

What does `(int) 3.99` evaluate to?

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

Explanation

Casting a floating-point value to int truncates toward zero, discarding the fractional part. It does not round, so 3.99 becomes 3.

Question 15Working with Java data types

Which of these is a valid `long` literal?

  1. long x = 3000000000Lf;
  2. long x = 3.0;
  3. long x = 3000000000L; Correct answer.
  4. long x = 3000000000;

Explanation

An integer literal larger than Integer.MAX_VALUE must carry the L suffix, otherwise the compiler treats it as an int and reports it as too large. The L suffix marks the literal as long.

Question 16Working with Java data types

Given `Integer a = 127; Integer b = 127;` and `Integer c = 128; Integer d = 128;` which comparison is true?

  1. a == b is false and c == d is false
  2. a == b is true and c == d is true
  3. a == b is true and c == d is false Correct answer.
  4. a == b is false and c == d is true

Explanation

Autoboxing caches Integer objects for values -128 through 127, so a and b reference the same cached object and == is true. The value 128 lies outside the cache, so c and d are distinct objects and == is false.

Question 17Working with Java data types

What is the result of `Integer.parseInt("1_000")`?

  1. It returns 1000.
  2. It throws NumberFormatException. Correct answer.
  3. It fails to compile.
  4. It returns 1.

Explanation

Underscores are allowed inside numeric literals in source code, but parseInt parses the runtime string and accepts only digits with an optional sign. The underscore makes the string invalid, so a NumberFormatException is thrown.

Question 18Working with Java data types

Which statement about the `char` type is correct?

  1. A char can hold a full supplementary Unicode code point above 0xFFFF.
  2. A char is identical to a one-character String.
  3. A char is an unsigned 16-bit value representing a UTF-16 code unit. Correct answer.
  4. A char holds a signed 16-bit value from -32768 to 32767.

Explanation

char is a 16-bit unsigned integral type ranging from 0 to 65535, holding a UTF-16 code unit. Supplementary code points require a surrogate pair and cannot fit in a single char.

Question 19Working with Java data types

What is printed by the following code? int i = 5; int j = i++ + ++i; System.out.println(i + " " + j);

  1. 7 12 Correct answer.
  2. 6 12
  3. 7 11
  4. 5 11

Explanation

Post-increment i++ yields 5 and then i becomes 6. Pre-increment ++i first makes i 7 and yields 7, so j is 5 + 7 = 12 and i is 7.

Question 20Working with Java data types

Which expression correctly compares two String values for equal content?

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

Explanation

equals compares the character content of two Strings, which is the intended equality check. The == operator compares object references, and compareTo returns an ordering int rather than a boolean.

Question 21Working with Java data types

Which statement about `var` in Java 11 is correct?

  1. var can be used for method parameters.
  2. var can be used for local variables with an initializer. Correct answer.
  3. var can be used for instance fields.
  4. var declares a dynamically typed variable.

Explanation

var is allowed for local variables that have an initializer, letting the compiler infer the static type. It is not permitted for fields, method parameters, or return types, and the variable remains statically typed.

Question 22Working with Java data types

What happens when you assign a double value to an int variable without a cast?

  1. A compile-time error occurs for the narrowing conversion. Correct answer.
  2. The value is rounded to the nearest int.
  3. The fractional part is truncated silently.
  4. A ClassCastException is thrown at runtime.

Explanation

Assigning a double to an int is a narrowing primitive conversion, which the compiler does not perform implicitly. An explicit cast is required, otherwise compilation fails.

Question 23Working with Java data types

Which literal represents the double value one thousand using an allowed underscore placement?

  1. double d = _1000.0;
  2. double d = 1000_.0;
  3. double d = 1_000.0; Correct answer.
  4. double d = 1__000__.;

Explanation

Underscores may appear between digits in a numeric literal, so 1_000.0 is valid. They may not be placed at the start or end of the literal or immediately adjacent to the decimal point.

Question 24Working with Java data types

Which statement about comparing a `double` with `==` is most accurate?

  1. It is reliable because doubles store exact decimal values.
  2. It can give unexpected results because of binary floating-point representation. Correct answer.
  3. It always throws an exception when values differ.
  4. It compares the values after rounding to two decimals.

Explanation

Many decimal fractions cannot be represented exactly in binary floating point, so computed values may differ slightly and == can fail unexpectedly. Comparisons usually use a tolerance instead.

Question 25Controlling program flow

Which statement about a labeled `break` inside nested loops is correct?

  1. It exits the method that contains the labeled loop.
  2. It is only permitted inside a labeled `for` loop, not a `while` loop.
  3. It exits the loop statement that carries the matching label. Correct answer.
  4. It exits only the innermost loop that encloses it.

Explanation

A labeled `break` transfers control past the end of the labeled statement, so it can leave several nested loops at once. The label must be attached to the loop (or block) being exited, and the label name must match exactly. It works with any loop form, including `while` and `do-while`.

Question 26Controlling program flow

Given `for (int i = 0; i < 3; i++) { }`, in which order do the loop parts execute on the second iteration?

  1. Condition, update, body
  2. Update, condition, body
  3. Condition, body, update Correct answer.
  4. Body, condition, update

Explanation

After the first iteration's body finishes, the update expression runs, then the condition is re-evaluated, and if it is true the body runs again. So from the perspective of entering an iteration, the order is condition, body, update. Initialization happens only once, before the first condition check.

Question 27Controlling program flow

What is printed by this code? ```java int x = 5; switch (x) { case 1: System.out.print("A"); case 5: System.out.print("B"); case 6: System.out.print("C"); break; default: System.out.print("D"); } ```

  1. B
  2. BCD
  3. ABC
  4. BC Correct answer.

Explanation

A classic `switch` jumps to the matching `case` label and then falls through subsequent labels until it meets a `break` or the end of the switch. Matching `case 5` prints B, falls into `case 6` printing C, then `break` exits. `case 1` is never entered because execution does not scan backward.

Question 28Controlling program flow

Which `switch` statement compiles under Java 11 rules for a `String` selector?

  1. `switch (s) { case 1: break; }`
  2. `switch (s) { case "a".toUpperCase(): break; }`
  3. `switch (s) { case null: break; }`
  4. `switch (s) { case "a": break; }` Correct answer.

Explanation

Case labels for a `String` selector must be compile-time constant expressions of type `String`, and a string literal qualifies. `null` is not a valid case label, and non-constant expressions such as a method call are rejected. The case constant type must also be assignment-compatible with the selector type.

Question 29Controlling program flow

What does this loop print? ```java int i = 0; do { System.out.print(i + " "); i++; } while (i > 3); ```

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

Explanation

A `do-while` executes its body before testing the condition, so the body always runs at least once. After printing 0 and incrementing, the condition `1 > 3` is false and the loop ends. This guarantees at least one execution even when the condition starts false.

Question 30Controlling program flow

Which declaration of an enhanced `for` loop correctly iterates over an `int[]` named `data`?

  1. `for (int v : data) { }` Correct answer.
  2. `for (int[] v : data) { }`
  3. `for (int v in data) { }`
  4. `for (data : int v) { }`

Explanation

The enhanced `for` uses a colon and declares the iteration variable with the element type of the array or `Iterable`. For an `int[]`, each element is an `int`. The variable type must be assignment-compatible with the element type, so `int[]` is invalid here.

Question 31Controlling program flow

Which condition correctly tests whether `n` is an even integer, including negative values?

  1. `n / 2 == 0`
  2. `n % 2 == 0` Correct answer.
  3. `n % 2 == 1`
  4. `n % 2 != 1`

Explanation

The remainder operator `%` yields 0 when `n` is an exact multiple of 2, and this holds for negative even numbers such as -4. Comparing against 0 is the reliable test. Testing against 1 fails for negatives because `-3 % 2` is -1, not 1.

Question 32Controlling program flow

Inside a `while` loop, what happens when `continue;` executes?

  1. An `IllegalStateException` is thrown.
  2. The loop terminates immediately.
  3. Control jumps to the loop's update/condition evaluation, skipping the rest of the body. Correct answer.
  4. The rest of the body runs and then the loop ends.

Explanation

`continue` abandons the remainder of the current iteration and proceeds to the next iteration. In a `while` loop that means re-evaluating the condition, while in a `for` loop the update expression runs first. Any statements after `continue` in the body are skipped for that iteration.

Question 33Controlling program flow

What is the result of compiling and running this code? ```java public class T { public static void main(String[] args) { final int A = 1; int b = 2; switch (b) { case A: System.out.print("one"); break; case b: System.out.print("two"); break; } } } ```

  1. It fails to compile because `case b` is not a constant expression. Correct answer.
  2. It prints `two`.
  3. It prints `one`.
  4. It throws an exception at runtime.

Explanation

Every `case` label must be a constant expression, and a `final` local initialized with a literal is one, so `case A` is fine. The variable `b` is not final, so `case b` is not a constant expression and compilation fails. Constants must be compile-time evaluable, not merely assigned once.

Question 34Controlling program flow

Which loop prints the values 1 through 5 inclusive exactly once each?

  1. `for (int i = 1; i <= 5; i++) System.out.print(i);` Correct answer.
  2. `for (int i = 0; i <= 5; i++) System.out.print(i);`
  3. `for (int i = 1; i < 6; i += 2) System.out.print(i);`
  4. `for (int i = 1; i < 5; i++) System.out.print(i);`

Explanation

Using `i = 1` with the condition `i <= 5` and incrementing by 1 produces 1, 2, 3, 4, 5. The start value, inclusive upper bound, and step size must all be consistent. Off-by-one errors in either the condition or the initial value change the printed set.

Question 35Controlling program flow

Which statement about the label on a labeled `continue` is true?

  1. The label is optional and, when omitted, `continue` exits the outer loop.
  2. The label may name any enclosing statement, including an `if`.
  3. The label must name an enclosing loop statement. Correct answer.
  4. The label must be the same name as the loop variable.

Explanation

A labeled `continue` requires its label to denote an enclosing loop, because it must resume an iteration of that loop. Attaching the label to a non-loop statement such as an `if` block makes the `continue` a compile-time error. An unlabeled `continue` always targets the innermost enclosing loop.

Question 36Controlling program flow

What does this code print? ```java int sum = 0; for (int i = 0; i < 5; i++) { if (i == 3) continue; sum += i; } System.out.print(sum); ```

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

Explanation

The loop adds 0, 1, 2, skips 3 with `continue`, then adds 4, giving 0+1+2+4 = 7. `continue` skips only the current iteration's remaining statements, so the update and condition still run. The skipped value is excluded from the total.

Question 37Controlling program flow

Which is a valid `switch` selector type in Java 11?

  1. `boolean`
  2. `double`
  3. `long`
  4. `char` Correct answer.

Explanation

Permitted `switch` selector types are the integral types `byte`, `short`, `char`, and `int`, their wrapper classes, `String`, and `enum` types. Floating-point and `boolean` selectors are not allowed. `long` is excluded even though it is integral.

Question 38Controlling program flow

What is printed by this code? ```java outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) continue outer; System.out.print(i + "" + j + " "); } } ```

  1. 00 10 20 01
  2. 00 01 02
  3. 00 11 22
  4. 00 10 20 Correct answer.

Explanation

For each outer value, the inner loop prints when `j == 0`, then at `j == 1` the labeled `continue outer` abandons the inner loop and advances the outer loop. So only one pair per outer iteration is printed: 00, 10, 20. The label makes `continue` target the outer loop rather than the inner one.

Question 39Controlling program flow

Which expression evaluates to `true` when exactly one of `a` and `b` is `true`?

  1. `a & b`
  2. `a | b`
  3. `a ^ b` Correct answer.
  4. `!a && !b`

Explanation

The exclusive-or operator `^` on booleans yields `true` when the operands differ and `false` when they are the same. That matches "exactly one is true." The `&` and `|` operators are inclusive and do not exclude the both-true case.

Question 40Controlling program flow

Why is short-circuit evaluation significant when the right operand has a side effect or may throw? ```java if (obj != null && obj.size() > 0) { } ```

  1. `&&` requires both operands to be boolean constants.
  2. The right operand is evaluated only when the left operand is true. Correct answer.
  3. Both operands are always evaluated, so `obj.size()` may throw.
  4. The right operand is evaluated first to avoid null checks.

Explanation

With `&&`, if the left operand is false the result is already known, so the right operand is not evaluated. That is why the null check on the left protects the dereference on the right. The non-short-circuit `&` would evaluate both sides regardless.

Question 41Java object-oriented approach

A class declares a private field, a protected method, and a public constructor. Which statement about access to the protected method is correct?

  1. It can be invoked from any class in the same package and from subclasses in other packages. Correct answer.
  2. It can be invoked only from within the declaring class itself.
  3. It can be invoked only from subclasses, never from same-package classes.
  4. It can be invoked from any class in any package without restriction.

Explanation

The protected modifier grants access to classes in the same package plus subclasses in other packages (through inheritance). Package-private access alone would exclude other-package subclasses, and public access would remove all restriction.

Question 42Java object-oriented approach

A class declares no constructor at all. What constructor does the compiler provide?

  1. A no-argument constructor with the same access as the class. Correct answer.
  2. A constructor taking a single Object parameter.
  3. A no-argument constructor that is always private.
  4. No constructor, so the class cannot be instantiated.

Explanation

When a class declares no constructor, the compiler inserts a default no-argument constructor whose access modifier matches the class. Once any constructor is declared explicitly, the compiler stops supplying this default.

Question 43Java object-oriented approach

Given the declaration `abstract class Shape { abstract double area(); }`, which statement is valid?

  1. A subclass may leave area() unimplemented and still be concrete.
  2. An abstract class cannot declare any abstract methods.
  3. The method area() must be declared static in every subclass.
  4. A concrete subclass must provide an implementation of area() or be declared abstract. Correct answer.

Explanation

A concrete subclass of an abstract class must implement all inherited abstract methods; otherwise it must itself be declared abstract. Abstract classes may freely mix abstract and concrete methods.

Question 44Java object-oriented approach

Which declaration correctly defines a functional interface that can be implemented with a lambda expression?

  1. interface Op { int apply(int a); int other(int b); }
  2. interface Op { int apply(int a, int b); } Correct answer.
  3. interface Op { default int apply(int a) { return a; } }
  4. interface Op { static int apply(int a) { return a; } }

Explanation

A functional interface has exactly one abstract method, so a single abstract method like apply(int, int) qualifies. Lambdas target that single abstract method, which is why the other declarations cannot be lambda targets.

Question 45Java object-oriented approach

An interface declares `default void log() { System.out.println("I"); }`. A class implements that interface and also inherits a concrete log() method from a superclass. Which method does an instance use?

  1. The method is chosen randomly at runtime.
  2. A compile error occurs because the methods conflict.
  3. The interface default method, because interfaces are checked first.
  4. The superclass method, because class methods take precedence over interface default methods. Correct answer.

Explanation

Class hierarchy methods win over interface default methods when resolving an inherited implementation. A compile error would arise only when two unrelated interfaces both supply conflicting defaults.

Question 46Java object-oriented approach

A class implements two interfaces that both declare `default void run()`. The class does not override run(). What is the result?

  1. The compiler silently selects the last interface listed.
  2. A compile error, which is fixed by overriding run() and calling one interface default explicitly. Correct answer.
  3. Both default methods execute in sequence at runtime.
  4. The compiler silently selects the first interface listed.

Explanation

Conflicting default methods from unrelated interfaces must be resolved by an explicit override in the implementing class. That override may delegate using InterfaceName.super.run().

Question 47Java object-oriented approach

Which statement about method overloading is correct?

  1. Overloaded methods must have identical parameter lists.
  2. Overloaded methods share a name but differ in parameter lists, and return type alone cannot distinguish them. Correct answer.
  3. Overloaded methods must be declared in different classes.
  4. Overloaded methods must differ only in return type.

Explanation

Overloading is resolved at compile time using the method name plus parameter types. Return type is not part of the signature, so two methods differing only in return type will not compile.

Question 48Java object-oriented approach

A subclass overrides a method with the signature `public void process() throws IOException`. Which override declaration is valid?

  1. public void process() throws Exception
  2. public void process() throws IOException, SQLException
  3. public void process() throws FileNotFoundException Correct answer.
  4. private void process()

Explanation

An overriding method may narrow the checked exceptions declared by the overridden method. FileNotFoundException is a subclass of IOException, so it is permitted; widening to Exception or adding unrelated checked exceptions is not.

Question 49Java object-oriented approach

A class declares a field `static int count;` and a method that does `int count = 5;` inside a block. Inside that block, how is the static field referenced?

  1. It cannot be referenced at all from that block.
  2. By qualifying it, for example ClassName.count, because the local variable shadows it. Correct answer.
  3. Directly as this.count, because static fields belong to the instance.
  4. Directly as count, because static fields always win over locals.

Explanation

A local variable shadows a field of the same name, so the field must be qualified with the class name (or an instance, though discouraged). Static fields do not belong to a particular instance.

Question 50Java object-oriented approach

What is the effect of declaring a class as final?

  1. The class cannot be instantiated.
  2. The class cannot declare instance fields.
  3. All methods in the class become static.
  4. The class cannot be extended, but its instances can still be created and used. Correct answer.

Explanation

The final modifier on a class prevents subclassing while leaving construction and use unaffected. Preventing instantiation is the role of an abstract class or a private constructor, not final.