: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

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

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

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Midterm I Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. September 30, 2010

Computer Organization & Systems Exam I Example Questions

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

Midterm 1 Exam Principles of Imperative Computation. Thursday 6 th October, This exam is closed-book with one sheet of notes permitted.

Midterm 1 Solutions Principles of Imperative Computation. Thursday 6 th October, 2016

15 122: Principles of Imperative Computation. Practice Exam August 4, 2015

Final Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. December 10, Name: Andrew ID: Section:

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

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

Lecture Notes on Binary Search Trees

Print out this PDF double-sided, staple pages in order, and write your answers on these pages neatly.

Lecture Notes on Binary Search Trees

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Lecture Notes on Memory Layout

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Midterm 2 Exam Principles of Imperative Computation. Tuesday 31 st March, This exam is closed-book with one sheet of notes permitted.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

Computer Science Foundation Exam

Lecture 8 Data Structures

Lecture Notes on Binary Search Trees

Lecture 15 Notes Binary Search Trees

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Lecture 15 Binary Search Trees

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Do not start the test until instructed to do so!

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

SOME TYPICAL QUESTIONS, THEIR EXPECTED ANSWERS AND THINGS

Lecture 18 Restoring Invariants

Arithmetic and Bitwise Operations on Binary Data

COMP1917 Computing 1 Written Exam Sample Questions

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Lecture Notes on Tries

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

Lecture Notes on Binary Search Trees

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

Computer Science Foundation Exam

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

The Building Blocks: Binary Numbers, Boolean Logic, and Gates. Purpose of Chapter. External Representation of Information.

Arithmetic and Bitwise Operations on Binary Data

15-122: Principles of Imperative Computation, Spring Written Homework 12. Due: Sunday 15 th April, 2018 by 10pm. Name: Andrew ID: Section:

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

COMPUTER SCIENCE PAPER 1

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

Lecture Notes on Interfaces

C0MP1911 Final Exam 1337 Computing 1

Lecture Notes on Ints

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

ECE264 Spring 2013 Final Exam, April 30, 2013

CS112 Lecture: Working with Numbers

Computer Science E-22 Practice Final Exam

Homework 1. Problem 1. The following code fragment processes an array and produces two values in registers $v0 and $v1:

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

High Performance Computing in C and C++

CSCI2467: Systems Programming Concepts

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

: Advanced Programming Final Exam Summer 2008 June 27, 2008

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

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

CSE 373 Spring Midterm. Friday April 21st

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.1

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 16 Notes AVL Trees

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Foundation Exam - Sample Problems for New Topics

COMPUTER SCIENCE Paper 1

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

: Principles of Imperative Computation. Summer Assignment 4. Due: Monday, June 11, 2012 in class

The type of all data used in a C (or C++) program must be specified

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS 354 Midterm 1a, 33.33% Monday, October 9th, 2000 Solution

CS1800 Discrete Structures Final Version B

STUDENT LESSON A12 Iterations

Chapter 3: Operators, Expressions and Type Conversion

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Chapter 12 Variables and Operators

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

Most of this PDF is editable. You can either type your answers in the red boxes/lines, or you can write them neatly by hand.

Section 05: Midterm Review

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CDA 3103 Computer Organization Exam 1 (Sep. 22th, 2014)

Computer Science Foundation Exam

Transcription:

15-122 Practice Exam - I Page 1 of 10 15-122 : Principles of Imperative Computation Victor Adamchik Practice Exam - I Name: Andrew ID: Answer the questions in the space provided following each question. We must be able to clearly understand your answer. It it is vague or confused, it will be marked wrong. Be sure to read the directions to each question carefully so that you know what it is asking for. Question Points Score 1 39 2 27 3 19 4 15 Total: 100

15-122 Practice Exam - I Page 2 of 10

15-122 Practice Exam - I Page 3 of 10 1. Modular Arithmetic. (a) In C 0, values of type int are defined to have 32 bits. In this problem we work with a version of C0 where values of type int are defined to have only 7 bits. All integer operations are still in two s complement arithmetic, but now modulo 2 7. For each of the following statements fill in the missing quantities, in the specified notation. (3) i. The minimal negative integer, in decimal: (3) ii. The maximal positive integer, in decimal: (3) iii. 4, in hexadecimal: (3) iv. 44, in hexadecimal: (3) v. 0x48, in decimal:

15-122 Practice Exam - I Page 4 of 10 (b) Assume int x, y, z has been declared in C 0 and initialized to unknown values. For each of the following, indicate if the expression always evaluates to true, or if it could sometimes be false. Explain your answer. (3) i. x-1 < x (3) ii. y * (x/y) + x % y = x. (3) iii. ~(x ~ (~ x)) == -1 (3) iv. x <= (1<<31)-1 (3) v. x+x == 2*x (3) vi. If x+z = y+z, then x = y.

