CS 134 Midterm Solutions Spring 2013

Similar documents
CS 134 Midterm Spring 2013

CS 134 Midterm Fall 2006

CS 134 Midterm Fall 2006

CS 134 Midterm Fall 2009

CS 134 Midterm Solutions Fall 2009

// Class used to hold a list of Strings interpreted as web site addresses. public class URLList {

Final Examination May

Final Examination --- SAMPLE SOLUTIONS May

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

From: The History of the Telephone Herbert N. Casson 1910

Programming Project. Remember the Titans

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

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

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

ECE/CSC 570 Section 001. Final test. December 11, 2006

MCS-378 Intraterm Exam 1 Serial #:

LECTURE PLAN. Script. Introduction about MAC Types o ALOHA o CSMA o CSMA/CD o CSMA/CA

CS 140 Midterm Examination Winter Quarter, 2012

Exercises of lecture Wireless Sensor Networks Winter 2006/2007 Sheet 4

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

EECS 311 Data Structures Midterm Exam Don t Panic!

Contention Protocols and Networks

Total Score /1 /20 /41 /15 /23 Grader

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Quiz III

Links, clocks, optics and radios

Algorithms and Data Structures CS-CO-412

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Winter 2017 Feb 13 th, 2017 CS106A Midterm. Last Name: First Name: Sunet ID (eg jdoe): Section Leader / Grader:

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

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

COS 226 Algorithms and Data Structures Fall Midterm

Horn Formulae. CS124 Course Notes 8 Spring 2018

Midterm Exam 2B Answer key

I. True/False: (2 points each)

Sample solution to Midterm

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

College of Computer and Information Sciences Department of Computer Engineering CEN444 Computer Networks Midterm 2 Exam Second Semester 1434/1435

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CMPSCI 453 Fall Midterm 10/26/2008

CS143 Final Fall 2009

CSE373 Fall 2013, Midterm Examination October 18, 2013

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Questions Total Points Score

CS 3114 Data Structures and Algorithms READ THIS NOW!

COS 140: Foundations of Computer Science

COS 126 General Computer Science Spring Written Exam 1

Practice Midterm 1. Problem Points Score TOTAL 50

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

COS 126 Written Exam 2, Fall 2009

ECE242 Data Structures and Algorithms Fall 2008

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

CPSC 35 Midterm Exam

EECS 122: Introduction to Communication Networks Final Exam Solutions

Final Exam Computer Networks Fall 2015 Prof. Cheng-Fu Chou

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

CS61B Spring 2016 Guerrilla Section 6 Worksheet

CS 101 Fall 2006 Midterm 1 Name: ID:

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

COS 140: Foundations of Computer Science

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CS244a: An Introduction to Computer Networks

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Medium Access Control

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

Midterm 2. Read all of the following information before starting the exam:

Networked Systems (SAMPLE QUESTIONS), COMPGZ01, May 2016

Computer Science 136. Midterm Examination

Do not start the test until instructed to do so!

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Model Solutions. COMP 103: Test May, 2013

COS 226 Midterm Exam, Spring 2009

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

CS 421: COMPUTER NETWORKS SPRING FINAL May 16, minutes

Data Link Layer, Part 3 Medium Access Control. Preface

Final Exam. COMP Summer I June 26, points

Medium Access Control

Midterm Exam CSCE 232: Computer Networks Fall Instructions:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

AP CS Fall Semester Final

CSE 1223: Exam II Autumn 2016

EECS489 Computer Networks, Take-home Makeup Midterm (Winter 2007) due in class Wednesday 3/28

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Computer Science 302 Spring 2007 Practice Final Examination: Part I

About this exam review

ECE 4450:427/527 - Computer Networks Spring 2017

CS 101 Exam 1 Spring 200 Id Name

King Fahd University of Petroleum and Minerals College of Computer Sciences and Engineering Department of Computer Engineering

Question: Total Points: Score:

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

Aloha and slotted aloha

ECE 653: Computer Networks Mid Term Exam all

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

CS171 Midterm Exam. October 29, Name:

