ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
for (Integer i : list)
if (i == 2) list.remove(i);
System.out.print(list);- A. [1, 2]
- B. ConcurrentModificationException is thrown
- C. [2, 3]
- D. [1, 3]
Correct answer: D. [1, 3]
list.remove(i) with an Integer removes the value 2. The size drops to 2 while the iterator's cursor is already 2, so hasNext() returns false and the loop ends before next() can detect the change. Removing any other element here would throw ConcurrentModificationException.
More OOP with C++ and Java questions
- What happens with this C++ code? cpp void f(int a, int b = 0) { cout << "two"; } void f(int a) { cout << "one"; } int main() { f(5); }
- What is printed? cpp struct Base { virtual void show() { cout << "Base "; } }; struct Derived : Base { void show() override { cout <<…
- What is printed? cpp class Base { public: Base() { hello(); } virtual void hello() { cout << "B"; } }; class Derived : public Base {…
- What is printed? cpp struct A { A() { cout << "A"; } }; struct B : virtual A { B() { cout << "B"; } }; struct C : virtual A { C() { cout…
- How many of these statements about C++ friend functions are correct? (i) A friend function can access the private members of the class…
- Interfaces A and B both declare default void hello(). What happens with class C implements A, B { } ?