UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Similar documents
UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger and Josh Hug

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

CS61B, Spring 2006 Final Examination P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger. Test #1 (corrected)

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

CS61B, Fall 2007 Final Examination P. N. Hilfinger

I have neither given nor received any assistance in the taking of this exam.

Before exam: [! ]. After exam: [! ]. UC Berkeley Computer Science CS61B: Data Structures. Midterm #1, Spring 2015

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

CSE331 Winter 2014, Midterm Examination February 12, 2014

CS61B, Fall 2009 Final Examination (revised) P. N. Hilfinger

CSE wi Midterm Exam 2/8/18. Name UW ID #

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

1 out of 3 points. 2 out of 3 points. 3 out of 8 points. 4 out of 6 points. 5 out of 5 points. 6 out of 6 points. Total. Birthday: Month: Day:

CSE331 Winter 2014, Midterm Examination February 12, 2014

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. Fall 2016.

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Examination Questions Midterm 1

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

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

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CSE 143, Winter 2009 Sample Midterm Exam #2

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CSE wi Midterm Exam 2/8/18 Sample Solution

I have neither given nor received any assistance in the taking of this exam.

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

COS 226 Algorithms and Data Structures Fall Midterm

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

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

TA office hours are over after this week, but Dan and Maja will still be around for the next month

UNIVERSITY REGULATIONS

CS61B Lecture #5: Arrays and Objects

Final Exam. COMP Summer I June 26, points

You must bring your ID to the exam.

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

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

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Midterm Exam (REGULAR SECTION)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CS/ENGRD 2110 SPRING 2018

CS 61B Discussion 5: Inheritance II Fall 2018

CMSC 341. Nilanjan Banerjee

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

UNIVERSITY OF CALIFORNIA

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

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

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

public static boolean isoutside(int min, int max, int value)

ESC101 : Fundamental of Computing

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

Lesson 7A Arrays. By John B. Owen All rights reserved 2011, revised 2014

CS 61B Discussion 5: Inheritance II Fall 2014

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

M1-R4: Programing and Problem Solving using C (JAN 2019)

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

Final Exam COMP Fall 2004 Dec 16, 2004

Full file at

Final Exam May 21, 2003

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

Midterm Exam CS 251, Intermediate Programming October 8, 2014

CS61BL: Data Structures & Programming Methodology Summer 2014

Midterm 1 Review Document

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Computer Sciences 302 Exam 2 Information & Sample Exam

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

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Computer Programming, I. Laboratory Manual. Final Exam Solution

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 Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

ext Total Score /20 /20 /15 /20 /25 /5 Grader

DO NOT REPRODUCE. CS61B, Fall 2008 Test #3 (revised) P. N. Hilfinger

CSC 1300 Exam 4 Comprehensive-ish and Structs

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

CS 307 Midterm 2 Fall 2008

COS 126 Midterm 1 Written Exam Fall 2012

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

egrapher Language Reference Manual

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

Transcription:

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS61B Fall 2015 P. N. Hilfinger Test #1 READ THIS PAGE FIRST. Please do not discuss this exam with people who haven t taken it. Your exam should contain 10 problems on 12 pages. Officially, it is worth 17 points (out of a total of 200). This is an open-book test. You have 110 minutes to complete it. You may consult any books, notes, or other non-responsive objects available to you. You may use any program text supplied in lectures, problem sets, or solutions. Please write your answers in the spaces provided in the test. Make sure to put your name, login, and TA in the space provided below. Put your login and initials clearly on each page of this test and on any additional sheets of paper you use for your answers. Be warned: my tests are known to cause panic. Fortunately, this reputation is entirely unjustified. Just read all the questions carefully to begin with, and first try to answer those parts about which you feel most confident. Do not be alarmed if some of the answers are obvious. Should you feel an attack of anxiety coming on, feel free to jump up and run around the outside of the building once or twice. Your name: Login: Login of person to your Left: Right: Discussion TA: 1. /2 2. /2 3. /2 4. /2 5. /2 6. /2 7. / 8. /1 9. /1 10. /3 1

