Search Header Logo

Unit 10 quiz topic 2

Authored by Michael Courtright

Computers

9th - 12th Grade

Used 1+ times

Unit 10 quiz topic 2
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

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

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?