
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
11 questions
algoritma

Quiz
•
12th Grade
10 questions
Unit 6

Quiz
•
12th Grade
9 questions
Java Basics #1

Quiz
•
10th - 12th Grade
12 questions
Arrays Intro

Quiz
•
9th - 12th Grade
10 questions
Bài tập trắc nghiệm C++

Quiz
•
3rd - 12th Grade
10 questions
2.2 Programming techniques - repetition

Quiz
•
11th Grade
12 questions
Compound Operators in Java

Quiz
•
9th - 12th Grade
10 questions
C++ Programming Quiz

Quiz
•
12th Grade
Popular Resources on Wayground
10 questions
Video Games

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

Interactive video
•
6th - 10th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
10 questions
UPDATED FOREST Kindness 9-22

Lesson
•
9th - 12th Grade
22 questions
Adding Integers

Quiz
•
6th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
20 questions
US Constitution Quiz

Quiz
•
11th Grade
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade
Discover more resources for Computers
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade
20 questions
Analog vs Digital

Quiz
•
9th - 12th Grade
10 questions
CTEA Computer Vocab Terms #1

Quiz
•
12th Grade
10 questions
Exploring Cybersecurity Techniques and Threats

Interactive video
•
6th - 10th Grade
10 questions
Understanding the Internet and Data Transmission

Interactive video
•
7th - 12th Grade