Test #1 Login: Initials: 2 Reference Material. public class IntList { /** First element of list. */ public int head; /** Remaining elements of list. */ public IntList tail; /** A List with head HEAD0 and tail TAIL0. */ public IntList(int head0, IntList tail0) { head = head0; tail = tail0; /** A List with null tail, and head = 0. */ public IntList() { this(0, null); /** Returns a new IntList containing the ints in ARGS. */ public static IntList list(integer... args) { // Implementation not shown /** Non-destructively returns a new IntList containing all my * values that have indices >= START and < END. Undefined if * any of the required items are non-existent or I is null. * The result shares no objects with me. */ public static IntList sublist(intlist L, int start, int end) { // Implementation not shown /** Returns true iff L (together with the items reachable from it) is an * IntList with the same items as this list in the same order. */ @Override public boolean equals(object L) { // Implementation not shown

Test #1 Login: Initials: 3 1. [2 points] logswap is a static method of the ArrayDiddle class that takes as arguments an array of ints and an index into that array. It takes the value at the specified index, v, and swaps it with whatever value, h, is at half that index (rounded down, as for Java integer division), if v < h. If the swap occurs, it then repeats the entire process, starting at the new (halved) index of v. If logswap receives a starting index that is out of the array s bounds, logswap throws an IllegalArgumentException. Fill in the blanks below to finish implement logswap. Put at most one semicolon per line. You need not use all the blanks. public class ArrayDiddle { /** Swap the value at ARR[INDEX] down the array (towards index 0), * starting with the element halfway between INDEX and 0 and * continuing to reduce the destination index by 1/2 as long as the * value there is strictly smaller. Throws * IllegalArgumentException if INDEX is out of bounds. * * For example, if A initially contains { 7, 6, 5, 4, 3, 2, 1, * then after logswap(a, 6), it contains { 1, 7, 5, 6, 3, 2, 4. * If B initially contains { 7, 1, 5, 4, 3, 2, 1, then after * logswap(b, 5) it contains { 7, 1, 2, 4, 3, 5, 1. */ public static void logswap (int[] arr, int index) {

Test #1 Login: Initials: 4 2. [2 points] Fill in the box-and-pointer diagram below to show the state of the program after executing the following code segment. You may not need to use all the boxes drawn. Show only the final state. IntList a = IntList.list(1, 2, 3); IntList b = new IntList(4, a.tail); b.tail.head = 5; IntList c = b.tail.tail; a.tail.tail = null; c.tail = new IntList(5, b); int d = a.tail.head; d = 2; a: b: c: d:

Test #1 Login: Initials: 5 3. [2 points] The Before diagram below shows a state of some IntList objects and one local variable. In the spaces provided, write the Java statements that transform it into the After diagram, subject to the following restrictions: Do not modify the.head variables in any IntList object. Do not introduce any new variables. p: Before: 4 2 p: After: 4 2

Test #1 Login: Initials: 6 4. [2 points] The following problems all involve three distinct classes, A, B, and C. a. Suppose that the following code compiles and runs without any exceptions. What can you say about the relationship between the classes A, B, and C? A x1 = new C(); B x2 = x1; B x3 = (B) x1; B x4 = (C) x1; b. Now suppose the code in part (a) fails to compile, but if we remove the second line it compiles and runs without any exceptions. What can you say about the relationship between the classes A, B, and C? c. Now suppose that the code in (a) fails to compile, and when we remove the second and fourth lines it compiles, but causes a ClassCastException at runtime. What can you say about the relationship between the classes A, B, and C?

Test #1 Login: Initials: 7 5. [2 points] Define the k-partial sums of a list L for an integer k > 0 to be the list of length k whose i th element is the sum of the elements of L whose positions in L are of the form i+nk for some nonnegative integer n. If L has length less than k then the extra partial sums of the resulting list are just 0. For instance, if L is [1, 2, 3, 4, 5, 6, 7] the 3-partial sums of L are [12, 7, 9] (because 1+4+7 = 12, 2+5 = 7, and 3+6 = 9) and if L is [1, 2, 3] then the 5-partial sums are [1, 2, 3, 0, 0]. The following functions compute partial sums of L by taking each element of L in turn and adding it to the appropriate element of the result list (rather than computing each element of the result list completely in turn). Fill in the missing lines so that partialsums(l, k) returns an IntList that represents the k-partial sums of L. Do not add any semicolons. /** Returns an IntList representing the K-partial sums of L. Assumes K>0. */ public static IntList partialsums(intlist L, int k) { IntList last = circularlist(k); IntList cur = last.tail; while (L!= null) { cur.head += L.head; ; ; IntList rtn = ; ; return rtn; /** Returns an IntList of length K filled with zeroes where the last node * links back to the first node (i.e. the list returned is circular). */ public static IntList circularlist(int k) { IntList last = new IntList(); IntList first = last; k -= 1; while ( ) { k -= 1; ; return first; ;

