You have three hours to complete this exam. There are 50 total points.

Similar documents
CS106A Handout 28 Winter February 28, 2014 Second Practice Second CS106A Midterm

private String longestisogram(arraylist<string> allwords) CS106A Handout 30 Winter March 6, 2013 Practice Second Midterm Exam #2

CS106A Handout 27 Winter February 26, 2014 Practice Second CS106A Midterm Exam

CS106A Handout 15 Winter 2015 February 4, 2015 CS106A Practice Midterm

Spring 2018 June 20 th, 2018 Extra Practice Problems. Short Problems

Below is the overall grade distribution for the second CS106A midterm exam:

CS106A Handout 18 Winter February 3, 2014 Practice Midterm Exam

Spring 2018 June 20 th, 2018 CS106A Practice Final #1

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

Practice Midterm Examination

Practice Midterm #2. Midterm Time: Monday, July 18 th, 7pm 9pm Midterm Location: Hewlett 200

Practice Final Examination #2

COMP-202: Foundations of Programming. Lecture 26: Image Manipulation; Wrap-Up Jackie Cheung, Winter 2015

Practice Midterm Examination

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Assignment 2: Welcome to Java!

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Practice Midterm Examination #1

Practice Midterm Examination

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

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

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

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

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

CSE 131 Introduction to Computer Science Fall Exam I

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Test 1: CompSci Exam 1 Solutions

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.

Section Solutions 8. /* The separator used to delimit the start and end of a flight. */ private static final String FLIGHT_DELIMITER = " -> ";

Quiz 1 (March 14, 2016)

Additional Practice Problem Solutions

CS103 Handout 42 Spring 2017 May 31, 2017 Practice Final Exam 1

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

c. Typically results in an intractably large set of test cases even for small programs

CSE331 Winter 2014, Midterm Examination February 12, 2014

CS314 Exam 1 - Fall Suggested Solution and Criteria 1

YEAH Hours. January , 7-8 PM Jared Wolens

Practice Midterm Examination #2

CSE 241 Algorithms and Data Structures Fall Semester, Midterm Exam

Practice Final Examination

Programming Project 1

Practice Final Examination #2

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

Practice Midterm 1. Problem Points Score TOTAL 50

Karel Prepares for the Olympics! (25 points)

CSE 142 Sp02 Final Exam Version A Page 1 of 14

Test 2: Compsci 100. Owen Astrachan. November 14, 2006

printf( Please enter another number: ); scanf( %d, &num2);

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Formulas, LookUp Tables and PivotTables Prepared for Aero Controlex

CIT Special final examination

CS1004: Intro to CS in Java, Spring 2005

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

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

MITOCW watch?v=0jljzrnhwoi

1. Answer as shown or -2 unless question allows partial credit. No points off for differences in spacing, capitalization, commas, and braces

CPSC 35 Midterm Exam

CSE 143 Au03 Final Exam Page 1 of 15

Introduction to Computer Science I

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli

Designing and Printing Address Labels

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

Assignment 1: Evil Hangman

CS106A, Stanford Handout #30. Coding Style

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

Database Systems, CSCI Exam #2 Thursday November 4, 2010 at 2 pm

CSE 303 Midterm Exam

XC Total Max Score Grader

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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

Introduction to Java

Exam 1 - (20 points)

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Final Exam Time: Friday, August 12th, 12:15pm - 3:15pm Final Exam Location: Split by last name

Section 1 Click Instructions Talking Points

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

CS103 Handout 14 Winter February 8, 2013 Problem Set 5

Practice exam for CMSC131-04, Fall 2017

Tips from the experts: How to waste a lot of time on this assignment

CS106X Handout 24 Winter 2018 February 9 th, 2018 CS106X Midterm Examination

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Conditions, logical expressions, and selection. Introduction to control structures

Parameters and Objects

#101 Page: 1 Name: CS32 Midterm Exam. E01, W15, Phill Conrad, UC Santa Barbara

MC302 GRAPH THEORY SOLUTIONS TO HOMEWORK #1 9/19/13 68 points + 6 extra credit points

Assignment #2: Simple Java Programs Due: 1:15pm on Friday, April 19th

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

CS 106A, Lecture 20 ArrayLists and HashMaps

CSE 331 Winter 2016 Midterm Solution

CS107 Handout 26 Spring 2007 May 9, 2007 CS107 Midterm Exam

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Transcription:

