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

Similar documents
Prelim 1. CS 2110, 13 March 2018, 7: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, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. 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, 14 March 2017, 5: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 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

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

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

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 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. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

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

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

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

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

Prelim 2 Solutions. 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 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

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

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

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

XC Total Max Score Grader

Place your name tag here

Final Exam. COMP Summer I June 26, points

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

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

University of Palestine. Mid Exam Total Grade: 100

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

University of Massachusetts Amherst, Electrical and Computer Engineering

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Introduction to Programming Using Java (98-388)

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

Do not start the test until instructed to do so!

CSC Java Programming, Fall Java Data Types and Control Constructs

CS100J Prelim 1 22 February 2007

CIS 120 Programming Languages and Techniques. Midterm II. November 12, 2010

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

CS 100J Prelim 2 Have a good break!!! 15 March 2007

Array. Prepared By - Rifat Shahriyar

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

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

Largest Online Community of VU Students

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Declarations and Access Control SCJP tips

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Midterm Exam CS 251, Intermediate Programming March 12, 2014

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

Instruction to students:

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

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

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

CS 1110 Regular Prelim 1, March 14th, 2017

CS 1110 Prelim 2 April 22, 2014

1st Semester Examinations CITS1001 3

CSCI 355 LAB #2 Spring 2004

CIS 120 Programming Languages and Techniques. Midterm II. November 12, Answer key

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CIS 110 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CS 163/164 Exam 2 Review

Final Exam COMP Fall 2004 Dec 16, 2004

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

Chapter 1: Introduction to Computers, Programs, and Java

Introduction to Computing II (ITI 1121) Final Examination

Java Simple Data Types

Key Java Simple Data Types

COMP 401 Spring 2013 Midterm 1

CSCE 206: Structured Programming in C++

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

Ministry of Higher Education Colleges of Applied Sciences Final Exam Academic Year 2009/2010. Student s Name

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

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

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

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

CS 101 Fall 2005 Midterm 2 Name: ID:

Fall 2002 Page 1 of 9 READ THIS NOW!

Selected Questions from by Nageshwara Rao

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CSCI 355 Lab #2 Spring 2007

CS 111X - Fall Test 1

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Computer Science II Data Structures

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CMP 326 Midterm Fall 2015

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Transcription:

Prelim 1 CS 2110, 13 March 2018, 5:30 PM 1 2 3 4 5 6 Total Question Name Short answer Exception handling Recursion OO Loop invariants Max 1 30 11 14 30 14 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, legibly, at the top of every page! There are 6 questions on 10 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. I will not talk about the exam with anyone in this course who has not yet taken prelim 1. (signature) 1. Name (1 point) Write your name and NetID, legibly, at the top of every page of this exam. 1 of 10

