Skip to content
Sharp

Reflection API · interview question

What string is produced when this MethodHandle invokes String.concat?

Multiple choiceMediumchapter-16-curatedpremiumreflection-apimethodhandles
java
import java.lang.invoke.*;

public class Test {
    public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mh = lookup.findVirtual(String.class, "concat", MethodType.methodType(String.class, String.class));
        String result = (String) mh.invokeExact("Hello, ", "World!");
        System.out.println(result);
    }
}
Choose one answer
Answer and explanation

Correct answer

D. Hello, World!

Why

The MethodHandle created by findVirtual is used to invoke the 'concat' method on the String 'Hello, ', resulting in the output 'Hello, World!'.

Test yourself on Reflection API

A free test with instant, explained results. No account needed.

Start free test
All Reflection API questions →