final.doc

26
CSC142 – Final – Fall 98 1 Multiple Choice Questions (35 points) Answer all of the following questions. READ EACH QUESTION CAREFULLY. Fill the correct bubble on your scantron sheet. Each correct answer is worth 1 point. Each question has EXACTLY one correct answer. (1-2) Suppose the following code fragment has been executed: double x = 2.9; double y,z; y = (int)x + 0.1; printf(“%.1f”,y); /* A */ z = (int)(x+0.1); printf(“%.1f”,z); /* B */ (1) What is printed by the printf statement on line A? (A) 2 (B) 2.1 (C) 2.9 (D) 3.0 (E) None of the above (2) What is printed by the printf statement on line B? (A) 2

Upload: gparya009

Post on 08-Sep-2015

18 views

Category:

Documents


3 download

TRANSCRIPT

Multiple Choice Questions (xx points)

CSC142 Final Fall 98 17

Multiple Choice Questions (35 points)

Answer all of the following questions. READ EACH QUESTION CAREFULLY. Fill the correct bubble on your scantron sheet. Each correct answer is worth 1 point. Each question has EXACTLY one correct answer.

(1-2) Suppose the following code fragment has been executed:

double x = 2.9;

double y,z;

y = (int)x + 0.1;

printf(%.1f,y); /* A */

z = (int)(x+0.1);

printf(%.1f,z); /* B */

(1) What is printed by the printf statement on line A?

(A) 2

(B) 2.1

(C) 2.9

(D) 3.0

(E) None of the above

(2) What is printed by the printf statement on line B?

(A) 2

(B) 2.1

(C) 2.9

(D) 3

(E) 3.0

(3) Which of the following statements is TRUE?

(A) C is an object oriented programming language

(B) struct is a legal name for a variable in a C program

(C) The variable declaration car ford;is illegal in any C program

(D) A function may return more than one value

(E) A variable name may begin with the symbol #

(4) Using De Morgans law, how would you rewrite the following conditional statement (that is rewrite the statement using && instead of ||)

!(x >= -2 || c == e)

(A) x >= 2 && c == f

(B) x < -2 && c == d

(C) x > -2 && c != e

(D) x < -2 && c < e

(E) x < -2 && c != e

(5) Functions are useful in a program because

(A) they increase the execution speed of a program

(B) they make the program organization clearer

(C) a program cannot compile if it does not contain a least one function

(D) the compiler automatically switches to the more powerful function mode when compiling a program with functions

(E) the more functions in a program the less bugs

(6 - 7) Consider the following code fragment:

int i = 5;

char number;

scanf(%c,&number); /* line A */

switch(number)

{

default:

i = 2 * i;

case 1:

case 2:

i = 3 * i;

case 3:

case 4:

i = 4 * i;

break;

}

printf(%i,i); /* line B */

(6) Assume that the user enters Z through the scanf on line A. What output is displayed by the printf statement on line B?

(A) 10

(B) 15

(C) 20

(D) 120

(E) The program generates a run time error

(7) The above piece of code is executed once again. Assume now that the user enters 2 through the scanf on line A. What output is displayed by the printf statement on line B?

(A) 5

(B) 10

(C) 15

(D) 60

(E) 120

(8) Consider the following code fragment:

int i=1;

int j=3;

int k=4;

printf(%i,i-j*5/k%2); /* A */

(8) What is the output displayed by the statement on line A once the code is executed? (recall that %has the same precedence as * and /)

(A) 0

(B) 1

(C) -1

(D) 3

(E) A run time error is generated because of a division by zero

(9-10) Consider the following code fragment (do not trust the indentations used by the programmer)

int pH;

scanf(%i,&pH); /* line A */

if (pHc

(12) Add parentheses to the above expression to make clear the order in which C will perform the operations:

(A) (((a*b).c)/e)+(d->c))

(B) (((a*b).c)/e)+d)->c)

(C) (((a*(b.c))/e)+(d->c))

(D) (((a*b).(c/e))+(d->c))

(E) Such an expression is illegal in a C program

(13) In the expression a*b.c/e+d->c, what is a possible type for the variable b?

(A) double

(B) int

(C) Such an expression is illegal in a C program

(D) struct somestruct

(E) struct somestruct*

(14) In the expression a*b.c/e+d->c, what is a possible type for the variable d?

(A) double

(B) int

(C) Such an expression is illegal in a C program

(D) struct somestruct

(E) struct somestruct*

(15-17) Consider the following program (Be careful):

#include

#include

int main(void)

{

char first[] = The old man;

char second[] = and;

char third[] = the sea;

char all[100];

int i,j;

for(i=0;*(first+i)!= \0;i++)

all[i] = first[i];

printf(%c\n,*all); /* line A */

all[i] = ;

for(j=1; second[j]!= \0; j++){

all[i] = second[j];

i++;

}

all[i] = \0;

printf(%s\n,all); /* line B */

do

{all[i] = third[j];

i++;

j++;

}while(third[j]!= \0);

printf(%s,all); /* line C */

return EXIT_SUCCESS;

}

