Midterm Exam 2B Answer key

Similar documents
Midterm Exam 2A Principles of Computing Fall November 10, 2014

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

Computer Organization and Levels of Abstraction

Software and Hardware

4. Write a sum-of-products representation of the following circuit. Y = (A + B + C) (A + B + C)

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

Computer Organization and Levels of Abstraction

UNCA CSCI 255 Exam 1 Spring February, This is a closed book and closed notes exam. It is to be turned in by 1:45 PM.

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Read this before starting!

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

CDS130 Mid-term exam: Sample

Introduction to Computer Science. Homework 1

ECE 341 Midterm Exam

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

EECS 270 Midterm Exam

Binary. Hexadecimal BINARY CODED DECIMAL

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Student Number: Lab day:

CSE COMPUTER USE: Fundamentals Test 1 Version D

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

N.B. These pastpapers may rely on the knowledge gained from the previous chapters.

Digital Fundamentals

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

15-110: Principles of Computing, Spring 2018

1. Which of the following Boolean operations produces the output 1 for the fewest number of input patterns?

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Question Total Possible Test Score Total 100

The LC3's micro-coded controller ("useq") is nothing more than a finite-state machine (FSM). It has these inputs:

Grade 7/8 Math Circles Fall November 6/7/8 Boolean Logic

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Read this before starting!

COMP combinational logic 1 Jan. 18, 2016

Basics of Information Worksheet

Practice Midterm Exam Solutions

TABLES AND HASHING. Chapter 13

CS150 - Sample Final

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

CMPUT 101 with Solutions Quiz 2 (50 minutes) November 16, 2000

UNIT 9A Randomness in Computation: Random Number Generators

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

Read this before starting!

CS150 Sample Final. Name: Section: A / B

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

15110 PRINCIPLES OF COMPUTING EXAM 3A FALL 2011

Test #2 October 8, 2015

Computer Science E-22 Practice Final Exam

Multiple-choice (35 pt.)

15-110: Principles of Computing Sample Exam #1

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS 1301 Exam 1 Fall 2010

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

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

CDS Computing for Scientists. Midterm Exam Review. Midterm Exam on October 22, 2013

1. Draw general diagram of computer showing different logical components (3)

HASH TABLES. Hash Tables Page 1

Structure and Interpretation of Computer Programs

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

UNIVERSITY REGULATIONS

CS1800 Discrete Structures Final Version A

CS/COE 0447 Example Problems for Exam 2 Spring 2011

Register Transfer and Micro-operations

Random Number Generators

CSE 130, Winter 2011: Final Examination

Structure and Interpretation of Computer Programs

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CMPUT 101 with Solutions Quiz 2 (50 minutes) November 7, 2003

CSC-105 Exam #1 October 10, 2013

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

1 /10 2 /12 3 /16 4 /30 5 /12 6 /20

Final Examination CSE 100 UCSD (Practice)

III Data Structures. Dynamic sets

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Grade 7/8 Math Circles Fall November 6/7/8 Boolean Logic

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CMPUT 101 with Solutions Quiz 2 (50 minutes) August 7, 2002

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Digital Data

Digital Systems. John SUM Institute of Technology Management National Chung Hsing University Taichung, ROC. December 6, 2012

DIGITAL SYSTEM DESIGN

Dec Hex Bin ORG ; ZERO. Introduction To Computing

Lecture (03) Binary Codes Registers and Logic Gates

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Digital Logic Design Exercises. Assignment 1

5.1 The String reconstruction problem

Chapter 10 Error Detection and Correction 10.1

CE161-4-AU UNIVERSITY OF ESSEX. Undergraduate Examinations 2014 DIGITAL SYSTEMS ARCHITECTURE. Time allowed: TWO hours

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

UNIVERSITY OF WISCONSIN MADISON

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

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

CS 1301 Exam 1 Fall 2010

1. Fill in the entries in the truth table below to specify the logic function described by the expression, AB AC A B C Z

Transcription:

Midterm Exam 2B Answer key 15110 Principles of Computing Fall 2015 April 6, 2015 Name: Andrew ID: Lab section: Instructions Answer each question neatly in the space provided. There are 6 questions totaling 31 subproblems on 12 pages. Not all problems are the same size or difficulty. Please read each question carefully. You have 50 minutes for this exam. No electronic devices allowed. Good luck! Max 1 14 2 22 3 19 4 25 5 14 6 6 Total: 100 Score

15-110 Midterm Exam 2B, Page 2/12 Answer key Questions 1. (6 points) This question deals with recursion and recursive algorithms. Here is a function definition in Python that uses recursion. def bad(n): if n <= 1: return True elif n % 2 == 0: return bad(n/2) else: return bad(n/1) (a) (1 point) What is the value returned by bad(8)? True. (b) (3 points) Think about what happens if we evaluate bad(3). Either give the value that would be returned and displayed by Python, or explain why no value would be returned and displayed. No value would be returned because the sequence of function calls would be bad(3), bad(3),..., because we start with an odd argument, which is divided by one, yielding an endless series of identical function calls.

