ProgrammingHardMicrosoft/Google SDE MCQ · GATE CS

Which of the marked lines are compile errors?

Which of the marked lines are compile errors?
int    g(int x) { return x; }
double g(int x) { return 2.0 * x; }       // line 1
void h(int x) { }
void h(int x, int y = 0) { }               // line 2
int main() {
    h(1, 2);                               // line 3
    h(1);                                  // line 4
}
  1. A. Lines 1 and 4
  2. B. Line 1 only
  3. C. Lines 1, 2 and 4
  4. D. Lines 2 and 4

Correct answer: A. Lines 1 and 4

Functions cannot be overloaded on return type alone, so line 1 redefines g. Declaring h(int, int = 0) next to h(int) is legal, and h(1, 2) has one viable candidate, but h(1) matches both equally: ambiguous.

More OOP with C++ and Java questions

Exams that ask this topic