Generics: Java 25 practice questions

Generics is 10% of the free Java 25 mock (5 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.

J.III · Java 25 practice question

According to the JLS SE 25 grammar for TypeParameter, which modifier is permitted directly on a type parameter declaration?

Choose one answer
Check answer and explanation

Correct answer: An annotation

Explanation

In the grammar for TypeParameter, TypeParameterModifier is defined exclusively as Annotation.

Common trap

The final keyword applies to classes, methods, or variables, not to type parameters. The static keyword applies to nested types or members, not to individual type parameter declarations.

Source: JLS SE 25 §8.1.2. Generic Classes and Type Parameters
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A developer declares a generic method with type parameters. How does JLS SE 25 describe the set of methods defined by that declaration?

Choose one answer
Check answer and explanation

Correct answer: One method for each possible invocation of the type parameter section by type arguments

Explanation

A generic method declaration defines a set of methods corresponding to each possible invocation of its type parameter section by type arguments.

Common trap

Claiming only Object is allowed confuses type parameter instantiation with raw or erased representations. Limiting methods to wrapper types invents an artificial restriction on type arguments. Limiting methods to wrapper types invents an artificial restriction on type arguments.

Source: JLS SE 25 §8.4.4. Generic Methods
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A developer declares a non-generic interface Auditable with no extends clause. According to JLS SE 25, what is the direct supertype of Auditable?

Choose one answer
Check answer and explanation

Correct answer: Object

Explanation

For a non-generic interface with no direct superinterface types, the type Object is its direct supertype.

Common trap

Even though an interface does not explicitly extend Object, JLS §4.10.2 defines Object as its direct supertype when it has no direct superinterfaces. Comparable is an ordinary library interface and is never an implicit supertype.

Source: JLS SE 25 §4.10.2. Subtyping among Class and Interface Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A developer examines a generic method declaration and wants to know where its type parameters are specified. Which section form does JLS SE 25 specify?

Choose one answer
Check answer and explanation

Correct answer: The same form as the type parameter section of a generic class

Explanation

The form of a generic method's type parameter section matches the form of a generic class's type parameter section.

Common trap

Claiming an annotation-only block is used misstates where type parameters are declared. Placing parameters at the end like arguments confuses type parameters with value parameters. Placing parameters at the end like arguments confuses type parameters with value parameters.

Source: JLS SE 25 §8.4.4. Generic Methods
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A generic class Cache<K, V> declares a method V lookup(K key). In the parameterized type Cache<String, Integer>, how is the type of lookup determined?

Choose one answer
Check answer and explanation

Correct answer: By substituting K with String and V with Integer in lookup's declared type

Explanation

For a parameterized type C<T1,...,Tn> with non-wildcard type arguments, the type of a member m is its declared type with each type parameter Ai substituted by Ti.

Common trap

Erasure happens during compilation to bytecode, not when determining the static member type of a parameterized type. Type arguments in a parameterized type are resolved at compile time via substitution, not left unbound until runtime.

Source: JLS SE 25 §4.5.2. Members and Constructors of Parameterized Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

When a generic class or interface is parameterized with non-wildcard type arguments in JLS SE 25, what must hold for each type argument in the list?

Choose one answer
Check answer and explanation

Correct answer: Each type argument in the given range is a type rather than a wildcard

Explanation

For a parameterization of this form with non-wildcard arguments, each argument in the range is a type rather than a wildcard.

Common trap

Requiring only interfaces ignores that classes are valid type arguments. Allowing only one argument when multiple type parameters exist mismatches the parameter count. Allowing only one argument when multiple type parameters exist mismatches the parameter count.

Source: JLS SE 25 §4.5.2. Members and Constructors of Parameterized Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A developer inspects a parameterized type written as Box<String>. According to JLS SE 25, what is Box in this expression?

Choose one answer
Check answer and explanation

Correct answer: The name of a generic class or interface

Explanation

In a parameterized type of the form C<T1,...,Tn>, C names the generic class or interface being parameterized.

Common trap

Treating C as a type variable confuses the generic type name with a declared type parameter identifier. Calling C an additional bound confuses type arguments with TypeBound syntax. Calling C an additional bound confuses type arguments with TypeBound syntax.

Source: JLS SE 25 §4.5. Parameterized Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

Given this Java 25 program using a parameterized type's constructor and member, what is printed?

public class Main {
    static class Box<T> {
        final T item;
        Box(T item) { this.item = item; }
        T unwrap() { return item; }
    }
    public static void main(String[] args) {
        Box<String> b = new Box<>("java25");
        System.out.println(b.unwrap().length());
    }
}
Choose one answer
Check answer and explanation

Correct answer: 6

Explanation

In Box<String>, substituting T with String gives unwrap() the return type String, so calling length() on "java25" compiles and prints 6.

Common trap

The string "java25" has 6 characters (j, a, v, a, 2, 5), not 5. Because b has the parameterized type Box<String>, unwrap() has static type String rather than Object.

Source: JLS SE 25 §4.5.2. Members and Constructors of Parameterized Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

How does JLS SE 25 characterize what a generic class or interface produces when type arguments are supplied?

Choose one answer
Check answer and explanation

Correct answer: It defines a set of parameterized types

Explanation

A generic class or interface defines a set of parameterized types formed from its type parameters and supplied type arguments.

Common trap

Saying it only produces a single raw type ignores parameterized types altogether. Claiming it creates an array type confuses parameterization with array construction. Claiming it creates an array type confuses parameterization with array construction.

Source: JLS SE 25 §4.5. Parameterized Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

In the JLS SE 25 grammar for TypeParameter, how can an additional bound be attached after a class or interface type in a TypeBound?

Choose one answer
Check answer and explanation

Correct answer: By preceding an InterfaceType with an ampersand

Explanation

An AdditionalBound in a TypeBound consists of an ampersand followed by an InterfaceType.

Common trap

Commas separate distinct type parameters in a parameter list rather than chaining bounds on one parameter. Repeating extends with another class does not match the AdditionalBound grammar rule. Repeating extends with another class does not match the AdditionalBound grammar rule.

Source: JLS SE 25 §4.4. Type Variables
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

In the parameterized type notation C<T1,...,Tn>, what does the bracketed list <T1,...,Tn> represent according to JLS SE 25?

Choose one answer
Check answer and explanation

Correct answer: Type arguments denoting a particular parameterization of the generic class or interface

Explanation

The list in angle brackets supplies type arguments that denote a specific parameterization of the generic class or interface.

Common trap

Treating the list as superinterfaces confuses type arguments with an implements clause. Calling it runtime reflection metadata mischaracterizes compile-time parameterization syntax. Calling it runtime reflection metadata mischaracterizes compile-time parameterization syntax.

Source: JLS SE 25 §4.5. Parameterized Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A programmer defines a non-generic class PlainTask that has no explicit extends clause. According to JLS SE 25, what counts among its direct supertypes?

Choose one answer
Check answer and explanation

Correct answer: The direct superclass type of PlainTask and its direct superinterface types

Explanation

For a non-generic class, its direct supertypes include its direct superclass type (if it is a class) and its direct superinterface types.

Common trap

Omitting the superclass type ignores the direct superclass rule for classes. Assuming unrelated generic interfaces are included invents supertypes not given by the declaration. Assuming unrelated generic interfaces are included invents supertypes not given by the declaration.

Source: JLS SE 25 §4.10.2. Subtyping among Class and Interface Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A software engineer writes a helper method inside a class that is not itself generic. Under JLS SE 25, what distinguishes this method as a generic method?

Choose one answer
Check answer and explanation

Correct answer: It introduces at least one type variable in its own declaration

Explanation

Declaring one or more type variables on the method itself makes it a generic method, even if the enclosing class is non-generic.

Common trap

Returning a wildcard-parameterized type uses an existing type rather than declaring method-level type variables. Overriding a generic method does not by itself declare new type variables on the overriding method. Overriding a generic method does not by itself declare new type variables on the overriding method.

Source: JLS SE 25 §8.4.4. Generic Methods
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

Where are type variables introduced in the Java language according to JLS SE 25?

Choose one answer
Check answer and explanation

Correct answer: By declaring a type parameter of a generic class, interface, method, or constructor

Explanation

Type variables are introduced by type parameter declarations on generic classes, interfaces, methods, or constructors.

Common trap

Using a class literal inside a static block produces a Class object rather than introducing a type variable. Importing a static constant only brings a member name into scope. Importing a static constant only brings a member name into scope.

Source: JLS SE 25 §4.4. Type Variables
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.III · Java 25 practice question

A developer writes a non-generic interface Marker that extends no other interfaces. According to JLS SE 25, what is a direct supertype of Marker?

Choose one answer
Check answer and explanation

Correct answer: The type Object

Explanation

When a non-generic interface has no direct superinterface types, Object is a direct supertype of that interface type.

Common trap

Picking Cloneable assumes an implicit interface bound that does not exist in the JLS rule. Picking Comparable invents a generic supertype where no superinterface was declared. Picking Comparable invents a generic supertype where no superinterface was declared.

Source: JLS SE 25 §4.10.2. Subtyping among Class and Interface Types
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


Back to all topics