Docsity

Midterm Exam 2 for Structured Programming C | CSCE 206, Exams of Computer Science

Toggle sidebar
bg1
CSCE 206 (506-510) Structured programming in C
2012 Spring - Exam 2
Form A
Name: ___________________________ UIN: ___________________________
TA: ___________________________ Section: ____________________________
True/False questions:
1. Suppose int i=3,j=4,k=1; What is the answer for -i+5*j>=k+1 T
2. The default statement is a requirement of a switch statement otherwise there could be an infinite loop.
F
3. X || Y && Z is the same as (X || Y) && Z F
4. A function declared as void may contain a return statement that specifies a value. F
5. Function prototypes must include the return type, function name, parameter types and parameter
names. F
6. A function must be placed before the function call, if no function prototype is provided before the call.
T
7. The following code: T
do {
X = 3;
Y = 2;
}
while (X = 1);
will result in an infinite loop.
8. Using a continue in a for loop skips all code in between it and the end of the loop including the
increment. F
9. Every function MUST return a value. F
10. Expression “printf(“%d”, ‘a’);” is invalid in C language because ‘a’ is of type char. F
11. The magnitude of the number that an int variable can hold is the same on every machine. F
12. Compiler will give error information while compiling if integer overflow exists in the program.
F
13. An unsigned int variable has a sign bit too. But this bit is not used at all in this variable. F
14. Since floating point types have more storage, floating arithmetic operations are usually exact. F
15. Functions getchar() and putchar() do not return value. F
16. x= a/5-4*b+1; and X=(a/5)-4*(b+1); are the same F
17. The following function parameters are declared correctly F
void function(int a,b,c, float d);
18. The code below will print “Howdy!” for 10 times. F
for(int i=0; i<10; i++);
printf("Howdy!\n");
19. A real numeric constant is by default type double. F
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Exam 2 for Structured Programming C | CSCE 206 and more Exams Computer Science in PDF only on Docsity!

CSCE 206 (506-510) Structured programming in C

2012 Spring - Exam 2

Form A

Name: ___________________________ UIN: ___________________________

TA: ___________________________ Section: ____________________________

True/False questions:

  1. Suppose int i=3,j=4,k=1; What is the answer for -i+5*j>=k+1 T
  2. The default statement is a requirement of a switch statement otherwise there could be an infinite loop. F
  3. X || Y && Z is the same as (X || Y) && Z F
  4. A function declared as void may contain a return statement that specifies a value. F
  5. Function prototypes must include the return type, function name, parameter types and parameter names. F
  6. A function must be placed before the function call, if no function prototype is provided before the call. T
  7. The following code: T do { X = 3; Y = 2; } while (X = 1); will result in an infinite loop.
  8. Using a continue in a for loop skips all code in between it and the end of the loop including the increment. F
  9. Every function MUST return a value. F
  10. Expression “printf(“%d”, ‘a’);” is invalid in C language because ‘a’ is of type char. F
  11. The magnitude of the number that an int variable can hold is the same on every machine. F
  12. Compiler will give error information while compiling if integer overflow exists in the program. F
  13. An unsigned int variable has a sign bit too. But this bit is not used at all in this variable. F
  14. Since floating point types have more storage, floating arithmetic operations are usually exact. F
  15. Functions getchar() and putchar() do not return value. F
  16. x= a/5-4b+1; and X=(a/5)-4(b+1); are the same F
  17. The following function parameters are declared correctly F void function(int a,b,c, float d);
  18. The code below will print “Howdy!” for 10 times. F for(int i=0; i<10; i++); printf("Howdy!\n");
  19. A real numeric constant is by default type double. F
  1. EOF is a MACRO in file stdio.h defined as -1. T

Multiple choice questions:

  1. ANSI C allows how many case labels in a switch statement C A. at least 5 B. at least 100 C. at least 257 D. at least 1000 E. 1024
  2. What is the resulting value of x from the following code: “x = 10? 50 : 100;” D A. 10 B. 150 C. 100 D. 50 E. 60
  3. What is the output of the following code? B int main() { int x = 2; switch(x) { case 1: printf("5 "); case 2: printf("4 "); case 3: printf("3 "); case 4: printf("2 "); case 5: printf("1 "); default: printf("0 "); } return 0; } A. 4 3 2 1 B. 4 3 2 1 0 C. 2 D. 4 E. 0
  4. What is printed to the screen when the following code is executed? E char h = ‘r’; printf(“%c”, h + 5); A. The ASCII value for the character w B. The ASCII value for the character m

