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

Similar documents
DO NOT. package java.lang; public final class String implements Serializable, Comparable, CharSequence {

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)

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 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.

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

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

CS61B, Fall 2007 Final Examination P. N. Hilfinger

CS61B, Spring 2006 Final Examination P. N. Hilfinger

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

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

Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

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

CS61B Lecture #2. Public Service Announcements:

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

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS61B Lecture #5: Arrays and Objects

ESC101 : Fundamental of Computing

Algorithms & Datastructures Laboratory Exercise Sheet 1

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE 331 Midterm Exam 11/9/15 Sample Solution

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

Full file at

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Public-Service Announcement

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

TeenCoder : Java Programming (ISBN )

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

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

COS 126 General Computer Science Spring Written Exam 1

Practice exam for CMSC131-04, Fall 2017

Prelim One Solution. CS211 Fall Name. NetID

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

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

CS61B, Summer 2002 Exam #1 Barath Raghavan

Midterm Exam (REGULAR SECTION)

Public-Service Announcement

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Use the scantron sheet to enter the answer to questions (pages 1-6)

Introduction to Programming Using Java (98-388)

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

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Final Exam. COMP Summer I June 26, points

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

Question: Total Points: Score:

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

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Lab5. Wooseok Kim

Examination Questions Midterm 1

CSE331 Winter 2014, Midterm Examination February 12, 2014

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

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

Midterms Save the Dates!

CSL 201 Data Structures Mid-Semester Exam minutes

CS1004: Intro to CS in Java, Spring 2005

Sample Examination Paper Programming and Software Development

CS 101 Fall 2006 Midterm 3 Name: ID:

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

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

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

Public-Service Announcements

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

CS 61B, Spring 1998 exam #1 Professor P. N. Hilfinger

CS 101 Fall 2005 Midterm 2 Name: ID:

Brief Summary of Java

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Read and fill in this page now

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Name CIS 201 Midterm II: Chapters 1-8

UNIVERSITY OF CALIFORNIA

Introduction to Programming (Java) 4/12

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

CS 251 Intermediate Programming Methods and Classes

Do not turn to the next page until the start of the exam.

Note that if both p1 and p2 are null, equals returns true.

6.005 Elements of Software Construction Fall 2008

Public-Service Announcements

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Programming with Java

Arrays.

WES-CS GROUP MEETING #9

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CSE 331 Midterm Exam Sample Solution 2/18/15

CSE 21 Intro to Computing II. JAVA Objects: String & Scanner

CSC Java Programming, Fall Java Data Types and Control Constructs

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Transcription:

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS61B Fall 2009 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 5 problems on 11 pages. Officially, it is worth 20 points (out of a total of 200). This is an open-book test. You have fifty minutes to complete it. You may consult any books, notes, or other inanimate 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 lab section 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: 1. /3 Login of person to your Left: Right: 2. /4 3. / Discussion section number or time: 4. /6 Discussion TA: 5. /7 Lab section number or time: TOT /20 Lab TA: 1

Test #1 Login: Initials: 2 Assorted Reference Material IntList public class IntList { public int head; public IntList tail; public IntList (int head, IntList tail) { this.head = head; this.tail = tail; Excerpt from class java.lang.string package java.lang; public final class String implements Serializable, Comparable, CharSequence { /** The Kth character of THIS, 0 <= K < length(). */ public char charat (int k) {... /** The number of characters in THIS. */ public int length () {... /** A value <0 if THIS comes before X in lexicographic order, ==0 * if equal, and >0 if THIS comes after X. */ public int compareto (String x) {... /** The String containing characters #START, START+1,..., END-1 of * THIS. */ public String substring (int start, int end) {... java.util.iterator package java.util; public interface Iterator<SomeType> { /** True iff there is an item for next() to deliver. */ public boolean hasnext (); /** The next item to be returned by THIS. */ public SomeType next (); /** (Optional) Remove the last item returned by THIS. */ public void remove ();

Test #1 Login: Initials: 3 1. [3 points] Provide simple and tight asymptotic bounds for the running times of the following methods as a function of the value of N, the length of A. If possible, give a single Θ bound that always describes the running time, regardless of the input. Otherwise give an upper and a lower bound. (To be precise, we assume there is some set of values for A, one of each size, and we want you to put as good a bound as you can on the running time as a function of the size that will hold for any such set of values). a. static void foo(int[] A) { int N = A.length; for (int n = N; n > 1; n -= 1) { for (int i = 1; i < n; i += 1) { if (A[i - 1] > A[i]) { int tmp = A[i - 1]; A[i - 1] = A[i]; A[i] = tmp; Bound(s): b. static int bar(int[] A) { int N = A.length; int S; S = 0; for (int i = 0; i < N; i += 1) { for (int j = i+1; j < N; j += 1) { if (A[j] == A[i]) { S += 1; break; return S; Bound(s):

Test #1 Login: Initials: 4 c. static boolean sump(int[] A, int S) { return sump1(a, S, 0); private static boolean sump1(int[] A, int S, int k) { int N = A.length; if (S == 0) return true; else if (k >= N) return false; else if (S >= A[k] && sump1(a, S-A[k], k+1)) return true; else return sump1(a, S, k+1); Bound(s) on sump: (Include time of the helper function sump1 in the time for sump).

