Indian Institute of Technology, Kharagpur Computer Science and Engineering Department Class Test I, Autumn 2012-13 Programming & Data Structure (CS 11002) Full marks: 30 Feb 7, 2013 Time: 60 mins. Name Roll No. Section 1. Answer all questions in the space provided. No extra sheet will be provided. 2. First 10 questions carry 1 point each and the next 10 questions carry 2 points each. 1. PB-1 Which one of the following data types requires minimum storage space and which one requires the maximum storage space for representation? char, int, float, double min: char, max: double 2. PB-1 What will be displayed when the following program executes? #define LOW 0.9876 int x = 100*LOW; printf("low=%f, x=%d\n", LOW, x); LOW=0.987600, x=98 1 2 mark each. 3. PB-1 What will be displayed when the following program executes? int x = 1, y=2; printf("%d, %d\n", x/y*100, 100*x/y); 0, 50 1 2 mark each. 1
4. PB-1 Write the unsigned binary representation for the decimal number 7.25, clearly showing the decimal point. 111.01 If the whole part (111) is correctly written but the fractional part is incorrect, then 1 mark should be awarded. 2 5. PB-1 What will be displayed when the following program executes? int i; for (i=0; i<10; i+=3) ; printf("i=%d\n",i); 12 6. RM-1 What will be displayed when the following instruction executes: printf("%d\n", C - F ); -3 7. RM-1 How many times will the for loop of the following C program execute and what will be the output of the program? main(){ int i; for(i=10; i=20; i++) printf("%d, ",i); Infinite number of times. Output: 20, 20, 20,... 8. RM-1 The following code segment contains an error. Underline the part that is erroneous and write only that part corrected in the following blank space. 2
int a=27; int b=11; int c=35; if (a<b<c) printf("a, b, c are sorted in ascending order.\n\n"); else printf("a, b, c are not sorted in ascending order.\n"); (a<b && b<c) 9. RM-1 What will be the output produced by the following program segment? int x=2, y=17, z=11, result=5; result -= x/5 * z % 13 *y / 3 * x; printf( "%d\n", result ); 5 10. RM-1 What will be displayed after the following code segment executes? char c= r ; switch(c){ case b : printf("blue \n"); case r : printf("red \n"); case g : printf("green \n"); break; case y : printf("yellow \n"); default: printf("other \n"); Red Green 11. AB-2 What will be printed after the following code segment executes? int total=1, num=5; while (num>1) total = total * --num; printf ("%d\n", total); 24 3
12. AB-2 Write the missing part of the for statement so that the following code segment displays the same output as in the previous question (Question 11)? int total=1, num=5; for ( ) /*write the missing code*/ total = total * --num; printf ("%d\n", total); ( ; num>1; ) Extra variables should not be introduced. One may get at most 1/2 marks out of 2 if he/she uses extra vars. 13. PB-2 What will be displayed when the following program executes? int i = 1; int c = c ; while (++i<5) { c++; printf("%c-",c+i); printf("\n"); f-h-j- 1 mark if one writes fhg (hyphens monitted), 1 1 2 marks if one writes f-h-j (last hyphen omitted). 14. PB-2 The following program is to first read an integer n. Then it has to determine the highest perfect square s that does not exceed n. Fill up the missing code parts. int n, s = 1; printf("enter a +ve int: "); scanf("%d\n",&n); while ( ) { /*write the missing code*/ s++; ; /*write the missing code*/ printf("max square <= %d = %d\n\n", n, s); 4
(s*s<=n) s=(s-1)*(s-1); 1 mark for each. 15. PB-2 For the unsigned 8-bit binary number 1010 0110, write the equivalent decimal and hexadecimal numbers. 166, A6 16. RM-2 What value will be printed after the following loop executes? int i,j; for (i=j=0; i<10; i++) j = i++ + j; printf("%d\n", j); 20 17. RM-2 Consider the following code segment that is intended to compare the fractional parts of two floating point numbers (num1 and num2) and print the one having the larger fractional part. Some parts of the program segment are missing (indicated by dotted line). Fill up the indicated missing parts. float num1=12.314, num2=7.589; if ( >= ) printf("number having larger fraction is= f\n", num1); else printf("number having larger fraction is= f\n", num2); (num1-(int)num1) >= (num2-(int)num2) 18. RM-2 Determine how many times the word Here will be printed. count1 = 1; while (count1 < 10) { count2 = 2; while (count2 <= 20) { printf("here\n"); 5
count2++; count1++; 19*9=171 19. RM-2 Write down the output of the following program. #include <stdio.h> int i=3; printf("%d\n", (--i + 3)); printf("%d\n", (i++ + 10)); /* end main */ 5 and 12 1 mark each. 20. RM-2 In the following code segment, if the user enters any character other than Y or N, then the program should display: You must type Y or N (for yes or no). However, the program does not work as desired, as there is a mistake in the conditional expression of the if statement. Write the correct expression in the empty box provided. char response; printf("please enter your response Y/N (for yes or no)\n"); scanf("%c",&response); if (response!= Y response!= N ) /* please correct */ printf("you must type Y or N (for yes or no)\n");!(response == Y response == N ) or (response!= Y && response!= N ) RM, AB, PB CSE Deptt., IIT KGP 6