Course contents

Reflection & annotations: interview questions & scenarios

Premium16 questions · 10 what-if follow-ups

These are the reflection and annotation questions I hear most in backend Java interviews. On paper they're about an API nobody calls every day, but in practice they're how interviewers find out whether you understand the frameworks you do use every day.

Here's how to use this page: read the question, say your answer out loud first, then open the model answer. The "What if..." follow-ups are the ones interviewers actually ask next. If a question stumps you, the lessons are in this topic: A guide to Java reflection, Built-in and custom annotations and How frameworks use annotations and reflection.

Question 1. ConceptWarm-upAsked very often

What is reflection, and when would you actually use it?

Tests Class, Method, Field, trade-offs

Show answerHide answer1 follow-up

Model answer

Reflection is the java.lang.reflect API (plus java.lang.Class) that lets code inspect classes at runtime and call methods, read fields or invoke constructors by name, without compile-time references to them.

I'd use it when I'm writing infrastructure that must work with classes it was never compiled against: a DI container, a serializer, a test runner, an ORM, a plug-in loader. In business code I avoid it. It skips compile-time checks, breaks with renames, is slower than a direct call and needs extra configuration for GraalVM native images. If I need a plug-in, I load the class reflectively once and cast it to an interface.

How I’d say it in the interview

"Reflection lets you inspect and call code at runtime through Class, Method, Field and Constructor. It's what frameworks are built on: DI, Jackson, Hibernate, JUnit. In application code I prefer interfaces, because reflection loses type safety and costs performance."

Signals depth

Mentioning Effective Java's "prefer interfaces to reflection", and the "load reflectively once, then use through an interface" pattern.

Red flags

"It's used to access private fields." That's one use, not the definition, and it suggests reflection is a hacking tool.

Follow-ups interviewers ask next

  1. What if I ask for three ways to get a Class object?

    Order.class, order.getClass() and Class.forName("com.shop.Order"). The literal doesn't initialise the class; forName does (run static blocks) unless you call forName(name, false, loader).

Question 2. ConceptWarm-upAsked very often

What's the difference between getMethods() and getDeclaredMethods()?

Tests getX vs getDeclaredX, inheritance

Show answerHide answer1 follow-up

Model answer

  • getMethods() returns public methods, including inherited ones from superclasses and superinterfaces (so Object's toString, equals and friends show up).
  • getDeclaredMethods() returns methods of every access level declared in that class only, nothing inherited, and possibly synthetic bridge methods.

The same split applies to fields and constructors. A protected method in a parent class appears in neither list; you have to walk getSuperclass() and call getDeclaredMethods() at each level.

How I’d say it in the interview

"getMethods is public plus inherited. getDeclaredMethods is everything declared in this class, private included, but nothing inherited. To find a private or protected method in a parent, I walk up the hierarchy myself."

Signals depth

Knowing that neither call guarantees order, and that each call returns fresh Method copies.

Red flags

"getDeclaredMethods returns all methods including inherited ones."

Follow-ups interviewers ask next

  1. What if I call getDeclaredMethod("save") on a class with save(Order)?

    NoSuchMethodException. Lookup is by name and parameter types: getDeclaredMethod("save", Order.class).

Question 3. ConceptWarm-upAsked very often

What does @Retention control, and what is the default?

Tests SOURCE, CLASS, RUNTIME

Model answer, how to say it: unlock with the Java Pass

Question 4. What does it print?Warm-upCommonly asked

What happens here, and what would @Override have changed?

Tests @Override, overloading vs overriding

Model answer, how to say it: unlock with the Java Pass

Question 5. ConceptCoreAsked very often

How does @Transactional work, and why doesn't it apply when a method calls another method in the same class?

Tests proxies, self-invocation, AOP

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 6. ConceptCoreCommonly asked

JDK dynamic proxy or CGLIB: what's the difference, and when does each get used?

Tests Proxy, InvocationHandler, subclass proxies

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 7. ConceptCoreCommonly asked

Can reflection read a private field? Can it change a final one?

Tests setAccessible, final fields, records

Model answer, how to say it: unlock with the Java Pass

Question 8. DebuggingCoreCommonly asked

Your custom @Audited annotation works in a unit test, but in production the auditing aspect never fires. Walk me through it.

Tests retention, proxies, annotation lookup

Model answer, how to say it: unlock with the Java Pass

Question 9. ConceptCoreCommonly asked

Why does JPA require a no-arg constructor and non-final entities? Can an entity be a record?

Tests JPA spec, reflective instantiation, lazy proxies

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 10. CodingCoreCommonly asked

Write a @Retry annotation with defaults and a method that reads it.

Tests annotation elements, defaults, getAnnotation

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 11. What does it print?CoreSometimes asked

What does this print?

Tests @Repeatable, container annotation

Model answer, how to say it: unlock with the Java Pass

Question 12. ScenarioSeniorAsked very often

After upgrading from Java 11 to Java 17, the service crashes at startup with InaccessibleObjectException. Walk me through it.

Tests JEP 396, JEP 403, --add-opens

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 13. ConceptSeniorCommonly asked

Is reflection slow? What changed in Java 18?

Tests inflation, JEP 416, MethodHandle

Model answer, how to say it: unlock with the Java Pass

Question 14. ScenarioSeniorCommonly asked

Our service takes 40 seconds to start and autoscaling can't keep up during traffic spikes. What would you look at?

Tests startup cost, classpath scanning, AOT

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 15. DebuggingSeniorSometimes asked

A bean method throws NullPointerException on a field that's clearly injected by the constructor. The method is final. What's going on?

Tests class-based proxies, Objenesis, final methods

Model answer, how to say it: unlock with the Java Pass

Question 16. DesignSeniorSometimes asked

How would you design a minimal dependency injection container?

Tests scanning, constructor injection, cycles, scopes

Model answer, how to say it: unlock with the Java Pass

Premium question set

Keep reading with the Java Pass

The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

From ₹199 · one-time payment · No auto-renewal

Lessons in Reflection & annotations