






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Wu; Class: Intro Prog&Prob Solving; Subject: Engineering Computer Science; University: University of California - Davis; Term: Fall 2008;
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!







st
Name: __________________________ Student ID: __________________________ E-mail: __________________________
close book/notes. We have totally 10 questions (two points for each, totally 20%). 10 pages Please write precise and clean answers. If you write down the steps regarding how you obtain the final answer, I might give you partial credits even the final answer itself is wrong. Please write clearly to let the instructor recognize/understand your hand writing. Every page of this exam book needs to be returned. Also, while turning in your exam book, please make sure to SIGN the special sign-up sheet. If you do NOT sign up and in case we lose your exam book, it will be hard to convince us that you actually turn in the exam book back to us. If we suspect any cheating behavior, we will pass the case to the academic committee immediately.
Q-01 Question/Answer (2%)
differences between the following two command lines. (Hint: please describe which file will be produced by which line and why.)
warnings) from the following pieces of code. (Hint: there are about 6~7 errors, but you only need to identify 4 of them to get the full credits for this question.) Please circle the errors and write down the fixes.
Q-05 Question/Answer: (2%)
“ bffffa08 ”, FIVE more lines of outputs should be printed (i.e., there are FIVE printf lines after the scanf). Please write down those five lines of outputs.
Q-06 Question/Answer: (2%)
printf("[06] %x\n", (int) (((int **)(&x)) + 1)); printf("[07] %x\n", (int) (((char **)(&x)) + 1)); What are those two extra outputs?
As long as it is a pointer, the size is four.
Q-08 Question/Answer: (2%)
following segment of code. double p, q; if (p >= q) { double tmp; tmp = p; p = q; q = tmp; } Description: When p is greater than and equal to q, swap their values.
Q-09 Question/Answer: (2%)
the following C program? Please write down the print out of the execution. #include <stdio.h> int swap(double p, double q) { if (p >= q) { double tmp; tmp = p; p = q; q = tmp; return 1; } else { return 0; } } int main (void) { double x, y; int rc = 0; x = 100.0; y = 50.0; rc = swap(x,y); printf("rc = %d, x = %f, y = %f\n", rc, x, y); return 0; } Output: (one line) rc = 1, x = 100.00, y = 50.
#include <stdio.h> int main(void) { // declaration of the variables int x,y,z; // getting the inputs printf("please enter the value of x:"); scanf("%d", &x); printf("please enter the value of y:"); scanf("%d", &y); printf("please enter the value of z:"); scanf("%d", &z); printf("x = %d, y = %d, z = %d\n", x,y,z); // input validation (x,y,z must all be non-negative.) if ((x < 0) || (y < 0) || (z < 0)) { return 1; } // printing these three numbers in the sorted order { // our plan: x: largest, y: middle, z: smallest int tmp; if (y < z) { tmp = y; y = z; z = tmp; } // now, y>=z, not sure about x if (x < y) { tmp = x; x = y; y = tmp; } // now, x is the largest, but unclear about y & z if (y < z) { tmp = y; y = z; z = tmp; } // x is the largest, then y, then z. printf(“%d\n%d\n%d\n”, x,y,z); } // return 0 only if the inputs are all valid return 0; }