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

Similar documents
Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name 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. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

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

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. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

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

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

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

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

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

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

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

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

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

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

Prelim 2 SOLUTION. 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 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.

Review Session. CS2110 Prelim #1 3/8/15. Primitive types vs classes. Default values. Wrapper Classes (Boxing) Strings are immutable.

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, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

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

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

CORRECTNESS ISSUES AND LOOP INVARIANTS

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

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

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

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

XC Total Max Score Grader

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

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

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

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

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

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Race Conditions & Synchronization

CS211 Spring 2004 Lecture 06 Loops and their invariants. Software engineering reason for using loop invariants

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

Final Exam. COMP Summer I June 26, points

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

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

Exam Percentage: / 55 = %

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

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

Final Exam COMP Fall 2004 Dec 16, 2004

CSE 131 Introduction to Computer Science Fall Exam II

CS100J Prelim 1 22 February 2007

CS 1110 Prelim 2 November 6th, 2012

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

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

CS 113 PRACTICE FINAL

CPS109 Computer Science 1

Array Dependence Analysis as Integer Constraints. Array Dependence Analysis Example. Array Dependence Analysis as Integer Constraints, cont

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Midterm Exam 2 CS 455, Spring 2015

Theory of Integers. CS389L: Automated Logical Reasoning. Lecture 13: The Omega Test. Overview of Techniques. Geometric Description

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

CS211 Prelim Oct 2001 NAME NETID

Logistics. Final Exam on Friday at 3pm in CHEM 102

Topics Introduction to Microprocessors

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

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

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

CORRECTNESS ISSUES AND LOOP INVARIANTS

Csci 102: Sample Exam

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

CS100J April 17, 2001

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

Questions Answer Key Questions Answer Key Questions Answer Key

Prelim 2, CS2110. SOLUTION

CS 101 Exam 1 Spring 200 Id Name

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS 1110 Final Exam May 9th, 2013 SOLUTIONS

CS 251 Intermediate Programming Inheritance

Midterm Exam 2 CS 455, Spring 2011

Chapter 10 Classes Continued. Fundamentals of Java

Java Object Oriented Design. CSC207 Fall 2014

Question 0. (out of 01) Question 1. (out of 10) Question 2. (out of 12) Question 3. (out of 12) Question 4. (out of 12) Question 5.

Question: Total Points: Score:

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

CS 1110 Prelim 1 October 4th, 2012

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

CS 1331 Fall 2016 Exam 2

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

Summer Session 2004 Prelim I July 12, CUID: NetID:

Object- Oriented Analysis, Design and Programming

CIT Special final examination

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

JAVA BASICS II. Example: FIFO

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

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Announcements SORTING. Prelim 1. Announcements. A3 Comments 9/26/17. This semester s event is on Saturday, November 4 Apply to be a teacher!

Final Exam Practice. Partial credit will be awarded.

Inheritance (Part 5) Odds and ends

Constraint-Based Query Evaluation in Deductive Databases

Transcription:

Prelim 1 CS 2110, 14 March 2017, 7:30 PM 1 2 3 4 5 Total Question Name Short answer OO Recursion Loop invariants Max 1 36 33 15 15 100 Score Grader The exam is closed ook and closed notes. Do not egin until instructed. You have 90 minutes. Good luck! Write your name and Cornell NetID, legily, at the top of every page! There are 5 questions on 7 numered pages, front and ack. 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 availale. 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. Amiguous answers will e considered incorrect. You should e ale to fit your answers easily into the space provided. In some places, we have areviated or condensed code to reduce the numer of pages that must e printed for the exam. In others, code has een ofuscated to make the prolem 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. I will not talk aout the exam with anyone in this course who has not yet taken prelim 1. (signature) 1. Name (1 point) Write your name and NetID at the top of every page of this exam. 1 of 7

