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

Similar documents
Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

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

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

Final exam. CS 2110, December 15, 2016, 9:00AM

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

Prelim 2. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Prelim 1. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

XC Total Max Score Grader

Prelim 2 Solution. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2 Solution. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

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

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

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

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

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

CS211 Prelim Oct 2001 NAME NETID

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

CS100J Prelim 1 22 February 2007

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

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

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

1 Inheritance (8 minutes, 9 points)

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

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Final Exam. CS2110 Spring May 12, Total. Max Score

Final Exam. COMP Summer I June 26, points

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

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

JAVA BASICS II. Example: FIFO

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

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

C a; C b; C e; int c;

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CPS122 Lecture: Detailed Design and Implementation

CS 1110 Prelim 1 October 4th, 2012

Self-test Java Programming

Final Exam COMP Fall 2004 Dec 16, 2004

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CSE 331 Midterm Exam 2/13/12

CS 113 PRACTICE FINAL

MCS-378 Intraterm Exam 1 Serial #:

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Last name:... First name:... Department (if not D-INFK):...

Learning objectives. Reading carefully. Managing your time. CS2110 Fall 2017 Assignment A1. PhD Genealogy. See CMS for the due date

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Introduction to Programming Using Java (98-388)

Definition of DJ (Diminished Java)

COMP 524 Spring 2018 Midterm Thursday, March 1

Prelim 2, CS2110. SOLUTION

Subclassing for ADTs Implementation

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CSE wi Midterm Exam 2/8/18 Sample Solution

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

CS 101 Fall 2006 Midterm 3 Name: ID:

CS 1110 Prelim 2 November 6th, 2012

Total Score /15 /20 /30 /10 /5 /20 Grader

Review sheet for Final Exam (List of objectives for this course)

CIS 110 Introduction To Computer Programming. November 21st, 2011 Exam 2

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

CS 1110 Final, December 8th, Question Points Score Total: 100

CSCI 355 LAB #2 Spring 2004

Total Score /15 /20 /30 /10 /5 /20 Grader

CSCI 355 Lab #2 Spring 2007

I. True/False: (2 points each)

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

I. True/False: (2 points each)

Final Exam CS 251, Intermediate Programming December 10, 2014

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

Chapter 4 Defining Classes I

CompuScholar, Inc. 9th - 12th grades

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

This examination has 11 pages. Check that you have a complete paper.

UMBC CMSC 331 Final Exam

Written by John Bell for CS 342, Spring 2018

EXAM Computer Science 1 Part 1

Agenda: Discussion Week 7. May 11, 2009

Computer Science 210: Data Structures

Transcription:

Prelim 1 CS 2110, September 29, 2016, 7:30 PM 0 1 2 3 4 5 Total Question Name Loop invariants Recursion OO Short answer Exception handling Max 1 15 15 25 34 10 100 Score Grader The exam is closed book and closed notes. Do not begin until instructed. You have 90 minutes. Good luck! Write your name and Cornell NetID at the top of every page! There are 5 questions on 8 numbered pages, front and back. Check that you have all the pages. When you hand in your exam, make sure your pages are still stapled together. If not, please use our stapler to reattach all your pages! We have scrap paper available. If you do a lot of crossing out and rewriting, you might want to write code on scrap paper first and then copy it to the exam so that we can make sense of what you handed in. Write your answers in the space provided. Ambiguous answers will be considered incorrect. You should be able to fit your answers easily into the space provided. In some places, we have abbreviated or condensed code to reduce the number of pages that must be printed for the exam. In others, code has been obfuscated to make the problem more difficult. This does not mean that it s good style. Academic Integrity Statement: I pledge that I have neither given nor received any unauthorized aid on this exam. That includes not talking to the people who took the 5:30 exam. (signature) 0. Name (1 point) Write your name and NetID at the top of every page of this exam. 1 of 8

1. Loop Invariants (15 points) (a) 6 points Consider the following precondition and postcondition. m Precondition: c unknown Postcondition: c m even Generalize them, completing the invariant below. Your generalization should introduce a new variable. Place variables carefully; ambiguous answers will be considered incorrect. t odd n x n x Invariant: c m n (b) 9 points Consider the following precondition, postcondition, and invariant. 0 n Precondition Q: d unknown 0 n Postcondition R: d < 5 = 5 > 5 0 h k t n Invariant P: d < 5 = 5 unknown > 5 Below, write a loop with initialization that uses invariant P to implement the comment given below. Assume d is already initialized. You don t have to declare variables, but you must assign appropriate values to h, k, and t where necessary. To swap d[i] and d[j], just say, Swap d[i], d[j]. Your grade depends only on your use of the four loopy questions to write the code. // Given Precondition Q, swap values of d[0..n] to truthify Postcondition R. 2 of 8

