NPTEL Week 5: Basic Data Structures and Lists Solutions


Solution of Week 5
 Basic Data Structures and Lists NPTEL Online Course
1. Assume that the structure of a Linked List node is as follows:
 struct node{
     int data;
     struct node *next;
 };
 What does the following function print for a given Linked List which has elements in the order 1->2->3->4->5->6. 

void fun(struct node* head){
    if(head == NULL)
        return;
    printf("%d  ", head->data);   
    if(head->next != NULL )
        fun(head->next->next);
    printf("%d  ", head->data); 
}
 1 2 3 4 5 6
 6 5 4 3 2 1
 1 3 5 5 3 1
 1 3 5 6 4 2
ANSWER:option c)  
1 point
2. What is the output of the following program in C and C++. 
/*assume required libraries are added*/
int main(){
    printf("%d",foo(5));        /*comment the printf statement for C++ */
    std::cout<<foo(5);          /*comment the cout statement for C */
    return 0;
}
int foo(int k){
    if(k==1) return 1;
    else return k*foo(k-1);
}
Ignore any compiler warnings for the above program and choose the correct answers. 
 The C program prints the output as 120
 The C++ program prints the output as 120
 C program does not compile.
 C++ Program does not compile.
ANSWER: option a & d) The C program prints the output as 120 & C++ Program does not compile.



1 point
class alpha{
    private:
        int data;
    public:
     alpha()
            {data = 10;}
     alpha(int x)
            {data = x;}
     void increase(int x){
           data = data + x;
     }
     void decrease(int x){
           data = data - x;
     }
}a;

int main(){
alpha b(2);
     int X;  
     a.increase(10);
     X = a.data * b.data;
     cout<<X;
     return 0;
}

3. Assuming the required libraries are included, Which of the following statements is true about the above program?
 The program compiles correctly and prints 20 as output.
 The program compiles correctly and prints 40 as output.
 The program compiles correctly and prints 0 as output.
 The program does not compile.
ANSWER: option d) The program does not compile

1 point
4. What is the time complexity of deleting the first node of a linked list? Assume that only the head pointer is given to you.
State your answer based on the algorithm followed in the lecture Linked Lists.
 O(1)
 O(n)
 O(n2)

 O(log n)
ANSWER: option a)
O(1)

1 point
5. Let's say we define an ADT myInt. addition(*) and multiplication(+) are the left associative operations with corresponding symbols defined on myInt.The priority of symbols is + > * .  Then the value of the expression 2 * 3 + 4 * 5 is ____ (2,3,4, and 5 are of myInt datatype).
ANSWER:19

1 point
6. Abstract Data Types are dependent on their implementation.

 T
 F
ANSWER: option b) F

1 point
7. Consider the following segment of code, in which p1 points to 4th element in a List l(1st element refers to the first non-empty element) and p2 points to 6th element. temp is of ElementType. p1 and p2 are of type Position as defined in the lecture.
   temp = l.retrieve(l.p1); //retrieves Element at p1
   l.insert(temp, p2);      //inserts temp1, after p2
   l.delete(p1);            //deletes the Element at p1
Given that the initial List is abcdefgh, with character as ElementType, What will be resultant List after running the above program. Specify your answer as a single string.
ANSWER: abcefdgh

1 point
8. Assume that the List ADT is implemented using a Linked List. A private function is defined as part of the ADT as indicated below. Consider the following function in which P and Q are of type CellType*(as defined in the lecture). In addition, listHead points to the head of a list of integers(value of the list is of type integer).
CellType* operation (CellType *P){
      if(P==NULL){
          return P;
      }
      if(P->next==NULL){
          listHead=P;
      }
      else{
          Q=operation(P->next);
          Q->next=P;
          P->next = NULL;
      }
      return P;       
}
Specify which one of the following statements is correct.
 Calling this function with parameter as the head of the list "listHead", exchanges the first and last nodes of the list.
 Calling this function with parameter as the head of the list "listHead",exchanges the last two nodes of the list.
 Calling this function with parameter as the head of the list ‘listHead’, reverses the input list.
 None of the above.
ANSWER: option c) Calling this function with parameter as the head of the list ‘list Head’, reverses the input list.

1 point
9. The List ADT is defined as in the lecture. p1 and p2 are two instances of the List ADT that have been populated with elements of type ElementType.   Consider the following segment of code:

    List p1, p2;
    Position p;
    ElementType x;
    p = p2.first();
    while (p != p2.end()) {
          x = p2.retrieve(p);
          p1.insert(p1.end(), x);
          p = p2.next(p);
    }

Which of the following statements is correct?
 Replaces elements from List p1 with that from List p2.
 Appends the List p2 to the end of p1.
 Merges the Lists p1 and p2 and removes duplicates from it.
 It will lead to a segmentation fault.
ANSWER: Option b) Appends the List p2 to the end of p1.



No comments:

Post a Comment

Share