# YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

## Data Structure Questions and Answers-Sum of n Natural Numbers using Recursion

Question 11 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

How many times is the function recursive....sum() called when the following code is executed?

#include<stdio.h> int recursive....sum(int n) { if(n == 0) return 0; return n + recursive....sum(n - 1); } int main() { int n = 5; int ans = recursive....sum(n); printf("%d", ans); return 0; }

4 | |

5 | |

6 | |

7 |

Question 11 Explanation:

The function recursive....sum is called 6 times when the following code is executed.

Question 12 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the output of the following code?

#include<stdio.h> int recursive....sum(int n) { if(n == 0) return 0; return n + recursive....sum(n - 1); } int main() { int n = 0; int ans = recursive....sum(n); printf("%d", ans); return 0; }

-1 | |

0 | |

1 | |

runtime error |

Question 12 Explanation:

The program prints the sum of first 0 natural numbers, which is 0.

Question 13 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER] |

What is the output of the following code?

#include<stdio.h> int recursive....sum(int n) { if(n == 0) return 0; return n + recursive....sum(n - 1); } int main() { int n = -4; int ans = recursive....sum(n); printf("%d", ans); return 0; }

0 | |

-10 | |

1 | |

runtime error |

Question 13 Explanation:

The above code doesn't handle the case of negative numbers and so the function recursive....sum() will be called again and again till the stack overflows and the program produces a runtime error.

There are 13 questions to complete.