Test #1 Login: Initials: 5 2. [4 points] a. Assuming that 0 m 31, fill in the blank with a Java expression involving only the variable m (not s or k) so that the program fragment below prints "OK": int k, s; k = 1; s = 0; for (int i = 1; i < m; i += 1) { s = s k; k = k<<1; if (s == ) System.out.println ("OK"); The expression in the blank may not call any methods. b. One of the three comments below must be wrong (that is, unsatifiable regardless of the method body). Which one and why just that one? [NOTE: you must tell why to get full credit.] /** Upon completion, A contains the concatenation of the original * list A in front of the list B. The original contents of * B are not affected. */ void concat1 (IntList A, IntList B) {... /** Returns the result of concatenating the contents of A in front of * the contents of B. The original contents of A and B are not * affected. */ IntList concat2 (IntList A, IntList B) {... /** Upon completion, A contains the concatenation of the original list * A in front of the list B. The original contents of B are * not affected. Assumes that A is not empty. */ void concat3 (IntList A, IntList B) {...

Test #1 Login: Initials: 6 c. What does the following legal program print when the main program is invoked? class Exceptions { public static void main (String... args) { try { f (42, new IllegalArgumentException ()); catch (IllegalArgumentException e) { System.out.println ("main"); static void f (int n, IllegalArgumentException err) { if (n > 39) { try { f (n-1, err); catch (IllegalArgumentException e) { System.out.printf ("f(%d) err%n", n); System.out.printf ("f(%d)%n", n); else { g (n, err); static void g (int n, IllegalArgumentException err) { if (n < 3) throw err; else f(n-1, err);

Test #1 Login: Initials: 7 d. If I compile and run the program below with java G, what is printed? (If execution causes a runtime error, just write error at the appropriate point in the printout. If the program won t compile, just say so.) abstract class F { abstract int a (int x); F c (F g) { System.out.println ("F.c"); return new C(this, g); F r (int n) { System.out.println ("F.r"); if (n <= 1) return this; else return this.c (r(n-1)); F c2 () { return this.r(2); static class C extends F { private F f, g; C (F f, F g) { this.f = f; this.g = g; int a (int x) { return f.a(g.a(x)); class G extends F { int i; F r (int n) { System.out.println ("G.r"); return new G (n * i); G (int i) { this.i = i; int a (int x) { return x + i; public static void main (String... args) { F f = new G (21); System.out.println (f.c2().a(0));

Test #1 Login: Initials: 8 3. [1 point] What kind of journey are we undertaking whan that Apryll, wyth hys shouris sote, the droughte of Marche hath percyd the rote? 4. [6 points] Fill in the following method to obey its comment. /** Set each R[k] to a sublist of L such that R[k] contains * <=k+1 elements and the concatenation of all the R[k] in order * gives a prefix of the original list L. Each list R[k] is made * as large as possible subject to these rules, with earlier lists * taking precedence. For example, if the original L contains * [ 1, 2, 3, 4, 5, 6, 7 ], and R has 6 elements, then on return R * contains [ [1], [2,3], [4,5,6], [7], [], []]. If R had only 2 * elements, then on return it would contain [[1], [2,3]]. * May destroy the original contents of the IntList objects in L, * but does not create any new IntList objects. */ static void triangularize (IntList[] R, IntList L) {

Test #1 Login: Initials: 9 5. [7 points] Programs that work with very long sequences of characters (such as text editors) typically do not use ordinary Strings to represent the sequences because most operations on long Strings are not efficient. Instead, these programs use representations implemented using collections of much smaller Strings, each no more than a fixed, maximum size. For example, if this maximum size were 4, then such a representation could internally represent the string"hi, world" as the list of Strings ["Hi, ", "worl", "d"] or as ["Hi", ", w", "orld" ], etc. The SegmentedString class declared below is one such representation (incomplete and not particularly efficient). a. Fill in the indicated blanks in the implementation of SegmentedString to fulfill the comments. From the point of view of the client who uses this class, it represents a list of characters. The fact that internally it breaks the input into smaller strings must remain completely invisible to the client. The compiler supports autoboxing, so you can assume that values of type char and values of type Character are pretty much interchangeable. /** A sequence of characters. */ public class SegmentedString extends AbstractList<Character> implements List<Character> { private final static int MAX_SEGMENT_LENGTH = <some integer>; /** The constituent substrings of THIS. The full sequence of characters * that THIS represents is the concatenation of the Strings in segments. * Each segment has length no greater than MAX_SEGMENT_LENGTH. Segments * need not be of maximum length. */ private List<String> segments; /** Initializes an empty sequence. */ public SegmentedString() { // FILL IN (about 1 line) /** Adds STR to the end of THIS character sequence. */ public void append(string str) { // FILL IN (about 5 lines)

Test #1 Login: Initials: 10 /** Returns the length of (number of characters in) the sequence. */ public int size() { // FILL IN (about 5 lines) /** The character at index K in the sequence. * Throws IndexOutOfBoundsException when K is negative or >= size(). */ public Character get(int k) { // FILL IN (about 10 lines)

Test #1 Login: Initials: 11 b. Suppose that we want to provide an iterator for SegmentedString that allows us to iterate through the entire sequence in time O(N), where N is the size of the string. We can do so by adding the following to the SegmentedString class. Fill in just the representation that is, the constructor, the instance variables and their comments for the SegIterator class below to allow this. Make sure the comments explain exactly what each instance variable is supposed to mean. public Iterator<Character> iterator () { return new SegIterator (); private class SeqIterator implements Iterator<Character> { SeqIterator () { FILL IN // FILL IN INSTANCE VARIABLES AND THEIR COMMENTS (ONLY)