Each Question Is Worth 5 Points Unless Otherwise Indicated

Each Question Is Worth 5 Points Unless Otherwise Indicated

Name______April 18, 2014

Exam IIComputer Science 115

Each question is worth 5 points unless otherwise indicated.

Write the letter of the answer in the blank provided on these multiple choice questions.

______1. An example of a char constant is:

A. '+'

B. "/"

C. 'ABC'

D. 65

______2. Which of the following creates a C string containing the name “Clive”:

A. string name = “Clive”;

B. char name[6] = “Clive”;

C. char name[5] = “Clive”;

D. char name = “Clive”;

______3. Which of following expressions is true?

A. 9 != 5 + 4 || 7 + 7 != 14

B. 10 < 4 & 7 == 3 + 4

C. 10 * 2 <= 30 & 5 < 21

D. 'w' == 'W'

______4. In an if statement, the else branch:

A. Is only executed when the condition is true.

B. Is only executed when the condition is false.

C. Is always executed.

D. Is require in the syntax of the if statement.

______5. The first step in debugging a program is:

A. Try changing the program.

B. Predict what the error might be.

C. Delete the program.

D. Conduct a test to validate cause.

______6. These types can be used to control a switch statement:

A. int and double

B. char and string

C. int and char

D. bool and double

7. In C, what is the difference between the operators = and ==?

8. Name at least three style guidelines for naming variables .

9. What is integer overflow?

10. str1 is initially "Hello", str2 is "Hi".
After strcpy(str1,str2), what is str1?
Omit the quotes.

11. Write relational expression to express the following conditions: (5 points each)

a. A person's height is less than six feet.

b. Side a equals side b and side b equals side c.

12.Show the output of this program.

#include <stdio.h>

int main(){

char lastInitial = 'C';

if (lastInitial > 'K'){

printf("Go to room 2432 for tickets.\n");

}

else{

printf("Get you tickets here.\n");

}

return 0;

}

13.Show the output.

#include <stdio.h>
#include <stdbool.h>
int main(){
bool flag = true;
if (flag)
printf("Track muddy.\n");
else
printf("Track OK.\n");
return 0;
}

14.Show the output.

#include <stdio.h>
#include <string.h>
int main(){
char name[20] = "Terry Wilson";
char address[20] = "123 Easy St.";
char city[20] = "Denver, CO 77881";
strcpy(address,"775 Idaho St.");
strcpy(city, "Boise, ID 87325");
printf("%s\n", name);
printf("%s\n", address);
printf("%s\n", city);
return 0;
}

15.Correct the error that causes the error messages that follow this program. Line numbers are provided to help with the debugging.

1 #include <stdio.h>

2

3 int main(){

4 float gal;

5 gal = 15.5;

6

7 if (gal > 10.0);

8 printf("Enough for trip\n");

9 else

10 printf("Not enough gas\n");

11

12 return 0;

13 }

Error Messages:

gas1.c: In function ‘main’:

gas1.c:9: error: ‘else’ without a previous ‘if’

16.This program is supposed in input a month number then output the corresponding month name. It does not work correctly. See the output below. Fix the error.

#include <stdio.h>

int main()

{

int month;

printf("Enter month number from 1 to 4>");

scanf("%d",&month);

switch (month)

{

case 1: printf("January\n");

case 2: printf("February\n");

case 3: printf("March\n");

case 4: printf("April\n");

}

return 0;

}

SAMPLE EXECUTION: (Input is grayed.)

Enter month number from 1 to 4>2

February

March

April

17.Write a program that compares the scores of the home team and visitors. Ask the use for the scores of the visitors and the home team. If the home teamscore is higher than the visitors score, output "We win!" otherwise output "Better luck next game." (10 points)

1