CS-211 Fall 2017 Test 1 Version A Oct. 2, 2017 True/False Questions... Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If I code a C program in file myprog.c, then I execute that program by typing./myprog.c. The C program must be compiled first... the.c code is not executable. (b) T F : If the main function of a C program returns the value 372, then the operating system knows that something went wrong in the execution of that code. Any non-zero return from main indicates an error. (c) T F : In ASCII encoding, every printable character maps to a number between 0 and 255. (d) T F : If a signed short integer in C can hold values between -32,768 and 32,767, then an unsigned short integer can hold values between 0 and 32,767. An unsigned short integer can hold values between 0 and 65,535. (e) T F : The char data type in C may refer to either the ASCII code for a single letter, or a number between -128 and +127, or a list of 8 bits. (f) T F : The arguments to the main function always have the same values, no matter how the program was invoked. Arguments to the main function depend on what was specified on the command line when the program was invoked. (g) T F : When a function is invoked, C first evaluates the argument expressions, then copies the results of those evaluations to the function parameters, then executes the function, and finally replaces the invocation with the return value of the function. (h) T F : In C, the assignment statement x=3; evaluates to the value 3, but that value is typically discarded. (i) T F : In C, a switch statement is used to switch between sequential instruction evaluation, and evaluation of every other instruction. A switch statement allows selection among several case statements depending on a comparison of a single variable to several different values. (j) T F : In C, a while(1) {... loop is an endless loop unless the loop body contains a break keyword. 2. (10 points) For the following, put a check in front of the statements that are valid declaration statements and will not cause a compiler error. int x=7; int x73=#seventy-three; big y=13845723; char w=742; floatx y=7.93e10; static double global x=3.72e19; char firstinitial=65; float temperature=83.2 long pi=3.1414; double onethirdoften = 10.0/3; Page 1 of 5
3. (10 points) Given the following declare statements: int x=17; f loat IF ; char small ; Put a check in front of the statements that are syntactically correct, and will not cause a compile error. x = 18; IF = 6.79e3; x = /* 3/0 */ x + 2; IF = /* /* 3/0 */ x/0 */ + IF * 3; x = a * 2; char init = T ; small = 3 * 110; int while=1; // int if x=7 x = small = (x+3); 4. (10 points) Given the following declarations: int a = 1 3 ; int b=4; int c =2; float fx =1.3; float fy = 7.0; Determine the value of the following C expressions. Make sure you include a decimal point for float values, but leave a decimal point out for integer values. (a) 21 = a + b * c (b) 1 = fy < fx (c) 1 = a % c (d) 1 = b < (fx+3.0) (e) -7 = fy > 0? 7:-7 (f) 0.0 = fy + 7 (g) 0 = (a<0) * b (h) 4.0 = 12-8.0 (i) 1 = b 17 (j) 0 = b & 1 5. (10 points) What does the following code print when compiled and executed? int a = 1 3 ; int x = 0 ; i f ( a > 0) { x++; i f (++x > 1) { x++; i f ( ( a < 0 ) && (++x > 3 ) ) { x++; p r i n t f ( x=%d\n, x ) ; x=3 Page 2 of 5
6. (10 points) What does the following code print if it is invoked as stats(69,249)? (Hint: Very little arithmetic is required to answer this question!) void s t a t s ( int h i t s, int atbats ) { int battingaverage = ( h i t s / atbats ) 1000; p r i n t f ( Batting average i s %d\n, battingaverage ) ; Batting avaerage is 0 7. (10 points) What does the following snippet of code produce when compiled and executed? int a = 1 3 ; // a i s 13 a = a // + 3 ; / a= 21 / 2 ; int c = / a + / 3 + / 4 / + 12 / 13 / ; / i f ( a<30) / { p r i n t f ( a=%d, c=%d\n, a, c ) ; // e l s e { p r i n t f ( a i s too big \n ) ; a=26, c=12 8. (10 points) Given the following C code: int x=1; i f ( x>0) i f (2==x%3) x =2; else i f (0==x%3) x =3; else x =5; p r i n t f ( x i s %d\n, x ) ; What will get printed to the terminal when this code is executed? x is 5 Page 3 of 5
9. (10 points) Given the following code: int main ( int argc, char argv ) { i f ( argc!=3) { p r i n t f ( Invoke as : %s <width> <height >\n, argv [ 0 ] ) ; double width=a t o f ( argv [ 1 ] ) ; i f ( width <=0 ) { p r i n t f ( The width, %s, has an i n v a l i d value o f %f \n, argv [ 1 ], width ) ; double h e i g h t=a t o f ( argv [ 2 ] ) ; i f ( height <=0 ) { p r i n t f ( The height, %s, has an i n v a l i d value o f %f \n, argv [ 2 ], height ) ; double area=width h e i g h t ; p r i n t f ( Width=%f h e i g h t=%f area=%f \n, width, height, area ) ; return 0 ; (a) If this is invoked as./area 2.5 4.0, what will get printed to the terminal window? Width=2.500000 height=4.000000 area=10.000000 (b) If this is invoked as./area 1.7, what will get printed to the terminal window? Invoke as./area <width> <height> (c) If this is invoked as./area 1.6 garbage, what will get printed to the terminal window? The height, garbage, has an invalid value of 0.000000 (d) If this is invoked as./area 3.0 5.0inches, what will get printed to the terminal window? Width=3.000000 height=5.000000 area=15.000000 10. (10 points) Write a function in C that takes three doubles; a, b, and c; as parameters, and returns the sum of a and b divided by c as a double precision floating point number. If c is zero, return 0.0. Solution: double c a l c ( double a, double b, double c ) { return ( c! = 0. 0 )? ( a+b )/ c : 0. 0 ; Page 4 of 5
Question: 1 2 3 4 5 6 7 8 9 10 Total Points: 10 10 10 10 10 10 10 10 10 10 100 Bonus Points: 0 0 0 0 0 0 0 0 0 0 0 Page 5 of 5