ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
List<Integer> l = new ArrayList<>(List.of(1, 2, 3)); l.remove(1); System.out.print(l + " "); l.remove(Integer.valueOf(1)); System.out.print(l);
- A. [2, 3] [3]
- B. [2, 3] [2, 3]
- C. [1, 3] [3]
- D. [1, 3] [1, 3]
Correct answer: C. [1, 3] [3]
remove(1) with an int literal matches remove(int index), removing the element at index 1 (the value 2). remove(Integer.valueOf(1)) matches remove(Object) and removes the value 1.
More OOP with C++ and Java questions
- What is printed? java List<Integer> list = new ArrayList<>(List.of(1, 2, 3)); for (Integer i : list) if (i == 2) list.remove(i)…
- 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…