Docsity

Practice Test 2 - Structured Programming In C - Spring 2007 | CPSC 206, Exams of Computer Science

Toggle sidebar
bg1
Test II — Spring 2007 — CPSC 206
Bart Childs — March 28, 2007
Last name, First Name Key Key Key
Last 4 digits of ID Write your section number:
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 Tfor true or Fby circling the appropriate letter.
For multiple choice questions the answer(s) is to be entered by circling the label. There may be
many cases with multiple correct answers.
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. Answers should be limited to 12 words per line provided.
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=210 =1024, M=220 =10242,G=230 =10243.
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 8 Total
Max Pts 2 15 15 15 15 16 16 16 110
Your
Points
pf3
pf4
pf5
pf8

Partial preview of the text

Download Practice Test 2 - Structured Programming In C - Spring 2007 | CPSC 206 and more Exams Computer Science in PDF only on Docsity!

Test II — Spring 2007 — CPSC 206

Bart Childs — March 28, 2007

Last name, First Name Key Key Key

Last 4 digits of ID Write your section number:

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 questions the answer(s) is to be entered by circling the label. There may be many cases with multiple correct answers.

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. Answers should be limited to 12 words per line provided. 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 = 210 = 1024, M = 220 = 10242 , G = 230 = 10243.

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 8 Total Max Pts 2 15 15 15 15 16 16 16 110 Your Points

True/False.

1. [1] F Functions in C are call-by-reference. The values of arguments are passed to the

functions and changes can be made to the values whose pointers are passsed.

2. [1] T A string is allocated by declaring an array of characters. The name of the string

is then used as the right hand side of an assignment statement with the left hand side having been declared char ∗ left. It is legal C to access the third character of the string by left [2].

3. [1] F The function ispunct () has one argument and it is of type char.

4. [1] T The unary operator sizeof (array) returns the product of the number of bytes

needed to store one element of array and the number of elements in array. The returned value is not of type int.

5. [1] F Variables of storage type (auto) will be in the same location each time the block

of code contaning their definition is entered.

6. [1] F If the user adds one blank line to a source file referenced in Makefile then

invocation of make will not cause recompilation since this is not a real change of the source.

7. [1] T If these numeric constants are output as integers (assume possible required

casting) the same value wout be output: 32 , ’ ’, and 0x

8. [1] F The output of printf ("%d", sizeof ( float )); and printf ("%d", sizeof (12.6));

yield the same result. Because default float constant is double.

9. [1] T In the body of a function, the compiler recognizes a name followed by

parentheses as a reference to the name of a function.

10. [1] F sizeof (−4) will return 4, which it gets by taking the absolute value of −4.

11. [1] F Each case in a switch statement should end with a break statement unless it is

the default case.

12. [1] T The execution results of a switch statement may depend upon the order of the

default and enumerated cases in the code.

13. [1] F A declaration int j j , ∗ i i i = NULL; followed by j j = ∗ i i i ; sets the

value of j j to zero. Causes segmentation fault on execution.

14. [1] T C source code is case sensitive on all systems.

15. [1] F The "%12s" conversion format for scanf () functions commands that at most 12

characters be input and stored in the appropriate destination.

Multiple Choice. There may be zero or multiple correct answers!!!

If the problem has more than one answer. If there are two correct answers and one is marked 2 of 3. If one is marked and a wrong one is marked 0 of 3.

  1. [3] Circle the prototypes from ctype.h that are correct. a. (X)int isalpha ( int c ); b. int isalpha (char c ); c. int isxdigit (char c ); d. char tolower (char c );
  2. [3] Circle the prototypes from string.h that are correct.

a. (X)char ∗strncpy (char ∗s , const char ∗t , int n ); b. char ∗ strcat ( const char ∗s , const char ∗t ); c. (X)int strncmp( const char ∗s , const char ∗t , int n ); d. int strstr ( const char ∗s , const char ∗t );

  1. [3] Given the following array declaration and initialization: int a[4] = {0, 1, 2}; what is the value of a[4]? a. 0 b. 1 c. 2 d. (X)a[4] is illegal.
  2. [3] What is the value of count after execution of the for loop?

int sum = 0, count; for (count=0; count <=8; count++) {sum+=count; if (sum==3) break;}

a. 0 b. 3 c. 4 d. (X) 2 e. the value of count is not defined after the for loop

  1. [3] In general, when a double variable and an int variable are combined in an expression by an arithmetic operator (+ − / ∗)

