Question

Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...

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

Homework Answers

Answer #1

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 :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Q1: Thefollowing code is supposed to return n!, for positive n. An analysis of the code...
Q1: Thefollowing code is supposed to return n!, for positive n. An analysis of the code using our "Three Question" approach reveals that: int factorial(int n){ if (n == 0)     return 1;   else     return (n * factorial(n – 1)); } Answer Choices : it fails the smaller-caller question.     it passes on all three questions and is a valid algorithm.     it fails the base-case question.     it fails the general-case question. Q2: Given that values is of...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num > 10)             return num;         else             return num + recurMethod(num / 4);     } And the initial call: int answer = recurMethod(320); list the values that will be returned from each call to the previous call of recursMethod. Start with the last recursive call, and work backwards, with one space between each returned value, ending with the value returned to the original...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following questions using the list below. You can record your answers in the essay textbox. data = [5,3,7] A. Write code to replace the value in the 0 position with the number 8. B. Add the value 10 to the end of the list. C. Insert the value 22 after position 1 in the list. D. Remove the value at position 2. E. Sort the...
please explain how does the following C code work. a) double ldexp(double x, int exponent) is...
please explain how does the following C code work. a) double ldexp(double x, int exponent) is a C math function that returns x multiplied by 2 raised to the power of exponent. How many narrowing and how many promotions happen automatically in the following code line? Name them one by one and explain each one separately. float f = 2.2f * ldexp(24.4, 3.0 * 2) - 4.4; b) int function1(int num1, int num2) { int num = num1 ^ num2;...
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
Given the following code snippet, what is this program calculating? for (int i = 0; i...
Given the following code snippet, what is this program calculating? for (int i = 0; i < n; i++) { sum += arr[i]; } sum /= n;
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
Answer the following questions: 1. Given the code segments below with n as the problem size,...
Answer the following questions: 1. Given the code segments below with n as the problem size, answer the following questions: //Code Segment 1 (Consider n as a power of 3) int sum = 0; for(int i = 1; i <= n; i = i*3) sum++;​​​​​​​// statement1 //Code Segment 2: (Consider both possibilities for x) if(x < 5){ for(int i = 1; i <= n; i++)    for(int k = 1; k <= i; k++)    sum++;​​​​​// statement2 } else{ for(int...