(15) What is printed by the printf statement on line A?

(A) The oldman

(B) T

(C) The old man

(D) Whichever memory address is stored in all

(E) None of the above

(16) What is printed by the printf statement on line B?

(A) The old man and

(B) T

(C) he old man and

(D) The old mannd

(E) None of the above

(17) What is printed by the printf statement on line C?

(A) The old man nd

(B) The old man and

(C) The old man and the sea

(D) The old mannd sea

(E) None of the above

(18) Consider the following code fragment:

int i,j;

for ( i = 1 ; i 0 ; j--)

printf( );

printf(*);

printf(\n);

}

(18) When executed this code fragment displays

(A) a line of * with the shape of

(B) a line of * with the shape of

(C) line of * with the shape of

(D) a line of * with the shape of

(E) a triangle of * with the shape of

(19) Which statement is a CORRECT C statement? (correct means that no error or warning is generated at the compilation)

(A) char name[3] = Kathryn;

(B) 2x = 2.0;

(C) double y[6] = {1.0,2.0};

(D) char *x = a;

(E) &x = 10;

(20) Among the following, which one is NOT a valid C identifier?

(A) concrete

(B) brick

(C) stone

(D) struct

(E) truss

(21-23) Consider the following program:

#include

#include

int main(void)

{

FILE *file1, *file2;

char letter;

file1 = fopen(chapter, r);

file2 = fopen(book, w);

while(fscanf(file1, %c,&letter)!=EOF)

fprintf(book, %c,letter);

fclose(file1);

fclose(file2);

return EXIT_SUCCESS;

}

(21) In the above program, chapter is

(A) a variable of type double

(B) a variable of type FILE

(C) the name of a file known to the computer outside the C program

(D) part of a message printed on the computer screen when the program is executed

(E) a member of the FILE struct book

(22) In the above program, EOF is?

(A) a variable of type FILE

(B) the value assigned to the variable letter by fscanf when reaching the end of the file read.

(C) a counter to keep track of the number of characters read

(D) A constant defined in the header file stdio.h which is used by some input/output functions (e.g. fscanf)

(E) The name of a function that detects viruses when reading files

(23) What is the result of the execution of the above program?

(A) Assuming that the text file chapter exists, it is copied into the file book

(B) Assuming that the text file book exists, it is copied into the file chapter

(C) The program cannot execute since there is an error at the compilation

(D) The variable pointed to by file1 is copied into the memory location of address file2

(E) The variable pointed to by file2 is copied into the memory location of address file1

(24) Which of the following is FALSE regarding a recursive function?

(A) A recursive function can always be rewritten with a loop

(B) A recursive function always executes faster than its loop equivalent

(C) A recursive function is a function that calls itself

(D) Recursion can sometimes yield a natural and simple solution to a problem that would otherwise be very difficult to solve

(E) When executing a recursive function, the computer transfers data to and from a memory area called the system stack.

(25) Consider the following truth table for the logical operation NAND

P

Q

P NAND Q

T

T

F

T

F

T

F

T

T

F

F

T

(25) Which of the following C conditional expressions would NOT reproduce the above truth table?

(A) !(P&&Q)

(B) !P || !Q

(C) !(P&&Q) || !P || !Q

(D) !(P&&Q) && (!P || !Q)

(E) (P&&Q) && (!P || !Q)

(26) Given the following definitions and declarations:

typedef struct{char name[20];

double latitude;

double longitude;

}

mountain;

typedef struct{mountain chain[10];} range;

range *cascades;

What are the types of the following expressions?

1) &(cascades->chain[1])

2) cascades->chain[1].name[0]

(A) 1) mountain *

2) char *

(B) 1) mountain

2) char *

(C) 1) mountain

2) char

(D) 1) mountain *

2) char

(E) 1) mountain **

2) char

(27) Consider a language with the two operators ( and . ( is left associative and binary. is right associative and unary. has precedence over (.

Add parentheses to the following expression to show how it would be evaluated.

x ( y ( x

(A) ( (x ( ( ( y)))) ( x

(B) (x ( (( ( y)) ( x))

(C) ( x) ( ( (( y) ( x))

(D) ( x) ( ( ( (y ( x)))

(E) (( x) ( ( ( y))) ( x

(28) If you see the following in a legal C program:

a = b->c[d(e)];

one thing that you can say for sure is:

(A) b is a pointer to a double

(B) e is an integer

(C) c is a function that returns a struct

(D) d is a function that returns an integer

(E) a and b have the same type

(29) Given the function body:

{

int i;

char c;

for (i=0; ival) index = i-1;

return index;

}