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

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

CS106A Handout 18 Winter February 3, 2014 Practice Midterm Exam

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

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

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

Practice Midterm Examination

Practice Midterm Examination

Assignment 2: Welcome to Java!

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

Practice Midterm Examination

Assignment #3 Breakout!

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Practice Midterm Examination #1

Mehran Sahami Handout #7 CS 106A September 24, 2014

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

Practice Midterm Examination #2

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Section Handout #7: Interactors and Classes

Karel Prepares for the Olympics! (25 points)

Introduction to Java

Object-Oriented Programming

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

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

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

Solutions to Midterm Exam

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

Programming via Java Defining classes

CS103 Handout 50 Fall 2018 November 30, 2018 Problem Set 9

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

Due Thursday, July 18 at 11:00AM

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

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm


Program One. Program Two. import acm.program.*;

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

CS103 Spring 2018 Mathematical Vocabulary

CS103 Handout 14 Winter February 8, 2013 Problem Set 5

There we are; that's got the 3D screen and mouse sorted out.

Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

What is Java? professional software engineering.

Assignment 1: Evil Hangman

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

Programming Project 1

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

Extra Practice Problems 2

UNIVERSITY REGULATIONS

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

Using X-Particles with Team Render

Programming Karel the Robot

Before we start: Any questions on Karel?

Variables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Section Handout #7: Interactors and Classes

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

Decisions: Logic Java Programming 2 Lesson 7

CS 2510 Exam 3 SOLUTION Spring 2011

Programming in the Real World

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

CS103 Handout 35 Spring 2017 May 19, 2017 Problem Set 7


Choosing Paper Size and Layout. UP TO SPEED Of Menus and Boxes

CSC D84 Assignment 2 Game Trees and Mini-Max

Graphing on Excel. Open Excel (2013). The first screen you will see looks like this (it varies slightly, depending on the version):

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

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

CSE 142 Su 02 Homework 4

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Turing Machines Part Two

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

Unit #13 : Integration to Find Areas and Volumes, Volumes of Revolution

Computer Science II Spring 2018 Final Exam Instructor: Arup Guha Date: 5/1/2018. Name:

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

PATTERN MAKING FOR THE PHOENIX HOOP

Lab 1 Implementing a Simon Says Game

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

Lecture 15. For-Loops

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Assignment 1: Evil Hangman

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

AP CSA 3rd Period MR. D. Course Map

ECE15: Lab #4. Problem 1. University of California San Diego

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

Solutions to the Second Midterm Exam, Math 170, Section 002 Spring 2012

Laboratory 1: Eclipse and Karel the Robot

Name. Criteria for grading. Total Points Earned. What to do to Create your own Tessellation

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, Jan 29 th

CS 3 Midterm 1 Review

Java Programming. Computer Science 112

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Practice Final Examination #2

CSE331 Winter 2014, Midterm Examination February 12, 2014

Dice in Google SketchUp

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

CS106A Review Session

Introduction To Inkscape Creating Custom Graphics For Websites, Displays & Lessons

Practice Midterm Exam #1

Practice Final Examination

CS244a: An Introduction to Computer Networks

CS106B Sprint 2017 April 27 th, 2017 CS106B Practice Midterm (KEY)

Transcription:

CS106A Handout 15 Winter 2015 February 4, 2015 CS106A Practice Midterm 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, and this exam is worth 20% of your grade in this course. Question (1) RectangleKarel Points / 10 (2) Jumbled Java hijinks / 10 (3) Slicing a Cake / 10 (4) Pebbling a Checkerboard / 10 (5) Damaged DNA Diagnoses / 10 / 50 Good Luck!

2 / 15 Problem 1: RectangleKarel (10 Points) In this problem, you will write a program to get Karel to fill a rectangular region of Karel's world with beepers. The rectangle's upper-left corner is given by Karel's initial position, and the rectangle's lowerright corner should be the lower-right corner of the world. For example: Initial World Result In implementing your solution, you can assume the following: Karel always begins facing East, but otherwise Karel's initial position is unspecified. Karel's world has arbitrary size, no beepers, and no internal walls. Karel's beeper bag has infinitely many beepers in it. Karel's final position and orientation are irrelevant. You are limited to the instructions in the Karel booklet. For example, the only variables allowed are loop control variables used within the control section of a for loop (where the loop upper bound is a fixed number), you must not use the break or return statements, etc. You may, however, use the &&,, and! operators in the conditions of if statements and while loops. Write your solution on the next page, and feel free to tear out this page as a reference.