a. the expression is evaluated without any conversion b. there is a syntax error, because double and int variables cannot be combined in one expression c. the value of type double is converted to type int before the expression is evaluated d. (X)the value of type int is converted to type double before the expression is evaluated

  1. [3] A new programmer writes this expression, i = ’z’ −’Z’; in a program where i is of type int. Given that ’Z’<’z’, which of the following is true?

a. It is illegal to do arithmetic on characters. b. The result cannot be 32 since there are only 26 characters in the alphabet. c. (X)The magnitude of the result will be 32. d. (X)The result of ’A’+32 is equivalent to ’b’−1.

equivalent to added to second exam

  1. [3] Assume char s [8]; Circle the label for each of the following that are legal. a. s = "\0"; b. (X)s [0] = ’\n’; c. (X)s [3] = ’0’; d. s [8] = ’\0’;
  2. [3] The initialization phrase/statements for a for (;;) loop is/are between the left parenthesis and the first semicolon. Circle the labels of the true statements.

a. (X)there may be multiple C assignments separated by commas. b. (X)this may be an empty statement. c. (X)if this is a nested for (;;) it is not recommended to assign a variable initialized in an enclosing for (;;). d. variables of type double cannot be assigned values in this portion. Can but should not

  1. [3] The values of x , y , z are −1, 0, 1 respectively. Circle the label(s) of the following expressions that evaluate to a logical false:

a. !y b. (x < y) || (z < y) c. (( x < y) || (z < y)) && (x < z) d. (( y < x) && (z < y)) || (x < z) Some exams have an extra left paren in item c. If so noted, give that student credit for that part.

  1. [3] Circle the labels of the following that are keywords in C. The fact they are presented in a bold font is not an indication. a. scanf b. (X)signed c. string d. cat

Problems — Remember the verbosity instructions.

  1. [6] Write the output from this code in the space provided.

i n c l u d e < s t d i o. h>

i n c l u d e < s t r i n g. h>

i n t main ( ) { char l i n e [ 6 4 ] = " Gordon Smiths Childs Barosz Janaczek " ; char ∗ t h e r e ; i n t c o u n t = 0 ; t h e r e = s t r s t r ( l i n e , " " ) ; f o r ( ; t h e r e != NULL ; c o u n t ++){ p r i n t f ( "%s\n" , t h e r e ) ; t h e r e = s t r s t r ( ++ t h e r e , " " ) ; } p r i n t f ( " count is %d\n" , c o u n t ) ; r e t u r n 0 ; }

Smiths Childs Barosz Janaczek Childs Barosz Janaczek Barosz Janaczek Janaczek count is 4

  1. [4] State the type and function/use of the return value and arguments of the sprintf() function. return: int number of characters printed.

first argument: **char *** string address to output to.

second argument: **char *** conversions specifications plus

second... argument: various types, data to be converted

  1. [4] State the type and function/use of the return value and arguments of the strstr() function. return: **char *** Pointer to second argument in first. NULL if not found.

first argument: **char *** string address of source being searched.

second argument: **char *** string address of substring being looked for in first argument.

  1. [16] This program aids in the extraction of fields from a file exported in a “comma separated values” format from a spreadsheet processer. The contents of a typical line have been input into a line as a string. / ∗ g e t f i e l d s. c ∗ /

i n c l u d e < s t d i o. h>

i n c l u d e < s t r i n g. h>

i n t t h e f i e l d s ( char ∗ t h e l i n e , char ∗ f i e l d s [ ] , i n t n f i e l d s ) ; i n t main ( ) { i n t f i e l d c o u n t =5 , n ; char t h e l i n e [ 1 2 0 ] , ∗ f i e l d s [ f i e l d c o u n t ] ; t h e l i n e = " 9870012 , von Eschen , Count , cve12@tamu .edu , U4 " ; n = t h e f i e l d s ( t h e l i n e , f i e l d s , f i e l d c o u n t ) ; i f ( n != f i e l d c o u n t ) { p r i n t f ( " Extraction failed , %d\n" , n ) ; e x i t ( n ) ; } r e t u r n 0 ; }

Write the function the_fields.c to perform the extraction of the fields. It will be in a separate source file. / ∗ t h e f i e l d s. c ∗ / # include # include i n t t h e f i e l d s ( char ∗ t h e l i n e , char ∗ f i e l d s [ ] , i n t n f i e l d s ) { char ∗ beginning , ∗ending ; i n t count ; b e g i n n i n g = t h e l i n e ; f o r ( count =0; count