CS106A Handout 22 Winter 2015 February 26, 2015 Practice Second CS106A Midterm Exam This exam is closed-book and closed-computer but open-note. You may have a double-sided, 8.5 11 sheet of notes with you when you take this exam. Please hand-write all of your solutions on this physical copy of the exam. In all questions, you may include methods, classes, or other definitions that have been developed in the course by giving the name of the method or class and the handout, chapter number, lecture, or file in which that definition appears. Unless otherwise indicated as part of the instructions for a specific problem, comments will not be required on the exam. Uncommented code that gets the job done will be sufficient for full credit on the problem. On the other hand, comments may help you to get partial credit if they help us determine what you were trying to do. Unless stated otherwise, you do not need to worry about efficiency. On the actual exam, there'd be space here for you to write your name and sign a statement saying you abide by the Honor Code. We're not collecting or grading this exam (though you're welcome to step outside and chat with us about it when you're done!) and this exam doesn't provide any extra credit, so we've opted to skip that boilerplate. You have three hours to complete this exam. There are 50 total points. Question Points Grader (1) Isograms (10) / 10 (2) Announcing Election Results (10) / 10 (3) Short Answer Questions (10) / 10 (4) Kerning (10) / 10 (5) Animal Hipsters (10) / 10 (50) / 50 Good Luck!

2 / 10 Problem One: Isograms (10 Points) An isogram is a word that contains no repeated letters. For example, the word computer is an isogram because each letter in the word appears exactly once, but the word banana is not because 'a' and 'n' appear three times each. Isogram is itself an isogram, but isograms is not because there are two copies of 's'. There are many long isograms in English; for example, uncopyrightable and computerizably. Write a method private boolean isisogram(string word) that accepts as input a string containing a single word, then returns whether that word is an isogram. The input word won't contain spaces or punctuation, but it might contain both upper-case and lowercase letters. private boolean isisogram(string word) {

3 / 10 Problem Two: Announcing Election Results (10 Points) Suppose that you are in charge of writing software to tally up votes in an election. Your job is to write a method private String electionwinner(string[] votes) This method accepts as input a String[] representing all the votes that were cast in an election. You should then determine whether any candidate received strictly more than half the votes. If so, you should return the name of that candidate. If no candidate won (perhaps, for example, there are three candidates and each got a third of the votes), you should return null as a sentinel. In writing this method, you can assume the following: Candidate names are case-insensitive, so Karel the Robot and KAREL the ROBOT should both count as votes for Karel the Robot. Because candidate names are case-insensitive, you can return the name of the winning candidate in any capitalization you'd like. There can be any number of candidates, not just two. There can be any number of votes in the input array, including zero. private String electionwinner(string[] votes) {

4 / 10 Problem Three: Short Answer (10 Points) (i) Comparing Data Structures, Part One (4 Points) The String type is Java's standard way of storing text. You could also use a char[] to store text by simply representing the text as an array of all of its characters. Give one advantage of representing text as a String over representing text as a char[] and vice-versa. (ii) Comparing Data Structures, Part Two (3 Points) We've seen ArrayList<type>s as one way of storing a growable sequence of elements of type type. Another alternative would be to use a HashMap<Integer, type>, where the keys represent the indices and the values represent the values stored at each position. Give one advantage of representing a list as an ArrayList<type> over representing the list using a HashMap<Integer, type> and vice-versa.

5 / 10 (iii) An ios Vulnerability (3 Points) Last year, Apple announced a serious security error in its ios operating system that made it possible for hackers to bypass security measures used when communicating over the internet. This was due to a small programming error that's understandable given just what you've seen in CS106A so far. Below is some code that contains an error similar to the one that caused the security bug in ios. For convenience, we've add line numbers to this code. /** * Given a number, returns whether that number is divisible by * two, three, or five. * * @param number The number in question. * @return true if that number is divisible by two, three, or five * and false otherwise. */ 1: private boolean hassmalldivisor(int input) { 2: boolean result = false; 3: 4: if (input % 2 == 0) 5: result = true; 6: 7: if (input % 3 == 0) 8: result = true; 9: result = true; 10: 11: if (input % 5 == 0) 12: result = true; 13: 14: return result; 15: } This method is incorrect because it always returns true. Explain why. Be specific.

