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

Similar documents
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, 7:30 PM Total Question Name Short answer

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

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

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

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

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name 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 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

CORRECTNESS ISSUES AND LOOP INVARIANTS

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

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

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

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

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

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

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CSC Java Programming, Fall Java Data Types and Control Constructs

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.

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

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

CS211 Prelim Oct 2001 NAME NETID

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

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

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

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

Final Exam CS 251, Intermediate Programming December 10, 2014

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

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

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

Topics Introduction to Microprocessors

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Prelim 2. 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, 5:30 PM Total. Sorting Invariants Max Score Grader

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

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Selected Questions from by Nageshwara Rao

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

JAVA BASICS II. Example: FIFO

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

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

XC Total Max Score Grader

CSE 331 Midterm Exam 2/13/12

CS 113 PRACTICE FINAL

CS100J Prelim 1 22 February 2007

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

Assertions, pre/postconditions

Object- Oriented Analysis, Design and Programming

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Prelim One Solution. CS211 Fall Name. NetID

CS 307 Midterm 2 Fall 2010

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

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

Midterm Exam 2 CS 455, Spring 2015

Prelim 2, CS2110. SOLUTION

CS 101 Exam 1 Spring 200 Id Name

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

Introduction to Programming Using Java (98-388)

Question: Total Points: Score:

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Class, Variable, Constructor, Object, Method Questions

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

Inheritance and Polymorphism

Midterm Exam 2 CS 455, Spring 2011

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CSC 1214: Object-Oriented Programming

CS212 Midterm. 1. Read the following code fragments and answer the questions.

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

Final Exam CS 251, Intermediate Programming December 13, 2017

Full file at Chapter 2 - Inheritance and Exception Handling

Instruction to students:

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Department of Computer Science Purdue University, West Lafayette

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Questions Answer Key Questions Answer Key Questions Answer Key

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Java Object Oriented Design. CSC207 Fall 2014

CSE wi Midterm Exam 2/8/18 Sample Solution

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Java Review: Objects

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

CS 180 Final Exam Review 12/(11, 12)/08

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

CS 61B Discussion 5: Inheritance II Fall 2014

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

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

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

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

CS 251 Intermediate Programming Inheritance

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

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

Transcription:

