Indian Institute of Technology Patna Date: 20 May 2022 Midsem: CS102: Programming & Data Structure Duration: 2 hours Full Marks 80 (A) 10 20 30 40 50 60 70 80 90 100 (B) Garbage number is printed unlimited number of times(answer) (C) 10 30 50 70 90 (D) Compiler error Instruction: Q1-16, 5 marks each. 1 mark will be deduced for each wrong answer. All options should be written on the table provided in the last page. Evaluation will be based Q(3) What this code is trying to compute? only on that table. Assume #include < stdio.h > and other int main(){ required header files are included for all questions. No doubts int n,x,y=0,i,k,flag, *arr; would be entertained. Write suitable assumptions, if necessary. printf("enter value of n\n"); scanf("%d",&n); Q(1) Predict output of following code? arr=(int*)malloc((n-1)*sizeof(int)); for(i=0;i<(n-1);i++){ int main() srand(time(0)); { do{ int i,j,flag1=0,flag2=0,x; flag=0; int arr[8]={-1,3,-5,7,8,9,-2,-7}; x=rand()%n; for(i=0,j=7;i<8,j>=0;) x=x+1; { for(k=0;k<i;k++) if(arr[i]>=0) if(arr[k]==x) flag1=1; flag=1; else }while(flag==1); i++; arr[i]=x; if(arr[j]<0) printf("%d ",x); flag2=1; y+=x; else } j--; printf("\n%d\n",n*(n+1)/2-y); if(flag1 && flag2) return 0; { } x=arr[i]; arr[i]=arr[j]; (A) Program is trying to generate (n-1) random number arr[j]=x; and finding sum of those numbers i++; (B) Program is trying to generate (n-1) random numbers j--; in the range of 1 to n and try to compute missing flag1=0; number flag2=0; (C) Program is trying to generate (n-1) unique random } numbers in the range of 1 to n and try to compute } missing number (answer) for(i=0;i<8;i++) printf("%d ",arr[i]); (D) none of the above return 0; } (A) (B) (C) (D) 3 7 9 8 -2 -5 -7 -1 (answer) -1 -7 -5 -2 8 9 7 3 -7 -5 -2 -1 3 7 8 9 3 7 8 9 -7 -5 -2 -1 Q(2) Predict output of following code? void main() { int i, j=10; for(;;) { printf("%d ",i*j); i++; } } 1 Q(4) Predict output of following code? Q(7) Predict output of following code? int fun(int arr[], int n) { int res = 0, i; for (i = 0; i < n; i++) int main() { int n; for (n = 9; n!=0; n--) printf("n = %d", n--); return 0; } res∧ = arr[i]; return res; } int main(void) { int arr[] = { 5, 5, 7, 3, 7, 3, 9 }; int n = sizeof(arr) / sizeof(arr[0]); printf("%d", fun(arr, n)); return 0; } (A) 9 8 7 6 5 4 3 2 1 (B) Infinite Loop (answer) (C) 9 7 5 3 1 (D) 9 7 5 3 Q(8) Predict output of following code? int main() { int i = 0; for (i=0; i<20; i+=2) { switch(i) { case 0: i += 1; case 1: i += 2; case 3: i += 3; default: i += 4; break; } printf("%d ", i); } return 0; } (A) 4 (B) 3 (C) 7 (D) 9 (answer) Q(5) Predict output of following code? int main() { int x = 11, y = 7; x = x∧ y; y = x∧ y; x = x∧ y; printf(" x =%d, y=%d",x,y); return 0; (A) 16 21 } (B) 10 16 22 (answer) (A) x=7,y=11(answer) (C) 1 6 12 18 (B) x=11,y=7 (D) Compilation Error (C) x=18,y=4 Q(9) Predict output of following code? (D) x=4,y=18 loop loop loop int main() { int a[5] = {1,2,3,4,5}; int *ptr = a+2; printf("%d %d", *(ptr+2), *(ptr-1)); return 0; } Q(6) Predict output of following code? int main() { int i = 1024; for (; i; i >>= 1) printf("Hi"); return 0; } (A) Compilation error (B) Run time error (A) “hi” will be printed for 11 times (answer) (C) 5 2 (answer) (B) “hi” will be printed for 1024 times (D) 4 1 (C) “hi” will be printed for 10 times (D) Compile Time Error 2 Q(10) Predict output of following code? Q(13) Predict output of following code? int main() { int x=3,y=5,z; z=(x<y)?(y-x): (x-y); printf("z=%d",z); return 0; } (B) z=5 int main() { int x=3; do { printf("IITP"); x--; }while(x<7); return 0; } (C) z=2(answer) (A) IITP is printed unlimited number of times(answer) (D) z=-2 (B) IITP (A) z=3 (C) No output Q(11) What is the output of following code? (D) Compiler error int main() { while(True) { printf("IITP"); break; } return 0; } Q(14) Predict output of following code? int main() { for(int i=1,j=10;i<11;i=i+1) { printf("%d ",i++*j); } return 0; } (A) IITP (B) IITP is printed unlimited number of times. (A) 10 20 30 40 50 60 70 80 90 100 (C) No output (B) 10 30 50 70 90(answer) (D) Compiler error(answer) (C) 20 40 60 80 100 Q(12) Predict output of following code? int main() { int x=3; while(x=7) { printf("IITP\n"); break; } printf("CS102"); return 0; } (D) Compiler error Q(15) Predict output of following code? int main() { int x = 1, y = 2, z = 3; { int x = 10; float y = 20; printf(" x = %d, y = %f, z = %d \n", x, y, z); { int z = 100; (A) IITP (B) CS102 } (C) Compiler error } (D) IITP CS102(answer) return 0; } (A) x = 1, y = 2, z = 3 (B) x = 1, y = 2, z = 3 (C) x = 10, y = 20.0, z = 3 (answer) (D) x = 10, y = 20.0, z = 100 3 Q(16) Predict the value of variable ‘sa’ at the end of the program Question # Option #/Output 1 2 void foo() { 3 int a = 10; static int sa = 10; 4 a += 5; sa += 3; 6 5 7 8 printf("a = %d, sa = %d\n", a, sa); 9 } 10 void main() { system("cls"); int i; 11 12 13 14 for (i = 0; i < 10; ++i) foo(); 15 } 16 (A) 30 (B) Depends on compiler (C) 10 Name (in Capital): (D) 40 (answer) Roll No: Signature: Phone Number: Midsem, CS102, 20 May 2022 For Office Use: No. of No. of Total (80) 4 Correct A (1-16) Wrong A (1-16) *5 *-1