ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
struct B {
void f(int) { cout << "Bi"; }
void f(double) { cout << "Bd"; }
};
struct D : B {
void f(double) { cout << "Dd"; }
};
int main() {
D d;
d.f(5);
}- A. Bd
- B. Compilation error: ambiguous call
- C. Bi
- D. Dd
Correct answer: D. Dd
Declaring any f in D hides every f inherited from B (name hiding), so only D::f(double) is considered and 5 is converted to 5.0: Dd. Adding using B::f; inside D would bring B::f(int) back and print Bi.
More OOP with C++ and Java questions
- How many of these are correct about C++ virtual functions on a typical implementation? (i) Each class with virtual functions has a table…
- A method call such as shape.area() runs different code depending on whether shape refers to a Circle or a Square object at run time. Which…
- A Java class declares only the constructor Account(String id) and no other constructor. What happens with the statement new Account()?
- What is printed? java int x = 5; x = x++ + ++x; System.out.print(x);
- What is printed? cpp class A { public: int x; A(int v) : x(v) {} }; int main() { A a(5); A b = a; b.x = 9; cout << a.x << b.x; }
- Which line fails to compile? java abstract class Shape { abstract double area(); } class Sq extends Shape { double area() { return 4; } }…