1. What is the value of variable n after the C statements below have been execut
ID: 3924511 • Letter: 1
Question
1. What is the value of variable n after the C statements below have been executed?
n = 3; m = 5; if ( m – 2 * n <= 0) { n += 2; } else { n--; }
2. What is the value of variable c after the following code executes, assuming the value input is 10 ?
int a = 0; int b; int c; scanf(“%d”, &a); switch (a) { case 0: c = 0; break; case 100: c = 1; break; case 10: c = 3; default: c = 2; }
3. What is displayed by the following code segment assuming variables x,y, and z are already initialized? Apply De Morgan's rules. (The actual values of x,y,z do not matter in this example.)
if ((x < y && y < z) == !(y <= x || y >= z)) printf("A"); else if (x< y || x >= y) printf(“B"); else printf("C");
4. For what exact range of values of variable g of type int does the following code segment display the string “B+” ?
if (g < 78) { printf("B-"); } else if (g <= 82) { printf("B"); } else if (g < 86) { printf("B+"); } else if (g < 91) { printf("A-"); } else { printf("A"); }
5. If the input to the program segment below is 87, what is its output?
scanf("%d", &s); if (s >= 92) printf("A "); else if (s >= 80) printf("B "); else if (s >= 85) printf("C "); else printf("D ");
Explanation / Answer
1) m-2*n is 5-6 which is less than 0. So n-- will be executed so n will be 2.
2) a = 10
case 10 will be executed and c becomes 3.
3) Always A will be printed.
Let us change !(y <= x || y >= z)) this can be rewritten by demorgans rules as (!(y<=x)) && (!(y>=z)). This will be x<y && y<z. So the first if condition will always be true.
4) The first if should be skipped so g>=78. Next else if g>82, but next else if must be true. So g<86.
So g can only be 83,84,85.
5) s = 87.
B will be printed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.