2. Short Answer (30 points) (a) 6 points. 1. (int) 'g' == 'g' 2. (char)('z' - 2) Below are six expressions. To the right of each, write its value. 3. new Character('c') == new Character('c') 4. (new Double(2.0 * 3.0)).equals(new Double(1.0 * 6.0)) 5. "I love CS2110".substring(5).substring(2, 5) 6. k == 2 6 / (k - 2)!= 7 (Note: k is of type int) (b) 5 points. State whether each of the following statements is true or false. 1. String x= "Hello World"; x= '3'; will compile. 2. The following code declares a two-dimensional array of ints: int[2] myints; 3. Generic types refers to the built-in Java types: byte, short, int, long, float, double, boolean, char. 4. It is possible for a method to be both overridden and overloaded. 5. One purpose of wrapper class Integer is to treat an int value as an object. (c) 4 points. Consider the following classes: public class Boat1 { public abstract String getname(); public class Boat2 extends Boat1 { public Boat1 makefriend() { Boat1 friend= new Boat1(); return friend; public class Boat3 extends Boat1 { public String getname() { return "Boaty McBoatface"; public class Boat4 extends Boat1 { private String name; public Boat4(String n) { name= n; I. Which classes need to be abstract in order to compile? II. Which classes will fail to compile even if they are abstract? 2 of 10

(d) 8 points. This function returns the sum of all the even integers in an array: /** Return the sum of the even integers in array b (return 0 if b is null). */ public static int sume(int[] b) { if (b == null) return 0; int sum= 0; for (int i= 0; i < b.length; i= i + 1) { if (b[i] % 2 == 0){ sum= sum + b[i]; return sum; I. Write down, in English, at least 5 distinct test cases that you need to consider. II. Write down, in Java, the code for these test cases. Assume that function sume is declared in class U. Hint: to declare an array with the values 1, 2, and 3, you can use: new int[]{1, 2, 3. @Test public void testsume() { 3 of 10

(e) 4 points. Write the algorithm for executing the procedure call p(3, 2.1). (f) 3 points. In A3, the doubly-linked list class DLLList had these fields: private Node first; // first node of linked list (null if size is 0)\\ private Node last; // last node of linked list (null if size is 0)\\ private int size; // Number of values in the linked list. while inner class Node had these fields: private Node prev; // Previous node on list (null if this is first node)\\ private E val; // The value of this element\\ private Node next; // Next node on list. (null if this is last node) Change the class invariants above so that the doubly linked list is a circular doubly linked list. 4 of 10

3. Exception handling (11 Points) (a) 8 points. What-input-is-needed-to-get-output. Using the class and procedure below, answer the questions to the right, providing an appropriate procedure call as needed. Write none if no procedure call will give the desired output. public class R { public static void b(int k, String s) { int x= 0; int y= 0; try { System.out.println("A"); y= s.length(); System.out.println("B"); x= k / y; System.out.println("C"); catch(nullpointerexception npe) { System.out.println("D"); x= k / (k-1); System.out.println("E"); catch(runtimeexception re) { System.out.println("F"); try { int z= k / (y-2); System.out.println("G"); catch(runtimeexception r) { System.out.println("H"); System.out.println("I"); int z= k / (k-5); System.out.println("J"); 2 points per option (all-or-nothing) Give one call of procedure b that will print the following: A D E I J What call on b does not print "J"? What call on b prints this: A B C I J What call on b will result in a thrown exception? (b) 3 points. Executing a try-statement. Write the algorithm (in English) for executing the following try-catch block, which is not within another try-block. try { S1 catch (RuntimeException re) { S2. 5 of 10

4. Recursion (14 Points) (a) 6 points Execute the three calls griesseq(1); griesseq(5); and griesseq(8); and write the return value of the calls in the places provided below. public static int griesseq(int n) { if (n == 0 n == 1) return 1; if (n % 2 == 0) { return griesseq(n - 1); return n * griesseq(n - 1); Return value for griesseq(1): Return value for griesseq(5): Return value for griesseq(8): (b) 8 points Consider the following class representing Elephants. Write the body of recursive procedure numnames. You must use recursion; do not use a loop! public class Elephant { private String name; // not null private Elephant child; // null if this Elephant has no child /** Constructor: an instance with name n and child c. * Precondition: n is not null. */ public Elephant(String n, Elephant c) { name= n; child= c; /** Return the number of Elephants in this Elephant's family that have * name n. This Elephant's family consists of this Elephant, its child, * its child's child, its child's child's child, etc. */ public int numnames(string n) { 6 of 10

5. Object-Oriented Programming (30 points) Below is class Course, which you will be using throughout this problem: public class Course { public int number; public String name; /** Constructor: Course with number num and name n. */ public Course(int num, String n) { number= num; name= n; (a) 4 points Complete the body of the constructor in class Student: public class Student { private int studentid; private double gpa; private String name; /** Constructor: Student with name n, student ID id, and gpa gpa. */ public Student(String n, int id, double gpa) { /** Return true if this student has a gpa > 2.0. */ public boolean ingoodstanding() { return gpa > 2.0; (b) 10 points Complete the body of the constructor and function ingoodstanding in class EnrolledStudent on the next page. 7 of 10

/** A Student who is currently enrolled in an institute. */ public class EnrolledStudent extends Student { private int maxc; // Max number of courses to take at one time. private Course[] schedule; // Student is taking courses private int numc; // schedule[0..numc-1] /** Constructor: Newly enrolled Student taking 0 courses, with * name n, student ID id, gpa gpa. Can take at most max * courses at one time. */ public EnrolledStudent(String n, int id, double gpa, int max) { /** Return true if this student has a gpa > 2.0 and is taking * at least 3 courses. */ public @Override boolean ingoodstanding() { (c) 16 points Below is a declaration of interface Enrolled. Above, do whatever is necessary in class EnrolledStudent to have it implement Enrolled. public interface Enrolled { /** If student is taking the max number of courses allowed, return false. * Otherwise, add c to the student's schedule and return true. */ public boolean addcourse(course c); /** Return the enrolled student's schedule. */ public Course[] getschedule(); 8 of 10

6. Loop Invariants (14 points) Consider class Person below, which has two methods isroyal() and isguard() to determine whether a Person is a member of the British Royal Family or the British Guard, respectively. You will use the four loopy questions to develop a single loop (with initialization) that modifies an array c to surround the Royal Family with British Guards. class Person { private boolean guard; public Person (boolean g) { guard= g; public boolean isguard() { return guard; public boolean isroyal() { return!guard; (a) 6 points Consider this precondition and postcondition for an array c of Person objects. 0 n Precondition: c? Postcondition: c 0 h k n isguard() isroyal() isguard() Sizes of c[0..h] and c[k+1..n] differ by at most 1 Complete the invariant below to generalize the above array diagrams. You will have to introduce a new variable. Place your variables carefully; ambiguous answers will be considered incorrect. Note: Several different invariants can be drawn; draw any one of them. Invariant: c 0 n 9 of 10

(b) 1 point Below, write the initialization that truthifies the invariant: (c) 2 points Write a while-loop condition and write something in the loop body that makes progress toward termination. (Hint: When the loop condition is false, the invariant must imply the postcondition.) (d) 5 points Write a loop body that keeps the invariant true. (Hint: use procedure swap(c,i,j) to swap array elements c[i] and c[j].) initialization: while ( ) { 10 of 10