15-110 Midterm Exam 2B, Page 3/12 Answer key (c) (4 points) Consider the following Python function g, defined for an integer n recursively as follows: def g(n): print(n, end=" ") if n < 3: return n else: return g(n // 2) - g(n // 3) Compute g(12) by drawing a recursion tree showing all of the recursive calls and then use your tree to compute the final answer. g(12) = -3 Recursion tree:

15-110 Midterm Exam 2B, Page 4/12 Answer key 2. This problem focuses on the representation of data in a computer. The following table may be helpful: 2 10 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1024 512 256 128 64 32 16 8 4 2 1 (a) (2 points) What is the largest number that can be represented as a signed two s complement integer using 5 bits? Give your answer in decimal notation, not binary. 15 (b) (2 points) What is the smallest (most negative) number that can be represented as a signed two s complement integer using 4 bits? Give your answer in decimal notation, not binary. -8 (c) (3 points) Compute the decimal value of the byte 10011001 if it is interpreted as a signed two s complement integer. Show your work. Explanation: the sign bit is set, so it is negative. One s complement: 01100110, plus 1, gives 01100111. So the magnitude is 64 + 32 + 4 + 3 + 1 = 103, or 6 16 + 7 = 103. (d) (2 points) When using odd parity, what is the correct parity bit for the 7-bit character 101 1011? 0 (e) (2 points) Suppose that the rightmost bit is corrupted during transmission of the eight bits from part (d) and is flipped from 1 to 0. Which of the following is true? Circle the appropriate letter: B A. The receiver cannot detect the error. B. The receiver can detect the error but cannot determine which bit is wrong. C. The receiver can detect the error and can correct the bit that is wrong. (f) (2 points) Suppose we re sending ASCII codes with a parity bit, using odd parity on an unreliable communication channel. With a reliability of 75%, our data source has a certain measurable entropy (information content). If the reliability of the communication channel goes up to 85%, will the entropy increase or decrease? decrease

15-110 Midterm Exam 2B, Page 5/12 Answer key (g) (2 points) Suppose you are given a frequency table for typical text in Russian using the 33 letters of the Cyrillic alphabet, and you calculate an information entropy value of 5.4 based on the table. If you use the same table to create a Huffman tree and use that tree to compress a typical Russian text of 100,000 letters, approximately how many bits would you expect to need to represent the compressed text? You should assume that the frequencies are accurate for the text that is compressed. Show your work. Multiply the entropy by the number of characters, so 540,000 bits. Consider the following Huffman tree: (h) (4 points) What word is represented by the following binary string, based on the Huffman tree (we have inserted spaces purely for readability; they do not indicate boundaries between letters): 111 110 100 011 110 010 11 SENSOR (i) (3 points) Show the encoding of the string TAR using the same Huffman tree. Leave spaces between the codes to indicate the boundaries between letters. 00 010 011

15-110 Midterm Exam 2B, Page 6/12 Answer key 3. This question is concerned with generating and using pseudorandom numbers. Recall that the Python function randint(n, m) returns a random integer between n and m, inclusive. Using the randint function, show how to compute the following using one Python expression: (a) (3 points) A random integer between 1 and 22 that is a multiple of 4. Write one expression using randint. randint(1, 5) * 4 or something equivalent (b) (3 points) A randomly chosen number from the list vals below. You are not allowed to use variables other than vals or any function other than randint or len. vals = [17.0, 7.7, 3.1, 5.0, 4.8, 2.6] vals[randint(0, len(vals) - 1)] or names[randint(0, 5)] (c) (4 points) Write a function called coin() that simulates a coin that comes up heads 60% of the time and tails 40% of the time. The function should return a string, either H or T. def coin(): random_v = randint(0,99) % alternative: randint(1,100) % or randint (0,9), etc. if random_v < 60: % and random_v <= 60 % or random_v < 6, etc. return "H" else: return "T" (d) (2 points) Describe in one or two sentences what method you could use to determine whether your code for part c behaves the way it should. Use a Monte Carlo method: call coin() a large number of times, count the number of heads, and calculate the proportion of all flips that are heads. Recall the linear congruential generator (LCG) formula: x (i+1) = (a x i + c) mod m (for positive integers a, c, and m) As described in class, there are certain conditions that should be obeyed for the LCG to have its maximum period: 1. c and m must be relatively prime; 2. a 1 must be divisible by every prime factor of m; and 3. if m is divisible by 4, then a 1 must be divisible by 4.

15-110 Midterm Exam 2B, Page 7/12 Answer key (e) (4 points) Consider only values of a that are less than m (We intended to consider only values larger than 1, but forgot to say that. Alternate acceptable answers below are in boldface. Assume that c = 4 and m = 45 (note that the prime factors of 45 are 3 and 5). List the possible values of a that yield the maximum period. For a 1, Rule 2 gives us the multiples of 15 less than 45: 0, 15, 30. Rule 3 does not apply. So a can be 16 or 31 or 1. (f) (3 points) With x 0 = 0, a = 7, c = 3, and m = 18, what are the values of x 1, x 2, and x 3? That is, starting with a seed of 0, what are the next three values calculated by the LCG? (Don t worry about the period of this LCG; it doesn t matter whether it has the maximum possible period.) 3, 6, 9 4. This question deals with data organization. (a) (3 points) An airline company wants to maintain a database of up to 3000 frequent flyer numbers of people who use their company most frequently so that it can be determined very quickly whether or not a given frequent flyer number is in the database before they serve the customer. Suppose that the speed of response is very important but efficient use of memory is not as important. Which of the following data structure would be most appropriate for this task? Circle the appropriate letter and explain why in at most two sentences. A. The frequent flyer number can be used as an index into the array, giving a constant lookup time. In the worst-case, the lookup time in a linked list is linear in the number of entries. A. an array with 3000 entries B. a linked list (b) (2 points) Suppose that we wish to store a collection of key and value pairs using a hash table. What is the most important property we want for the hash function so that we have efficient search time? It should distribute the keys across the table as evenly (uniformly) as possible. It should also have O(1) time complexity. (c) (3 points) Why are collisions undesirable in a hash table? A collision causes multiple data elements to be stored in a single bucket. The lookup time increases as the number of items in a bucket increases.

15-110 Midterm Exam 2B, Page 8/12 Answer key (d) (4 points) The inputs d and lst to function mystery below are, respectively, a dictionary and a list. def mystery(d,lst): s = 0 for k,v in d.items(): if k in lst: s = s + d[k] return s Suppose that we execute the following commands in the given order. function call return? >>> myd = {"x":10, "y": 15, "z":20, w": 5} >>> mylst = ["x","w"] >>> mystery(myd,mylst) What does the 15 (e) (4 points) The binary search tree shown below was constructed by inserting a sequence of items with integer keys into an empty tree. Which of the following input sequences will not produce this binary search tree? Circle your answer. A. 18 20 21 14 10 15 3 13 B. 18 14 20 10 15 21 3 13 C. 18 14 20 10 15 21 13 3 D. 18 15 29 10 14 3 13 21

15-110 Midterm Exam 2B, Page 9/12 Answer key D (f) (4 points) Draw the binary search tree that would result from the insertion order you circled in the previous part. (g) (3 points) In general for a binary tree with 127 nodes, what is the minimum number of levels the tree can have? Assume that a tree consisting of a single node is considered to have one level. 7 (h) (2 points) If we store a collection of items in a binary search tree, why is it desirable to construct a tree using as small a number of levels as possible? Because the search time is proportional to the number of levels in the tree.

15-110 Midterm Exam 2B, Page 10/12 Answer key 5. This question deals with Boolean logic, gates and computer organization. (a) (4 points) Write the truth table for the Boolean expression A B, where A and B are Boolean variables. Hint: Think about how many rows you need to have in your table. A B A B A B 0 0 1 1 1 0 1 1 0 1 1 0 0 1 1 1 1 0 0 0 (b) (4 points) The truth table you have written above can be implemented using a single gate with the inputs A and B. Which of the following gates is it? AND, OR, NOT, NAND, NOR, XOR. Explain your reasoning. NAND. First, transform the expression using De Morgan s Law to (A B). This is directly implementable using a NAND gate. (c) (3 points) The circuit below computes the Boolean function Q give input A, B, C and D. What is output on the Q line when A = 1, B = 1, and C = 1? 1

15-110 Midterm Exam 2B, Page 11/12 Answer key (d) (3 points) The following is a description of how a program is executed in a typical computer that conforms to the von Neumann architecture. Fill in the blanks. Load the program counter to point to the first instruction in memory. Then, repeatedly 1. the instruction indicated by the program counter from memory into the instruction register. 2. the instruction to a control signal and get any data it needs (possibly from memory). 3. the instruction with data in ALU and store results (possibly into memory), incrementing the program counter at the end Load the program counter to point to the first instruction in memory. Then, repeatedly Fetch the instruction indicated by the program countre from memory into the instruction register. Decode instruction to a control signal and get any data it needs (possibly from memory). Execute instruction with data in ALU and store results (possibly into memory, incrementing the program counter at the end

15-110 Midterm Exam 2B, Page 12/12 Answer key 6. This question deals with simulation. (a) (6 points) Recall the simulation for the spread of a flu virus in a population we did in class. In the simulation code the constants HEALTHY and IMMUNE are used to represent, respectively, the immune and healthy states of an individual. The population is represented using a two-dimensional list called matrix where all elements of matrix are of the same length. The following is a Python function to check whether the simulation has reached a state where the virus cannot spread any further because everyone is either immune or healthy. Fill in the blanks. Note that we did not write this function in class. Hint: The function will return True only if the function has examined every cell in the matrix and has not found anyone that is in a state other than immune or healthy. def all_good(matrix): for i in range(0,len(matrix)): for j in : return True if : return False def all_good(matrix): for i in range(0,len(matrix)): for j in range(0,len(matrix[0])): # Alternatively len(matrix[i]) if matrix[i][j]!= IMMUNE and matrix[i][j]!=healthy: # Alternatively, not (matrix[i][j] == IMMUNE or matrix[i][j] == HEALTHY # Or something equivalent return False return True