return 0; } A. d B. e C. f D. g E. h

  1. What is the output of the following segment code? C int i,k,j=14; i=(j==14)?12:13; k=(i>j)?i:j; printf("%d",k); A. 12 B. 13 C. 14 D. 25 E. None of the above
  2. What is the output of the following segment code? D char a,b[2],c=’h’; c=(c>=’A’&&c<=’H’)?c-(‘a’-‘A’):c; printf(“%c”,c);

A. C B. c C. H D. h E. None of the above

  1. What would be the output of the following code fragment? E int m = 3; printf(“%d %d”, m++, ++m);

A. 5 4

B. 4 5

C. 4 4

D. 4 6

E. 3 5

  1. A switch statement REQUIRES which of the following: A A. an expression to test the cases against B. a break after every case C. a break at the end of the switch statement to exit D. a default statement at the end E. all of the above are not required
  1. What is the output of the following program: D int main(void) { int i=11; switch(i) { case 9: i+=1; case 10: i+=1; case 11: i+=1; case 12: i+=1; case 13: i+=1; } printf(“%d\n”, i); return 0; } A. 11 B. 12 C. 13 D. 14 E. None of the above
  2. The value of i, j, k are 5, -5, 5 respectively. Which of the following expressions will return a logical FALSE: C

A. i && j && k B. i || j || k C. !(j-5) D. i<j || i==k && j<k E. i

  1. Which of the following statements execute a body of the code first and then check if repetition is needed? A A. do –while B. while C. for D. switch E. if
  2. for loops contain : (select the most complete answer) E A. a boolean expression and a body B. an initialization statement and body C. an update statement and body D. an initialization statement, boolean expression, and body E. an initialization statement, boolean expression, incrementation statement, and body

return 0; } A. 1 2 3 B. 1 2 3 4 C. 2 3 4 D. 0 1 2 3 E. 0 1 2 3 4

  1. What is the difference between the following while() and do/while() code? A int i = 0; int i = 0; do while(i) { { printf("%d\n",i); printf("%d\n", i); }while(i); }

A. The do/while() loop prints "0" and the while() loop doesn't print anything. B. Both print "0" C. The do/while() loop does not print anything and the while() loop prints "0" D. Both loops do no print anything E. Both are infinite loops

  1. Which of the following is(are) way(s) for a function to finish? E A. By returning a value using the return keyword. B. Using break keyword. C. Using goto keyword. D. By reaching the closing ‘}’ of the function E. A & D
  2. Which of the following description is FALSE? E A. As long as a function is not declared as void, it can be used as an operand in an expression. B. The return statement can be coded more than once within a function. C. The } that ends a function causes the function to return, the same as a return without any specified value. D. The built-in arguments, argc and argv, are used to receive command line arguments. E. The return statement always causes immediate termination of the entire program.
  3. Here are two C functions which calculate factorials. Given the same argument, they should return identical values. The only difference is that factorial is a non-recursive function and factorialR is a recursive function. Please complete the factorialR function so that it performs the same job as factorial. Choose one of the following codes to fill the empty line. D

/non-recursive/ int factorial(int n) { int i, answer; answer = 1;

for(i = n; i >= 1; --i) answer = answer * i; return answer; }

/* recursive */ void factorialR(int n) { int answer; if (n==1) return 1;

/* recursive call */ ______________________ // fill out this line

return answer; }

A. answer = answer * n; B. answer = factorialR(n-1); C. answer = factorialR(n); D. answer = n * factorialR(n-1); E. answer = n * factorialR(n);

  1. Which of the following operators is used to find the number of bytes needed to store an object? C A. space(object) B. strlen(object) C. sizeof(object) D. bytes(object) E. $(object)
  2. Which of the following input can terminate the program attached in UNIX system? D

#include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF){ putchar(c);} return 0; } A. Ctrl + ‘O’ B. Ctrl + ‘x’ C. “EOF” D. Ctrl + ‘d’

D. double E. A and B

  1. What is the output of the following code? A

include <stdio.h>

int main(void) { char c = 'h'; printf ("%c", c + 1); printf ("%d", c + 2); return 0; } A. i B. j C. i D. j E. k

  1. What is the output of the following program? B

#include <stdio.h>

/* Assume we use ASCII character set*/ int main(void) { char a = 'J', b = 'F', c = 'M', d;

printf("%d ", a - 'A' < 0 || a - 'Z' > 0); printf("%c\n", (d = a > b? a : b) > c? d : c);

return 0; }

A. 0 F

B. 0 M

C. 1 F

D. 1 J

E. 1 M