In: Computer Science
The following questions are on recursion in C++ Programming.
I had some trouble with these questions, can you please help me? Thank you!
Consider the following recursive function
void funcEx8(int u, char v) //Line 1
{ //Line 2
if (u == 0) //Line 3
cout << u << " "; //Line 4
else //Line 5
{ //Line 6
int x = static_cast (v); //Line 7
if (v < 'A') //Line 8
v = static_cast (x + 1); //Line 9
else if (v > 'Z') //Line 10
v = static_cast (x - 1); //Line 11
cout << v << " "; //Line 12
funcEx8(u - 2, v); //Line 13 }
//Line 14 }
//Line 15 }
Identify the base case? Using line numbers
a. |
Lines 3,4 |
|
b. |
Lines 1 through 4 |
|
c. |
Lines 7,8 |
|
d. |
Lines 5,6 |
Consider the following recursive function
void funcEx8(int u, char v) //Line 1
{ //Line 2
if (u == 0) //Line 3
cout << u << " "; //Line 4
else //Line 5
{ //Line 6
int x = static_cast (v); //Line 7
if (v < 'A') //Line 8
v = static_cast (x + 1); //Line 9
else if (v > 'Z') //Line 10
v = static_cast (x - 1); //Line 11
cout << v << " "; //Line 12
funcEx8(u - 2, v); //Line 13 }
//Line 14 }
//Line 15 }
Identify the Recursive case? Using line numbers
a. |
Lines 5 Through 15 |
|
b. |
Lines 3,4 |
|
c. |
1,2 |
|
d. |
Lines 2,3 |
Consider the following recursive function
void funcEx8(int u, char v) //Line 1
{ //Line 2
if (u == 0) //Line 3
cout << u << " "; //Line 4
else //Line 5
{ //Line 6
int x = static_cast (v); //Line 7
if (v < 'A') //Line 8
v = static_cast (x + 1); //Line 9
else if (v > 'Z') //Line 10
v = static_cast (x - 1); //Line 11
cout << v << " "; //Line 12
funcEx8(u - 2, v); //Line 13 }
//Line 14 }
//Line 15 }
Valid or Invalid
funcEx8(26, '$'); is a valid call,
a. |
None |
|
b. |
Yes and No |
|
c. |
No |
|
d. |
Yes |
Consider the following recursive function:
void recFun(int u)
{
if (u == 0) cout << "Zero! ";
else
{
cout << "Negative ";
recFun(u + 1);
}
}
what is the output if recFun(8)
a. |
Infinite loop, nonegative |
|
b. |
5 |
|
c. |
0 |
|
d. |
8 |
Consider the following recursive function:
void recFun(int u)
{
if (u == 0) cout << "Zero! ";
else
{
cout << "Negative ";
recFun(u + 1);
}
}
. what is the output if recFun(0)
a. |
12 |
|
b. |
infinite loop |
|
c. |
8 |
|
d. |
zero |
Consider the following recursive function:
void recFun(int u)
{
if (u == 0) cout << "Zero! ";
else
{
cout << "Negative ";
recFun(u + 1);
}
}
. what is the output if recFun(-2)
a. |
8 |
|
b. |
Negative Negative Zero! |
|
c. |
-2 |
|
d. |
zero |
Consider the following recursive function:
void recFun(int x)
{
if (x > 0)
{ cout << x % 10 << " ";
recFun(x / 10);
}
else
if (x != 0)
cout << x << endl;
}
what is the output of the above statement if recFun(258)?
a. |
12 |
|
b. |
3 4 6 |
|
c. |
15 |
|
d. |
8 5 2 |
Consider the following recursive function:
void recFun(int x)
{
if (x > 0)
{ cout << x % 10 << " ";
recFun(x / 10);
}
else
if (x != 0)
cout << x << endl;
}
. what is the output of the above statement if recFun(7)?
a. |
7 |
|
b. |
8 |
|
c. |
8 5 2 |
|
d. |
12 |
Consider the following recursive function:
void recFun(int x)
{
if (x > 0)
{ cout << x % 10 << " ";
recFun(x / 10);
}
else
if (x != 0)
cout << x << endl;
}
what is the output of the above statement if recFun(36)?
a. |
5 3 8 |
|
b. |
12 |
|
c. |
6 3 |
|
d. |
2 4 5 |
Consider the following recursive function:
void recFun(int x)
{
if (x > 0)
{ cout << x % 10 << " ";
recFun(x / 10);
}
else
if (x != 0)
cout << x << endl;
}
what is the output of the above statement if recFun(-85)?
a. |
12 |
|
b. |
-85 |
|
c. |
8 5 2 |
|
d. |
85 |
Q1)
Answer is Option A
In Recursive Function the best case is when the recursive part didn't run and the function ends . So In the given function if u is 0 then it ends just by printing u so it is the best case
Q2)
Answer is Option A
Recursive Part starts from line 5 when if condition is False and it comes in else part and then it starts the recursion under else part .
Q3)
Answer is Option D
Function needs two parameters i.e. one Int and other is Char So we pass 26 as Int and '$' as char . So Yes it is valid call
Q4)
Answer is Option A
As In recursion we are incrementing u by 1 each time so it will never become 0 and recursion only ends when u is 0 . So It goes in infinite loop
Q5)
Answer is Option D
The passes argument is 0 so if condition i.e. u==0 is true so we print the output Zero and function ends
Q6)
Answer is Option D
Now as u <0 So first else is true so negative will print and function is call by making -2 + 1= -1 So Again else is true and negative printed . Now u =0 hence we print zero
So Output is Negative Negative Zero
Q7)
Answer is Option D
Lets see how function will run
We have x = 258
First x% 10 = 8 is printed and x = 258/10 = 25
Second x% 10 = 5 is printed and x = 25/10 = 2
At last x %10 = 2 and x= 2/10 = 0
Now Both If and else if is false so function ends
Hence Output is 8 5 2
Q8)
Answer is Option A
Similarly as we do in last part Again x = 7
So x % 10 = 7 and x /10 = 0
Now Both If and else if is false so function ends
Hence Output is 7
Q9)
Answer is Option C
Now x = 36
So x%10 = 6 and print 6 , x /10 = 36 /10= 3
x % 10 = 3 and print 3 , 3/10 =0
Now Both If and else if is false so function ends
Hence Output is 6 3
Q10)
Answer is Option B
Now as X<0 So If condition didn't satisfy and we go to else and as x is not equal to 0 so it is True and we print x . So -85 is printed
These are the answer for each of the question
If u like the answer do Upvote it and have any doubt ask in comments
Thank You