Midterm Exam 1 Solution - Introduction to Programming and Problem Solving | ECS 030, Exams of Computer Science

Material Type: Exam; Professor: Wu; Class: Intro Prog&Prob Solving; Subject: Engineering Computer Science; University: University of California - Davis; Term: Fall 2008;

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-19a
koofers-user-19a 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ecs30b – 1st Midterm Exam. Fall 2008 – October 17, 2008
ECS-30b, Fall 2008, 1st Midterm Exam Key
Name: __________________________
Student ID: __________________________
E-mail: __________________________
Important Instructions:
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.
-1/10-
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Exam 1 Solution - Introduction to Programming and Problem Solving | ECS 030 and more Exams Computer Science in PDF only on Docsity!

ECS-30b, Fall 2008, 1

st

Midterm Exam Key

Name: __________________________ Student ID: __________________________ E-mail: __________________________

Important Instructions:

 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%)

(Different Options/Phases in gcc, the GNU C Compiler) Please describe the

differences between the following two command lines. (Hint: please describe which file will be produced by which line and why.)

  1. % gcc –c ecs30b_hw1.c
  2. % gcc ecs30b_hw1.c
  3. compiling only (ecs30b_hw1.o)
  4. compiling & linking (a.out, executable file) Q-02 Question/Answer: (2%)

(Finding C Syntax Errors) Please identify/correct 4 syntax ERRORS (not

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.

01 #inc 1 ude <stdio.h>

02 #define KMS_PER_MILE 1.

03 int

04 main(void)

05 [

06 double miles ”missing ;”

07 double kms;

08 print ”?f” ("Enter the distance in miles> ?” );

09 scanf("%lf", &miles ?) ;

10 kms = KMS_PER_MILE * miles;

11 printf("That equals %f kilometers.\n", KMS );

12 return 0;

(Value/Content/Address/Pointer) The following two questions (Questions 5 and

  1. are related to the following program: // y.c #include <stdio.h> int main(void) { int x; int xp; printf("size [int, char] = [%d, %d]\n", (int) sizeof(int), (int) sizeof(char)); xp = &x; printf("&x = 0x%x\n", (int) &x); fprintf(stderr, "Please enter the value of x >"); scanf("%x", xp); printf("[01] %x\n", (int) &x); printf("[02] %x\n", (int) (((int )x))); printf("[03] %x\n", (int) (((int )(xp)))); printf("[04] %x\n", (int) (((int *)(&x)) + 1)); printf("[05] %x\n", (int) (((char *)(&x)) + 1)); return 0; } Assuming we are running the above program on a 32-bits address machine. The following is the execution of the program until the statement of “scanf”: MacBook-3:/Users/wu% MacBook-3:/Users/wu% gcc -Wall -Wstrict-prototypes y.c MacBook-3:/Users/wu% ./a.out size [int, char] = [4, 1] &x = 0xbffffa Please enter the value of x > bffffa

Q-05 Question/Answer: (2%)

(Program Execution and its Outputs) Based on the program and the input of

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.

[01] bffffa

[02] bffffa

[03] bffffa

[04] bffffa0c

[05] bffffa

Q-06 Question/Answer: (2%)

(More Outputs) If I add two more lines of code right before the “return 0;” line:

printf("[06] %x\n", (int) (((int **)(&x)) + 1)); printf("[07] %x\n", (int) (((char **)(&x)) + 1)); What are those two extra outputs?

int *

(int *) *

char *

As long as it is a pointer, the size is four.

[06] bffffa0c

[07] bffffa0c

Q-08 Question/Answer: (2%)

(The Intention of a Code Segment) Please describe the purpose/intention of the

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%)

(Call-by-Value, Basic Functions/Modules in C) What would be the output of

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; }