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

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

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

Consider the following code:

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

Which of the following lines is the recurrence relation for the above code?

(n - 1) +recursive....sum(n) | |

n + recursive....sum(n) | |

n + recursive....sum(n - 1) | |

(n - 1) + recursive....sum(n - 1) |

Question 6 Explanation:

The recurrence relation for the above code is: n + recursive....sum(n - 1).

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

Consider 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 = 5; int ans = recursive....sum(n); printf("%d", ans); return 0; }

Which of the following is the base case for the above recursive code?

if(n == 0) | |

return 0 | |

return n + recursive....sum(n - 1) | |

none of the mentioned |

Question 7 Explanation:

"if(n == 0)" is the base case for the above recursive code.

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

What is the time complexity of the above recursive implementation used to find the sum of the first n natural numbers?

O(1) | |

O(n) | |

O(n ^{2}) | |

O(n ^{3}) |

Question 8 Explanation:

The time complexity of the above recursive implementation used to find the sum of first n natural numbers is O(n).

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

Which of the following methods used to find the sum of first n natural numbers has the least time complexity?

Recursion | |

Iteration | |

Binomial coefficient | |

All of the mentioned |

Question 9 Explanation:

Recursion and iteration take O(n) time to find the sum of first n natural numbers while binomial coefficient takes O(1) time.

Question 10 [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 = 5; int ans = recursive....sum(n); printf("%d", ans); return 0; }

10 | |

15 | |

21 | |

none of the mentioned |

Question 10 Explanation:

The above code prints the sum of first 5 natural numbers, which is 15.

There are 10 questions to complete.