Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THE FOLLWING IS DEFINED FOR THE QUESTIONS 5-8 INCLUSIVE: struct entry { int num;

ID: 3570103 • Letter: T

Question

THE FOLLWING IS DEFINED FOR THE QUESTIONS 5-8 INCLUSIVE:

struct entry

{

    int num;

    entry * next;

};

entry * head, * cur, * pent;

int n = 5, cnt=0;

pent = new entry;

pent->num = 7

pent->next = NULL;

-------------------------------------------------------

5. for ( cur=head; cur!=NULL; curr= cur-> next)

{   if ( cur->num ==n)

                  cout << "hi" << endl;

}

If entry, cur, head and n are defined as earlier and head points to a linked list of numbers, the above code dispalys "hi",

A. Only for the first occurrence of the variable n in the linked list.

B. Only for the last occurrence of the variable n in the linked list.

C. For all occurrences of the viarable n in the linked list.

D. For each entry in the linked list.

------------------------------------------------------------------------------------------------------------------------------------------

6. for ( cur=head; cur!=NULL; curr= cur-> next)

{   

         if ( cur->num >= n)

            break;

        cnt++;

}

cout << cnt<< endl;

If entry cur,head, num and cnt are defined as earlier and head points to a linked list of numbers, the above code displays.

A. the total number of entris in the linked list if head is pointing to an order linked list.

B. the total number of entries which has a number greater than n.

C. The total number of entries which has a number greater than n if head is pointing to an order linked list.
D. the total nubmer of entries which has a nubmer less than n.

E. The otal number of entire which ahs a number less than n if head is pointing to an order linked list.

---------------------------------------------------------------------------------------------------------------------------------

7. pent-> next=head;

head=pent;

If cur, head and pent are defined as earlier code above and head points to a linked list, the code fragment in this problem,

A. add an entry at the head.

B. Removes an entry from the head.

C. removes an entry from the end of the list.

D. A and B statements are correct.

E. B and C statements are correct

8. head=head->next;

If cur, head and pent are defiend as earlier code above and head points to a linked list, the code fragment in this probelm,

A. Adds an entry at the head.

B. Removes an entry from head.

C. Both a and b statements are correct

D. Both a and b statements are inncorrect..

9. ALgorithm that finds the solution to a given probelm by reducing the probelm to smaller verisons of itself is called recursive algorithm.

A. True

B. False

10. Recurisve algorithm must have only one base case

A. True

B. False

11. Which of the follwing is true about he a base case

A. Is a case in whic hteh solution is obtained directly

B. Stops the recursion

C. Both A and B statemetns are correct

D. Both A and B statements are incorrect

12. Given the recursive function

void PrintArr ( con in arr [ ], int first, in last   0

   {

      if ( first > last)

        cout << "Done";

     else

       {

           PrintArr( arr, first + 1, last);

           cout << arr [first];

       }

}

which code segment below proces the same output as the follwing fucntion call?

                            PrintArr(arr,  0,   5);

A.   for ( i=0; i < 6; i++)

            cout << arr[i];

          cout << "Done";

B. cout << "Done";

         for (i = 0; i < 6; i++)

            cout << arr[i];

C. for (i=0; i < 6; i++)

        {

           cout << arr[i}

           cout << "Done"

D. for (i=5; i >= 0; i--)

            cout << arr[i}

         cout << "Done"

E. cout << "Done"

           for (i=5; i >= 0; i--)

                cout << arr[i];

13. Infinite recurison is the case here every recursive call resutls in another recursive call.

A. True

B. False

14. The following recursive function is supposed to calculate factoral of a number. It has no errors and will execute correctly

int fact (int num)

{

   if (num ==0))

     return 1;

else

          return fact (num -1);

}

A. True

B. False

Explanation / Answer

5.

If entry, curr, head and n are defined as earlier and head points to a linked list of numbers, the above code displays “hi” for all occurrences of the variable n in the linked list.

The correct option is C)

Explanation: This is because until the curr->num is equal to n, the condition is true for every occurrence of n in the list.

6.

If entry curr, head, num and cnt are defined as earlier and head points to a linked list of numbers, the code displays the total number of entries which has a number less than n.

Thee correct option is D)

Explanation: This is because as soon as the curr->num is less than num, all the numbers in the list will get displayed. If the number is greater than n, the break statement will be executed and will come out of the loop.

7.

If curr, head and pent are defined as earlier code and head points to a linked list, the code fragment in this problem add an entry at the head.

The correct option is A)

Explanation: This is because, the position of the head is shifted to the next of pent by the statement pent->next=head. The value of the head is assigned to pent using the statement head = pent.

8

If curr, head and pent are defined as earlier code and head points to a linked list, the code fragment in this problem removes an entry from the head.

The correct option is B)

9.

It is True that an algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself is called recursive algorithm.

The correct option is A) True

10.

It is false that a recursive algorithm must have only one base case. It may have many base cases.

The correct option is A). True.

11.

It is true about the base case that it is the case in which the solution is obtained directly and it stops the recursion also.

The correct option is C) Both the statements A and B are correct.

12.

The code statement that will process the same output as the above function call is given in option E)

Explanation: The given code is as follows:

void PrintArr ( con in arr [ ], int first, in last   0

   {

      if ( first > last)

        cout << "Done";

     else

       {

           PrintArr( arr, first + 1, last);

           cout << arr [first];

       }

}

·         The else part will be executed as the function call PrintArr (arr, 0, 5) is executed. This is because, first = 0 and last =5 and the if part is executed only when first > last.

·         Inside the else part, PrintArr (arr, first+1, last); is called. Call PrintArr( arr, 1, 5), then recursively call PrintArr(arr, 2, 5) -> PrintArray(arr, 3,5)->PrintArray(arr, 4, 5)->PrintArray(arr, 4, 5)->PrintArray(arr, 5, 5)->PrintArray(arr, 6, 5)->at this point first>last, so the if part executes and cout << “Done”; is printed. Then, follow the backtracking and it can be observed that we must back track from 5 to 0.

·         So, the correct option is as follows:

cout << "Done"

          for (i=5; i >= 0; i--)

               cout << arr[i];

13.

It is true that infinite recursion is the case where every recursive call results in another recursive call.

The correct option is A) True.

14.

The following code is wrong. It is false that the code will execute correctly. The error exists in the line return fact(num-1). The correct line will be num*fact(num-1).

The correct option is B) False.