Q4 85-92 Flashcards
/*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”);
/* grade = ‘I’ – failed grade = ‘B’ – Passed, points earned = 3; grade = ‘b’ – failed */
/*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;
/* 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 */
/* 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?
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;
/* 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. */
if(years < 0) printf(“bad input.”); else if (years == 0) printf(“none.”); else if (years else if (years else if (years else printf(“college”);
/* 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. */
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;
/* 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). */
if (opt_min && age_min < age < age_max && smoker==false) printf(“Astronaut, yes?”);
/* 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 */
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;
/* 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 */
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;