#126 Passing Primitives and References to a Method

Companies:
BFT
Themes:
JavaJava BasicsCode Review

What will be printed to the screen?

import java.util.*;

public class Main {

    public static void main(String[] args) {
        int i1 = 1;
        List<Integer> i2 = new ArrayList<>();
        i2.add(1);

        fun(i1, i2);

        System.out.println("i1 = " + i1);
        System.out.println("i2 = " + i2);
    }

    private static void fun(int i1, List<Integer> i2) {
        i1++;
        i2.add(2);
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in