Download as pdf
Download as pdf
You are on page 1of 13
UNIVERSITY OF TORONTO FACULTY OF APPLIED'SCIENCE AND ENGINEERING FINAL EXAMINATION, APRIL 27, 2009 APS 106 - FUNDAMENTALS OF COMPUTER PROGRAMMING Examiners: L. Shu, V. Sinnathurai Student Last Name (print): Student First Name (print): Student signature: Student Number: DO ALL WORK IN THIS BOOKLET, AND DO NOT SEPARATE THE PAGES IMPORTANT NOTE #1: If your C code program contains errors, part marks will be awarded for a correct algorithm. Marks will be awarded for correctness of your algorithm, C syntax, adherence to recommended C coding practice, code efficiency, and the clarity of your program. IMPORTANT NOTE #2: For all parts of this exam, unless specified, you may assume that required libraries have been tinclude’d, and that a system(“pause") is not required. Closed book (Type C) - one 8.5 x 11 yellow aid sheet allowed NO calculator. Time allotted: 2 hours and 30 minutes (3. minutes per mark) le lo ro S & ln lou Ie iO IZ Question Maximum Mark Actual Mark 1. Multiple choice 12 IL. What's the output 15 Tl. Find the errors 5 8 IV. Write a function V. Write a function 10 Total 50 Page 1of 13 [You must write the UPPER case letter corresponding to your choice in the box provided to obtain marks! 1. Multiple-choice questions (12 marks) You must write the UPPERCASE letter corresponding to the correct choice in the box'to obtain marks. 1. The expression \(a | 1b) is equivalent to which of the following expressions? (all b) (ta) | (tb) (ta) && (tb) . Ha &eée b) (a Ib) && (a && b) mone Write UPPERease letter corresponding to choice in box: 2. Which of the following statements about variables in C is true? A. A variable must be declared before it is used. B. The same variable name cannot be used in two different functions. C. A local variable cannot have the same name as a global variable. D. All of the above are true. E. None of the above are true, Write UPPERcase letter corresponding to choice in box: 3. Consider the following code segment: switch (x) { case 10: y: break; case 20: case 30: y='b’; break; default: y-’c’; Which of the following code segments is equivalent to the one shown above? A if (x==10) y="; B e D. if (x<=10) y="a’y else if (X>=20 && x<=30) y else y='c’; : EB if (x==10) y='a’; © else if (x>=20 &é x<=30) y: else y='c’; Write UPPERcase letter corresponding to choice in box: Page 20f 13 jin marks! the box provided to obtai ice in [You must write the UPPER case letter corresponding to your cho 4. Consider writing a function whose sole purpose is to write an error message using printf. Which of the following best characterizes the choice between making the function's return type void and making it int? A. The retum type should be void because the function performs an operation and does not compute a value. The return value type should be int because that is the default return type in C. The return type should be void because void functions are more efficient than int functions. The return type should be int because int functions are more efficient than void functions. The retum type should be void because the function does not take any arguments. moos Write UPPERcase letter corresponding to choice in box: 5. Which of the following is true about function prototypes / definitions? A. They must be provided, or the function will not compile. B. They must specify the function return value type. C. They must specify the names of all parameters passed into the function. D. A and B above E. Allof the above Write UPPERcase letter corresponding to choice in box: 6. Consider the following code segment: if (n>0) n= -n; if (n<0) n =0; This segment is equivalent to which of the following? A. n=O; B. if (n>0)n C. if (n<0) n=0; D. if (n>0) n=n; else E. if (n<0) n=0; else n Write UPPERcase letter corresponding to choice in box: 7. Which of the following cannot be done at compile time? A. Determine whether are any calls to a function that violate its prototype/ definition. B. Determine whether there are any case labels that are not inside a switch statement. C. Determine whether there are any variables that are used before being declared. D. Determine whether there are any open curly braces with no matching closing curly brace. E. Determine whether there are any division by zero errors, Write UPPERcase letter corresponding to choice in box: Page 30f 13 the box provided to obtain marks! vice in [You must write the UPPER case letter corresponding to your cho 8. A programmer wants to include an if statement in a program and is trying to choose between the two versions shown below. (Note that the only difference between the two versions is that Version Tis all on one line, While Version 2 is broken up into several lines.” Version 1 if ((x>0) 11 ( ‘y) )ixtey; y" Version 2 if ( (20) 11 (xeey)) { xteyi yinyi i { yeti Which best characterizes the choice between the two versions? A. Version 2 must be chosen; because Version 1 has several statements in one line, it will not compile. B. Version 1 should be chosen; because the code is all on one line, the compiled code will be more efficient than the compiled code for Version 2 C. Version 2 should be chosen; because the code is broken up into several lines, the compiled code will be more efficient than the compiled code for Version 1. D. Version 2 should be chosen; although the compiled code for Version 1 will be more efficient, Version 2 is easier to understand, and that is more important than the efficiency of the compiled code. E. The compiled code will be equally efficient for both versions; however, Version 2 should be chosen because the code is easier to understand. Write UPPERcase letter corresponding to choice in box: 9. Consider the following 3 code segments, each of which is intended to define and initialize variable k: Segment | Segment II Segment II] int k= int i k tn = t 10 k ‘ 10 Which of the code segments will compile without error? Tonly Yand Ironly - Land Ill only Mand Ill only L land Il mMOIOB> Write UPPERease letter corresponding to choice in box: Page 4 of 13, the box provided to obtain marks! ice in [You must write the UPPER case letter corresponding to your cho 10. Consider the following code segment involving initialized integer variables x and y; Which of the following statements is true? The final value of x is the same as the initial value of x. The final value of x is the same as the initial value of y. The final value of y is the same as the initial value of x. The final value of y is the same as the initial value of y. Itis not possible to say anything about the final values of x and y without knowing their initial values. moORD> Write UPPERease letter corresponding to choice in box: Questions Ti and 12 refer to the following information: Assume that N is an integer constant, that k is an integer variable and A is an array of integers. Consider the following expression (Ck <=N) && (Ak) = 0)) 11 (Ak] == 0) 11. Under which of the following conditions must the expression evaluate to true? ‘A[K]is not equal to zero. Ak] is equal to zero. kis less than N. kis less than or equal to N. none of the above. one Write UPPERcase letter corresponding to choice in box 12. Assume that A contains N+1 values. Recall that an out-of-bounds array index causes a run- time error. Which of the following statements is true? A. Evaluating the expression will never cause a runtime error, B. Evaluating the expression will cause a runtime error whenever A[k] is zero. C. Evaluating the expression will cause a runtime error whenever A[k] is not zero. D. Evaluating the expression will cause a runtime error whenever k is equal to N. E. Evaluating the expression will cause a runtime error whenever k is greater than N. Write UPPERcase letter corresponding to choice in box: Page 5 of 13 Question II - Write your answers in the boxes, “1, (2 marks) Find the output of the following code: int main() 4 int A(3] = (5, 6, 7); int D[3}'= (0, 0, 07 int i= 0 do { D[i] = Ati] - itt; printé("éd ", D{i-1}); } while (+#i1<=3); return 0; 2. (3 marks) Find the output of the following code: void runningSum(int A[]) ( int i, Sum = 0; for(is0; i<3; itt) ( Sum += ALi]; ALi] = Sum; ) , int main() ( int A(3] = (1,2,3); int 4; for (i=0; i<3; itt) ( runningSum (A) ; printe ("3d ", Ali]); ) return 0; Page 6 of 13 3. (2 marks) Find the output of the following code char * prnAndGo(char * p) { int N= tp - '0'; printé ("td ", N); return +#p; int main() char * msg = “Score: Canada-5; England-4."; char *ptr = msg; while (*ptr != '\0") { if(*ptr>'0' g& *ptr<'9') ptr=prnandGo (ptr) ; else ptr++; , return 0; 4, (2 marks) Consider the following functions: void Modify(int x, int ty) { x= ty; vy = ty #10; ) void Mystery(int a, int b) { Modify (a, &b); print£("td $d", a, b); ) ‘What is output as a result of the call Mystery(1, 2)? 5, (1 mark) If addition had higher precedence than multiplication, the value of the expression: 2*344"1 would evaluate to what value? Page 7 of 13 6. (1 mark) Consider the following function: int” Compute (int x, int y, int z) { 4 (x==0) ( af (y= ) else z+=4; return 2; d What value is returned by the call Compute(0, 1,0)? 7. (2 marks) What is the output of the following code? void £0) { static int g=1; gt 2; printf(“td ", g); ) int main() ( £0; £0; return 0; 8. (2 marks) What is the output of the following code? int g=10; void £0) { int g=1; gt 2: print£("8d ", 9); , int main() { £0; printé("8d ", g); return 0; Page 8 of 13 ‘spiomu uiniaa 1 {pion dara [esspionulspioj Maas “eww CHINN == [SpiOME)sp:0RN) "(Gepooss, sept po eut = [apiONspIO,K, 0s [4 SerGpapon dua t Se { {Se = [easmqpalpion Guy ya (osm pion duaas [Te -spuomulspiog yan tame: (TION == [spi0me)sPIOAM) J jomu)spiog elpiow dua) 0 = spuomu Saeqou i TPN W'S, MPRMIO AT IH Tau aah on CHET Yo] OF uly W Spon w pap! TONS aq! ‘p05 Uo AfooHp IOUS WaT “POD Powe 1 oF 1S aouureur papuayurun ue ut aaeyaq 0} wreafoud ayp asneD ing “wor RD—XKa JOU! ynoaxe SuLINp yse9 0} wesBord ayy asneo KELL IN “UL “apoo aamnos a idiwoo s9y30u yuaaasd siossa 2180“ {ui u@naid youop stoma aum}-umy]-Z jouoneyidwon ywanaid sioaa xeuds ° :tueaflosd 3 e ut siozs9 Jo sadAy aanyy aze 22044 ‘po ‘ze suumnjoa ysour yy Ss 49] a4) Ut SzaqumU aut] ay, “sIMB20 30329 ayy axoY| JaqUINU BUT] ay) dOUIO}O_E wwies8oad ay ut soz19 Ayuo ayp 2194 9 iapuiadaput so1s0 Y>e9 year], “uun ayy ut (218030 ‘aun “aumjoo yxau axp ur Si03s9 ay) aquDsap “uumnjod apoo ay UM aPa> 247 UO ALIDOIIp SiON axp XY ‘9]qeL MO]Iq aU UT “SION G sUTEIUOD ULEASOU 2 ;Papnpput are sa]!j zapeay papaau [je aunssy “poyeunuaay [Im os[e a2 Yo! ‘p1OM Yova 10} SoLIOWTeW saye>0I[° 94,1 Su] sxajpexeyp o¢ ueY) 210W OU a3e sp2om Jey) ue ‘2oeds aTSuNS e Aq payezedas aze spuom ey) “ZaIDeIeYD 2B2eds ay) pu 519119} A[uO sUTE}UDD BuIZIs 94) 44] aumssy “punoy spiom jo requ ay suargas pur ‘SuLz}suanr8 e Woy spzom jo Kvsse Ue sayBaID ye LOTR B 40} 9095} mOjag [ SHAEUH ¢] “[]] Won, Question IV. [8 marks ] Ina game similar to’Addemup, a randomly generated xow x col matrix A of integer numbers, | or 2 is generated. This matrix grid can be printed using a simple print-function print. This matrix is then sent to another function, SumThemUp, that would calculate the sum of all the values around each number in the grid. This sum is converted to hexadecimal base and placed in the corresponding position of another row x col matrix C of characters. The resulting matrix grid can be printed by a second print-function, printC. The following two @ x @ grids are one of such instances: 01020121 34868643 12222110 4BAASADT Oh aiaeou2 te11 2) 69BEBAT2 11121200 —+> 6BDABBAS 12220211 7BDBEB93 12212202 spcg997c4 22100202 9c9797B4 22001111 67523463 The pre-processor definitions of rows and columns, the prototype for the three functions and the random matrix generation and the function calls in the main function are given below: fidefine rows & fidefine cols 8 void printA(int A[][cols]); void printC(char C[][cols]); void SumThemUp(int A[] [cols], char C{]{cols]); int main() ‘ srand (time (NULL)) ; int Alrows] [cols] ; char C[rows] [cols] ; int i, 3; for (i=0; i

You might also like