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
}- A. Lines 1 and 4
- B. Line 1 only
- C. Lines 1, 2 and 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
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.put("a", 1)); System.out.print(m.put("a", 2))…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.merge("k", 5, Integer::sum); m.merge("k", 5, Integer::sum); m.merge("k"…
- What is printed? java TreeMap<Integer, String> t = new TreeMap<>(); for (int k : new int[]{40, 10, 30, 20}) t.put(k, "v")…
- What is printed? java Deque<Integer> d = new ArrayDeque<>(); d.push(1); d.push(2); d.offer(3); d.addFirst(4); System.out.print(d.pop() +…
- What is printed? java Integer[] arr = {1, 2, 3}; List<Integer> l = Arrays.asList(arr); arr[0] = 7; l.set(2, 9); System.out.print(l + " " +…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4, 5)); l.subList(1, 3).clear(); System.out.print(l);