CS-211 Fall 2017 Test 1 Version Practice For Test on 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 : In mathematics, we learned that every integer, n has a successor, n + 1, that is greater than n. This is also true in the C language. In C, integers must have a type, which controls the number of bits used to represent that number, and since the number of bits is finite, there are limits on the size of an integer that can be represented in C. (b) T F : A single C code file, compiled on different machines, can produce different results. The C compiler translates the C code to the machine language of the machine it is running on. While the intent of the C code is normally followed, there are enough differences in machine languages so that it is impossible to guarantee that a program will run the same on different machines. (c) T F : If aliens from outer space prevent all computers from typing (or cut and pasting) the character (right curly brace), then no one could write any new C code. In C, almost all statements must be in a function, and it is impossible to create a function without using a right curly brace. (d) T F : Lines in C code that start with a # (pound sign) are pre-processor directives, and do not follow the normal C syntax requirements. (e) T F : All variables in C have a name, a value, a type, and a location in memory. (f) T F : There are four different types of integers in C so that we can save memory. Use char for small integers, short for medium size integers, int for normal integers, and long for larger integers. (g) T F : A C statement must fit on a single line. Multiple statements can appear on one line, or one statement may span multiple lines. (h) T F : If the left hand side of an assignment is a char, the value of the right hand side is truncated to 8 bits. (i) T F : It is legal to invoke a function as long as that function is declared and defined in the same C program file, even if the declaration comes after the invocation. A function must be declared before it can be invoked. (j) T F : Almost all code in C is contained inside functions, where a function has a name, a return type, a parameter list, and a function body. 2. (10 points) For the following, put a check in front of the statements that are are valid constant expressions. 32 768 9934 0x1ABd 6.23e22 so called 0340721 x 0b729 CS-211 4.729 Page 1 of 5
3. (10 points) For the following, put a check in front of the statements that are valid declaration statements. int i = 37 295; int j=1,024; float pi=3.1514; double break=5.0; float fraction=7; unsigned short width=-3; int = a ; double bignumber=inf; float constant=3.5e0x16; int num cookies=093; 4. (10 points) Given the following declare statements: int num=17; f loat area ; char l i t t l e ; Put a check in front of the statements that are syntactically correct, and will not cause a compile error. num=12 * height; area = num * 12.835e0; little = 1.43e7; if (12.3>=area) then printf( Not that big. ); do num++ while (num<32); area * 13 = 772.6; area = width times height; little = (12 % num) num; for(num>6; ++num; num=-num) num - -; littler=little-1; 5. (10 points) Given the following declarations: int a = 7 ; int b = 3 ; int c = 4 ; float fx =2.5; float fy = 1.1 e0 ; 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) 10 = a + b (b) 10.0 = c * fx (c) 7 = (c = 7) (d) 1 = a % 3 (e) a = fx 0? a : b (f) 1 = b 17 (g) 40 = (a+b) * c (h) 3 = b 1 Page 2 of 5
6. (10 points) What does the following code print when compiled and executed? int a = 1 3 ; int b = ++a ; i f (++b < ++a ) { p r i n t f ( Condition 1\n ) ; else { i f ( b > a ) { p r i n t f ( Condition 2\n ) ; else { i f (++b < a++) { p r i n t f ( Condition 3\n ) ; else { p r i n t f ( Condition 4\n ) ; p r i n t f ( a=%d, b=%d\n, a, b ) ; Condition 4 a=15, b=15 7. (10 points) What does the following code print when compiled and executed? char a = 0 b01000100 ; int pos =0; char mask=0b00000001 ; while ( mask ) { i f ( mask & a ) { p r i n t f ( Bit %d i s on\n, pos ) ; mask <<=1; pos++; Bit 2 is on Bit 6 is on Page 3 of 5
8. (20 points) Answer the questions below based on the following C program: #include <s t d i o. h> / Round up the usual s u s p e c t s / #include <s t d l i b. h> int nextfactor ( int n ) ; int main ( int argc, char argv ) { i f ( argc!= 2) { // I f not e x a c t l y one argument p r i n t f ( Invoke as : %s <number>\n, argv [ 0 ] ) ; return 1 ; // q u i t with an e r r o r return code int n = a t o i ( argv [ 1 ] ) ; // Save the u s e r s argument as i n t e g e r n p r i n t f ( %d =, n ) ; // Print n= int f = nextfactor ( n ) ; // Find the f i r s t f a c t o r while ( f < n ) { p r i n t f ( %d x, f ) ; // p r i n t out t h i s f a c t o r n = n / f ; // Remove t h i s f a c t o r from n f = nextfactor ( n ) ; // Find the next f a c t o r p r i n t f ( %d\n, f ) ; // p r i n t out the l a s t f a c t o r return 0 ; // T e l l OS e v e r y t h i n g i s c o p a c e t i c int nextfactor ( int n ) { int f = 2 ; while ( f f<= n ) { i f (0== n%f ) return f ; // I f f d i v i d e s n e v e n l y f = f + 1 ; return n ; (a) Why does the code that states int nextfactor(int n) appear twice in this program? To avoid upside-down code - nextfactor must be declared before it is used. (b) Would this program work if the variable f in the nextfactor fucntion was initialized to 1 instead of 2? If not, why not? nextfactor would always return 1, since 1%n=0 for any n. The loop in main would never end. (c) What would this program print if compiled and invoked as./primefact 30? 30 = 2 x 3 x 5 (d) When this program prints factors of a number, what order are those factors in? Least to Greatest, Greatest to Least, or Random? Least to Greatest Page 4 of 5
9. (10 points) Write a function in C that takes two integers, a and b, as parameters, and returns the average of the two parameters, rounded to the nearest integer using a round towards zero rounding policy. (Hint: This is much easier than it sounds.) Solution: int avg ( int a, int b ) { return ( a+b ) / 2 ; Question: 1 2 3 4 5 6 7 8 9 Total Points: 10 10 10 10 10 10 10 20 10 100 Bonus Points: 0 0 0 0 0 0 0 0 0 0 Page 5 of 5