6 / 10 Problem Three: Kerning (10 Points) Although we've used GLabel, we've never discussed how the computer actually displays text. Internally, the computer maintains a set of images representing what each character looks like. To display text on the screen, the computer lays out these images side-by-side. For example, to display the string VAT, the computer begins with a set of images for the letters V, A, and T, then places them side-byside to form the string. This is shown here: V A T VAT Unfortunately, this approach to laying out text will distort certain strings. For example, consider the following rendition of the string THE VATICAN: THE VATICAN Notice how the V, A, and T in VATICAN appear to be spaced out more than the T, I, and C. The reason for this is that the images for the letters V, A, and T have a lot of whitespace in them. When the images for the letters are placed next to one another, this whitespace adds up and spaces the letters farther apart than they should be. To correct for this, the computer typically overlaps the images for certain pairs of letters to reduce whitespace. For example, if we slightly overlap the images for V and A and the images for A and T, we get this rendering of the word VAT: VAT The amount that the images of two letters overlap is called the kern, and the process of overlapping letters this way is called kerning. Kerning can make text much more aesthetically pleasing. Compare the above rendition of THE VATICAN, which had no kerning, to this one, which has been kerned: THE VATICAN Notice how there is less blank space between the V, A, and T in VATICAN. Your task in this problem is to write a method that will accept as input images of two letters, then will kern the images by some specified amount. For example, here is the sample output of this method on the letters V and A with several different kerns; the vertical bar in the outputs marks the end of the V image:

7 / 10 Kern 0 Kern 1 Kern 2 For simplicity, and to avoid some of the complexities of GImage, we will represent the images of letters as two-dimensional arrays of booleans indicating for each pixel in the image whether the pixel is white (false) or black (true). As an example, the letter A might be represented as follows: Write a method { { false, false, false, false, true, false, false, false, false }, { false, false, false, false, true, false, false, false, false }, { false, false, false, true, false, true, false, false, false }, { false, false, false, true, false, true, false, false, false }, { false, false, true, false, false, false, true, false, false }, { false, false, true, false, false, false, true, false, false }, { false, true, true, true, true, true, true, true, false }, { false, true, false, false, false, false, false, true, false }, { false, true, false, false, false, false, false, true, false }, { true, true, true, false, false, false, true, true, true } } private boolean[][] kernletters(boolean[][] first, boolean[][] second, int kern) that accepts as input two boolean arrays representing images of letters, along with an amount to overlap the two images, then returns a new boolean array representing the image formed by kerning the two letters by the given amount. You can assume that the two images have the same height, though they might not have the same width. You can also assume that the amount to kern the letters is nonnegative and is smaller than the widths of either image. As shown in the sample outputs at the top of this page, the resulting image should be no wider than necessary. If the kern is zero, the width of the resulting image should be the width of the two individual images put together. As the kern increases, the width of the result image should decrease. Write your solution on the next page, and feel free to tear out this page and the previous as a reference.

8 / 10 private boolean[][] kernletters(boolean[][] first, boolean[][] second, int kern) {

9 / 10 Problem Five: Animal Hipsters (10 Points) Suppose that you have a social network represented as a graph, like this one here: Amy Brie Chris Devney Elena Fan Hal As in lecture, we will represent this graph as a HashMap<String, ArrayList<String>>, where each key in the HashMap is the name of a person and each value is an ArrayList of the names of the people they are friends with. Let's suppose that every person in a social network has a favorite animal. We'll say that a person is an animal hipster if their favorite animal is different from all of their friends' favorite animals. For example, suppose that everyone's favorite animals are specified as follows: Amy (Quokka) Brie Brie (Springbok) Chris (Nubian Ibex) Devney (Springbok) Elena (Quokka) Fan Hal (Springbok) Given the above social network, we would have that Amy, Chris, Elena, and Fan-Hal are animal hipsters, but Brie and Devney are not (because both of them like springboks and are they friends of one another). Although both Amy and Elena like quokkas, they are still animal hipsters because they are not friends of one another. Write a method private ArrayList<String> findanimalhipsters(hashmap<string, ArrayList<String>> network, HashMap<String, String> favoriteanimals) that accepts as input a social network network and a HashMap<String, String> favoriteanimals associating each person in the network with their favorite animal, then returns an ArrayList<String> containing all the people in the network who are animal hipsters. (Continued on the next page)

10 / 10 In writing this method, you should assume the following: The network and favoriteanimals HashMaps have the same set of keys, so every person in the graph has a favorite animal and everyone who has a favorite animal is in the graph. For simplicity, you can assume animal names are case-sensitive, so Nubian Ibex and nubian ibex should be treated as different animals. You are free to return the animal hipsters in any order that you'd like, though each animal hipster should appear in the list at most once. Although we haven't seen this yet, you can iterate over the keys of a HashMap by using a range-based for loop by writing Write your method in the space below. for (String key: map.keyset()) { /* */ } private ArrayList<String> findanimalhipsters(hashmap<string, ArrayList<String>> network, HashMap<String, String> favoriteanimals) {