Professional Documents
Culture Documents
Part 1 (50 points) Write the following programs using the C-language IDE. Make sure the
programs compile and run
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.
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
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