Ethernet. Introduction. CSE 3213 Fall 2011

Transcription:

CS 134 Midterm Solutions Spring 2013 This is a closed book exam. You have 75 minutes to complete the exam. There are 5 questions on this examination. The point values for the questions are shown in the table below. Your answers should fit in the space provided in the exam booklet. Paper for scrap work will be made available during the examination. NAME: LECTURE SECTION: 09.00 10.00 Question Points Score 1 20 2 15 3 25 4 25 5 15 TOTAL 100 I have neither given nor received aid on this examination. Signature:

Question 1 Write Java code to implement the two methods described below. Your solutions should not use any builtin String methods that do substring replacement (i.e., replace and replaceall). You may use other String methods including indexof, substring, etc. a)define a method removeletter that takes two parameters, word and letter, and returns the String obtained by removing one copy of letter from word. You may assume that letter is a String of length one. For example, removeletter( mississippi, p ) would return mississipi. private String removeletter( String word, String letter ) { int pos = word.indexof( letter ); if ( pos == -1 ) { return word; else { return word.substring( 0, pos ) + word.substring( pos+1 ); 2

b)define a method removeallletter that takes two parameters, word and letter, and returns a String obtained by removing all copies of letter from word. You may assume that letter is a String of length 1. For example, removeallletter( mississippi, s ) would return miiippi. You may use the removeletter method from part (a) in your definition of removeallletter even if you are not confident your removeletter implementation is correct. private String removeallletter( String word, String letter ) { while ( word.contains( letter ) ) { word = this.removeletter( word, letter ); return word; 3

Question 2 Consider the code shown below. Much of it should look quite a bit like code you wrote as part of this week s lab. public class StringList { private boolean empty = true; private String first; private StringList others; public StringList() { public StringList( String text, StringList existing ) { empty = false; first = text; others = existing; public String tostring() { if ( empty ) { return ""; else { return others.tostring() + first + " "; public int R() { if ( empty ) { return 0; else { return first.length() + others.r(); public StringList T( String target ) { if ( empty ) { return new StringList(); else if ( first.startswith( target ) ) { return others; else { return new StringList( first, others.t( target ) ); public StringList V() { if ( empty ) { return new StringList(); else { return new StringList( first, others ); 4

Suppose that we have executed the assignment: StringList words = new StringList( "greater", new StringList( "than", new StringList( "all", new StringList( "the", new StringList( "parts", new StringList() ) ) ) ) ); Indicate the String or int value that would be produced by each of the following expressions. If possible, also provide a brief description of what the first method invoked in each expression does in general to a StringList. a) words.tostring() parts the all than greater The method converts the list into a String containing all of the Strings in the list in reverse order followed by spaces. b) words.r() 22 The method determines the sum of the lengths of all the strings in a StringList. c) words.v().tostring() part the all than greater The method makes a copy of the first object in the StringList so that the resulting StringList looks just like the original. d) words.t( "all" ).tostring() parts the than greater The T method returns a StringList forming a copy of the original StringList with all Strings that contain the specified String removed. 5

! Question 3 Imagine that you would like to efficiently transmit the titles of songs and albums by the Irish folk rock band Clannad, such as Anam An Gleann Macalla (meaning Soul, The Valley, and Echo, respectively). The table below shows the counts of the letters A, C, G, L, M, and N in the small sample of Clannad s songs listed above. C G M L N A 1 1 2 3 4 7 One possible Huffman tree that can be derived from the table above is: 0 1 0 C 0 1 G 0 1 L 0 M 1 1 N A a. Show the binary sequence that would be used to encode the title Macalla using the tree above. (i.e. In the table below, fill in the correct binary code for each letter in the word.) M A C A L L A 001 1 0000 1 010 010 1 b. How many bits are needed to encode Macalla? 16 6

c. Note that the length of the longest codeword given by the tree on the previous page is 4. Derive another Huffman tree from the table of letter occurrences above, where the length of the longest codeword represented by the tree is 5. A N L M C G d. Since there are 6 symbols in the alphabet we re considering (A, C, G, L, M, and N), a fixed-length code would require that each symbol be represented by three bits. Is there a Huffman tree for the letters and occurrence counts above for which the longest codeword would be 3? If so, show the tree. If not, briefly explain why not. NO. The alphabet involved is small enough that you can simply argue that following the Huffman algorithm, no matter which choice you make at each point that a choice is possible because there is more than one symbol/subtree of minimal weight, you get a tree of height at least 4. Alternately, one can argue that a tree of height 3 with 6 leaves must have 4 paths of length 3 and two of length 2. Assigning the more common letters (N and A) to the shorter codes results in a code that requires 43 bits to encode all of the song names. Any of the Huffman trees considered only require 42 bits. Thus, a tree of height 3 cannot be optimal so it cannot be a Huffman tree. 7

8

Question 4 Suppose that computers A, B, C, and D are all connected to an Ethernet as shown below so that the length of the cables between any pair of computers is equal to L/2, where L is the maximum allowed length between any pair of computers on an Ethernet. Note that with this configuration, the distance between any pair of computers in the group A, B, and C is L. D A B C! a) Suppose that A becomes ready to send a packet while D is already transmitting. Approximately how much time will elapse between the point when C hears the last bit of D s packet and the first bit of A s? L/c. It takes time L/2c for D s last bit to reach both A and C. Because Ethernet is 1- persistent, A will start sending soon after it senses this last bit because that is when it will detect that the network is idle. Once A transmits its first bit, it takes L/2c for it to reach D and another L/2c for it to continue on to B and C. So, C will detect the signal L/c + i time units later where i is the amount of time it takes to detect that the network is idle. (Although we have not discussed it in class, the Ethernet protocol places a minimum on i. A computer must wait 96 bit transmission times before concluding the network is idle.) b) Suppose that D becomes ready to send a packet while A is already transmitting. Approximately how much time will elapse between the point when C hears the last bit of A s packet and the first bit of D s? close to 0. It takes time L/2c for A s last bit to reach D and L/c time for A s last bit to reach C. D will start sending soon after it senses the last bit from A because that is when it will detect that the network is idle. Once D transmits its first bit, it takes L/2c for it to reach C. So, C will detect the signal from D just a little more than L/c time units after A sent its last bit, which is essentially the same time it will receive D s first bit. c) Suppose that A, B and C all try to send packets containing 640 bytes (i.e., 5120 bits or 10 times the minimum packet size) at exactly the same time. All three computers will detect the collision simultaneously and then choose random delays according to the binary exponential backoff algorithm. Assume that after it delays, A is the first computer to successfully transmit and that no additional collisions have occurred. What random delay must each of the three computers involved have chosen given that A transmits successfully? Briefly explain your answer. After the collision, all three machines will have to choose a delay value of 0 or 1. If A is going to succeed, its choice must be different from the choice made by B and C. Since there are only two choices, this means B and C must choose the same delay. Since this would lead to a collision if B and C choose the shorter delay, A must choose the delay of 0 and B and C must delay for one slot time. By the time B and C finish delaying, A will be successfully sending so B and C will follow the rules of carrier sense and wait for A s transmission to complete 9

d) Now, assume that A actually had two packets to send, so that A, along with B and C, will try to transmit again as soon as A s first transmission completes. This will lead to a second collision which all three computers will detect simultaneously. Again, all three computers will choose random delays. From what range of backoff values will each of the three computers choose? What is the probability that B is the first to transmit successfully during this second round of competition without further collisions? Explain briefly. 3/32. Since A just complete a successful transmission, it will choose to delay 0 or 1 slots after the collision. For B and C, on the other hand, this will be the second collision experienced so they will choose between delays of 0, 1, 2, and 3. To succeed, B must send before A, so B must choose a delay of 0 and A must choose a delay of 1. C must delay more than B, so it may choose 1, 2, or 3. The probability for each machine is determined by the number of slots it gets to choose from. B will choose 0 with probability 1/4, A will choose 1 with probability 1/2 and C will choose a slot other than 0 with probability 3/4 = (1-1/4). The probability all these events will occur is just the product of these probabilities: 1/4x1/2x3/4 = 3/32. e) Suppose that A, B, and C each have many 640 byte packets to send so that the collision resolution process repeats many, many times. Assume that the network transmission rate is 10 megabits per second and that network monitoring shows that the network is operating at 90% efficiency and that the three computers are each able to send roughly equal numbers of packets. How many packets per second is A able to send? Don t bother getting out your calculator for this question. A clear formula will be a better answer than an actual value. (.9 x 10 7 bits/second ) / ( 3 computers x 640 bytes/packet x 8 bits/byte ) = (.9 x 10 7 bits/second ) / ( 3 computers x 5120 bits/packet ) =.9 x 10 7 / 15360 packets per computer per second f) Consider a network identical to that considered in (d) except that A, B, and C each have many 64 byte (i.e. 512 bit) packets to send. What value would you predict for the efficiency of this network based on the measured efficiency of the network described in part (e) and the formula from the paper by Metcalfe and Boggs shown below? P/R 1 efficiency = ----------------- = -------------------- WT + P/R WT/(P/R) + 1 As usual, justify your answer. The key to this question is to realize that the size of the packets being transmitted on this network will not change the amount of time wasted because of collisions and idle slots between successful transmission. As a result, of the four variable P, R, T and W used to predict the efficiency of a network, only P changes between this question and the preceding question. Plugging the measured value of the efficiency from part (e) into the second form of the efficiency equation gives where P is 5120. 1.9 = ----------------- ===> WT/(P/R) = 1/9 WT/(P/R) + 1 Now, if P is 512, we know that P/10 = P and therefore that WT/(P /R) = WT/(P/10R) = 10/9. Plugging this into the efficiency formula gives 1 1 ----------------- = ----------------- = 9/19 =.47 WT/(P /R) + 1 19/9 10

" " " " Question 5 The program on the next page draws five buttons in a window. These buttons -- button1, button2, button3, button4, and button5 -- are labeled CIRCUMFERENCE, EQUALS, PI, TIMES, DIAMETER, respectively, as shown below. Pressing the buttons changes the labels of various buttons. button1 button2 button3 button4 button5 " Suppose that the buttons named button1, button2, button3, button4, and button5 are clicked in sequence. Indicate the labels on each of the buttons after the buttonclicked method is invoked in response to each of the five clicks in this sequence. i) After clicking button1 CIRCUMFERENCE EATS CAKE PLUS DIAMETER ii) After clicking button2 VOLUME EATS CAKE PLUS RADIUS^2 iii) After clicking button3 AREA EATS CAKE PLUS RADIUS^2 iv) After clicking button4 AREA EQUALS CAKE TIMES RADIUS^2 v) After clicking button5 AREA EQUALS PI TIMES RADIUS^2 11

// import statements removed to save space public class ExamButtons extends GUIManager { private JButton button1, button2, button3, button4, button5; public ExamButtons() { this.createwindow( 600, 150 ); button1 = new JButton( "CIRCUMFERENCE" ); button2 = new JButton( "EQUALS" ); button3 = new JButton( "PI" ); button4 = new JButton( "TIMES" ); button5 = new JButton( "DIAMETER" ); contentpane.add( button1 ); contentpane.add( button2 ); contentpane.add( button3 ); contentpane.add( button4 ); contentpane.add( button5 ); public void buttonclicked( JButton which ) { button4.settext( "PLUS" ); if ( which == button1 ) { button3.settext( "CAKE" ); button2.settext("eats"); if ( which == button2 ) { button1.settext( "VOLUME" ); if ( which == button3 ) { button1.settext( "AREA" ); if (button3.gettext().equals("pi")) { button3.settext("ice CREAM"); if ( which == button4 ) { if (button3.gettext().equals("ice CREAM")) { button2.settext("slurps"); else { button2.settext("equals"); if ( which == button5 ) { button4.settext( "TIMES" ); if (button3.gettext().equals("cake")) { button3.settext("pi"); else if ( which == button2 ) { button5.settext( "RADIUS^2" ); 12

1