Approximately a Test II CPSC 206 Sometime in history based on Kelly and Pohl Last name, First Name Last 5 digits of ID Write your section number(s): All parts of this exam are required unless plainly and explicitly announced by the instructor during class. Each True/False problem is to be answered T for true or F by circling the appropriate letter. For multiple choice and questions the answer is to be entered by circling the label. The worth of individual problems is shown like [9]. If you have a question about a problem, write the question, the answer you are assuming, and continue. In other words, do not ask it aloud!! Verbosity is discouraged. We have provided sufficient lined space for answers. In some cases we may have provided more than enough. Written answers should be written in good English. Neatness counts and we reserve the right to assign lower grades for sloppiness, much lower! Do not remove the staple or tear exam packet apart! Calculators, PDAs, CD players, radios, and headphones are not allowed! K, M, G may be used. If so, K = 2 10 = 1024, M = 2 20 = 1024 2, G = 2 30 = 1024 3. Aggies do not lie, cheat, or steal and do not tolerate those who do! I have obeyed the Aggie code of honor in my answers on this exam. Signature Page 1 2 3 4 5 6 7 Total Max Pts 3 15 15 20 24 23 20 120 Your Points
True/False. 1. [1] T F The presence of a semicolon after the closing right parenthesis of a function header indicates that this header is a prototype. 2. [1] T F The code fragment char *p="howdy boys\n" "and girls\n"; printf("%s", p); will compile cleanly and run without error. 3. [1] T F A return NULL; statement should appear just before the closing brace for a function of type void. 4. [1] T F A potential line of C source contains the assignment short_string = "Howdy!"; Assume the declaration char *short_string preceeded the assignment. The effect of the assignment statement is to assign the address of the string constant to the pointer short_string. 5. [1] T F A potential line of C source contains *shorter_string = A ; Assume that shorter_string has been declared by char *shorter_string; and initialized to a non-trivial string. The effect of the assignment statement is to assign the address of the character constant to the pointer shorter_string. 6. [1] T F A declaration int *ii = NULL, jj; followed by jj = *ii; sets the value of jj to zero. 7. [1] T F A variable named bool of type int was created for logic purposes and given a value. The result of bool ==!(! bool) is always true. 8. [1] T F Call-by-value means that the value of an argument is passed to and returned from the called function. 9. [1] T F This type definition is valid in C: typedef enum {A= A, B, c} Letters; 10. [1] T F The exit(return_value); statement is quite similar to the return return_value; except that it can be used in functions and returns control all the way to the operating system. 11. [1] T F Enumerators (the enumerator constants) can be explicitly initialized. 12. [1] T F A programmer can use the typedef statement to create another name for a fundamental data type. 13. [1] T F The name of an array when used without subscripts can be used in all places that a pointer can be used. 14. [1] T F These initializations are legal in C: int *p = &i, i = 65535; 15. [1] T F The statement printf("%c", \t ); will output a tab character to the screen. 2
16. [1] T F The constants "z" and z are not the same. 17. [1] T F The function isalnum returns true if the argument is an alphabetic character, numeric digit, or an underscore. 18. [1] T F These numbers are all the same in C: 7, 07, and 0x07. 19. [1] T F The expected output of printf("%d",sizeof( z )); is the digit 1. 20. [1] T F The unary operator sizeof(arg) returns the number of bytes needed to store the arg if it is a constant or the number of bytes allocated for arg if it is a variable. 21. [1] T F In C it is guaranteed that sizeof(short) <= sizeof(int) <= sizeof(long) 22. [1] T F In C it is guaranteed that sizeof(signed) == sizeof(int) == sizeof(unsigned) 23. [1] T F In C, sizeof(char *) == sizeof(char) 24. [1] T F Any variable regardless of type always uses 4 bytes of memory. 25. [1] T F The function putchar has no arguments and returns an int. 26. [1] T F Local (auto) variables are not automatically initialized by the system to 0. 27. [1] T F If ptr is declared int *ptr; then &*ptr is equivalent to ptr. 28. [1] T F Local (auto) variables can be used only in a block or function body in which they are declared. The problems on the rest of this page should be answered in the context of C, unix, and similar programming environments as reflected in the class, notes, and laboratories. 29. [1] T F This command on a unix command line my_program > junk.txt causes the input of my_program to be taken from the file junk.txt. 30. [1] T F The value of the parameter argc as defined in main(int argc, char *argv[]) is the number of arguments after the name of the executable. 3
31. [2] What is the error in this code? Multiple Choice. #include <stdio.h> int main(void){ char Alpha[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char alpha[27]="abcdefghijklmnopqrstuvwxyz"; alpha = Alpha; printf("alpha is now %s\n", alpha); return 0; } a. Array incorrectly initialized b. illegal assignment c. Variable names spelled the same d. printf does not need a pointer 32. [2] Which of the following is a valid preprocessor directive which will insert a header file containing prototypes for standard character type macros and functions? a. #include "stdlib.h" b. #include <stdio.h> c. #include <stdlib.h> d. #include <ctype.h> e. #insert "standard char prototype file" 33. [2] In general, when a double and long variables are combined in a single expression then a. the value of type double is converted automatically to type long before the expression is evaluated b. both values are converted automatically to long double to increase precision c. the expression is evaluated without any conversion d. the value of type long is converted automatically to type double before the expression is evaluated e. there is a syntax error, because double and long variables cannot be combined in one expression 34. [2] What is the value of count after execution of the for loop? int sum = 0, count; for (count=0; count <=4; count++) sum+=count; a. 0 b. 3 c. 4 d. 5 e. the value of count is not defined after the for loop 35. [2] A user-defined function can be placed (assuming that the function s prototype is located correctly): a. before the main() function b. after the main() function c. in another file d. all of the above is true 4
36. [2] Why does the following piece of code give a compile time error? const int integer = 20000; integer++; a. integer is not a proper variable name b. the value 20000 is too big for a variable of type int c. it increments a variable declared as const d. const cannot be used in that context 37. [2] Given the following declaration: int a[1500]; which of the following is not a valid C expression? a. a - 1 b. a + 1500 c. a++ d. a[150] 38. [2] Given the following array declaration and initialization: int a[5] = {0, 1, 2}; what is the value of a[4]? a. 0 b. 1 c. 2 d. a[4] is not initialized. 39. [2] Which one is correct? In C, a string a. is an array of characters, terminated by a b. is a fundamental data type \0 c. cannot be initialized d. is another name for type char 40. [2] Given the following declaration: int a[10]; which of the following gives the address of the third element of a? a. a[3] b. a + 1 c. *(a + 2) d. a + 3 41. [2] What does the function test do? void test(char *s1, const char *s2) { int i; for (i = 0; i < strlen(s2); i++) s1[i] = s2[i]; s1[i] = \0 ; } a. It concatenates the strings s1 and s2, and the result is stored in s1. b. It compares the strings s1 and s2. c. It computes the length (size) of the string s1. d. It copies elements of the string s2 to the string s1. 5
42. [2] When an array is passed as a parameter to a function, what is actually passed? a. A copy of all elements of the array b. pointer to the first element of the array c. The first element of the array d. A pointer to the end of the array 43. [2] What is the value of j after the following code is run int i, j = 0; int a[] = {0, 1, 2, 3, 4, 5, 6, 7}; for (i = 0; i <= 7; i++) j += (a[i] <= 3); a. 0 b. 2 c. 4 d. 6 Problems Remember the verbosity instructions. 44. [5] Why is this construct considered to be dangerous typedef enum {true, false} Bool;? 45. [5] The use of the scanf() function normally requires that we use the address operator (&). Why is this not required when we input a string with a statement scanf("%s", string_name); 46. [5] What does the function test2 do? unsigned test2( const char *S ) { unsigned int i = 0; while( S[i]!= \0 ) i++; return i; } 6
47. [20] Assume that you have an email message with uppercase and lowercase letters. For example, some sentences begin with lowercase letters and the rest of the sentence may be all UPPERCASE. You will complete the program below by replacing all letters in a line with lowercase letters but each sentence must start with an uppercase letter. Complete the program mail_correction.c which will make the indicated corrections to such messages. Test data will be in the file message.in. What command line instruction will you issue to: Compile the program: Execute the program with data: These assumptions are to be made about each email message a. There will not be any leading spaces on a line. b. Each sentence in the data files will be on one line. c. There may be more than one sentence on a line. d. The period is the only sentence ending mark used. e. There will be only one space after the sentence ending period. f. Thus, later sentences on a line follow a period and space. This is the program you need to complete. #include <stdio.h> #include <string.h> int main(void){ int i=0, j=0, k=0, n; /* if needed */ char a_line[132]; while( gets( a_line )!= NULL ) { /* Your lines of code go here */ printf("%s\n", a_line); } return 0; } You should use the functions toupper and tolower. You may use isalpha. These take a character as an argument and return a character. Assume the function gets(char a[]); reads one line of characters into the array a until a newline or end-of-file is encountered. A \0 is appended after the last read character in the array. It returns the address of a if successful, NULL otherwise. What assumptions about the functions toupper and tolower are made? 7