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...
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...
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...
Given the following function: int bar(int n) {     if( n < 0 )         return...
Given the following function: int bar(int n) {     if( n < 0 )         return -2;     else if (n <= 1)         return 2;     else       return (3 + bar(n-1) + bar(n-2)); } What is returned by bar (4)? Show all the steps
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum of the digits of n. Use the following main function to test your code: int main() { int n, sn; cout << "Enter q to quit or an integer: "; while ( cin >> n ) { sn = sumofdigits(n); cout << "sumofdigits( " << n << " ) = " << sn; cout << "\nEnter q to quit or an integer: "; }...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
The text's array-based queue implementations use the fixed-front approach. Group of answer choices True False Recall...
The text's array-based queue implementations use the fixed-front approach. Group of answer choices True False Recall that within the LinkedQueue the front and rear variables are of type LLNode<T> holding references to the front and rear nodes of the underlying linked list, and the numElements variable is an int and holds the current size of the queue. Which of the following code sequences could be used to correctly implement the isEmpty operation? Group of answer choices return (rear != null);...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...