Test #1 Login: Initials: 8 6. [2 points] Sorting (below) is a utility class whose exported method merges multiple sorted lists into one sorted list. Fill in the blanks to return the correct result. import java.util.arraylist; public class Sorting { /* Assuming each IntList in LISTS is sorted in ascending order, return * a List of all their values sorted in ascending order. Destructive: * May modify LISTS. For example, if LISTS contains IntLists * containing [1, 5], [3], and [2, 4], the returned List should contain * [1, 2, 3, 4, 5]. */ public static List<Integer> mergesortedlists(intlist[] lists) { ArrayList<Integer> toreturn = new ArrayList<>(); int smallestelemindex = indexoflistwithsmallestelement(lists); while (smallestelemindex!= -1) { smallestelemindex = indexoflistwithsmallestelement(lists); return toreturn; /* Return the index in LISTS of a non-empty list whose head is smallest. * Returns -1 if all elements of LISTS are empty. */ private static int indexoflistwithsmallestelement(intlist[] lists) { int index; for (int i = 0; i < lists.length; i += 1) { return index;

Test #1 Login: Initials: 9 7. [1 point] What common thing (having nothing to do with animals) do the words apenstaartje (Dutch for monkey s tail ), snabel (Danish for elephant s trunk ), kissanhnta (Finnish for cat s tail ), klammeraffe (German for spider monkey ), πaπȧκι (Greek for little duck ), kukac (Hungarian for worm ), grisehalehka (Norwegian for pig s tail ), and sobachka (Russian for little dog ) all refer to? 8. [1 point] Assume that a and b are initially non-negative integers less than 8. What is the value of y after the following code executes? int x, y, z; x = a; y = b; z = x ^ y; x = (x & y) << 1; y = z; z = x ^ y; x = (x & y) << 1; y = z; z = x ^ y; x = (x & y) << 1; y = z; z = x ^ y; x = (x & y) << 1; y = z; 9. [1 point] A certain type of bracketed list consists of words composed of one or more lower-case letters alternating with unsigned decimal numerals and separated by commas, as in these examples: [] [wolf, 12, badger, 11] [dog,10, cat] As illustrated, commas may be followed (but not preceded) by blanks, and the list may have odd length (ending in a word with no following numeral.) There are no spaces around the [] braces. Write a Java Pattern that matches such lists.

Test #1 Login: Initials: 10 10. [3 points] A simple computational network is composed of value queues (represented by squares in the example below) and computation nodes (circles in the example below). Both value queues and computation nodes have inputs and outputs. Integer values come into each value queue, which accumulates them until the computation node on its output indicates there are enough values for its computation. At that point, the value queue sends its accumulated values to its computation node, which computes a value that is sent to another value queue. In the example below, + nodes wait for two inputs and compute their sum; nodes wait for two inputs and compute their product; and P nodes wait for one input and print it (producing nothing): 1, 5 3, 6 Q1 Q2 A1 + A2 + 6 9 Q3 A3 54 Q4 A4 P We ll represent value queues with the class ValueQueue and describe computations with an interface called Computation. Here is ValueQueue: import java.util.arraylist; public class ValueQueue { public void attach(computation sink) { _sink = sink; public void accept(integer value) { _queue.add(value); if (_sink.enabled(_queue.size())) { _sink.consume(_queue); _queue.clear(); private Computation _sink; private ArrayList<Integer> _queue = new ArrayList<>(); The following code sets up the network and inputs pictured in the diagram. ValueQueue Q1 = new ValueQueue(), Q2 = new ValueQueue(), Q3 = new ValueQueue(), Q4 = new ValueQueue(); Computation A1 = new Adder(Q3), A2 = new Adder(Q3), A3 = new Multiplier(Q4), A4 = new Printer(); Q1.attach(A1); Q2.attach(A2); Q3.attach(A3); Q4.attach(A4); Q1.accept(1); Q2.accept(3); Q2.accept(6); Q1.accept(5);

Test #1 Login: Initials: 11 Working backwards from the code, fill in suitable definitions for the Computation interface and the Adder class. public interface Computation public class Adder

Test #1 Login: Initials: 12