AlgoMaster Logo

Reflection - Quiz

Last Updated: December 6, 2025

1 min read

Reflection Exercises

31 quizzes

1
Code Completion

Obtain the Class object for the User class to start reflection-based inspection

java
1
Class<?> clazz = .class;

Click an option to fill the blank:

2
Code Completion

Create a new instance of a type represented by clazz using its no-argument constructor

java
1
Object instance = clazz.getDeclaredConstructor().();

Click an option to fill the blank:

3
Code Completion

Access a private field called password on a User instance using reflection

java
1
Field field = userClass.getDeclaredField("password"); field.(true);

Click an option to fill the blank:

4
Multiple Choice

Which package contains the core reflection classes like Method, Field, and Constructor?

5
Multiple Choice

What does Class.forName("com.example.MyService") primarily do?

6
Multiple Choice

Given String s = "hi";, what is the type of s.getClass()?

7
Multiple Choice

Which Class method returns the direct superclass of a class?

8
Multiple Choice

Which method lists all declared fields of a class, including private ones?

9
Multiple Choice

Which reflection method do you use to obtain a no-argument constructor?

10
Multiple Choice

Which is a common risk when using reflection to access private members?

11
Multiple Choice

In a dependency injection framework, reflection is often used to:

12
Multiple Choice

Which Modifier utility can you use to check if a class is abstract?

13
Multiple Choice

For serialization libraries using reflection, why is setAccessible(true) often used on fields?

14
Multiple Choice

Which statement about performance and reflection is generally true?

15
Sequencing

Order the steps to inspect a class and invoke a no-arg method reflectively.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to create an object using a parameterized constructor via reflection.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

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}
18
Output Prediction

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}
19
Output Prediction

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}
20
Output Prediction

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}
21
Output Prediction

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}
22
Bug Spotting

This method tries to create an instance using reflection. Identify and fix the bug.

Click on the line(s) that contain the bug.

java
1
public class Factory {
2
    public Object create(String className) throws Exception {
3
        Class<?> clazz = Class.forName(className);
4
        return clazz.newInstance();
5
    }
6
}
23
Bug Spotting

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.

java
1
import java.lang.reflect.Field;
2
 
3
class Config {
4
    private String apiKey = "SECRET";
5
}
6
 
7
public class Reader {
8
    public static String readKey(Config config) throws Exception {
9
        Class<?> clazz = config.getClass();
10
        Field f = clazz.getDeclaredField("apiKey");
11
        String value = (String) f.get(config);
12
        return value;
13
    }
14
}
24
Matching

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.

25
Matching

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.

26
Fill in the Blanks

Complete the code to list all declared method names of a class using reflection.

java
1
import java.lang.reflect.Method;
2
3
public class Inspector {
4
public static void printMethods(Class<?> type) {
5
[] methods = type.getDeclaredMethods();
6
for ( m : methods) {
7
System.out.println(m.getName());
8
}
9
}
10
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to dynamically create a Person instance with a name using a parameterized constructor.

java
1
import java.lang.reflect.Constructor;
2
3
class Person {
4
private final String name;
5
public Person(String name) { this.name = name; }
6
}
7
8
public class Factory {
9
public static Person create(String value) throws Exception {
10
Class<Person> clazz = Person.class;
11
Constructor<Person> ctor = clazz.getDeclaredConstructor(.class);
12
return ctor.(value);
13
}
14
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to check if a class is public using reflection utilities.

java
1
import java.lang.reflect.Modifier;
2
3
public class Checker {
4
public static boolean isPublicClass(Class<?> type) {
5
int mods = type.();
6
return Modifier.(mods);
7
}
8
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to use reflection for a simple field-based logger that prints all field names and values.

java
1
import java.lang.reflect.Field;
2
3
public class Logger {
4
public static void log(Object obj) throws IllegalAccessException {
5
Class<?> type = obj.getClass();
6
Field[] fields = type.getDeclaredFields();
7
for (Field f : fields) {
8
f.(true);
9
System.out.println(f.getName() + "=" + );
10
}
11
}
12
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that uses reflection to dynamically create an instance of the class.

Click on the line to select.

java
1
Class<?> clazz = Class.forName("com.example.Plugin");
2
Object plugin = clazz.getDeclaredConstructor().newInstance();
3
System.out.println(clazz.getName());
31
Hotspot Selection

Click the line where a private field is made accessible via reflection.

Click on the line to select.

java
1
Field field = user.getClass().getDeclaredField("email");
2
field.setAccessible(true);
3
Object value = field.get(user);

Premium Content

Subscribe to unlock full access to this content and more premium articles.