
Unit 10 quiz topic 2

Quiz
•
Computers
•
9th - 12th Grade
•
Hard
Michael Courtright
Used 1+ times
FREE Resource
9 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
class ReverseString{
static void message(String s, int i) {
if(i<s.length()) {
char ch=s.charAt(i); message(s,i+1);
System.out.print(ch); } }
public static void main(String[] args){ message("Welcome", 0); }
}
Which of the following statements is the recursive condition in the above code?
message(s,i+1);
if(i<s.length())
if(i<=s.length())
message("Hello", 0);
if(i==s.length())
Answer explanation
The recursive condition is if(i<s.length()), which means that the recursive call will happen until the value of i is less than the length of the string.
2.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
class Recursion {
int func (int n) {
int result; result = func (n - 1);
return result; } }
class Output {
public static void main(String args[]) { Recursion obj = new Recursion() ; System.out.print(obj.func(12)); } }
What will be the output of the above code?
compile-time error
1
0
12
run-time error
Answer explanation
The base case of the recursive function func() is not defined, so an infinite loop occurs and results in overflow of stack memory.
3.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
class FactorialRec{
static int factorial(int i){ /*missing statement*/ }
public static void main(String[] args){
System.out.println(factorial(10)); } }
Which of the following is the correct replacement for /*missing statement*/ in the above code?
if(i==0 || i==1)
return 1;
return i * factorial(i-1);
if(i==0)
return 1;
return i * factorial(i-1);
if(i>=0 || i>=1)
return 1;
return i * factorial(i-1);
if(i==0 && i==1)
return 1;
return i * factorial(i-1);
if(i ==1)
return 1;
return i * factorial(i-1);
Answer explanation
First, you need to check to see if the value of i is 0 or 1 if(i==0 || i==1), as the factorial will be 1 in either of these cases, so it will return 1. Otherwise, for numbers greater than 1, the factorial value will be calculated by return i * factorial(i-1);.
4.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
Which of the following is/are correct if a recursive method does not have a base case?
I. The program will have an infinite loop.
II. The program will stop after some time.
III. The program will execute only once.
I only
II only
III only
I and II
II and III
Answer explanation
If a recursive method does not have a base case, then an infinite loop occurs, which results in overflow of stack memory.
5.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
public static void countdown(int n) {
if (n == 0) { System.out.println("Blastoff!"); }
else {
System.out.println(n); countdown(n - 1); } }
What will be the output of the code if n = 3?
Blastoff!
run-time error
3
1
Blastoff!
3
2
1
3
2
1
Blastoff!
Answer explanation
The execution of countdown begins with n == 3, and since n is not 0, it displays the value 3 and then invokes itself.
Then, the execution of countdown begins with n == 2, and since n is not 0, it displays the value 2 and then invokes itself.
Then, the execution of countdown begins with n == 1, and since n is not 0, it displays the value 1 and then invokes itself.
Then, the execution of countdown begins with n == 0, and since n is 0, it displays Blastoff! and then returns.
So, the total output will be:
3
2
1
Blastoff!
6.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
class MyArray{
static int find(int arr[], int i) {
if (i <= 0) { return 0; }
return find(arr, i-1 ) + arr[i-1]; }
public static void main(String[] args) {
int arr[] = {5, 10, 15, 20, 25, 30};
int ans = find(arr, arr.length); System.out.println(ans); } }
What will be the output of the above code segment?
105
0
error
6
75
Answer explanation
The output of the code is 105. The code is finding the sum of array elements. The first recursive call is the last position of the array.
In each recursive call, the index is decremented by 1 and the element is added to the previous value of the recursive call, stacked in the memory.
7.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following are the types of recursion?
front recursion
back recursion
tail recursion
mid recursion
8.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following are the types of recursion?
front recursion
back recursion
mid recursion
head recursion
9.
MULTIPLE CHOICE QUESTION
3 mins • 1 pt
Which of the following is/are incorrect about recursion in Java?
I. Recursion makes repeated calls until the base function is reached.
II. The recursion process is faster than the iteration.
III. Recursion uses more memory than iteration does.
I only
II only
III only
I and II
I and III
Answer explanation
Recursion is much slower than iteration because all function calls must be stored in a stack to allow the return back to the caller functions. Iteration does not involve any such overhead.
Similar Resources on Wayground
10 questions
Recursive Functions in Python

Quiz
•
12th Grade
10 questions
Unit 6

Quiz
•
12th Grade
12 questions
Enumeration and Recursion

Quiz
•
11th Grade - University
14 questions
Section 4A: Decompose the code using functions

Quiz
•
12th Grade
10 questions
Python. Пользовательские функции и процедуры. Рекурсия.

Quiz
•
10th Grade
12 questions
for dan if bahasa C

Quiz
•
11th Grade
10 questions
C++ vs Python: A Quiz Introduction

Quiz
•
11th Grade - University
10 questions
Arrays

Quiz
•
11th Grade - University
Popular Resources on Wayground
18 questions
Writing Launch Day 1

Lesson
•
3rd Grade
11 questions
Hallway & Bathroom Expectations

Quiz
•
6th - 8th Grade
11 questions
Standard Response Protocol

Quiz
•
6th - 8th Grade
40 questions
Algebra Review Topics

Quiz
•
9th - 12th Grade
4 questions
Exit Ticket 7/29

Quiz
•
8th Grade
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
19 questions
Handbook Overview

Lesson
•
9th - 12th Grade
20 questions
Subject-Verb Agreement

Quiz
•
9th Grade
Discover more resources for Computers
40 questions
Algebra Review Topics

Quiz
•
9th - 12th Grade
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
19 questions
Handbook Overview

Lesson
•
9th - 12th Grade
20 questions
Subject-Verb Agreement

Quiz
•
9th Grade
40 questions
LSHS Student Handbook Review: Pages 7-9

Quiz
•
11th Grade
24 questions
Scientific method and variables review

Quiz
•
9th Grade
10 questions
Characteristics of Life

Quiz
•
9th - 10th Grade
19 questions
Mental Health Vocabulary Pre-test

Quiz
•
9th Grade