2. Short Answer (36 points.) (a) 5 points. Below are five expressions. To the right of each, write its value. 1. c == + 1 2. 5-0 3. (doule) (doule) (int) 5.2 == 5 4. ((Doule)(Oject)(3.2)).equals(3.2) 5. k == 0 5/k!= 8 [note: k is of type int] () 4 points. Consider classes A and B declared to the right. Will these classes compile? If not, give as many reasons as possile for why they won t compile. pulic astract class A { pulic astract void m(); pulic class B extends A { pulic void p() { A = new A(); (c) 5 points. Write Java code to: Assign array element [h] to variale k, ut if it throws an ArrayIndexOutOfBoundsException, store 0 in k. Do not use an if-statement, conditional expression, switch statement, or loop. Assume that all variales have already een defined. (d) 6 points. Put a check mark efore each of the following sentences that is correct and an X efore each that is incorrect. 1. An astract class cannot have a constructor ecause it cannot e instantiated. 2. A class can extend only one interface. 3. Methods in an interface are necessarily astract, ut you can make them pulic or private. 4. If a suclass implements an interface, its superclass cannot implement that interface. 5. A local variale declared with type int is automatically initialized to contain 0. 6. Every constructor must start with a call on a super-class constructor. 2 of 7

(e) 12 points. To the right is class CC and its suclass CB. Below is method main of class CC it elongs in class CC. Execute a call on method main. Write the value that is printed y each println statement to the right of that println statement. pulic static void main(string[] p) { CC a= new CC(); System.out.println(a.x); System.out.println(a.y); System.out.println(a.m(a)); CB = new CB(); System.out.println(.x); System.out.println(.y); System.out.println(.m()); pulic class CC { pulic int x= 2; pulic int y= 10; pulic CC(int p) { x= p; pulic CC() { this(3); pulic int m(cc c) { return c instanceof CB? 5 : 6; pulic class CB extends CC { pulic CB() { super(4); pulic @Override int m(cc c) { return y + super.m(c); (f) 4 points. Suppose you have an astract class A and its only components are pulic astract methods. You would like a class B to extend A, ut B already extends a class and it can extend only one. How can you rewrite astract class A to solve this prolem? 3 of 7

3. Oject-Oriented Programming (33 points) (a) 5 points To the right are classes H1 and H2. Method p() is not overridden in class H2. Modify class H2 so that a variale will contain the numer of times during execution that method p() is called as a method of any oject of class H2 (instead of as an oject of class H1 only). pulic class H1 { pulic void p() {... pulic class H2 extends H1 { Your modifications should consist of inserting a declaration in class H2 and overriding method p.... () 10 points Below are two class declarations. Complete the odies of the constructor and function tostring in class Outhouse. Be careful; pay attention to access modifiers. pulic class Outhouse extends Building { private int num; // numer of seats /** Constructor: instance at address * ad with s seats */ pulic Outhouse(String ad, int s) { pulic class Building { private String address; /** A uilding at address ad. */ pulic Building(String ad) { address= ad; /** Return the uilding s address, a * space, and numer of seats. */ pulic String tostring() { /** Return this uilding s address */ pulic String tostring() { return address; 4 of 7

(c) 5 points Complete the ody of method equals, which elongs in class Outhouse.: /** Return true iff o is an Outhouse and * o has the same numer of seats as this Outhouse. */ pulic @Override oolean equals(oject o) { (d) 5 points Write down the steps in executing a method call m(args). (e) 8 points Consider the interface and class declarations given elow. Next to each piece of Java code in the righthand column, write whether it produces no error, a run-time error, or a compile-time error. (Assume that each piece is independent of the others.) Hint: It will help to draw ojects of the classes. interface J1 { interface J2 { interface J3 extends J1 { class D1 implements J2 { class D2 implements J2 { class D3 implements J3 { class D4 extends D2 implements J1 { (a) J2 a= new J2(); () J2 = new D2(); (c) D3 c= new D4(); (d) D2 d= new D4(); (e) D4 e= new D3(); (f) D4 f= (D4)(new D2()); (g) J2 g1= new D2(); D4 g2= new D4(); g2= g1; (h) J1 h1= new D4(); J2 h2= new D2(); h2= h1; 5 of 7

4. Recursion (15 Points) (a) 10 points. Write the ody of recursive function nf, whose specification and header appear elow. Do not use loops. Use only recursion. /* Return the numer of times [k] appears in a row at the eginning of [k..] * Precondition: 0 <= k <.length. * Examples: For containing [2, 2, 2, 3, 2, 6], * nf(, 0) = 3 and nf(, 2) = 1. */ pulic static int nf(int[], int k) { () 5 points. Below is function putblank. It is complete except for the ase-case if-condition. Circle all possile expressions from the list elow that could e used for the ase-case if-condition. 1. s.length() < 2 2. s.length() 2 3. s.length() == 2 4. Integer.parseInt(s) < 100 5. s.length() == 0 /** Return s formatted y inserting a lank efore every second digit. * E.g. 1000 is formatted as 10 00, 56 is 56, and 1234567 is 1 23 45 67. * Precondition: s is a non-signed integer and the leftmost digit is not 0. **/ pulic String putblank(string s) { if ( ase-case if-condition ) return s; return putblank(s.sustring(0,s.length()-2)) + + s.sustring(s.length()-2); 6 of 7

5. Loop Invariants (15 points) (a) 2 points State the formula for the numer of values in array segment [h..k 1]. () 13 points Consider the following precondition, invariant, and postcondition. The postcondition has two alternatives either section [0..h] or section [j + 1..k] is empty (the other one might e, ut it is not necessary). Precondition: Invariant: 0 j n? x 0 h j k n? x x? x? Postcondition: OR 0 j k n x x? x 0 h j n? x x x Write a loop with initialization that uses the invariant given aove to implement the comment given elow. Thus, the loop should continue as long as oth? sections are non-empty. Assume that, j, and n are already initialized. Identifier x can t e used in the program; it just stands for the value in [j]. Don t declare variales, ut do assign appropriate values to h and k wherever necessary. To swap [i] and [j], just say, Swap [i] and [j]. Your grade depends only on how well you use the four loopy questions to write the code. // Given the Precondition as shown aove, swap values of array // segment [0..n] so that the Postcondition holds. 7 of 7