0% found this document useful (0 votes)
92 views4 pages

C Exam13

This document contains a C programming exam with multiple choice and free response questions testing knowledge of C programming concepts such as data types, operators, functions, control structures, and more. The free response section includes writing code to calculate factorials and series, find the equation of a line given two points, represent mathematical equations in C, simulate a guessing game, and generate triangular patterns of numbers.

Uploaded by

gauravsoni1991
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views4 pages

C Exam13

This document contains a C programming exam with multiple choice and free response questions testing knowledge of C programming concepts such as data types, operators, functions, control structures, and more. The free response section includes writing code to calculate factorials and series, find the equation of a line given two points, represent mathematical equations in C, simulate a guessing game, and generate triangular patterns of numbers.

Uploaded by

gauravsoni1991
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 C Programming Exam I Student No: Part 1: Multiple Choice (72 points - 3 points per question) (D) 1.

Which statement about C comments is true? (A) Good comments may improve execution-time performance. (B) A linker is used to translate a high-level language program into machine instructions. (C) Lines beginning with a # are processed at execution time. (D) none of the above (B) 2. Which type of errors is omitting dention of a variable? (A) semantic (B) syntax (C) linkage (D) execution (B) 3. Which includes functions involving memory allocation, process control, and conversions? (A) <stdio.h> (B) <stdlib.h> (C) <io.h> (D) none of the above (A) 4. Which produces an alert sound? (A) \a (B) \n (C) \t (D) none of the above (D) 5. Which of the following is false? (A) switch is a multiple selection instruction (B) & is an address operator. (C) list1 is an valid identier. (D) ?: is a binary operator. (C) 6. Consider the declarations: char c = \xc; int i = 8; Give the value and the data type of the expression c * i. (A) \140, char (B) 96, char (C) \x60, int (D) none of the above (B) 7. Which statement is true? (A) == is an assignment operator. (B) % has the lower precedence than ++. (C) ?: associates from left to right. (D) none of the above (A) 8. a, b, c, and d are 4 integers and and a = 3, b = -6, c = 2. What is the value of d after running d = a-- - b % c * c++ + --a;? (A) 5 (B) 6 (C) 7 (D) none of the above (C) 9. Which interation structure will do at least once? (A) for (B) while (C) do ... while (D) none of above (B) 10. Which data type cannot be used in the expression for a switch statement? (A) char (B) oat (C) unsinged (D) none of the above (B) 11. If x = 6 and y = -8, what is the value of x for x *= x % y > x - y ? --x : y++? (A) -42 (B) -48 (C) 64 (D) none of the above (C) 12. Which can be used to convert a upper case letter ch to a lower case? (A) ch = ch + A - a (B) ch -= z + Z (C) ch = ch - Z + z (D) none of the above (D) 13. Which is a double selection structure? (A) if (B) when (C) switch (D)if ... else (C) 14. Which is a multiple selection? (A) if ... else (B) while (C) switch (D) none of the above (D) 15. What is the value of i after running i = 1; while (i++ <= 10);? (A) 9 (B) 10 (C) 11 (D) none of the above (D) 16. Assume a = 6 and b = 8, which of the following is true? (A) a > b && b > a % b (B) b * b > a - b && a/b > a % b (C) a > a % b (D) none of the above (D) 17. Which is the conversion specications for a short integer in C? (A) %u (B) %h (C) %d (D) none of the above (C) 18. Which is equivalent to the expression if (n != 8)? (A) if !(n = 8) (B) if (n > 8 && n < 8) (C) if (n - 8) (D) none of the above (A) 19. Which function is used to seed a new random number sequence? (A) srand (B) seed (C) rand (D) none of the above (C) 20. Which is a benet of functions? (A) Reduce programming errors (B) Make a program more ecient (C) Avoid code repetition (D) none of the above (A) 21. Which function will return 8.0? (A) oor(8.6) (B) round(8.6) (C) ceil(8.6) (D) none of the above (B) 22. Which function returns remainder of two oating-point numbers? (A) div() (B) fmod() (C) remain() (D) none of above (A) 23. How to generate a random number between -a and a? (A) -rand() % (a + 1) + rand() % (a + 1); (B) -rand() % a + rand() % a; (C) a - rand() % a; (D) none of the above (A) 24. What value does function sum return when called with a value of 5? (A) 13 (B) 33 (C) 39 (D) none of the above int sum (int n) { if (n < 1) return 1; else return n + sum(n - 1) + sum(n - 2) - n; } Part 2: Questions and Answers (54 points) 1. (a) (5 points) Write a function that passes n and return n!. (b) (5 points) Write the main function that reads x and n and invokes the above function to calculates the following series: x x2 2! + x3 3! xn n!. Ans: Name:

double fac(int n) { int i; double f = 1; for(i = 1; i <=n ; i++) f = f * i ; return f ; } main() { int i, n; double x, s, t; printf("Enter x, n: "); scanf("%lf%d", &x, &n); for (i = 1, s = x, t = -x * x; i <= n; i++, t *= -x) s += t * fac(2 * i); printf("The sum of series is %f.\n", s); } 2. (8 points) Consider the following code that reads two points and shows the equation of the line passing these two points. main ( int x1, y1, x2, y2, d, m, c; print( Enter the two points: ); scan("%lf %lf %lf %lf", x1, y1, x2, y2); if (x2 - x1 <> 0.0) ( m = (y2 - y1)/(x2 - x1); c = (x2 x y1 - x1 x y2)/(x2 - x1); print( The line equation is y = %lfx + %lf.\n , &m, &c); ) else if (y2 - y1 <> 0.0) c = (x1 x y2 - x2 x y1)/(y2 - y1); print( The line equation is x = %lf.\n , &c); else print("These two points cannot form a line.\n"); ) Ans: main() { double x1, y1, x2, y2, d, m, c; printf("Enter the two points: "); scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2); printf("The two points are (%lf, %lf) and (%lf, %lf)\n", x1, y1, x2, y2); d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); printf("The distance between two points is %lf\n", d); if (x2 - x1 != 0.0) { m = (y2 - y1)/(x2 - x1); c = (x2 * y1 - x1 * y2)/(x2 - x1); printf("The line equation is y = %lfx + %lf.\n", m, c); } else if (y2 - y1 != 0.0) { c = (x1 * y2 - x2 * y1)/(y2 - y1); printf("The line equation is x = %lf.\n", c); } else printf("These two points cannot form a line.\n"); } 3. (10 points) Represent the following two equations in C programming language. (a) (3 points) y = (b) (3 points) x = (c) (4 points) x = Ans: (a) y = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)). (b) x = (-b + sqrt(b * b - 4 * a * c))/(2 * a). (c) x = fabs(c)/sqrt(a * a + b * b) * exp(t). 4. (8 points) Write the result after executing the following programs. (x1 x2 )2 + (y1 y2 )2 .
b+ b2 4ac . 2a c t e . Hint: a2 +b2

fabs is a function for the absolute value.

(a) (4 points) main() { int w = 0, x = 1, switch(x) { case 1: x *= 2; case 2: w = x + default: z += w } printf("w = %d, x } (b) (4 points) main() { int a = 3, b = -6, c; c = a++ * b--; c *= --a - ++b; c = c % 3 ? a++ * --b : --a * b++; printf("a = %d, b = %d, c = %d\n", a, b, c); } Ans: (a) w = 5, x = 2, y = 3, z = 3 (b) a = 2, b = -5, c = -12 5. (8 points) Write a program that simulates the game of guess the number as follows: Your program chooses a number between 1 and 10 and let you guess until you guess right. A possible session may look like: Guess a number between 1 and 10: 8 Too high. Try again: 4 Too low. Try again: 6 Too high. Try again: 5 Excellent! You guessed the correct number! Ans: int main() { srand(time(0)); int number = rand() % 10 + 1, guess; printf("Guess a number between 1 and 10: "); do { scanf("%d", &guess); if (guess > number) printf("Too high. Try again: "); else if (guess < number) printf("Too low. Try again: "); else printf("Excellent! You guessed the correct number!\n"); } while(guess != number); } 6. (10 points) Write a program that reads n and generates a triangle as follows. A possible run may look like: Enter 2: 212 2 Enter n: 3 32123 323 3 Ans: int main() { int n, i, j; printf("Enter n: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j < i; j++) printf(" "); y = 2, z = 3; y = w++ + z; y; break; % x ; = %d, y = %d, z = %d\n", w, x, y, z);

for (j = n; j >= i; j--) printf("%d", j); for (j = i + 1; j <= n; j++) printf("%d", j); printf("\n"); } }

You might also like