Recursion — C++ MCQs

30
Score: 0
Attempted: 0/30
1. What is recursion in C++?



2. What is essential for every recursive function?



3. What will the following code print?
int fun(int n) { if(n == 0) return 0; else return n + fun(n – 1); }
int main() { cout << fun(3); }




4. Recursion terminates when:



5. What is the output of this code?
void print(int n) { if(n == 0) return; cout << n << " "; print(n – 1); }
int main() { print(3); }




6. Which of the following correctly describes recursion?



7. What will this code print?
int fact(int n) { if(n == 1) return 1; return n * fact(n – 1); }
int main() { cout << fact(4); }




8. Recursive functions use which data structure internally?



9. What happens if base condition is missing in recursion?



10. What is the output of this code?
void test(int n) { if(n == 0) return; test(n – 1); cout << n; }
int main() { test(3); }




11. What is recursion that calls itself again before completing one call known as?



12. What will the code print?
void fun(int n) { if(n == 0) return; cout << n << " "; fun(n – 1); cout << n << " "; }
int main() { fun(2); }




13. What is the output of this code?
int fun(int n) { if(n <= 1) return 1; return fun(n – 1) + fun(n – 2); }
int main() { cout << fun(4); }




14. What type of recursion is this?
int fun(int n) { if(n == 0) return; fun(n – 1); }




15. What will be printed?
void fun(int n) { if(n == 0) return; cout << n; fun(n – 1); }
int main() { fun(3); }




16. Which of the following recursion types call itself at the end of the function?



17. What is the output of this program?
void show(int n) { if(n <= 0) return; show(n – 1); cout << n; }
int main() { show(3); }




18. Which recursion is faster in execution?



19. What happens when recursion depth becomes too large?



20. What is the output?
int sum(int n) { if(n == 0) return 0; return n + sum(n – 1); }
int main() { cout << sum(5); }




21. In recursion, each function call is stored in:



22. Which of the following is an example of indirect recursion?



23. What is the output?
int fun(int n) { if(n <= 1) return 1; return n * fun(n – 1); }
int main() { cout << fun(3); }




24. Recursion can be replaced by:



25. What will this code output?
void print(int n) { if(n == 0) return; cout << n; print(n – 1); }
int main() { print(4); }




26. What is the output?
void rec(int n) { if(n < 1) return; rec(n – 1); cout << n; }
int main() { rec(4); }




27. Which of the following is an advantage of recursion?



28. Which of the following is a disadvantage of recursion?



29. What is the output?
void test(int n) { if(n == 0) return; cout << "*"; test(n – 1); }
int main() { test(3); }




30. What kind of recursion occurs when a function calls itself more than once?



Contents Copyrights Reserved By T4Tutorials