3 / 15 import stanford.karel.*; public class RectangleKarel extends SuperKarel { public void run() {

(Extra space for Problem One, if you need it) 4 / 15

5 / 15 Problem Two: Jumbled Java hijinks (10 Points Total) (i) Solve for x (6 Points) For each of these programs, determine whether there are any values of x that can be entered so that the program will print success without causing any errors and without printing anything else. If there are multiples values of x that will work, just give one of them. If there are no values of x that will work, write no solution. No justification is necessary. /* Program A */ int x = readint(); if (x!= 0 && x / 2 == 0) { println("success"); } Answer for Program A: /* Program B */ int x = readint(); if (x >= 10000) { while (x!= 0) { if (x % 10!= 9) { println("failure"); } x /= 10; } println("success"); } Answer for Program B: /* Program C */ int x = readint(); if (x > 7 && x / 0 == x) { println("success"); } Answer for Program C: /* Program D */ int x = readint(); if (x!= 0 x!= 1) { println("failure"); } println("success"); Answer for Program D: /* Program E */ int x = readint(); if (1 3 5 x == -10) { println("success"); } Answer for Program E: /* Program F */ int x = readint(); if (x / 2 * 3 == 6) { println("success"); } Answer for Program F:

6 / 15 (ii) Program Tracing Determine the output of the following program and write it in the indicated box. (4 Points) import acm.program.*; public class FrozenJava extends ConsoleProgram { public void run() { int anna = 16; int elsa = 18; } anna = arendelle(anna, elsa); println("anna = " + anna); println("elsa = " + elsa); private int arendelle(int elsa, int anna) { String kristoff = "hans"; weselton(kristoff); println("kristoff = " + kristoff); } elsa = kristoff.length(); return anna; } private void weselton(string olaf) { olaf += "el"; println("olaf = " + olaf); } Write the output of the program in this box:

7 / 15 Problem Three: Slicing a Cake (10 Points) Suppose that n people want to share a rectangular cake that's n inches long (that is, the cake's length is equal to the number of people, in inches). Each person takes turns going up to the cake and cutting a slice for herself. In a perfectly fair world, each person would cut a one-inch piece of cake so that everyone would end up with the same amount of cake. We'll say that a person is happy if she gets at least a one-inch slice of cake. Unfortunately, people are greedy and won't always cut a piece of the right size. Suppose that the cake gets sliced in the following way. Each person, one after the other, goes up to the cake to slice a piece. If there is at least 2 of cake left, the current person randomly chooses an amount of cake between 1 and 2 and cuts a piece of that size, ending up happy. If there Cake! is between 1 and 2 of cake left, then the current person takes whatever's left and is happy. Otherwise, there's less than 1 left, and the current person takes what's left but isn't happy. n Your job in this problem is to write a program that estimates the average number of people who will end up happy this way. On startup, your program should prompt the user for the number of people who will split the cake. You should then run NUM_TRIALS simulations of slicing the cake according to the above rules. (In the sample runs below, NUM_TRIALS is 10). After each simulation, your program should print out how many total people end up happy (that is, with at least a 1 slice of cake). Once all the simulations finish, you should print out the average number of happy people across all the rounds. Two sample runs of this program are shown below: Write your answer on the next page, and feel free to tear out this page as a reference.

8 / 15 import acm.program.*; import acm.util.*; public class SlicingACake extends ConsoleProgram { private static final int NUM_TRIALS = 10; public void run() {

(extra space for Problem Three, if you need it) 9 / 15

10 / 15 Problem Four: Pebbling a Checkerboard (10 Points) Suppose that you have a checkerboard of some size. You begin with three checkers in the lower-left corner in an L-shape, as shown here: In this game, each move consists of removing a checker from a square and replacing that checker with two checkers one directly above the original checker and one directly to the right of the original checker. It is only legal to move a checker if both of these adjacent squares are unoccupied. For example, in the diagram to the left, it's legal to move checker B by replacing it with the two checkers B₁ B₁ above it and B ₂ to its right, yielding the given result. However, the other A B A B₂ checkers in the initial setup can't C C move checker A has a checker to its right (namely, B) and checker C has a checker above it (namely, B). After moving checker B, the player can either move checker A or move checker C. Your job is to write a program that lets the user play this game. Specifically, when your program begins, you should draw three checkers in an L shape in the bottom-left corner of the screen. Whenever the user clicks on a checker, if the squares directly above the checker and directly to the right of the checker are free, then you should remove the checker that was clicked on and add two checkers, one directly above it and one directly to the right. A sample run of the program is shown below: User clicks this checker User clicks this checker (Continued on next page)

11 / 15 When implementing this program, you should adhere to the following: The checkers should be filled in solid black and have width and height given by the constant CHECKER_SIZE. You do not need to (and, for the sake of time, shouldn't) actually draw the squares on the checkerboard. You just need to show where the checkers are. You do not need to leave any margins between the checkers. The checkers can be flush up against one another. The player never wins or loses this game; all you need to do is implement the logic to let the user make moves. If the user clicks on a checker that cannot move, your program shouldn't move it. However, you do not need to report an error or otherwise inform the user of why the checker can't move. If the user clicks on a checker that's at the very top or far right of the window, it's okay to let the user move that checker, even though the resulting checkers will be off the screen. As a reminder, you can use the getelementat method to find which checker, if any, is at a particular location on the screen. You may want to make multiple calls to getelementat in order to determine whether the user has clicked on a checker and whether there are any checkers above or to the right of the selected checker. import acm.program.*; import acm.graphics.*; import java.awt.*; import java.awt.event.*; public class PebblingACheckerboard extends GraphicsProgram { /* The size of a checker. */ private static final double CHECKER_SIZE = 50;

(extra space for Problem Four, if you need it) 12 / 15

13 / 15 Problem Five: Damaged DNA Diagnoses (10 Points) DNA molecules consist of two paired strands of nucleotides, molecules which encode genetic information. Computational biologists typically represent each DNA strand as a string made from four different letters A, C, T, and G each of which represents one of the four possible nucleotides. The two paired strands in a DNA molecule are not arbitrary. In normal DNA, the two strands always have the same length, and each nucleotide (letter) from one strand is paired with a corresponding nucleotide (latter) from the second strand. In normal DNA strands, the letters are paired as follows: A is paired with T and vice-versa. C is paired with G and vice-versa. Below are two matching DNA strands. Note how the letters are paired up according to the above rules: GCATGGATTAATATGAGACGACTAATAGGATAGTTACAACCCTTACGTCACCGCCTTGA CGTACCTAATTATACTCTGCTGATTATCCTATCAATGTTGGGAATGCAGTGGCGGAACT In some cases, errors occur within DNA molecules. This problem considers two types of DNA errors: Point mutations, in which a letter from one strand is matched against the wrong letter in the other strand. For example, A might accidentally pair with C, or G might pair with G. Unmatched nucleotides, in which a letter from one strand is not matched with a letter from the other. We represent this by pairing a letter from one strand with the character in the other. For example, consider these two DNA strands: GGGA-GAATCTCTGGACT CCCTACTTA-AGACCGGT Here, there are two unmatched nucleotides (represented by one DNA strand containing a dash character instead of a letter) and two point mutations (A paired with G and T paired with T). Although both of these types of errors are problematic, we will consider point mutations to be a less costly error than an unmatched nucleotide. Let's assign a point mutation (a letter matched with the wrong letter) a cost of 1 and an unmatched nucleotide (a letter matched with a dash) a cost of 2. Your job is to write a method private int costofdnaerrorsin(string one, String two) that accepts as input two strings representing DNA strands, then returns the total cost of all of the errors in those two strands. You may assume the following: The two strings have the same length. Each string consists purely of the characters A, C, T, G, and (the dash character). All letters will be upper-case. A character in one string is never paired with a character in the other.

14 / 15 Here are some example inputs to the method, along with the expected result. For simplicity, all errors have been highlighted in bold: Strands ACGT TGCA A-C-G-T-ACGT TTGGCCAATGCA AAAAAAAA TTTATTTT GATTACA CTATT-T CAT-TAG-ACT GTATATCCAAA -------- ACGTACGT TAATAA ATTATT GGGA-GAATATCTGGACT CCCTACTTA-AGACCGGT Result 0 (No errors) 8 (Four unmatched nucleotides) 1 (One point mutation) 3 (One point mutation, one unmatched nucleotide) 6 (Two point mutations, two unmatched nucleotides) 16 (Eight unmatched nucleotides) 0 (No errors) 6 (Two point mutations, two unmatched nucleotides) Feel free to tear this and the preceding page out as a reference, and write your implementation on the next page.

private int costofdnaerrorsin(string one, String two) { 15 / 15