COS 126 General Computer Science Spring Midterm 1

Similar documents
COS 126 General Computer Science Fall Written Exam 1

COS 126 General Computer Science Fall Midterm 1

COS 126 MIDTERM 1, FALL

COS 226 Midterm Exam, Spring 2009

COS 126 General Computer Science Spring Written Exam 1

COS 226 Midterm Exam, Spring 2011

COS 126 Midterm 1 Written Exam, Fall 2009

COS 126 General Computer Science Fall Exam 1

COS 126 Midterm 1 Written Exam Spring 2015

COS 126 General Computer Science Fall Exam 1

COS 226 Algorithms and Data Structures Fall Midterm

COS 126 General Computer Science Spring Written Exam 2

COS 126 Midterm 1 Written Exam Fall 2011

COS 226 Algorithms and Data Structures Spring Midterm

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

COS 226 Algorithms and Data Structures Fall Midterm

COS 126 General Computer Science Fall Exam 1

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

11/22/1999 7pm - 9pm. Name: Login Name: Preceptor Name: Precept Number:

Midterm 1 Written Exam

Computer Science Foundation Exam

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

COS 126 Midterm 1 Written Exam Fall 2012

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Midterm Exam, Spring 2010

COS 226 Algorithms and Data Structures Spring Midterm

COS 126 General Computer Science Spring Written Exam 1

COS 226 Algorithms and Data Structures Fall Final

COS 126 Midterm 1 Written Exam Fall 2014!

CS 223: Data Structures and Programming Techniques. Exam 2

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

COS 226 Algorithms and Data Structures Spring Midterm

Computer Science E-119 Practice Midterm

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

CS150 Sample Final. Name: Section: A / B

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

COS 126 Written Exam 2, Fall 2009

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

CSE 143 Sample Midterm Exam #4

Computer Science 217 Final Exam May 15, :30pm-4:30pm

Computer Science 217 Final Exam January 25, :30-4:30pm

The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

COS 226 Algorithms and Data Structures Spring Second Written Exam

CS150 Sample Final Solution

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Computer Science Foundation Exam

CSE 142 Wi01 Midterm 2 page 1 of 6

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE 143 SAMPLE MIDTERM

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

COS 126 Midterm 2 Programming Exam Fall 2012

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

C0MP1921/2011/2091 SAMPLE Final Exam Data Structures and Algorithms/Data Organisation/Computing 2

CS2500 Exam 2 Fall 2011

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

'C' Programming Language

Midterm, Spring 2013

S.E. (Computer) (First Semester) EXAMINATION, 2011 DATA STRUCTURES AND ALGORITHM (2008 PATTERN) Time : Three Hours Maximum Marks : 100

CS106B Sprint 2017 April 27 th, 2017 CS106B Practice Midterm (KEY)

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Practice Midterm Exam #1

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

CSE413 Midterm. Question Max Points Total 100

This exam is worth 24 points, or 24% of your total course grade. The exam contains six

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

COSC242 Lecture 7 Mergesort and Quicksort

Information Science. No. For each question, choose one correct answer and write its symbol (A E) in the box.

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Question 2. [5 points] Given the following symbolic constant definition

COS 226 Final Exam, Spring 2009

Goals of this Lecture

COS 226 Algorithms and Data Structures Spring Midterm Exam

UNIVERSITY OF WISCONSIN MADISON

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C Programming

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE P 501 Exam 12/1/11

This is CS50. Harvard University Fall Quiz 0 Answer Key

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Draw a diagram of an empty circular queue and describe it to the reader.

CSCI 2132 Software Development. Lecture 19: Generating Permutations

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

CS13002 Programming and Data Structures, Spring 2005

Transcription:

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