CSC142 – Midterm1 – Win 99 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:

int i;

double x;

double y;

x = 4.9;

i = (int)((int)(x) + 0.5); /* A */

y = (int)(x+0.5); /* B */

(1)After the execution of the statement on line A, what is the value of i?

(A)0

(B)0.5

(C)4

(D)5

(E)5.0

(2)After the execution of the statement on line B, what is the value of y?

(A)0

(B)0.5

(C)4

(D)5

(E)5.0

(3)Which of the following statements is true?

(A)When a program executes, execution begins by the first statement of the first function listed in the program

(B)Using numbers in a variable name is not allowed

(C)void is not a legal type for a variable

(D)Assigning a doubleto an integer in a program generates a fatal error

(E)character is the C keyword to declare a character

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

!( c != ‘a’ || x > 8.0)

(A)‘a’ != c & 8.0 > x

(B)x > 8.0 & c != ‘a’

(C)c == 8.0 & x <= ‘a’

(D)c == ‘a’ & x < 8.0

(E)c == ‘a’ & x <= 8.0

(5)In the context of computer science, API refers to

(A)a powerful microprocessor developed by Advanced Processor Inc.

(B)a library of functions and stands for Application Programming Interface

(C)a computer language named Another Programming Innovation

(D)an information exchange standard called American Portable Information

(E)nothing in particular

(6 - 7) Consider the following code fragment:

char letter;

int i = 10;

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

switch(letter)

{

default:

i = i * 2;

case ‘a’:

case ‘A’:

i = i * 3;

break;

case ‘b’:

case ‘B’:

i = i * 4;

}

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

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

(A)10

(B)20

(C)30

(D)40

(E)60

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

(A)10

(B)20

(C)30

(D)40

(E)60

(8)Consider the following code fragment:

double x=4;

int i=3;

int j=4;

printf(“%.2f”,i%j/(int)x); /* A */

(8) What is the output displayed by the statement on line A once the code is executed?

(A)0.00

(B)0.25

(C)0.75

(D)1.00

(E)1

(9-10) Consider the following code fragment (Read Carefully)

int y;

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

if ( y/100==0 )

if ( y%50 ==0 )

printf(“Red\n”);

else if ( y%25==0 )

printf(“Orange\n”);

else

printf(“Blue\n”);

else

if (y%200 == 0)

printf(“Green\n”);

else if (y%100 == 0)

printf(“Yellow\n”);

printf(“Magenta\n”);

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

(A)Red

Magenta

(B)Blue

Magenta

(C)Yellow

(D)Yellow

Magenta

(E)Magenta

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

(A)Red

Magenta

(B)Blue

Magenta

(C)Yellow

(D)Yellow

Magenta

(E)Magenta

(11)What is the value of the expression (8%6%4%2) ?

(A)0

(B)2

(C)4

(D)6

(E)8

(12-13) Consider the following piece of code. The programmer’s style is unconventional. Although the program executes, it does not behave as you might expect. Read carefully and recall the pitfalls of the if statement mentioned in class.

int integer;

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

if (integer == 0);

integer = integer + 1;

if (integer == 1)

integer = integer + 2;

integer = integer + 3;

if (integer == 2) {}

integer = integer + 3;

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

(12)Assume that the user inputs 0 at scanf on line A. What is printed by the printf statement on line B?

(A)integer = 0

(B)integer = 1

(C)integer = 4

(D)integer = 7

(E)integer = 9

(13)The above code is executed once again. Assume that the user inputs -1 at scanf on line A. What is printed by the printf statement on line B?

(A)integer = -1

(B)integer = 0

(C)integer = 3

(D)integer = 6

(E)None of the above

(14)Knowing that 1996 and 2000 are both leap years and that 1900 is not a leap year, which condition below is true when year is a leap year? (year is a variable of type int).

(A)year%4 == 0

(B)year%4 == 0 || year%100 == 0

(C)year%4 == 0 & (year%400 ==0 & year%100 != 0)

(D)year%4 == 0 || (year%400 !=0 || year%100 == 0)

(E)year%4 == 0 & (year%400 ==0 || year%100 != 0)

(15-16) Consider the following program:

#include <stdio.h>

int do_something(int a, int b);

int main(void)
{

int a;

int b;

a = do_something(1,2);

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

b = do_something(do_something(1,2),do_something(1,2));

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

return 0;

}

int do_something(int c, int d)

{

int a, b;

return c*d;

a=c+1;

b=d+1;

return a*b;

}

(15)What is the output displayed by the printf on line A?

(A)a = 2

(B)a = 4

(C)a = 6

(D)a = 36

(E)The program generates a run time error

(16)What is the output displayed by the printf on line B?

(A)b = 2

(B)b = 4

(C)b = 6

(D)b = 36

(E)The program generates a run time error

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

a/b*c/d

(A)(a/b)*(c/d)

(B)((a/b)*c)/d

(C)a/(b*(c/d))

(D)(a/(b*c))/d

(E)a/((b*c)/d)

(18-19) Consider the following function body:

{

if (a/2==0)

return ‘2’;

else if (b/3==0)

return ‘3’;

return c;

}