15-122 Practice Exam - I Page 5 of 10 (3) vii. Using only bitwise operators, find the k-th bit in the two s complement of the C 0 integer n. (3) viii. Compute 33 14 in binary using 8-bit two s complement representation.

15-122 Practice Exam - I Page 6 of 10 2. Wild Elephant Safari. Your computer is on safari to find an elephant in Africa. It assumes that Africa corresponds to a rectangular array represented as a one-dimensional array A. In the array A, the presence of an elephant is marked by number 9. The program on the next page looks at every element of the array from north to south and, on each row, from west to east. Unfortunately, it fails to find the elephant, even though there is at least one elephant in Africa. The reason is that elephants may move while your computer is searching for them. An elephant may move to a position that your program already looked at in an earlier iteration. A computational zoologist confirms that elephants may move at every step of your function implementation that executes an assignment or increment/decrement (like i++ or i--). Elephants move either 1 cell in the north-south direction or 1 cell in the east-west direction, which concurrently changes the memory contents of array A by swapping an elephant to a neighbor cell. The computational zoologist also knows that elephants never travel farther than 1 move away from their home cell! Hint: You decide to change your program so that when it checks for elephants at a cell, it also looks for the elephant in the immediate neighbor cells, just in case the elephant has moved. The computational zoologist warns you that you need to look around to neighboring cells without using assignments or increment/decrements or else the elephant may move away again. (5) (a) Fill in the requires and ensures clauses of the safari function (see next page). (5) (b) Fix the loop invariants in the safari function if they are broken (see next page). (12) (c) The safari function is broken. Fix the implementation of the safari function (see next page), so that it always finds an elephant. Make sure you clearly add a new statement, and clearly modify or delete a given statement, or clearly rewrite the implementation as needed. (d) (5) What is the worst-case asymptotic complexity of your safari implementation as a function of width and height, in big-o notation? Please briefly justify your answer.

15-122 Practice Exam - I Page 7 of 10 typedef int elephants; int safari(elephants[] A, int width, int height) //@requires ; //@requires ; //@requires ; //@ensures ; { } for (int i = 0; i < height; i++) //@loop_invariant 0 < i && i <= height; { for (int j = 0; j < width; j++) //@loop_invariant 0 < j && j <= width; { int r = i * width; int e = r + j; if (A[e] == 9) return e; } } // there IS an elephant in Africa, so cannot get here //@assert false; return -1;

15-122 Practice Exam - I Page 8 of 10 3. Asymptotic Notations. (5) (a) Provide the formal definition of the big-o notation and briefly state the two key ideas behind this definition in two sentences and briefly explain why and how it captures those key ideas: (b) For each of the following, indicate if the statement is true or false. (2) i. O(n 2 + 1024n log n + 10 10 ) = O(5n 2 1) (2) ii. O(n log(n)) = O(n 2 ) (2) iii. O(n 2 ) = O(n log(n)) (2) iv. O(3 log 2 n) = O(2 log 3 n) (2) v. O((10 log n + 3 5) n) = O(n log(n 3 )) (2) vi. O((log n + 3 n) (n 2 + (log n)/2 + n)) = O((1 + n) 2 (log(log n) + n log n)) (2) vii. O(2 n ) = O(2 2n )

15-122 Practice Exam - I Page 9 of 10 (15) 4. Linked Lists. Recall the definition of linked lists: struct list { int data; // a decimal digit struct list* next; }; typedef struct list* list; We define struct ring { list start; // a pointer to the first node in the ring int size; // the ring size }; typedef struct ring* ring; A ring is represented as a circular linked list with one node designated as a start. For example, a ring with items 1, 2, 3, 4, 5 (in this order, 1 at the front and 5 at the back) would be represented as the following ring: 4 3 5 2 data next data next 1 ring 5 start size

15-122 Practice Exam - I Page 10 of 10 As an example, here is a function that checks if the ring is empty. bool ring_empty(ring R) //@requires is_ring(r); { return R->start == NULL && R->size == 0; } Write a function (and contracts) that creates a new ring from a given string, such that each character is a node in the list. Any errors in inputs must be handled by contracts. The first digit of the string must be pointed by R->start. For example, the function call ring_new("12345") will create a ring as depicted in the picture above. To convert a char to an int, you should use ascii values, in particular 0 has value 48 and 9 has value 57. The C 0 string library is provided to you in the appendix. ring ring_new(string str){