GATE CS 2017 Set 1 — Question 36
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+2 / -0.67MediumFunctions & RecursionC ProgrammingProgramming & Data StructuresData Types & Operators
Programming & Data Structures → C Programming → Data Types & Operators
Last updated
Question
Consider the C functions Invocations of
foo and bar given below:int foo(int val) {
int x = 0;
while(val > 0) {
x = x + foo(val--);
}
return val;
}
int bar(int val) {
int x = 0;
while(val > 0) {
x = x + bar(val-1);
}
return val;
}
foo(3) and bar(3) will result in :Correct answer
(C) Abnormal termination and infinite loop respectively.
Solution
Analysis of
Inside
The argument to the recursive call is
So,
This creates an infinite recursion:
Infinite recursion leads to a Stack Overflow, which causes Abnormal termination.Analysis of
Inside
Control returns to
Note that
So
This is an Infinite loop.Thus,
foo(3):Inside
foo(3), the loop condition is val > 0 (3 > 0). The body executes x = x + foo(val--);.The argument to the recursive call is
val--. The post-decrement operator returns the current value of val (which is 3) and then decrements val to 2.So,
foo(3) calls foo(3) recursively.This creates an infinite recursion:
foo(3) -> foo(3) -> foo(3) ...Infinite recursion leads to a Stack Overflow, which causes Abnormal termination.Analysis of
bar(3):Inside
bar(3), the loop condition is val > 0 (3 > 0). The body executes x = x + bar(val-1);.bar(3) calls bar(2).bar(2) calls bar(1).bar(1) calls bar(0).bar(0) checks val > 0 (0 > 0), which is false, and returns 0.Control returns to
bar(1). x becomes x + 0. The loop checks val > 0 (1 > 0) again.Note that
val is never modified inside the bar function (it is passed by value, and val-1 does not change the local val).So
bar(1) stays in the while(1 > 0) loop forever, repeatedly calling bar(0) and adding 0 to x.This is an Infinite loop.Thus,
foo(3) results in abnormal termination, and bar(3) results in an infinite loop.Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]