Last Updated: December 6, 2025
31 quizzes
Obtain the Class object for the User class to start reflection-based inspection
Class<?> clazz = .class;Click an option to fill the blank:
Create a new instance of a type represented by clazz using its no-argument constructor
Object instance = clazz.getDeclaredConstructor().();Click an option to fill the blank:
Access a private field called password on a User instance using reflection
Field field = userClass.getDeclaredField("password"); field.(true);Click an option to fill the blank:
Which package contains the core reflection classes like Method, Field, and Constructor?
What does Class.forName("com.example.MyService") primarily do?
Given String s = "hi";, what is the type of s.getClass()?
Which Class method returns the direct superclass of a class?
Which method lists all declared fields of a class, including private ones?
Which reflection method do you use to obtain a no-argument constructor?
Which is a common risk when using reflection to access private members?
In a dependency injection framework, reflection is often used to:
Which Modifier utility can you use to check if a class is abstract?
For serialization libraries using reflection, why is setAccessible(true) often used on fields?
Which statement about performance and reflection is generally true?
Order the steps to inspect a class and invoke a no-arg method reflectively.
Drag and drop to reorder, or use the arrows.
Order the steps to create an object using a parameterized constructor via reflection.
Drag and drop to reorder, or use the arrows.
What is the output of this code that uses reflection to inspect class metadata?
1class Base {}
2class Sub extends Base {}
3
4public class Main {
5 public static void main(String[] args) {
6 Class<?> clazz = Sub.class;
7 System.out.println(clazz.getSuperclass().getSimpleName());
8 }
9}What does this reflection-based code print about interface implementation?
1interface Service {}
2class EmailService implements Service {}
3
4public class Main {
5 public static void main(String[] args) {
6 Class<?>[] interfaces = EmailService.class.getInterfaces();
7 System.out.println(interfaces[0].getSimpleName());
8 }
9}What is the output when using reflection to read a field value?
1import java.lang.reflect.Field;
2
3class User {
4 private String role = "ADMIN";
5}
6
7public class Main {
8 public static void main(String[] args) throws Exception {
9 User user = new User();
10 Class<?> clazz = user.getClass();
11 Field field = clazz.getDeclaredField("role");
12 field.setAccessible(true);
13 System.out.println(field.get(user));
14 }
15}What is the output when dynamically invoking a method with reflection?
1import java.lang.reflect.Method;
2
3class Greeter {
4 public String greet() { return "Hello"; }
5}
6
7public class Main {
8 public static void main(String[] args) throws Exception {
9 Class<?> clazz = Greeter.class;
10 Object instance = clazz.getDeclaredConstructor().newInstance();
11 Method m = clazz.getMethod("greet");
12 Object result = m.invoke(instance);
13 System.out.println(result);
14 }
15}What does this code print about a method's declaring class?
1import java.lang.reflect.Method;
2
3class Parent {
4 public void work() {}
5}
6class Child extends Parent {}
7
8public class Main {
9 public static void main(String[] args) throws Exception {
10 Method m = Child.class.getMethod("work");
11 System.out.println(m.getDeclaringClass().getSimpleName());
12 }
13}This method tries to create an instance using reflection. Identify and fix the bug.
Click on the line(s) that contain the bug.
public class Factory { public Object create(String className) throws Exception { Class<?> clazz = Class.forName(className); return clazz.newInstance(); }}This code tries to access a private field with reflection. Find the bug and correct it.
Click on the line(s) that contain the bug.
import java.lang.reflect.Field; class Config { private String apiKey = "SECRET";} public class Reader { public static String readKey(Config config) throws Exception { Class<?> clazz = config.getClass(); Field f = clazz.getDeclaredField("apiKey"); String value = (String) f.get(config); return value; }}Match each reflection class with what it represents.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the reflection use case with the best description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to list all declared method names of a class using reflection.
import java.lang.reflect.Method;public class Inspector { public static void printMethods(Class<?> type) { [] methods = type.getDeclaredMethods(); for ( m : methods) { System.out.println(m.getName()); } }}Click an option to fill blank 1:
Complete the code to dynamically create a Person instance with a name using a parameterized constructor.
import java.lang.reflect.Constructor;class Person { private final String name; public Person(String name) { this.name = name; }}public class Factory { public static Person create(String value) throws Exception { Class<Person> clazz = Person.class; Constructor<Person> ctor = clazz.getDeclaredConstructor(.class); return ctor.(value); }}Click an option to fill blank 1:
Complete the code to check if a class is public using reflection utilities.
import java.lang.reflect.Modifier;public class Checker { public static boolean isPublicClass(Class<?> type) { int mods = type.(); return Modifier.(mods); }}Click an option to fill blank 1:
Complete the code to use reflection for a simple field-based logger that prints all field names and values.
import java.lang.reflect.Field;public class Logger { public static void log(Object obj) throws IllegalAccessException { Class<?> type = obj.getClass(); Field[] fields = type.getDeclaredFields(); for (Field f : fields) { f.(true); System.out.println(f.getName() + "=" + ); } }}Click an option to fill blank 1:
Click the line that uses reflection to dynamically create an instance of the class.
Click on the line to select.
Class<?> clazz = Class.forName("com.example.Plugin");Object plugin = clazz.getDeclaredConstructor().newInstance();System.out.println(clazz.getName());Click the line where a private field is made accessible via reflection.
Click on the line to select.
Field field = user.getClass().getDeclaredField("email");field.setAccessible(true);Object value = field.get(user);