COP 3223 Section 1 Exam #1

Form A

Fall 2003

9/24/03

Lecturer: Arup Guha

Name: ______

WebCT User ID: ______

Directions:Answer all multiple choice questions on the scantron. Each question has a single correct answer. In case of ambiguities, choose the most accurate answer. Each of these questions is worth 3 points for a correct answer. Incorrect answers and questions left blank are worth 0 points. Place your answer for each short answer question and each program on the answer sheet given to you. The point values for these questions are denoted on the answer sheet. Partial credit will only be given on the short answer section and the programs. Hand in ONLY the scantron and answer sheet.

MULTIPLE CHOICE SECTION

1) What is the output of the following C statement?

printf("Hello\nWorld\\n.");

a)Hello World. b)Helloc)Hello d) None of

World\ World\n. the above.

2) Which of the following is not necessary for the Hello World program to compile and run properly?

a) function main b) header comment c) The printf statement d) None of the above.

Questions 3 through 5 refer to the following C program:

#include <stdio.h>

int main(void) {

double taxrate;

int itemvalue;

printf("Enter the state sales tax as a percentage.\n");

/*** stmt1 ***/

printf("Enter the price of the item, (an integer.)\n");

/*** stmt2 ***/

printf("Your total price is %1.2lf.\n", /*** exp3 ***/);

return 0;

}

3) Assuming that the statement that needs to be inserted into the slot /*** stmt1 ***/ reads in the sales tax percentage into the variable taxrate, which of the following statements properly completes the task?

a) scanf(taxrate); b) scanf("%d", taxrate); c) scanf("%d", &taxrate); d)None of the above.

4) Assuming that the statement that needs to be inserted into the slot /*** stmt2 ***/ reads in the cost of an item into the variable itemvalue, which of the following statements properly completes the task?

a) scanf("%d"); b) scanf("%d", itemvalue); c) scanf("%d", &itemvalue); d) None of the

above.

5) Assuming that the last statement prints out the total cost of an item with the price itemvalue taxed at the percentage stored in taxrate, which of the following expressions should replace /*** exp3 ***/?

a) taxrate*itemvalueb) (1+taxrate/100)*itemvaluec) (1+taxrate)*itemvalue d) None of

the above.

6) What type of directive is a #define?

a) preprocessor b) compiling c) run-time d) execution time e) None of the above.

7) What is the output of the following segment of code? (Assume the variable x has already been declared as a double.)

x = 1.3456;

printf("%8.2lf", x);

a)1.34 b) 1.34 c)1.35 d) 1.35 e)None of

the above.

8) Which of the following can not be used as a variable name in a C program?

a) hellob) worldc) ifthend) dowhilee) None of the above.

9) What is the value of the following C expression to 2 decimal digits?

1+2-3*4/5-6+7*8/9

a) 0b) .22 c) .82d) 1e) None of the above.

10) Which of the following statements could be valid?

a) cnt++;b) 7++;c) (cnt+1)++;d) ++1/cnt;e) None of the above.

11) Which of the following statements changes the value of the positive int variable x?

a) x = x%(2*x); b) x == x+1;c) x--; d) y = x; e) None of the above.

Questions 12 and 13 refer to the following C program:

#include <stdio.h>

int main(void) {

int num;

scanf("%d", &num);

if (num > 5 & num <= 18)

if (num < 3 || num > 2)

printf("A.\n");

else

printf("B.\n");

return 0;

}

12) If the program produces output, what is the output it produces?

a)A.b)B.c) A.d) The program can e) None of the above.

B. produce different

outputs.

13) How many different positive integers less than 19 entered for num cause the program to produce no output?

a) 0b) 4c) 5d) 6e) None of the above.

14) What is the output of the following segment of code? (Assume that sum and index are declared as integer variables.

sum = 0;

for (index = 1; index < 5; index++);

sum += index;

printf("%d %d\n", sum, index);

a)10 4b)10 5c)15 5d)0 5e)5 5

15) What is the output of the following segment of code? (Assume that cnt and num are declared as integer variables.

cnt = 0;

num = 100;

while (num > 0) {

if (num < 10)

cnt++;

num--;

}

printf("%d\n", cnt);

a)8b)9c)10d)45 e)None of the above.

16) Let a, b, c, d, and e be int variables all with the value of 0 or 1. How many possible combinations of values of these variables would make the following boolean expression true?

(a & b & c) || !(d || e)

a)1 b)4c)12d)16e)None of the above.

17) Which of the following expressions is illegal in C? (Assuming a, b, and c are int variables.)

a) a > b > cb) a = b == cc) a == bd) (a > b) != ce) None of the above.

18) Which of the following expressions will be true if and only if the int variable b is greater than the int variable a and less than the int variable c?

a) c > b > ab) b > a < cc) a < b || b < c d) b > a & c > be) None of the above.

19) What segment of code would be equivalent to the following segment?

while (1) {

c++;

break;

if (c < 10)

c += 5;

}

a)c++;b)while (c < 10)c) while (c++)

c += 5;if (c<10)

c+=5;

d) if (c < 10)e) None of the

c += 5; above.

20) The Boston Red Sox play home games at Fenway Park. In what city is Fenway Park located?

a) Massachusettsb) Orlandoc) Bostond) Red Soxe) None of the above.

SHORT ANSWER SECTION

21) (5 pts) The following program converts the temperature the user enters from Celsius to Fahrenheit, and prints this converted value to the screen. Fill in the missing statement indicated in the program below so that it will properly complete its task:

The formula that converts F° Fahrenheit to C° Celsius is .

#include <stdio.h>

int main(void) {

int cel;

double fahr;

printf("Enter the temperature in Celsius.\n");

scanf("%d", &cel);

/*** stmt1 ***/

printf("%d Celsius = %lf Fahrenheit\n", cel, fahr);

return 0;

}

22)(5 pts) The following program is supposed to ask the user to enter an integer and determine if the integer ends in a 0 or 5, or if it ends in some other digit and print out this information. Fill in the appropriate expression to make the program below run properly.

#include <stdio.h>

int main(void) {

int num;

printf("Enter a number.\n");

scanf("%d", &num);

if (/*** expr1 ***/)

printf("Your number ends in a 0 or 5.\n");

else

printf("Your number does not end in a 0 or 5.\n");

return 0;

}

23) (5 pts) Name two reasons for putting comments in your programs.

Scratch Page – No work on this page will be graded, but you may use it to work out your answers.

Fall 2003 COP 3223 Section 1

Exam 1 Answer Sheet FORM A

Name : ______

WebCT ID: ______

21) (5 pts) stmt1 is ______

22) (5 pts) expr1 is ______

23) (5 pts) ______

______

______

24) (10 pts) Complete the program below so that it reads in 3 integers from the user and prints out the smallest of the three. The user should NOT be prompted to enter in the values.

#include <stdio.h>

int main(void) {

int a,b,c;

scanf("%d %d %d\n", &a, &b, &c);

return 0;

}

25) (15 pts) Complete the program below so that prints out a triangle in n lines using *'s. An example of the shape of the triangle for n=4 is provided below. A portion of the program that reads in the value of n from the user is written for you. You may add variable declarations before the printf given and the rest of your code after the scanf.

****

***

**

*

#include <stdio.h>

int main(void) {

int n;

printf("Enter the size of your triangle.\n");

scanf("%d", &n);

return 0;

}