Skip to content
CodeKruPro

A method that reassigns a list parameter

QuizMediumJava onlyAsked in: Infosys

What does this print?

static void reassign(List<String> l) {
    l = new ArrayList<>();
    l.add("x");
}

List<String> names = new ArrayList<>(List.of("a"));
reassign(names);
System.out.println(names);
  • [a]
  • [a, x]
  • [x]

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in