set4 85-92 Flashcards

1
Q

/*85
What output line(s) are displayed by the statements that follow when grade is ‘I’? When grade is ‘B’? and
When grade is ‘b’?
*/

switch(grade)
 {
 case 'A':
 points = 4;
 break;
 case 'B':
 points = 3;
 break;
 case 'C':
 points = 2;
 break;
 case 'D':
 points = 1;
 break;
 case 'E':
 case 'I':
 case 'W':
 points = 0;
 }
 if(points \> 0)
 printf("Passed, points earned = %d\n", points);
 else
 printf("Failed, no points earned\n");
A

/*
grade = ‘I’ – failed
grade = ‘B’ – Passed, points earned = 3;
grade = ‘b’ – failed
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

/*86
Explain the difference between the following two programs. For each group of statements, give the final value of x if
the initial value of x is 1.
*/
Program 1:
if(x >= 0)
x = x + 1;
else if(x >= 1)
x = x + 2;

Program 2:
if(x >= 0)
x = x + 1;
if(x >= 1)
x = x + 2;

A

/*
Program 1 executes the elif statement only if the first condition (ie x >= 0) is not met, whereas Program 2 will check move if conditions separately.

Program 1
Input x = 1; Output x = 2
Program 2
Input x = 1; Output x = 4
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

/* 87
Study the following program fragment:
*/
if(x==1)
if(y==2)
if(z==3)
printf(“a\n”);
else
printf(“b\n”);
else
printf(“c\n”);
printf(“d\n”);
/*
What are the range of values of x, y, and z, that will lead to the execution of each printf statement?

A

Print ‘a’ iff x = 1, y = 2, and z = 3;
Print ‘b’ iff x = 1, y = 2, and z != 3;
Print ‘c’ iff x = 1 and y !=2
Print d’d iff x = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

/* 88
Write a multiple-alternative if statement to display a message indicating the educational level of a student based on
the student’s number of years of schooling (0, none; 1-5, elementary school; 6-8. middle school; 9-12. high school;
more than 12, college). Print a message to indicate bad data as well.
*/

A
if(years \< 0) printf("bad input.");
 else if (years == 0) printf("none.");
 else if (years \<= 5) printf("elementary school.");
 else if (years \<= 8) printf("middle school.");
 else if (years \<=12) printf("high school.");
 else printf("college");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

/* 89
Write a switch statement to select an operation based on the value of inventory. Increment total_paper by
paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is
‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘x’. Do nothing if
inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters.
*/

A

switch(value)

case ‘B’: case ‘C’:
total_paper += paper_order; break;
case ‘E’: case ‘F’: case ‘D’:
total_ribbon += ribbon_order; break;
case ‘A’: case ‘x’:
total_label += label_order; break;
case ‘M’: break;
default:
printf(“Invalid input.”); break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

/* 90
Write an if statement that displays an acceptance message for an astronaut candidate if the person’s weight is
between the values of opt_min and opt_max inclusive, the person’s age is between age_min and age_max
inclusive, and the person is a nonsmoker (smoker is false).
*/

A

if (opt_min <= weight <= opt_max

&& age_min < age < age_max

&& smoker==false)

printf(“Astronaut, yes?”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

/* 91
Write a switch statement that assigns to the variable lumens the expected brightness of a standard light bulb
whose wattage has been stored in watts. Use this table:
Watts: 15 25 40 60 75 100
Brightness (in Lumen): 125 215 500 880 1000 1675
Assign -1 to lumens if the value of watts is not in the table
*/

A

switch(watts)
case 15: lumens = 125; break;
case 25: lumens = 215; break;
case 40: lumens = 500; break;
case 60: lumens = 880; break;
case 75: lumens = 1000; break;
case 100: lumens = 1675; break;
default: lumens = -1; break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

/* 92
Write a nested if statement that assigns to the variable lumens the expected brightness of a standard light bulb
whose wattage has been stored in watts. Use this table:
Watts: 15 25 40 60 75 100
Brightness (in Lumen): 125 215 500 880 1000 1675
Assign -1 to lumens if the value of watts is not in the table
*/

A
if (watts == 15) lumens = 125;
 else if (watts = 25) lumens = 215;
 else if (watts = 40) lumens = 500;
 else if(watts = 60) lumens = 880;
 else if (watts = 75) lumens = 1000;
 else if (watts = 100) lumens = 1675;
 else lumens = -1;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly