Sei sulla pagina 1di 16

koofer

^lK1,0007jBffl|E*tr-i'h B I Z R E A C h

Skander D>X

45

'Ml*,iU750Jj\im.\:i>c>'>X\t>Z
Exam 2 for CSCE 206 with Professor Hurley at A&M
Was this exam helpful? YES NO +0 Helpful Like 0 Favorite Report

Wo

description

Related D o c u m e n t s

Department of Computer Science and Engineering, Texas A & M University

CPSC-206 (501-512) Structured Programming in C Fall 2011-Exam 2 Question set A


Name: TA: UIN: Lab Section:

True/False Questions: 1. Square brackets are operators that increase the precedence of the operations inside them (F) 2 . E x p l ? e x p 2 : e x p 3 stands for i f Expl then (T/F) Exp2 else Exp3 (T)

3. The null character is represented by 7 0 ' 4. A for loop like the following: i n t x; f o r ( x =1 ; x < 3; x++){ (T)

} works the same as: int x=l; while(x<3){ x+ + ; } 5. ' d ' + ( ' Z ' - ' z ' ) is ' d ' (F) (T)

6. The following statements are different x= y / 3 - 4 * t e m p + l ; I S

X=(y/3)-4*(temp+1);

For the next 5 questions Choose correct answers (False(O) or True(l)) int i=3,j=4,k=l; double x = l . l , y=4.4;

Example
Num 7 8 9 10 11

i<j+k
Answer (T) (F) (T) (E) (T)

Answer is 1 (True)

Problem -i+5*j>=k+l x+k+7<y/k x!=y !i : j+4 x||i&&j-3

12. A break statement will exit all loops: the inner and the outer loops.

13. The default statement is a requirement of a switch statement otherwise there could be an infinite loop. F F

14. The code below will print "Howdy!" for 10 times. for(inti=0;i<=10;i^) printf("Howdy!\n"); 15. The code below is an infinite loop: int i = 2; while(i = 1)

i
int j = 10; } 16. If you declare a variable inside of an if statement, then you can only use that variable in that if statement. T

17. Every function MUST return a value. F 18. When a function is called by value, the formal parameter is implemented as a local variable. T 19. Function prototypes must include the return type, function name, parameter types and parameter names. F 20. A function must be placed before the function call, if no function prototype is provided before the call. T 21. As long as a function is not declared as void, it can be used as an operand in an expression. T 22. Two variables in different function can have the same variable name. T 23. You can have more than one return statement in a function. T 24. Function definitions can be nested. F 25. Local variables are stored on the run-time stack created upon entry into its block and freed 2

from memory upon exit. T Multiple Choice Questions: 26. The following two segment code is exactly same X=10; if(x>9) Y=100; else Y=200; (D) (T)

X=10; Y=x>9? 100:200;

J>

27. What is the output of the following segment code?

