🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: static, final and Nested Classes: all 10 questions

Quiz: static, final and Nested Classes · Question 3 of 10

What does this print? (code)

What does this print?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        final List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(7);
        System.out.println(list.size());
    }
}
  1. A.Compile error: list is final
  2. B.0
  3. C.2
  4. D.Runtime exception
Answer & explanation

Correct answer: 2

final only stops list from pointing to another object. Adding elements to the same list is allowed.

Can you get the rest right without peeking?

Take the timed Quiz: static, final and Nested Classes quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →