You are on page 1of 5

COSC 1320 Exam 1 (Chapters 1, 2, and 3)

Part 1 (50 points) Write the following programs using the C-language IDE. Make sure the
programs compile and run

1. Write a program that prompts the user to input two numbers.


(5 points) The program should multiply the two numbers together and then output the
answer.
(5 points)Programming requirement: The program shall be written in 3 well-defined
sections: Input – Processing - Output

2. (20 pts) Write a program that calculates and displays a person’s body mass index (BMI). A
person’s BMI is calculated with the following formula:
BMI = weight× 703/height2

Where weight is measured in pounds and height is measured in inches. The program
should display a message indicating whether the person has optimal weight, is
underweight, or is overweight. A person’s weight is considered optimal if his/her BMI is
between 18.5 and 25. If the BMI is less than 18.5, the person is considered underweight. If
the BMI value is greater than 25, the person is considered overweight.

3. (20 pts) Write a program that simulates a fast-food restaurant.


Ask the user to enter which menu number they want.
Write a menu that looks as follows:
• 1 – Single meat hamburger $2.99
• 2 – Double meat hamburger $3.99
• 3.- Salad with no meat $4.99
• 4 – Salad with meat $5.99
In addition to asking for the menu number, your program shall ask if the user wants
cheese or not. If the user answers ’yes’, then add $1.00 to the total. At the end calculate the
tax (i.e., 8.25%) of the total and then provide the total of the entire purchase.
Programming note: Your program must use a ‘switch’ statement.

To Submit the programs requested above:


Please copy/Paste the source code to the above 3 questions onto 3 separate Notepad files.
You may ‘zip’ them together into one file to Submit them.
COSC 1320 Introduction to C Programming Test 1
Part 2 (50 points) Please provide the answers by either highlighting them, or, if you prefer
in a notepad/text file in the format 1-a, 2-b, 3-c etc.
1. Which of the following is not a rule that must be followed when naming identifiers?
(a) Identifiers can contain spaces.
(b) The first character must be one of the letters a-z, A-Z, and underscore or a
dollar sign.
(c) Uppercase and lowercase characters are distinct.
(d) After the first character, you may use the letters a-z, A-Z, the underscore, a
dollar sign, or digits 0-9.

2. Which of the following is not a primitive data type?


(a) int (b) char (c) float (d) String
3. Which of the following statements is valid (may be more than one) ?
(a) float y;
y = 54.9;
(b) char z;
z = 934.21;
(c) int w;
w = 1.0;
(d) char answer;
answer = “Yes”;
4. What is the result of the following expression?
25 / 4 + 4 * 10 % 3
(a) 19 (b) 5.25 (c) 7 (d) 3
5 What will be the value of z as a result of executing the following code?
int x = 5, y = 28;
float z;
z = (float) (y / x);
(a) 5.60 (b) 5.0 (c) 3.0 (d) 5.6

Explain your answer:

Page 2
COSC 1320 Introduction to C Programming Test 1
6 What will be displayed as a result of executing the following code?
int value1 = 9;
printf(value1);
int value2 = 45;
printf(value2);
printf(value3);
value3 = 16;

a) 9
45
16
b) 94516
c) 9 45 16
d) Nothing, this is an error

7 If the following code is executed, what will be the value of ‘x’?


const int x = 10;
x = 20;
a) x will have the value 10
b) x will have the value 20
c) x will have the value 1020
d) This will cause an error

8 Which of the following is the correct way to input an ‘int’ from the user?
a) scanf(number1);
b) scanf(“%d”, &number1);
c) scanf(“&number1);
d) scanf(“%d &number1”);

9 Which of the following is the correct boolean expression to test for: int x being a
value between, but not including, 500 and 650?
a. (x >= 500 && x <= 650)
b. (x > 500 && x < 650)
c. (x > 500 AND x < 650)
d. (x < 500 && x > 650)

Page 3
COSC 1320 Introduction to C Programming Test 1
10 If cResponse is a char, which of the following ‘if’ statements would be considered
valid to see if cResponse is equal to the character?
a) if (cResponse = ‘a’)
b) if (cResponse == ‘a’)
c) if (cResponse == “a”)
d) if (cResponse .EQ. ‘a’)

11. What is the value of ans after the following code has been executed?
int x = 40;
int y = 50;
int ans = 0;
if (x == y) ;
ans = x + 10;
(a) 50
(b) 0
(c) 80
(d) 30
(e) No value, this is a syntax error.

12 What would be the value of discountRate after the following statements are
executed?
float discountRate;
char custType = ’B’;
switch (custType){
case ’A’:
discountRate = .08;
break;
case ’B’:
discountRate = .06;
case ’C’:
discountRate = .04;
default:
discountRate = 0.0; }
(a) .08
(b) .06
(c) .04
(d) 0.0

Page 4
COSC 1320 Introduction to C Programming Test 1

13 Which of the following correctly tests the char variable chr to determine
whether it is not equal to the character B?
a. if (chr != ’B’)
b. if (chr < ’B’)
c. if (chr > ’B’)
d. if (chr != "B")

14 When testing for character values, the switch statement does not test for the
case of the character.
(a) true (b)false
15 An important style rule you should adopt for writing if statements is to write
the conditionally executed statement on the line after the if statement test.
(a) true (b)false
16 When writing an ‘if’ statement: if there is only one statement that follows the
test, the curly braces do not have to be used.
(a) true (b)false
17 The if/else statement will execute the first group of statements if its boolean
expression is true and the second group if its boolean expression is false.
(a) true (b)false
18 Because the && operator performs short-circuit evaluation, your boolean
expression will usually execute faster if the subexpression that is most likely
false is on the left of the && operator.
(a) true (b)false
19 A local variable’s scope always ends at the closing brace of the block of code in
which it is declared.
(a) true (b)false
20 A variable of type ‘char’ can only hold a single character at any one time.
(a) true (b)false

Page 5

You might also like