char a , b [ 2 ] , c = ' f ' ; c = ( c > = ' A ' &&c<=' F ' ) ? c - ( * a ' - : * A ' printf(w%c",c); A. B. C. D. E. C c F f None of the above

28.

What is the output of the following segment code? (B/D) i n t a; for(a=0;a<5;af+); printf("%d",a); A. B. C. D. E. 0123 01234 012345 5 None of the above (D)

29.

If x has the value 5, the expression ! ( ! x ) will evaluate to A. - 5 B. 5 C . a l o g i c a l FALSE


D. 1

E. none

of

the

above

30. Find the error in the following segment code

(d)

int a=0; if(a) { printf("t e s t \n"); }; else { printf("\ntest\n\n"); } A. B. C. D. E. Error at line 1 Error at line 2 Error at line 3 Error at line 4 None of the above

<- line 1 <- line 2 <r line 3 <- line4

31. What would be the output of the following code fragment? int m = 2; printf("%d %d", m++, ++m); A. B. C. D. E. 43 34 33 35 24

(E)

32. If all conditions of a series of if- else if statements are false and there is no else statement what happens? (D) A. The compiler requires an else statement for all if statements B. An infinite loop C. The computer reboots D. Nothing occurs and execution continues E. An error occurs when running the program 33. 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

34. If you nest one loop inside another what happens? A. a compiler error B. a runtime error

C. The inner loop completes every iteration of the outer loop D. The inner loop completes then the outer loop completes E. None of the above 35. What is the int main() { int x = 2; switch(x) { case 1: printf("5 "); case 2: printf("4 "); case3:printf("3"); case 4: printf("2 "); case5:printf("l"); default: printf("0 "); } return 0;
}

output

of

the

following

code?

A. B. C. D. E.

4 0 5 432 1 4 3 2 10 D

36. What is the output if one tries to compile and run the following code? int main() { for(x= l ; x < 3 ; x + + ) y = x; printf("%d%d ",x,y); return 0; } A. B. C. D. 1122 33 23 34 11 Compilation error

E. 1122 37. Which of the following loop statements execute the body of the loop first and then check the loop - continuation - statement to decide whether to continue loop? A A. do -while B. b. while C. c.for D. d. switch E. e. if 38. Which of the following types can be used with a switch? D A. int B. char C. float D. A a n d B E. None of the above 39. A while loop can be exited without the Boolean expression evaluating to false if A A. the break statement is executed B. the while loop body is empty C. a method is called in the while loop body D. an if statement's Boolean expression evaluates to false inside the while loop body E. a while loop cannot be exited if the Boolean expression evaluates to true 40. What is the output of the following fragment? A int i = 1; intj = l; while (i < 5) { i++; J=j*2; } printf("%d",j); A. B. C. D. E. 16 4 8 32 64

41. for loops contain 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

42. How many times will the statement be printed? int main() { int x = 0; do { printf("Hello World\n"); x++; }while(x=10); return 0; } A. B. C. D. E. can't say 10 1 infinite None of the above

C/D

43. All of the following can be used as the condition of a while loop except: A A. the return of a function with a return type of void B. the return of a function that returns an integer C. a scanf("%d", &variable); where variable exists D. a number such as 1 E. None of the above 44. How many iterations would the following while loop be executed? #include <stdio.h> int main(void) { int i = 0; while (i<10) { if (i<l) continue; if (i==5) break; i++; } } A. B. C. D. E. 1 infinite times 6 10 9 B

45. What is the output of the following program: B

intz = 100; while (z > 1 ) { z/=5; printf("%d ", z); } A. B. C. D. E. 20 4 20 4 0 20 95 0

46. What is the output of the following program: A inti, n=10; for (i=0; i<n; i++) { printf("%d", i); n=i*2; } A. 0 B. 123456789 C. 2468 D. 0137 E. Infinite loop 47. What is the return type of the following function? D char float2int(double x); A. int B. float C. double D. char E. None of the above 48. What is the output for the following segment of code? A i n t main()
{

x=l; while (x < 4) < p r i n t f ("%d", x ) ; ++x;


}

r e t u r n 0; 1

A. 1 2 3 B. 1 2 3 4 C. 2 3 4 D. 0 1 2 3 E. 0 1 2 3 4 49. What is the difference between the following w h i l e () and d o / w h i l e () code? A i n t i = 0;


do
{

i n t i = 0; while(i)

1
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 50. 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 b r e a k keyword. C. Using g o t o keyword. D. By reaching the closing }' of the function E. A & D 51. Which of the following is TRUE about functions? A A. v o i d is a valid return type B. You do NOT need to specify a return type C. v o i d CANNOT be used for a parameter D. v o i d is NOT a valid return type E. None of the above 52. A switch statement REQUIRES which of the following: 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 53. Say we want to write a function which has two integers and one double as arguments in that order. The function returns nothing. Which of the following can be the function prototype of this function? B
Study Stre:

A. i n t funA(void) { i n t a, b; double c;} B. void funcB(int a, i n t b, double c)

C. funcCfint a, b, double c ) ; D. void funcD(int, double, int); E. int a, b; double c;


54. Run the following program with the command line. % a.out Johns Hopkins

Chris D. 4 2 - Is the Confused be either. it's just C

Posted 4 mc Steven M.:|'rr because x to x, which i while (x=10) it is never 0 Chris D.: Th again, I thin ago

What will be the output?

on

#include <stdio.h> int main(int argc, char *argv[])


t

a < a
exit(1) ; } printf("argc = %d\n", argc); return 0;

Reply Connor E. Does anyc Posted 4 mc Steven M.:gr< of argumenl In this case, Hopkins whi count a.out! nnlv arnn =

A. Please type your last name or your first name. B. Please type your last name or your first name. argc = 1 C. Please type your last name or your first name. argc = 2 D. argc = 3 E. argc = 4 55. What is the output of the following program? B

