Sail E0 Webinar

MCQs

Total Questions : 14 | Page 1 of 2 pages
Question 1. Consider the following recursive implementation used to convert a decimal number to its binary equivalent:
#includeint arr[31], len = 0;void recursive_dec_to_bin(int n){      if(n == 0 && len == 0)      {          arr[len++] = 0;          return;      }      if(n == 0)          return;        __________;      recursive_dec_to_bin(n/2);}int main(){     int n = 100,i;     recursive_dec_to_bin(n);     for(i=len-1; i>=0; i--)     printf("%d",arr[i]);     return 0;}Which of the following lines should be inserted to complete the above code?
  1.    arr[len] = n
  2.    arr[len] = n % 2
  3.    arr[len++] = n % 2
  4.    arr[len++] = n
 Discuss Question
Answer: Option C. -> arr[len++] = n % 2


The line "arr[len++] = n % 2 should be inserted to complete the above code.


Question 2. What is the time complexity of the above iterative code used to find the smallest and largest element in a linked list?
  1.    O(1)
  2.    O(n)
  3.    O(n2)
  4.    O(n3)
 Discuss Question
Answer: Option B. -> O(n)


The time complexity of the above iterative code used to find the largest and smallest element in a linked list is O(n).


Question 3. Consider the following recursive implementation to find the nth fibonacci number:int fibo(int n){     if(n == 1)        return 0;     else if(n == 2)        return 1;     return ________;}int main(){     int n = 5;     int ans = fibo(n);     printf("%d",ans);     return 0;}Which of the following lines should be inserted to complete the above code?
  1.    fibo(n "“ 1)
  2.    fibo(n "“ 1) + fibo(n "“ 2)
  3.    fibo(n) + fibo(n "“ 1)
  4.    fibo(n "“ 2) + fibo(n "“ 1)
 Discuss Question
Answer: Option A. -> fibo(n "“ 1)


The line fibo(n “ 1) + fibo(n “ 2) should be inserted to complete the above code.


Question 4. What is the time complexity of the above iterative implementation used to find the largest and smallest element in an array?
  1.    O(1)
  2.    O(n)
  3.    O(n2)
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> O(n)


The time complexity of the above iterative implementation used to find the largest and the smallest element in an array is O(n).


Question 5. What is the space complexity of the above recursive implementation to find the factorial of a number?
  1.    O(1)
  2.    O(n)
  3.    O(n2)
  4.    O(n3)
 Discuss Question
Answer: Option A. -> O(1)


The space complexity of the above recursive implementation to find the factorial of a number is O(1).


Question 6. Which of the following lines should be inserted to complete the following recursive implementation used to find the length of a linked list?#include#includestruct Node{      int val;      struct Node *next;}*head;int recursive_get_len(struct Node *current_node){      if(current_node == 0)        return 0;      return _____;}int main(){      int arr[10] = {1,2,3,4,5}, n = 5, i;      struct Node *temp, *newNode;      head = (struct Node*)malloc(sizeof(struct Node));      head->next = 0;      temp = head;      for(i=0; ival = arr[i];          newNode->next = 0;          temp->next = newNode;          temp = temp->next;      }      int len = recursive_get_len(head->next);      printf("%d",len);      return 0;}
  1.    recursive_get_len(current_node)
  2.    1 + recursive_get_len(current_node)
  3.    recursive_get_len(current_node->next)
  4.    1 + recursive_get_len(current_node->next)
 Discuss Question
Answer: Option D. -> 1 + recursive_get_len(current_node->next)


The line "1 + recursive_get_len(current_node->next) should be inserted to complete the above code.


Question 7. What is the output of the following code?void my_recursive_function(int n){    if(n == 0)    return;    printf("%d ",n);    my_recursive_function(n-1);}int main(){    my_recursive_function(10);    return 0;}
  1.    10
  2.    1
  3.    10 9 8 "¦ 1 0
  4.    10 9 8 "¦ 1
 Discuss Question
Answer: Option D. -> 10 9 8 "¦ 1


The program prints the numbers from 10 to 1.


Question 8. Consider the following recursive implementation used to find the length of a string:#includeint recursive_get_len(char *s, int len){      if(s[len] == 0)        return 0;      return ________;}int main(){      char *s = "abcdef";      int len = recursive_get_len(s,0);      printf("%d",len);      return 0;}Which of the following lines should be inserted to complete the above code?
  1.    1
  2.    len
  3.    recursive_get_len(s, len+1)
  4.    1 + recursive_get_len(s, len+1)
 Discuss Question
Answer: Option D. -> 1 + recursive_get_len(s, len+1)


The line "1 + recursive_get_len(s, len+1) should be inserted to complete the code.


Question 9. What is the output of the following code?#include#includestruct Node{     int val;     struct Node* next;}*head;int linear_search(int value){      struct Node *temp = head->next;      while(temp -> next != 0)      {            if(temp->val == value)            return 1;            temp = temp->next;      }      return 0;}int main(){     int arr[6] = {1,2,3,4,5,6};     int n = 6,i;     head = (struct Node*)malloc(sizeof(struct Node));     head->next = 0;     struct Node *temp;     temp = head;     for(i=0; inext = 0;           newNode->val = arr[i];           temp->next = newNode;           temp = temp->next;     }     int ans = linear_search(60);     if(ans == 1)       printf("Found");     else       printf("Not found");     return 0;}
  1.    Found
  2.    Not found
  3.    Compile time error
  4.    Runtime error
 Discuss Question
Answer: Option B. -> Not found


The condition in the while loop "temp->next == 0, checks if the current element is the last element. If the current element is the last element,the value of the current element is not compared with the value to be searched. So, even though the number 6 is present in the linked list, it will print not found.


Question 10. Consider the following recursive implementation of linear search:#includeint recursive_search_num(int *arr, int num, int idx, int len){     if(idx == len)     return -1;     if(arr[idx] == num)       return idx;     return __________;}int main(){      int arr[5] ={1,3,3,3,5},num=2,len = 5;      int indx = recursive_search_num(arr,num,0,len);      printf("Index of %d is %d",num,indx);      return 0;}Which of the following recursive calls should be added to complete the above code?
  1.    recursive_search_num(arr, num+1, idx, len);
  2.    recursive_search_num(arr, num, idx, len);
  3.    recursive_search_num(arr, num, idx+1, len);
  4.    recursive_search_num(arr, num+1, idx+1, len);
 Discuss Question
Answer: Option C. -> recursive_search_num(arr, num, idx+1, len);


The recursive call "recursive_search_num(arr, num, idx+1, len) should be added to complete the above code.


Latest Videos

Latest Test Papers