CSE 130 Solutions Sample Exam-1 1. (a) Yes (b) No (c) No (d) Yes (e) Yes 2. (a) False (b) 8 (c) True (d) False 3. switch (class) { case 1: dues = 100 + salary * 0.02; break; case 2: dues = 50 + salary * 0.01; break; default: printf("Wrong class"); } 4. (a) No error (b) Type should be char and not character. (c) constant in place of const (d) !== is not a valid opeartor. 5. The innermost for loop must have only one statement that prints a[j]. It does not need a body. But after every row, we must print \n. So outer for loop has two statements. The first one is a the inner for loop, and the second one that prints \n. Actually we want two \n's here. See the comment below. # include int main ( ) { int i, j; int a[5] = {0, 1, 2, 3, 4}; for (i = 4; i >= 0; i--) // worth 4 points. --> { for (j = i, j >= 0; j--) printf("%d", a[j]); // worth 2 points. printf("\n"); } <--- Insert { } here. 2 points // Actually, here we want two \n, like \n\n. // I forgot to include that. One student pointed // that out. We did not deduct any points for this. return 0; } 6. (a) 10 (b) 6 (c) int sum = 0, n; n = ??? // some value if (n < 0) n = -n; // Move this outside. while (n > 0) { sum = sum + n; n = n-1; } 7. # include main ( ) { int distance, speed, hours, minutes; float time; printf ("Enter distance and speed: "); scanf ("%d%d", &distance, &speed); <--- time = ((float)distance)/speed; hours = (int)(time); <-- Get the integer part. // time - hours is minutes in decimal. Multiply by 60, // and cast to an integer. minutes = (int) ( (time - hours)*60 ); printf ("Time required: %d hours and %d minutes", hours, minutes); } 8. int even (int m) // 2 points { if (m%2 == 0) return 1 else return 0; } // 3 points 9 (a) Just a simple swap, there was no need to write scanf and printf statements. temp = A[p]; A[p] = A[q]; A[q] = temp; (b) One if statement was expected. Both p and q should be >= 0 and <= 6. Anything else would be an error. if ( (p < 0) || (q < 0) || (p > 6) || (q > 6) ) error = 1; else error = 0; Inner brackets are added for clarity. They are not required. If any of the 4 conditions are violated then we have an error. That why we use OR operation (||).