#include <stdio.h> int power(int b, int n ) ; int main(void) { int b = 2, n = 3; printf("power(%d,%d) = %d\n", b, n, power(b, n) return 0; } int power(int b, i n t n) { int p; f o r (p = 1 ; n > 0; n ) p = p + b; return p;
10

} A. B. C. D. E. power(2,3) = power(2,3) = power(2,3) = power(2,1) = power(2,l) = 2 7 8 2 8

56. The function f is defined in below. What does void mean: void f() { } A. B. C. D. E.

After executing f, the function does NOT return any value. After executing f, the function does NOT return any more. After executing f, the function can return any type of value. After executing f, the function returns void type value. None of the above B

57. What is the output of the following program: #include <stdio.h> int main(void) { int i=3; while(i<10) I 1 if(i<6) {i+=2; continue; } else printf(" } A. B. C. D. E. } 6 78 8 9 10 5 6 7 8 9 10 6789 5 77 8

%d

",++i);

nmen

58. A "return 0;", when used in main, is roughly equivalent to what statement? A. endO; B. break 0; C. exit(0); D. continue 0; E. None of the above

11

59. A continue statement in a loop can result in equivalent execution as a break statement (ie the loop ends without another iteration and execution proceeds) C A. Always B. Never C. Sometimes D. It depends on the compiler E. None of the above 60. What is the output of the following program: B
int m, t; for (m=0; m<2; m++) for (t=0; t<5; t++) { printf("%d", t); if (t==3) break; }

A. B. C. D. E.

0123 01230123 0123401234 01234 None of the above

61. What is the output of the following program: E


#include <stdio.h> intf(int); int main(void) { intx=10; printf("%d ", x); x = f(x); printf("%d ", x); return 0; } int f(int t) { intx = 99; printf("%d ", x); return (t+1); }

12

A. B. C. D. E.

10 10 10 10 99 10 99 99 11 99 10 11 10 99 11

62 Functions must be: A. Declared B. Defined C. Both (A) and (B) D. Either (A) or (B) E. None of the above

63 Which is true of a function?

(i) Each function is a discrete block of code (ii) A function's code is private to that block. (iii) Variables defined inside a block are local to only that function. (iv) Local variables cannot hold its values between calls to itself.

A. All of the statements are true B. Only (i) is true. C. Only (i) and (ii) are true. D. Only (i) and (iii) are true. E. None of the above are true.

64. Global variables differ from local variables in the following manner?

A. Global variables take up memory the entire time of the program. B. Using a global variable where a local variable will do makes a function less general. C. Global variables can make the program more difficult to debug.

13

D. (A), (B) and (C) are all true. E. None of the above.

65. What's wrong with the following function? void function(int a) {//line 1 hit y,x=5; y=x>5?l:2; //line 2 (y)++; //line 3 x=++y + y++; //line 4 return x; } A. Error in line 2 B. Error in line 3 C. Error in line 4

D. Return type is declared as void, but the function returns a value E. None of the above

66. Identify the correct option with regard to the following code segment void funcl(int a) { int x; x=5; int func2(int x) { printf("%d",x); return (x+2); } }

A. This code segment will print the value of x B. This code segment will not compile C. The code will compile but may cause a run time error

14

D. Both (A) and (C) are correct E. None of the above

67. What is the return value of count when the function below finishes execution? Assume it was called with count(8): D int count(int a) // function was called like count(8): { int x = 1; x+=x; if(a % 3 ==0) return x; x+=x; if(a % 2 == 0) return x; x+=x; return x; } A. B. C. D. E. 1 2 3 4 None of the above

68. When a function is defined after the main function, a function prototype is required to be declared. This is for the following reason(s): D

A. To allow the compiler to know the number of parameters. B. To allow the compiler to know the type of parameters. C. Because it is an ASCII standard. D. Both (A) and (B) E. None of the above.

15

69. Assume we want to write a program that calculates the sum of two numbers. Fill in the blank: C public int getSum(int a, int b){ int sum = a + b; } A. B. C. D. return a; return return sum; return getSum;

E. don't need to put anything

70. How can we call function f given the function prototype: void f(int i, int k, int j); A. int a = f(l, 2, 3); B. int b = f ( l . 1,2,3); C. f(l,2, 3); D. f(l,2); E. Both A and C.

16

Al ASGA

Koofers is a proud member of the ASGA in support of academic integrity.

2013 Privacy Terms Contact Us Support Give Feedback

Potrebbero piacerti anche