Prelim 1. Solution CS 2110, 14 March 2017, 5: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 1. Name (1 point) Write your name and NetID at the top of every page of this exam. 2. Short Answer (36 points.) (a) 5 points. Below are five expressions. To the right of each, write its value. 1. (int) == true. Rememer that char is a numer type. 2. (char)( a +3) d 3. new Boolean(false) == new Boolean(false) false. Each new-expression creates a new oject, and the pointers to these ojects are different. 4. ((Oject)(new Integer(3))).equals(3) true 5. k!= 0 && 5/k == 8 [note: k is of type int] false. Short-circuit evaluation is used. () 4 points. Consider a class C with a method m. What are the two consequences of making C and m astract? Making C astract means that ojects of class C cannot e created. Making m astract means that any non-astract suclass must override m. (c) 5 points. Function Integer.parseInt(String s) returns the int value of the integer that is in String s. But if s does not contain an integer, the function throws a NumerF ormatexception. Write a statement that stores in variale dn the value of the function call: Integer.parseInt(somestring) ut stores 1 in dn if a NumerF ormatexception is thrown. try { dn= Integer.parseInt(somestring); catch (NumerFormatException e) { dn= 1; 1 of 5

(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. A class can extend only one non-astract class ut any numer of astract classes. false 2. All fields in an astract class must e pulic. false 3. An astract class cannot have a constructor ecause it cannot e instantiated. false 4. If a class implements an interface, its suclasses must not implement that interface. false 5. A local variale declared at the eginning of a method maintains its value from one call of the method to the next. false 6. Every constructor must start with a call on a super-class constructor. false (e) 12 points. To the right is class M1 and its suclass M2. Below is method main of class M1 it elongs in class M1. 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) { M1 a= new M1(); System.out.println(a.x); //ans: 3 System.out.println(a.y); //ans: 100 System.out.println(a.m());//ans: 6 M2 = new M2(); System.out.println(.x); //ans: 4 System.out.println(.y); //ans: 100 System.out.println(.m());//ans: 105 pulic class M1 { pulic int x= 2; pulic int y= 100; pulic M1(int x) { this.x= x; pulic M1() { this(3); pulic int m() { return this instanceof M2? 5 : 6; pulic class M2 extends M1 { pulic M2() { super(4); pulic @Override int m() { return 100 + super.m(); (f) 4 points. What is the purpose of a constructor? What constructor does Java insert into a class C if no constructor is defined in it? The purpose of a constructor is to initialize fields so that the class invariant is true. If no constructor is defined in class C, Java inserts this one: pulic C(){. 2 of 5

3. Oject-Oriented Programming (33 points) (a) 5 points To the right are classes K1 and K2. Method m() is not overridden in class K2. Modify class K1 so that a variale will contain the numer of times during execution that method m() is called as a method of an oject of class K2 (instead of as an oject of class K1 only). Your modifications should consist of inserting a declaration in class K1 and adding code at the eginning of method m(). pulic class K1 { //no. times m called in a K2 oject static int c; pulic void m() { if (this instanceof K2) c= c+1;... pulic class K2 extends K1 {... () 10 points Below are two class declarations. Complete the odies of the constructor and function tostring in class Surgeon. Be careful; pay attention to access modifiers. pulic class Doctor { private String name; /** A doctor named n. * Precond.: no space in n */ pulic Doctor(String n) { name= n; /** Return this doctor s name */ pulic String tostring() { return name; pulic class Surgeon extends Doctor { private int ops; //no. of ops performed /** Constructor: instance with name n * and op operations * Precond.: no space in n */ pulic Surgeon(String n, int op) { super(n); ops= op; /** Return this surgeon s name, a * space, and numer of operations. */ pulic String tostring() { return super.tostring() + " " + ops; (c) 5 points Complete the ody of method equals, which elongs in class Doctor.: /** Return true iff o is a Doctor and * o has the same name as this Doctor. */ pulic @Override oolean equals(oject o) { if (!(o instanceof Doctor)) return false; return name.equals(((doctor)o).name); (d) 5 points Write down the steps in evaluating a new-expression new C(args). 1. Create (draw) an instance of class C, with default values for the fields; 2. Execute the constructor call C(args); 3. Return as value of the new-expression the name of (pointer to) the created oject. 3 of 5

(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.) Here s a hint: First draw an oject. interface I1 {... interface I2 {... interface I3 extends I1 {... class C1 implements I1 {... class C2 implements I2 {... class C3 implements I3 {... class C4 extends C3 implements I2 {... (a) I2 a= new I2(); // Compile-time error () I2 = new C2(); // no error (c) C3 c= new C4(); // no error (d) C2 d= new C4(); // Compile-time error (e) C4 e= new C3(); // Compile-time error (f) C4 f= (C4)(new C3()); // Runtime error (g) I1 g1= new C1(); // no error C4 g2= new C4(); // no error g1= g2; // no error (h) I1 g1= new C4(); // no error I2 g2= new C2(); // no error g2= g1; // Compile-time error 4. Recursion (15 Points) (a) Write the ody of recursive function nf, whose specification and header appear elow. Do not use loops. Use only recursion. Here is a restriction, which should help you hone in on a simple solution: The only String functions you should use are charat, length, and sustring. /** Return the numer of times the first * char of s appears at the eginning of s. * Precondition: s is not null and contains at least 1 char. * Example: nf("c$") = 3. * Example: nf("c$") = 1. */ pulic static int nf(string s) { if (s.length() == 1) return 1; if (s.charat(0)!= s.charat(1)) return 1; return 1 + nf(s.sustring(1)); () Below is function comfy. 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() < 3 no 2. s.length() 3 yes 3. s.length() == 3 no 4. Integer.parseInt(s) < 1000 yes 5. s.length() == 0 no 4 of 5

/** Return s formatted y adding a comma efore every third digit. * E.g. 1000 is formatted as 1,000, 56 is 56, and 1234567 is 1,234,567. * Precondition s is a non-signed integer and the leftmost digit is not 0. **/ pulic String comfy(string s) { if ( ase-case if-condition ) return s; return comfy(s.sustring(0,s.length()-3)) +, + s.sustring(s.length()-3); 5. Loop Invariants (15 points) (a) 2 points State the formula for the numer of values in array segment [h..k 1]. k h // it s Follower First () 13 points Consider the following precondition, invariant, and postcondition. The postcondition has two alternatives either section [h..j 1] 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. int h= 0; int k= n; while (h < j && j < k) { if ([h] <= [j]) h= h+1; else { Swap [h] and [k]; k= k - 1; 5 of 5