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

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

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

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

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

CORRECTNESS ISSUES AND LOOP INVARIANTS

Prelim 1. 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. 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.

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

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

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

Assertions, pre/postconditions

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

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

CSE 143 SAMPLE MIDTERM

Midterm Exam 2 CS 455, Spring 2011

XC Total Max Score Grader

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

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

CS211 Prelim Oct 2001 NAME NETID

Question Points Score

Midterm Exam 2 CS 455, Spring 2015

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

CS 113 PRACTICE FINAL

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

Csci 102: Sample Exam

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

Chapter 10 Classes Continued. Fundamentals of Java

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

Topics Introduction to Microprocessors

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

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

Inheritance and Polymorphism

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

Race Conditions & Synchronization

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

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

Rules and syntax for inheritance. The boring stuff

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

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

Full file at

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

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

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

CSE 131 Introduction to Computer Science Fall Exam II

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

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS/ENGRD 2110 SPRING 2018

CPS109 Computer Science 1

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

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

CS100J Prelim 1 22 February 2007

CSC 1351 The Twelve Hour Exam From Hell

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

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

Inheritance and Polymorphism

Class, Variable, Constructor, Object, Method Questions

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Questions Answer Key Questions Answer Key Questions Answer Key

CS 251 Intermediate Programming Inheritance

CSE 331 Midterm Exam 2/13/12

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

Introduction to Programming Using Java (98-388)

CSC Java Programming, Fall Java Data Types and Control Constructs

Selected Questions from by Nageshwara Rao

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

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

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

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

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

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

CS111: PROGRAMMING LANGUAGE II

Introduction to Programming (CS112): Sample

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

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

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Prelim One Solution. CS211 Fall Name. NetID

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

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

Inheritance and Polymorphism. CS180 Fall 2007

CS2102, D15 Exam 1. Name:

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

CSE 5317 Midterm Examination 4 March Solutions

ECS 140A Midterm 2 November 19, Name (Last, First):, Sign your name:

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

Programming II (CS300)

Transcription:

Prelim 1. Solution 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 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. c == + 1 true 2. 5-0 5 3. (doule) (doule) (int) 5.2 == 5 true 4. ((Doule)(Oject)(3.2)).equals(3.2) true 5. k == 0 5/k!= 8 [note: k is of type int] true () 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. (1) Since A is astract, the expression newa() is illegal. (2) Since m() is astract, class B is illegal ecause it does not override m. pulic astract class A { pulic astract void m(); pulic class B extends A { pulic void p() { A = new A(); 1 of 5

(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. try {k= [h]; catch (ArrayIndexOutOfBoundsException e) { k= 0; (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. false 2. A class can extend only one interface. false 3. Methods in an interface are necessarily astract, ut you can make them pulic or private. false. They have to e pulic 4. If a suclass implements an interface, its superclass cannot implement that interface. false 5. A local variale declared with type int is automatically initialized to contain 0. false. local variales are uninitialized. 6. Every constructor must start with a call on a super-class constructor. false. It could start with this(...); (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. Printed are the ints 3 10 6 4 10 15 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); 2 of 5

(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? Make A an interface. 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). Your modifications should consist of inserting a declaration in class H2 and overriding method p. pulic class H1 { pulic void p() {... pulic class H2 extends H1 { // no. times p() called as a component // of an oject of class H2 pulic static int q; pulic void p() { super.p(); q= q+1;... () 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 { pulic class Building { private String address; private int num; // numer of seats /** Constructor: instance at address * ad with s seats */ pulic Outhouse(String ad, int s) { super(ad); num= s; /** Return the uilding s address, a * space, and numer of seats. */ pulic String tostring() { return super.tostring() + " " + num; /** A uilding at address ad. */ pulic Building(String ad) { address= ad; /** Return this uilding s address */ pulic String tostring() { return address; 3 of 5

(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) { if (!(o instanceof Outhouse)) return false; return num == (((Outhouse)o).num); (d) 5 points Write down the steps in executing a method call m(args). 1. Push a frame for the call onto the call stack. 2. Assign values of arguments to the parameters. 3. Execute the method ody. 4. Pop frame for call from call stack; If this is a function push return value onto call stack. (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(); syntax compiletime () J2 = new D2(); no error (c) D3 c= new D4(); syntax compiletime (d) D2 d= new D4(); no error (e) D4 e= new D3(); syntax compiletime (f) D4 f= (D4)(new D2()); semantics runtime (g) J2 g1= new D2(); no error D4 g2= new D4(); no error g2= g1; syntax compiletime (h) J1 h1= new D4(); no error J2 h2= new D2(); no error h2= h1; syntax compiletime 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. /* 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) { if (k ==.length-1) return 1; if ([k]!= [k+1]) return 1; return 1 + nf(, k+1); () 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. 4 of 5

1. s.length() < 2 no 2. s.length() 2 yes 3. s.length() == 2 no 4. Integer.parseInt(s) < 100 yes 5. s.length() == 0 no /** 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); 5. Loop Invariants (15 points) (a) 2 points State the formula for the numer of values in array segment [h..k 1]. k h () 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. h= j-1; k= n; while (0 <= h && j < k) { if ([h] <= [j]) h= h-1; else {Swap [h] and [k]; k= k-1; 5 of 5