ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class Account {
    double balance;
public:
    explicit Account(double b) : balance(b) {}
    double get() const { return balance; }
};
void deposit(Account acc) { cout << acc.get(); }
int main() {
    deposit(Account(100));
    deposit(100);
}
  1. A. 100100
  2. B. Compile-time error only on deposit(100): explicit blocks the implicit double-to-Account conversion needed here
  3. C. Compile-time error on both calls
  4. D. 100 then undefined behaviour

Correct answer: B. Compile-time error only on deposit(100): explicit blocks the implicit double-to-Account conversion needed here

explicit disables using the single-argument constructor for implicit conversions. deposit(Account(100)) explicitly constructs an Account, which is fine. deposit(100) tries to implicitly convert the int/double 100 into an Account to match the parameter type, which explicit forbids, so only that call fails to compile.

More OOP with C++ and Java questions

Exams that ask this topic