LearnFrenzy
Sign in Take a free test

Java Programming · Topic 20 of 28

Generics

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Generics · Study material

Java Generics – Interview Questions

1. What are generics?

A way to parameterise classes, interfaces and methods by type for compile-time type safety and reuse.

2. What are the benefits?

Compile-time type checking, no casts, and reusable algorithms and data structures.

3. What is a generic method?

A method that declares its own type parameters before its return type, e.g. static <T> T first(List<T> l).

4. What is a bounded type parameter?

A type parameter restricted with extends, such as <T extends Number>, allowing calls to the bound's methods.

5. What is a wildcard?

The ? symbol representing an unknown type, optionally bounded: ? extends T or ? super T.

6. Explain PECS.

Producer Extends, Consumer Super: read from ? extends T, write to ? super T.

7. Why is List<Integer> not a List<Number>?

Generics are invariant; otherwise a Double could be added to a list of Integers.

8. What is type erasure?

The compiler removes generic type information after checking, replacing type parameters with their bounds and inserting casts.

9. Why can't you create new T()?

T is erased, so the run-time type is unknown; pass a Supplier<T> or Class<T> instead.

10. Why can't generics use primitives?

Type parameters erase to Object (or a bound), which primitives are not.

11. What is a raw type?

A generic type used without type arguments, e.g. List; it disables generic checks and exists for backward compatibility.

12. What is heap pollution?

A variable of a parameterised type referring to an object of a different parameterisation, usually through raw types or unchecked casts.

13. Can you overload methods that differ only in generic parameters?

No – f(List<String>) and f(List<Integer>) have the same erasure.

14. Can a generic class have static fields of type T?

No – static members are shared by all parameterisations.

15. What does <T extends Comparable<T>> mean?

T must be comparable to other T objects – a recursive bound.

16. Can you create a generic array?

Not directly (new T[n] or new List<String>[n]); use lists or an array of Object with a cast.

17. Can exception classes be generic?

No; catch clauses need the run-time type.

18. What is the diamond operator?

<> in new ArrayList<>(), letting the compiler infer type arguments (Java 7+).

19. What is @SafeVarargs?

An annotation promising that a generic varargs method does not misuse its array, suppressing heap-pollution warnings.

20. What is a bridge method?

A compiler-generated method that preserves overriding after erasure.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Java Programming

Java is one of the most used languages in the world – for enterprise software, Android apps, banking systems and backend services. This section covers core Java from syntax and data types to OOP, strings, arrays, exceptions, collections and multithreading, with clear explanations and practice questions for every topic.

Why it matters

  • Java is the main language in service-company placement tests and many product companies
  • OOP, String, collections and exception questions appear in nearly every Java interview
  • Strong core Java is the base for Spring Boot, Android and backend development
  • Java questions are common in university exams and certification tests

Who it is for

  • School students (Class 11–12 / ICSE computer applications) learning Java
  • College students (B.Tech, BCA, MCA) preparing for exams, labs and projects
  • Freshers preparing for placements, Java developer roles and technical interviews
  • Developers revising core Java before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Java developer interviews
  • Service company tests
  • University exams
  • Certification exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links