NOTE: This was an 1 1/2 hour exam, and your exam will also be 1 1/2 hours. ------------------------------------------------------------------------ 1. The source code part1.c is translated into machine code, which is then linked with machine code from libraries to form an executable file a.out ------------------------------------------------------------------------ 2. (I wouldn't ask this question) a. stdio.h b. stdio.h c. ctype.h d. simpio.h ------------------------------------------------------------------------ 3. 0 + 0 != 1 .75, 0.00 ------------------------------------------------------------------------ 4. (note that it's bchar<'l', not bchar<'1' (small l, not one) 7 8 9 10 ------------------------------------------------------------------------ 5. x=z=1; while (x<=y) { z*=y; x++; } y^x (y to the xth power). Even simpler, it's y^y. For example, for y=5, the result is 3125, which is 5*5*5*5*5 ------------------------------------------------------------------------ 6. When evaluating expressions with logical AND (&&) or logical OR (||), the left expression is evaluated before the right, and the right expression is not evaluated if it cannot have any result on the expression. This is short-ciruit evaluation Example: To test that x is nonzero and it divides evenly into y: (book, p. 109) if ((x!=0) && (y&x==0) ) { Another, somewhat more bizzare, example: if ((done==0) || (x=3)) { ... } The statements within the brackets will always be executed! If done==0, then the left hand expression of || is TRUE, and so the whole expression is TRUE. If (done!=0), then the left hand expression of || is FALSE, and so the right hand expression of || is evaluated, which sets x=3 and evaluates as 3, which for the purposes of a boolean expression is TRUE (anything <>0 is TRUE) ------------------------------------------------------------------------ 7. a. TRUE b. TRUE c. TRUE d. TRUE e. TRUE (typo on exam, listed as f instead of e) for (e), note that !x=0, and !!x is !(!x) which is 1. for (d), it is extremely nasty to use double as expressions for && - I would never test on that. ------------------------------------------------------------------------ 8. a. Can't rewrite it as a switch statement, since y is not an integer-like variable. You can get it to compile by casting y to an integer: switch ((int)y) { case 0: printf("ZERO"); break; case 1: printf("ONE"); break; default: printf("NOT ZERO OR ONE"); } but this doesn't give the same results as the given if statement. For an input of 1.1, say, the above switch statement will print "ZERO" while the if statement will print "NOT ZERO OR ONE" b. switch (kid) { case freshman: case sophomore: case junior: case senior: printf("Undergrad"); break; case masters: case doctoral: printf("Grad"); break; default: printf("Not a student."); } ------------------------------------------------------------------------ 9. a. typedef enum {pawn=1,knight,bishop,rook,queen,king} pieceT; b. Note: there's a horrible typo in the given code fragment! It should be "chesspiece=GetInteger()", not "pieceT=GetInteger()" typedef enum {pawn=1,knight,bishop,rook,queen,king} pieceT; void main() { pieceT chesspiece; printf("Enter the piece value: "); chesspiece=GetInteger(); switch (chesspiece) { case pawn: printf("pawn"); break; case knight: printf("knight"); break; case bishop: printf("bishop"); break; case rook: printf("rook"); break; case queen: printf("queen"); break; case king: printf("king"); break; } } ------------------------------------------------------------------------ 10. myChar is set to 'h', the first character entered. Then the while loop is entered, and the condition is true until the the newline character is read. So the value of the character is assigned to myChar, and all the other characters entered up til the newline are read but then "thrown away". NOTE: This is a different form of the while loop - there is no block of statements, or even a single statement, after the conditional expression. Instead there's just a semicolon, indicating that there are no statements at all to execute. I don't think this is even discussed in the book. ------------------------------------------------------------------------ 11. Error 1: no semicolon after #define VALUE = 0 Error 2: no equals in #define VALUE = 0 (so the line should be #define VALUE 0) Error 3: = in (myInt=VALUE) should be (myInt==VALUE) Error 4: getchar(); after the printf shoudl be myChar=getchar(); Error 5: Does anybody see another error? The only thing I can imagine that they're referring to is the \n at the beginning of printf("\nEnter a char: "); but I'm not sure I would characterize that as an "error". ------------------------------------------------------------------------ 12. The problem doesn't say whether the integers can be negative. Assume they can be, to make it a bit harder than the hw problem. Here's one way to do it: #include #include "simpio.h" void main() { int max=0; /* this is not being set to 0 as a "default max", as in the hw, but only to follow good programming practice of always initializing variables, even if you don't think it's needed. */ int val,i=0; printf("Get Integer:"); max=GetInteger(); for (i=1; i<10; i++) { /* get the other 9 numbers */ printf("Get Integer:"); val=GetInteger(); if (val>max) { max=val; } } printf("The max is %d\n", max); } % a.out Get Integer:4 Get Integer:3 Get Integer:2 Get Integer:1 Get Integer:0 Get Integer:-1 Get Integer:-2 Get Integer:-3 Get Integer:-4 Get Integer:-5 The max is 4 %