ProgrammingHardProduct-company MCQ pattern
What happens with this C++ code?
What happens with this C++ code?
void f(int a, int b = 0) { cout << "two"; }
void f(int a) { cout << "one"; }
int main() { f(5); }- A. Compilation error: the call f(5) is ambiguous
- B. Prints two
- C. Compilation error at the second declaration
- D. Prints one
Correct answer: A. Compilation error: the call f(5) is ambiguous
Both declarations are legal overloads, and f(5, 6) would work. But f(5) matches f(int) and f(int, int = 0) equally well, so the call is ambiguous and the program does not compile.
More OOP with C++ and Java questions
- 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 { } ?
- How many of these are correct in Java (8 or later)? (i) An abstract class can have a constructor. (ii) An interface can declare instance…