COS 126 General Computer Science Spring 2003 Midterm 1 This test has 9 questions worth a total of 50 points. You have 120 minutes. The exam is closed book, except that you are allowed to use a one page cheatsheet. No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. Write out and sign the Honor Code pledge before turning in the test. I pledge my honor that I have not violated the Honor Code during this examination. Problem Score Problem Score 0 5 1 6 2 7 3 8 4 Sub 1 Sub 2 Total Name: Login ID: Precept: 1 MF 10:00 Donna 2 MF 11:00 Kai 3 MF 1:30 Lisa 4 MF 2:30 Lisa 5 M 7:30 Lisa F 2:30 1
2 PRINCETON UNIVERSITY 0. Miscellaneous. (2 points) (a) Write your name and arizona login in the space provided on the front of the exam, and circle your precept number. (b) Write and sign the honor code on the front of the exam. 1. Number systems. (6 points) (a) Convert the following binary number to decimal. Circle your final answer. 10110110111 1463 (base 10) (b) Convert the following hexadecimal number to octal. Circle your final answer. 2B3D 25475 (base 8) (c) Give the sum (in hex) of the two hexadecimal numbers: 3A6 and 5B. Circle your final answer. 401 (base 16)
COS 126 MIDTERM 1, SPRING 2003 3 2. C and Shell basics. (8 points) prog.c: #include <stdio.h> int main(void) { int num; scanf("%d", &num); printf("%d", num/2); return 0; Suppose that you compile the program above as follows: Windows C:\> lcc126 prog.c Unix % gcc126 prog.c -o prog Assume the following: (a) The file input has a single number 6 in it; and (b) if the keyboard input is needed, you will type 6. Answer what the following commands do by filling out the table. If the command results in an error, write yes in the error column and leave everything else blank. keyboard terminal output output command error input output file text 0 prog yes yes 3 1 input < prog yes 2 input prog yes 3 prog < input yes 3 4 prog output yes 5 prog > output yes yes 3 6 prog prog yes yes 1 7 prog < input prog yes 1 8 prog prog prog > output yes yes 0
4 PRINCETON UNIVERSITY 3. Functions. (5 points) /* Some noticed the missing # */ #include <stdio.h> int main(void) { int i,n; scanf("%d",&n); i=2; while (n > 1) { if (n % i == 0) { printf("%d ",i); n = n/i; else { i++; return 0; (a) What does this program print if n is 1050? 2 3 5 5 7 (b) What does this program do in general? Prints the prime factors.
COS 126 MIDTERM 1, SPRING 2003 5 4. Stacks. (6 points) POSTFIX EXPRESSION: 6 5 4 3 + 2 1 - * + - (a) Show the state of the stack array after each step of evaluating the postfix expression above. Assume an array-based implementation of the stack which performs no unnecessary array operations. Circle the array element containing the value of the evaluated expression. INITIAL 0 0 0 0 0 0 STEP 1 6 0 0 0 0 0 STEP 2 6 5 0 0 0 0 STEP 3 6 5 4 0 0 0 STEP 4 6 5 4 3 0 0 STEP 5 6 5 7 3 0 0 STEP 6 6 5 7 2 0 0 STEP 7 6 5 7 2 1 0 STEP 8 6 5 7 1 1 0 STEP 9 6 5 7 1 1 0 STEP 10 6 12 7 1 1 0 STEP 11-6 12 7 1 1 0 ------- (b) What is the infix expression for this postfix expression? (6 - (5 + ((4 + 3) * (2-1)))) (While the given postfix to infix algorithm keeps the order of digits the same, all equivalent infix expressions were accepted.)
6 PRINCETON UNIVERSITY 5. Arrays. (5 points) Consider the following C program (the numbers on the left are line numbers for reference only and are not part of the code): 1 #include <stdio.h> 2 #define N 20 3 #define NONP 0 4 #define P 1 5 #define UNKNOWN 2 6 7 int main(void) { 8 int i, j, a[n]; 9 10 for (j = 2; j < N; j++) 11 a[j] = UNKNOWN; 12 13 for (i = 2; i < N; i++) { 14 if (a[i] == UNKNOWN) { 15 a[i] = P; 16 printf("%d ", i); 17 for (j = i; i*j < N; j++) 18 a[i*j] = NONP; 19 20 21 return 0; 22 (a) What will the contents of the a[] array be after the loop at lines 10-11 is complete? a[0], a[1] uninitialized a[2] -> a[19] assigned 2
COS 126 MIDTERM 1, SPRING 2003 7 (b) What will the program print? 2 3 5 7 11 13 17 19 (c) What does this program do in general? Prints prime numbers less than N
8 PRINCETON UNIVERSITY 6. Data Structures. (6 points) POINT.h: typedef struct { double x; double y; Point; (a) Define a type Rect for rectangles that are parallel to the axes in Cartesian coordinate system. Represent a rectangle by its lower left and upper right endpoints using the Point type above. typedef struct { Point LowerLeft; Point UpperRight; Rect; (b) Write a function RECTarea() that computes the area of a rectangle. double RECTarea(Rect r) { double width, height; width = r.upperright.x - r.lowerleft.x; height = r.upperright.y - r.lowerleft.y; return width * height;
COS 126 MIDTERM 1, SPRING 2003 9 (c) Is Point an ADT? Why or why not? No. The structure is not hidden from client. (d) Is Rect an ADT? Why or why not? No. The structure is not hidden from client. (Note that the client passes a value of type Rect to RECTarea.) (e) How do ADTs help programmers manage code? 1. Implementation can be changed without changing clients. (Common interface) 2. Hiding implementation behind a well-defined interface allows programmers to focus on higher-level issues without concern for lower-level details.
10 PRINCETON UNIVERSITY 7. Recursion. (6 points) Consider the following recursive functions. int mystery1(int n) { if (n == 0) return 0; else return mystery2(n - 1); int mystery2(int n) { if (n == 0) return 1; else return mystery1(n - 1); (a) What is the return value of mystery1(5)? Circle your answer. 1 (b) Approximately how many function calls are made when calling mystery1(n) for some large positive integer n. Circle the best answer. i. log 2 n ii. n iv. 2 n v. n! iii. n 2 The correct answer is: ii. n
COS 126 MIDTERM 1, SPRING 2003 11 (c) Given a positive integer n, what does the function call mystery1(n) return? Explain its function in one English sentence. Do not paraphrase the code. 1 if odd, 0 if even (d) Rewrite the function mystery1() without recursion. int mystery1(int n) { return n%2;
12 PRINCETON UNIVERSITY 8. Quicksort. (6 points) Suppose that the standard in-place recursive quicksort (choose the rightmost element as the partitioning element) is used to sort an array containing the following keys: A B C D E F G H (a) For the above keys, list each call to partition with the values of its arguments. (First call is given.) First call: partition(a, 0, 7) partition(a, 0, 6) partition(a, 0, 5) partition(a, 0, 4) partition(a, 0, 3) partition(a, 0, 2) partition(a, 0, 1) (b) In general, how many partition s does quicksort perform on a sorted list of N elements? n - 1
COS 126 MIDTERM 1, SPRING 2003 13 For the following keys: E C B F G H A D (c) For the above keys, list each call to partition with the values of its arguments. First call: partition(a, 0, 7) partition(a, 0, 2) partition(a, 4, 7) partition(a, 6, 7) (d) To keep the number of partitions to a minimum, the ideal partition value has what property? median value