2. Recursion (15 Points) (a) Write the following function occ, in Java. For example, occ(2, d ) is dd. You must use recursion. Do not use a loop. Do not write assert statements for the Precondition. /** = a String containing n occurrences of c. * Precondition: n >= 0. */ public static String occ(int n, char c) { (b) Write the following function expand, in Java. Use recursion. Do not use a loop. Do not write assert statements for the Precondition. You can use function occ. As an example, p( 2A0B3V ) produces a string with 2 As, 0 Bs, and 3 Vs, i.e. it produces AAVVV. You will need to convert a character-digit like 4 to an int. There are several ways to do that. /** Return s but with each pair "ic" of characters, where i is a digit, * replaced by i occurrences of c. * Precondition: s contains an even number of characters, and the * first of each pair is a digit. */ public static String expand(string s) { 3 of 8

3. Object-Oriented Programming (25 points) (a) 10 points Below are two class declarations. Complete the bodies of the constructor and function tostring in class Grad. Be careful; pay attention to access modifiers. public class Student { private String name; /** A student named na. * Precon.: no spaces in na. */ public Student(String na) { name= na; public class Grad extends Student { private String advisor; /** Constructor: a grad named * na with advisor ad. * Precon.: no spaces in na, ad. */ public Grad(String na, String ad) { /** = name of this candidate */ public String tostring() { return name; /** = String containing grad s * name, a comma and space after * it (", "), and advisor. */ public String tostring() { (b) 5 points Suppose the following assignment has been executed, where the arguments... are strings containing a person s name and advisor. Grad g= new Grad(...,...); Write a sequence of statements to extract the advisor of the person in object g and store it in String variable v. You don t have to declare v or any other variables. It doesn t matter whether you wrote methods in part (b) correctly; we go by the method specifications. 4 of 8

(c) 5 points Method equals, shown below, is to be placed in class Grad. Complete the method body. Also, after the method body, write what happens if the type of parameter ob is changed to Grad. /** Return true iff ob is a Grad and * ob has the same advisor as this Grad. */ public @Override boolean equals(object ob) { (d) 5 points won t compile. Below are two classes and one interface. Below them, state two reasons why this public abstract class A implements I { public abstract int m(); public int f= 10; public interface I { public class B extends A implements I { A a= new A(); public void p() { f= 20; 5 of 8

4. Short Answer (34 points.) (a) 5 points Write down the steps in executing a method call m(args). (b) 5 points. Below are five expressions. To the right of each, write its value. 1. (int) @ == @ 2. (char) ( d - 2) 3. new Double(5) == new Double(5) 4. ((Object)(new Integer(7))).equals(3+4) 5. (int) 3.5 + 4.1 (c) 5 points. Consider these declarations of classes and interfaces: public class A implements I, J {... public class B extends A implements I {... public interface I {... public interface J {... Consider the statement: B var= new B(... ); Write down a list of all things to which variable var can be cast. (d) 6 points. Put a check mark before each of the following sentences that is correct and an X before each that is incorrect. 1. In a while loop while(b) {int x;..., variable x is allocated new space each time the loop body is executed. 2. In a class class C {public static int y= 5;..., every time an expression new C(...) is evaluated, y is set to 5. 3. To make testing easier, Java allows methods in a JUnit testing class to access private fields of objects it is testing. 6 of 8

4. If a class implements an interface, its subclasses may also implement that interface. 5. During execution of a Java program, the call stack contains at most one frame for each method. 6. If you don t start a constructor body with a call on another constructor, your program will not compile. (e) 8 points. To the right or below class C2, write the output printed by a call on method main of class C2 below. Please be extremely careful. public class C2 { private int p= 1; private static int q= 2; private int m1(int p) { p= q+1; q= q+3; return q; private int m2(int q) { p= q+1; q= q+3; return q; public static void main() { C2 c= new C2(); int x= c.m1(5); System.out.println(x + ", " + c.p + ", " + q); q= 2; c.p= 1; x= c.m2(5); System.out.println(x + ", " + c.p + ", " + q); (f) 5 points. Below, write an enum that has the constants AM and PM. Name the enum anything you want. 7 of 8

5. Exception handling (10 Points) Execute the three calls C.me(-1); C.me(0); and C.me(1); on procedure m shown below. You know that calls on System.out.print print on the Console. As you execute the calls on me, place the output of the calls on System.out.print in the places provided on the right below; don t be concerned about starting each print output on a new line. import java.io.*; public class C { public static void me(int p) { CONSOLE FOR C.m(-1); System.out.print("8. "); int y= p / (p - 1); try { System.out.print("7. "); if (p!= -1) throw new RuntimeException(); System.out.print("6. "); y= p / 0; CONSOLE FOR C.m(0); System.out.print("5. "); catch (ArithmeticException e) { System.out.print("4. "); if (p == p) throw new RuntimeException(); System.out.print("3. "); catch (RuntimeException e) { System.out.print("2: "); CONSOLE FOR C.m(1); System.out.print("1: "); 8 of 8