Unit 10 quiz topic 2

Unit 10 quiz topic 2

9th - 12th Grade

9 Qs

quiz-placeholder

Similar activities

Single Array

Single Array

11th Grade

10 Qs

Skill Development - Debugging Practice1

Skill Development - Debugging Practice1

10th Grade - Professional Development

10 Qs

Вказівник

Вказівник

12th Grade - University

12 Qs

C++ Recursion

C++ Recursion

8th - 12th Grade

12 Qs

AP CSA Recursion

AP CSA Recursion

9th - 12th Grade

10 Qs

Recursion in Java

Recursion in Java

9th - 12th Grade

10 Qs

AP CS A Unit 6 Quiz PRACTICE

AP CS A Unit 6 Quiz PRACTICE

9th - 12th Grade

10 Qs

Recursive Functions in Python

Recursive Functions in Python

12th Grade

10 Qs

Unit 10 quiz topic 2

Unit 10 quiz topic 2

Assessment

Quiz

Computers

9th - 12th Grade

Hard

Created by

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.