(18)What would be a correct header for this function (correct means that it would not trigger any error or warning message at the compilation).

(A)void f(int a, int b, int c)

(B)void f(double a, double b, double c)

(C)char f(char a, char b ,double c )

(D)char f(int a, int b, char c)

(E)char f(a,b,c)

(19)Assuming that the function has the header selected above, what is the value of the expression f(3,2,‘a’)?

(A) ‘2’

(B)‘3’

(C)‘c’

(D)‘a’

(E)None of the above

(20)Using a bad style, a programmer has written the following piece of code

if (x)

printf(“Yes ”);

else

printf(“No”);

If x is a double variable equal to 3.4, what is printed by the above piece of code?

(A)such an expression generates a compiler error

(B)such an expression generates a run time error

(C)Yes

(D)No

(E)Yes No

(21) Consider the following code fragment:

int i = 4;

int j;

while( i >0 )

{

j = 0;

while( j<=i )

{

printf(“*”);

j = i+j;

}

printf(“\n”);

i = i-1;

}

(21)When executed this code fragment displays

(A)****

(B)*

*

*

*

(C)****

****

(D)**

**

**

**

(E)***

***

***

***

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

(A)char x = 2.0;

(B)define PI 3.1415

(C)void x = ‘’;

(D)double int;

(E)char car;

(23)Among the following, which one is not a valid C identifier?

(A)single

(B)double

(C)triple

(D)quadruple

(E)quintuple

(24-25) Suppose that i and j are both integers. Consider the following code fragment:

i = 2*i + j;

j = -2*i - j;

i = 2*i + j;

(24)If the initial values of i and j are 1 and 1, what will be their values after the above fragment is executed?

(A)i = 3 and j = -3

(B)i = -1 and j = -7

(C)i = 1 and j = 7

(D)i = 1 and j = -1

(E)i = 1 and j = -7

(25)Which one of the following holds true irrespective of the initial values of i and i?

(A)The new value of i is equal to the negative of the initial value of j

(B)The new value of i is greater than the new value of j

(C)The new value of i is smaller than the new value of j

(D)The new value of i is equal to the initial value of j

(E)The new value of i is equal to the new value of j

(26) Consider the following program:

#include<stdio.h>

#define RADIUS 10

#define DIAMETER RADIUS+RADIUS

int main(void)

{

printf(“The perimeter is %.2f”,3.14*DIAMETER);

return 0;

}

(26)What is printed by the above program when executed?

(A)The perimeter is 31.40

(B)The perimeter is 41.40

(C)The perimeter is 51.40

(D)The perimeter is 62.80

(E)An error occurs at the compilation

(27)When comparing double variables, it is not recommended to use the relational operators ==and!= because

(A)these operators apply to int variables only

(B)the accuracy of computer calculations with double variables is limited.

(C)a program should only use the operators >, <, >= and <= for comparisons.

(D)these operators are notorious for slowing down program execution

(E)they make the program difficult to understand

(28) Consider the following truth table for a logical operator @

P / Q / P @ Q
T / T / F
T / F / F
F / T / F
F / F / T

(28)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 & !(P||Q) & !Q

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

(29)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.

$ $ a @ $ b @ b

(A)($ ($ (a @ ($ b)))) @ b

(B)(($ ($ a)) @ ($ b)) @ b

(C)$(($ a) @ ($ (b @ b)))

(D)$( $ (a @ ($ (b @ b))))

(E)($ ($ a)) @ (($ b) @ b)

(30)Given the prototype of a function:

double compute(double a, int b);

Which one of the following is false?

(A)The function returns a value of type double.

(B)compute( 2.0, 1) is a correct way to call the function.

(C)If x is an int variable, x = (int)compute(x,x) is a C statement that generates a warning when compiled.

(D)If x is an int variable, x = compute((double)x,x) is a C statement that generates a warning when compiled.

(E)the above prototype could be written compute(double, int);

(31)Which statement is true?

(A)A return statement might simply read return;

(B)The value of a #define constant may be changed during execution

(C)Though not a valid variable name, 123go is a valid function name

(D)Actual and formal parameters of a function must have the same name

(E)The first function of a program must be the main function

(32)Consider the following C statement:

apple = grocery(item);

Which statement is false?

(A)groceryis a function that might return a double value

(B)apple must have the same type as grocery

(C)The function grocery might have the prototype int grocery(char item_list);

(D)item is an actual parameter in the call to the function grocery

(E)The call to grocery is done by value as any other function call in a C program

(33)An example of an input operation in a program would be

(A)assigning a value to a variable

(B)displaying a message on the screen of the monitor

(C)reading the value of a variable stored in the memory

(D)executing a printf command

(E)None of the above

(34) Consider the following code fragment:

int pow2(int n)

{

int count=0;

while(n%2==0)

{

count=count+1;

n=n/2;

}

return count;

}

(34)What is the value of pow2(32)?

(A)1

(B)2

(C)3

(D)4

(E)5

(35) In C, functions are called by value. This means that

(A)a function might modify the caller’s data without the caller’s knowledge

(B)a function always needs at least one argument to be executed

(C)a function always returns a value

(D)a function operates on copies of the actual parameters

(E)a call to a function must always appear in an assignment statement