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

(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 Morgan’s 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 (pH<7)

printf(“Acidic\n”);

if (pH<2)

printf(“Very Acidic\n”);

else

printf(“Alkaline\n”);

if (pH>12)

printf(“Very Alkaline\n”);

else if (pH%7==0)

printf(“Neutral\n”);

(9)Assume that the user inputs the integer 14 on line A. What is the output displayed once the code is executed?

(A)Neutral

(B)Acidic

Neutral

(C)Alkaline

Very Alkaline

(D)Very Alkaline

(E)Alkaline

Very Alkaline

Neutral

(10)The above code is executed once again. Assume that the user inputs the integer 0 on line A. What is the output once the code is executed?

(A)Neutral

(B)Acidic

Very Acidic

Neutral

(C)Acidic

Very Acidic

(D)Acidic

Neutral

(E)Very Acidic

Neutral

(11)Consider the following code fragment (read carefully):

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

if ( (i==1) & (i=2) )

i=i+1;

printf(“i=%i”,i); /* line B */

(11) Assume that the user enters 1 through the scanf on line A. What is printed by the printf statement on line B?

(A)i=1

(B)i=2

(C)i=3

(D)i=i

(E)This program cannot execute. A compilation error is generated.

(12-14)Consider the following expression

a*b.c/e+d->c

(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 <stdio.h>

#include <stdlib.h>

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 <= 5 ; i++)

{

for ( j = i ; j > 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<stdio.h>

#include<stdlib.h>

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 FILEstructbook

(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)dis 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; i<max || fscanf(quark, “%c”,&c)!=EOF; i++)

fprintf(particle, “%c”,c);

}

What would be the correct header for this function body?

(A)FILE *func(int quark, int particle, double max)

(B)void func(FILE *quark, FILE *particle, int max)

(C)FILE func(FILE *quark, FILE *particle, int max)

(D)void func(FILE quark, FILE particle, int max)

(E)void func(struct quark, struct particle, int max)

(30)Consider the following program:

#include <stdio.h>

#include <stdlib.h>

int func(int i);

int main(void)

{

int x[4];

int i;

for (i=0; i<4; i++)

{

x[i] = func(i);

printf(“%i ”,x[i]);

}

return EXIT_SUCCESS;

}

int func(int i)

{

if (i>0)

return i + func(i-1);

else

return 0;

}

(30) What is the output once the above program is executed?

(A)0 1 3 6

(B)0 1 2 3

(C)0 0 0 0

(D)1 1 1 1

(E)3 3 3 3

(31)In the context of computer science, a library is

(A)another name for a function prototype

(B)a memory area where functions variables are stored while the program executes

(C)a set of compiled functions that can be called by the programmer without having to write them all over again.

(D)a list of C books that can be used as reference

(E)another name for hard drive of a computer

(32) Consider the following code fragment:

void onoff(int *pixel)

{

/* change *pixel to 1 if *pixel is 0 */

/* change *pixel to 0 if *pixel is 1 */

/* missing code */

}

(32)What should be written instead of the /* missing code */ line for the function to behave as expected?

(A)return 1;

(B)return 0;

(C)if (*pixel==1) return 0; else return 1;

(D)*pixel = (*pixel+1)/2;

(E)*pixel = (*pixel+1)%2;

(33)What is wrong with the following recursive function?

int nope(int i) /* line A */

{

if (i<=0)

return 1; /* line B */

else if (i%2 == 0) /* line C */

return i;

else

nope(i-3); /* line D */

}

(A)When executed it may get stuck in an infinite loop

(B)Not all paths of the function algorithm return a value

(C)A recursive function must have a void type

(D)The condition i%2==0 is always true

(E)The function name nope might be confused with a logical operator by the compiler

(34)How would you fix the function code of question 33?

(A)replace line A by void nope(int i)

(B)replace line A by int nono(int i)

(C)replace line B by return EXIT_SUCCESS;

(D)replace line C by else if (i==0)

(E)replace line D by return nope(i-3);

(35)C is called C because

(A)it is not a very good language and as such deserves a C grade

(B)it started as the B language (B for Bell laboratories) and later evolved into the C language (big hint: This could be the right answer)

(C)the programmer who wrote it was fond of whistling the C note

(D)all the other letters of the alphabet were already used for names of computer languages

(E)in its early developments, the programs written in C often crashed. The language was nicknamed C as a short for Crash.

Programming Questions (10 points)

(1)[5 points]

Using a recursive algorithm, write a function count_s that counts the number of occurrences of the character ‘s’ in a string

The prototype of the function is

int count_s(char str[], int len);

where str is the string and len is the length of the string (not counting ‘\0’)

Thus count_s(“I miss Mississipi”,17) is 6

int count_s(char str[],int len)

{

/* complete the following code */

/* your algorithm must be recursive */

if (len==1)

{

if (*str == ‘s’)

/* to be completed */

return 1;

else

/* to be completed */

return 0;

}

else

{

if (*str == ‘s’)

/* to be completed */

return 1 + count_s(str+1,len-1);

else

/* to be completed */

return count_s(str+1,len-1);

}

}

(2)[5 points]

The elements of an array a of integers are ordered in increasing order. The array a has a dimension given by the integer size.

Write a function that takes as parameters a, size, and an integer val. The function returns an integer which is the position of the element of a less or equal to val and closest to val. If there is no such element, the function returns –1.

The prototype for the function is

int where(int a[],int size, int val);

If a is {1,3,5,7,10} and val is 6, the function returns 2 which is the position of the element equal to 5.

And if a is {1,3,5,7,10} and val is 0, the function returns –1.

int where(int a[],int size, int val)

{

/* your code goes here */

/* do not forget any declaration */

/* write legibly */

int index,i;

index = size –1;

for(i=size-1;i>=0;i--)

if (a[i]>val) index = i-1;

return index;

}