You are on page 1of 10
[ESC 10%: Fundamentals of Computin End-sem Exam (22 Nov 2019) 1. This question paper contains 5 pages (10 fully printed sides of paper). Please verity 2. Write your name, roll number, department and section on every sheet of this oook\et. 3. Write your final answers neatly with a blue/black pen. Pencil marks may get smudged Q1. Write T or F for True/False (write only in the box on the right hand side) (20x1=20marks) We can check if a string is palindrome using a single queue implemented as an array, and no additional variables/pointers 7 We can reverse a string using a stack implemented as an array, and no additional | variables/pointers. If the input array is already sorted, then bubble sort will be faster than quick sort. have copiedicheated in one or more components of this course and there is a | non-zero probability that | might have been caught If a static variable has been initialized as 0 and its value has been modified one or more times afterwards, it is not possible to reset/reinitialize its value to 0. 6 | 'break;' statement can only be used inside loops. Given a list of numbers stored in a singly linked list and a pointer ONLY to the 7 | node containing one of these numbers (except the last one), we can't delete this number without using loops. 8 | Increasing the hash table size does not affect the frequency of collisions. Using recursion without base condition will result in the program running forever but not give a runtime error. If we use the middle element of the input array as pivot, then quick sort won't be slow (i.e., won't have O(N*2) time complexity) 11 | Bitwise operators cannot be used with float values. 12 | Given a singly linked list with N nodes, we can reverse it in O(N) time. Both void func(int al][4]) and void func(int alj[)) are valid function declarations while passing an array a[5]/4] to func. 14 | Suppose n = 5. printf(ceil(n/2) == 3) will print 1 In case of mutual recursion (@.g., two or more functions calling each other), itis necessary to declare the function declarations/prototypes 16 | We can find the size of a circular linked list with only one poirter 17 | We cannot declare and define a function within a structure in G | 18 | Ina sorted array, hashing will always perform better than binary search. laa | Pass by reference is not possible without using pointers (note that arrays are also | pointers) 4) a}4] YF) A] 4] 7] A] Ap }5 IEEGEFT | | 20 | What a while loop can do can also be done by a for loop. Page 2 of 10 Q2. Fill the circle (don't tick) next to the correct option (only one choice correct).(6x2=12marks) 2.1 Output of the code on right will be int main() { int al] = (1,2,3,4,5,6); for(int i = 0; a < 1; i++) No output, Segfault for(int j = 0; 3 <= a; 344) ji printé ("$d8e", 3[a],j==(4-1)?'\n': ! return 0; D | 4:2:3:4:5 RB No output. Compilation err 2.2 What will be the output of the code below? void £(int* x) { x= x + 1;} t O44 z ; S int main int bl] = (1,2,3,4,5}: |B} : Q £(b); c 3 oO printf ("¢d\n", b[0]); ‘Dp! return 0; 7 1 oO t 2.3 The file “exam_solutions.txt” does not exist in the current directory. Still, you try to run a C code with this statement: “FILE* ptr = fopen("exam_solutions.txt’,"r");”. What will happen? [| It will result in a runtime error. QO B | It will result in a compile time error. Oo | C | ptr will have the value NULL, and the execution will continue normally. ® [D [A file will be created with the name “exam_solutions txt’, and ptr will point to it ° 2.4 Which of the following algorithms canbe 2.5 Suppose we had two macros defined as solved using recursion, and not iteratively? _—_"#define SUM(a,b) a + b" and “define MULT(c.d) c*(d)". What is the value A | Quick Sort © of ‘a’ after this statement: B | Merge Sort © ‘a= MULT(SUM(,2) SUM(10,20))" [¢ | Binary Search in a sorted array | a] 150 © D | None of the above @ B63 @ c| 43 @) ke eae 2.6 You are given a set A of n distinct integers. They are stored in a sorted array S and also in a hash table T. Which of the following is correct? Finding the smallest integer in A greater than 100 will take O(n) time using S and | O(n) time using T Finding the smallest integer in A greater than 100 will take O(1) time using S and O(n) time using T. Finding the smallest integer in A greater than 100 will take O(log(n)) time using S and O(1) time using T. | Finding the smallest integer in A greater than 100 will take O(log(n) time using S Land O(n) time using T A B c dD O@®0CO ESC 101: 2 Nov 2019) | 100 marks Page 3 of 10 Q3. Fill in the circles next to ALL CORRECT options (multiple may be correct).(5x3=15marks) 3.1 Mark the options that are true for the code below. void f(int *p){ | int main(){ Alp is passed by value |@ int q=10; int x=205 Halse otpasssalbytwreravancalceli@) pia int *p © &; B|p is passed by reference ¥ #(P)5 C| Output is 10 O print#("%d",*p)3 D|output is 20 3.2 Mark the options that are true for the code below int main(){ [a|Gives run-time error char p20]; char *s = "string"; int i, length = strlen(s); for(i=0;i int count = 0 int fib(int n) { count++; if (n <= 2) return 1; return fib(n-1) + fib(n-2) + Fib(n-3); A|For n it prints 1, 2 |C) [B|For n= 3, it prints 3,4 C|For a= 5, it prints 9, 12 |() D/£ib(9) returns 193 O > int main() { int nj scanf("Kd", &n); printf("%d, ", #ib(n)); printf("%d", count); Page 4 of 10 Q4 in the space provided, write down the output of the program when given the input indicated. All test cases have equal weight 44 | ant n; | scang("8d", 6) ; int a=0; while (n) { 61) *1; printé("%a",a); xeturn 0; y nt main() { (3+5+4+-445=21marks) input | Outpu ® |9ee uw | oO 53 0 65 0 4.2 [aint £2 (int a,int b); int f1(int a,int b){ if(b>a) return 0; if (b22) return £2 (a,b); if('b) return 1; } int £2(int a,int b){ if(b>a) return 0; if(!b$2) return £1 (a,b); if(!b) return 1; return £1(a-1,b)+£1(a-1,b-1) ; return £1(a-1,b)+£1(a-1,b-1);—|/113 3 int main() { int a,b; scanf ("$d %d", 6a, 6b) ; print£("$d",£1(a,b)) return 0 Input 104 g Output typedef struct node{ int a; struct node *b; } node; 43 void £1(node *a,node *b) { a->b = bs ) node £2(node +a) { | printf ("%d->",a->a) ; ) return a; £2(a->b) ; int main() ( node arr[10]; for(int i=0;i<10:i++) { arr[i].a=i; arr[i] .b=sarr [i]; y for(int i=0;i<9;i++) £1 (Sarr[i],garr[itl]); £2(garx[4]); print£("\n") ; £2 (Garx(0]); return 0; Output h-y5—> 6-74-9879 O73 474 2 Nov 2019) | | 100 marks Page 5 of 10 4.4] int main() ( char pat [100] ; int M, lps[100], len = 0, i = 1; scanf("$d", 6M); Input output scanf("ts", pat) ; Ips[0] = 0; 1 01012012346 print£("td ", 1ps[0]); while (i = almid]) Me = wide else hij ps mid —1 } return ho o : // should return low or high? int lower_bound(int a[], int n, int key) { int low = 0, high =n, mid; while(low < high) { mid = (low + high) / 2; if (key <= almid]) kigls = wd : else lop ser =widy) } return rv : // should return low or high? ) int count_elements(int a[], int n, int key) { // returns the number of times key occur in a[] (a is sorted) return upper_bound(a, n, key) - lower _bound(a, n, key); ESC 101: Fundamentals of Computin: End-sem Exam (22 Nov 2019) | Name 100 marks Roll No Page 7 of 10 5.2 In this problem, given a linked list, your aim is to complete a function that reverses the linked list k nodes at a time. For example, given a linked list 1->2->3->4->5->6->7->NULL and k=3, the new linked list should be 3->2->1->6->5->4->7-»NULL. So as you can see this linked list has been reversed 3 nodes at a time. You are given only the structure of the linked list and the function Reverse_k which you have to complete. The arguments of the Reverse_k function is the head pointer of the linked list and the number k. Assume that there are functions to push values to the linked list and print the linked list. Also, assume that the main function exists, and you won't get any compilation error. struct Node { int data; struct Node* next; Ve struct Node *Reverse_k(struct Node *head, int k) { struct Node *current = head,*next, *prev = NULL; int count = 0; // This while loop reverses first k elements. while ( current !5 NULL && count dLake yaa : untae Sik Peake = beoay : Ua = Wd nox} count++; } NULL) // Use Reverse _k function recursively if(current ! current =4,(ovence _\ {head if (head != NULL) // Set the new head edd= Wud _buxt; return prev; Page 8 of 10 5.3 The given code takes an integer n (denoting the number of elements), another integer k (denoting the required sum), and n positive integers of a set. It is supposed to tell us whether a subset exists wl Input format: nk hemp % 10; Aoife! 18 | dnweaLlol = cher( Cony + ea'0'): lage! \S. | ndtv --- END OF EXAM

You might also like