Q1:
Given the following code, what is returned by tq(4)?
int tq(int num){
if (num == 0) return 0;
else
if (num > 100) return -1;
else
return num + tq( num – 1 );
}
Group of answer choices:
0
4
-1
10
Q2:
Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)?
int mystery(LLNode<Integer> list)
{
if (list.getLink() == null)
return list.getInfo();
else
return mystery(list.getLink());
}
Group of answer choices:
returns sum of the numbers on the values list
returns the last number on the values list
returns 0
returns how many numbers are on the values list
Q3:
Given that values is of type LLNode<Integer> and references a linked list (possibly empty) of Integer objects, what does the following code do if invoked as mystery(values)?
int mystery(LLNode<Integer> list)
{
if (list == null)
return 0;
else
return 1 + mystery(list.getLink());
}
Group of answer choices:
returns 0
returns sum of the numbers on the values list
returns the last number on the values list
returns how many numbers are on the values list
Q4:
What does the following return, when passed the argument 3?
int example(int n)
{
if (n == 0)
return 0;
else
return example(n – 1) + n * n * n;
}
Group of answer choices:
0
29
36
27
Q1.
Here, we have n = 4, the result would be 4 + 3 + 2 + 1 + 0 = 10.
Therefore, correct choice is option d).
Q2.
In the given code we iterate the link until we reach the last node which has no link. Therefore, the given code returns the last number on the values list.
Therefore, correct choice is option b).
Q3.
In the given code, we are adding 1 until we reach a null node. Therefore, the given code returns how many numbers are there on the value list.
Therefore, correct choice is option d).
Q4.
For n = 3, we will get 3*3*3 + 2*2*2 + 1*1*1 + 0 = 27 + 8 + 1 + 0 = 36.
Therefore, correct choice is option c) 36.
Hope this resolves your doubt.
Please give an upvote if you liked my solution. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.