CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Similar documents
CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

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

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

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

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

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2012

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

CS 113 PRACTICE FINAL

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

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Exam Duration: 2hrs and 30min Software Design

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Fundamentals of Object Oriented Programming

F I N A L E X A M I N A T I O N

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

CS 251, Intermediate Programming Midterm Exam October 9, 2013

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

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

CS 113 MIDTERM EXAM 2 SPRING 2013

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

CS159 Midterm #1 Review

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

CS 1331 Fall 2016 Exam 3 Part 1 ANSWER KEY

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

Full file at Chapter 2 - Inheritance and Exception Handling

INSTRUCTIONS TO CANDIDATES

CSCI 135 Final Fall Problem Points Score Total 100

CSCI 261 Computer Science II

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

JAVA MOCK TEST JAVA MOCK TEST II

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)

Exception-Handling Overview

Index COPYRIGHTED MATERIAL

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

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

COMP-202 Unit 9: Exceptions

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Collections Algorithms

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

COP 3330 Final Exam Review

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

EXCEPTIONS. Fundamentals of Computer Science I

UMBC CMSC 331 Final Exam

CSci 1103 Final. Name: Student ID:

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

CSCI 201L Written Exam #1 Fall % of course grade

Binary search. int index = Collections.binarySearch(list, element);

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

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CSCI 201L Written Exam #1 Fall % of course grade

Vendor: Oracle. Exam Code: 1Z Exam Name: Java SE 8 Programmer. Version: Demo

CS159. Nathan Sprague

CSE wi Final Exam 3/12/18 Sample Solution

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

CSE 131 Introduction to Computer Science Fall Exam II

Programming for Mobile Computing

CSE331 Spring 2015, Final Examination June 8, 2015

Final Exam Practice Questions

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

About this sample exam:

CIS 110: Introduction to computer programming

More on Exception Handling

Java Review: Objects

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

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

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

More on Exception Handling

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS 11 java track: lecture 3

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Object-Oriented Programming

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Final Exam CS 251, Intermediate Programming December 10, 2014

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Declarations and Access Control SCJP tips

COMP-202 Unit 9: Exceptions

CS 1331 Fall 2016 Exam 2

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

CSE wi Final Exam 3/12/18. Name UW ID#

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CMSC 331 Second Midterm Exam

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

Inheritance (continued) Inheritance

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

ECE Fall 20l2, Second Exam

CO Java SE 8: Fundamentals

Transcription:

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013 Name: This exam consists of 5 problems on the following 7 pages. You may use your single-side hand-written 8 ½ x 11 note sheet during the exam. You may use a simple handheld calculator. No computers, mobile devices, cell phones, or other communication devices of any kind are permitted. If you have a question, raise your hand and I will stop by. Since partial credit is possible, please write legibly and show your work. Problem Points Score 1 8 2 15 3 12 4 12 5 12 Total 59 1

1. Loops, Input, Exceptions (8 points). Consider the following program: public class Prob1 public static void main(string[] args) int i = 0; double d = 0.0; for (String s : args) try d += Double.parseDouble(s); i++; catch (NumberFormatException e) System.out.printf("%d / %d : %.2f\n", i, args.length, d); Below are four example executions of the program. Give the output produced by the program. If the given input would cause a runtime error, write "runtime error". If it would cause a compile error, write "compiler error". Command line Output % java Prob1 1.0 2.0 0.5 3 / 3 : 3.50 % java Prob1-1 -2-0.5 3 / 3 : -3.50 % java Prob1 1.0 two 0.5 2 / 3 : 1.50 % java Prob1 0 / 0 : 0.00 2

2. Inheritance, Collections (15 points). Consider the class hierarchy given on the last page of the exam. a) Which data types could you successfully create with the new operator? Circle all that apply. I. Person II. Student III. Employee IV. Faculty V. Staff b) Assume someone has declared an array of type Employee. Concrete objects of what types could be stored in this array? Circle all that apply. I. Person II. Student III. Employee IV. Faculty V. Staff c) The tostring() method in the class Faculty is an example of what? Circle the single best answer. I. Overloaded method II. Overridden method III. Polymorphism IV. Auto-boxing V. Ducking an exception d) The constructors in the class Staff are an example of what? Circle the single best answer. I. Polymorphic methods II. Abstract methods III. Overloaded methods IV. Overridden methods V. Auto-unboxed methods e) Consider the following code snippet: ArrayList<Faculty> list = new ArrayList<Faculty>(); //... (code that adds a bunch of faculty members) Collections.sort(list); // sort by number of advisees You get the following compile error: Bound mismatch: The generic method sort(list<t>) of type Collections is not applicable for the arguments (ArrayList<Faculty>). The inferred type Faculty is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> What do you need to do to the Faculty class to make the sort work? Circle all that apply. I. Add implements Comparable<Faculty> to the class declaration. II. Add implements Comparable<ArrayList<Long>> to the class declaration. III. Add implements Comparable to the declaration of the Faculty class. IV. Add implements Comparable<Integer> to the class declaration. V. Add a method public int compareto(person other) VI. Add a method public void sort(arraylist<faculty>) VII. Add a method public int compareto(faculty other) 3

f) Various instance variables are declared in the class hierarchy. Put a checkmark in the boxes corresponding to the instance variables (left column) that are directly accessible in instance methods of each class (top row). That is, instance variables that could be used without going through another method. Person Student Employee Faculty Staff name id gpa totalcredits salary sickdays advisees tenured safety g) Each Faculty object keeps track of the list of student ID s that faculty member is responsible for. Advisees can be added and removed one-at-a-time by ID. Add code to the following three methods to correctly implement this functionality: public Faculty(String n, int sal, boolean ten) super(n, sal); this.tenured = ten; advisees = new ArrayList<Long>(); public void addadvisee(long id) advisees.add(id); public boolean removeadvisee(long id) return advisees.remove(id); 4

3. Loops, Conditionals, String (12 points). A school converts letter grades to a value as follows: A A- B+ B B- C+ C C- D F 4.0 3.7 3.3 3.0 2.7 2.3 2.0 1.7 1.0 0.0 A student s GPA (grade point average) is computed by using these floating-point values weighted by the number of credits of the class in which each grade was earned. So for example, if a student has completed two classes, getting an A in a 4-credit class and a B- in a 3-credit class, his GPA is: (4 * 4.0 + 3 * 2.7) / 7 = 3.442857 a) Complete the following helper method that converts a String containing a letter grade to its floatingpoint value. Your method should work regardless of whether the grade is in upper- or lower-case. Recall that the String data type has methods touppercase() and tolowercase(). If the passed in grade is not a grade in the above table, throw a RuntimeException indicating Invalid grade. private double convertgrade(string grade) String [] grades = "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"; double [] val = 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.0, 0.0; for (int i = 0; i < grades.length; i++) if (grade.touppercase().equals(grades[i])) return val[i]; throw new RuntimeException("Invalid grade"); b) Using your convertgrade() method, implement the addcourse() method in the Student class from the diagram on the last page of the exam. Your method should result in updating the object s instance variables so as to reflect the student s new GPA and total credits. public void addcourse(string grade, int credits) double cum = gpa * totalcredits; cum += convertgrade(grade) * credits; totalcredits += credits; gpa = cum / totalcredits; 5

4. Exceptions, Collections (12 points). a) Java has both checked and unchecked exceptions. Briefly describe the difference. Provide an example of a typical exception of each type. Checked exceptions must be placed within a try-catch block (otherwise you will get a compile error). An example is FileNotFoundException which must be caught when creating a PrintWriter object. Unchecked exception are not required to be in a try-catch block. Normally this our a programming error such as the ArrayIndexOutOfBoundsException or NullPointerException. b) Describe a problem where a list abstract data type (ADT) would be most appropriate. Why is a list the best choice for this problem? Storing a list of names in alphabetical order. The list ADT tracks the order of elements in the collection which would be required if we wanted to keep them in a certain order. c) Describe a problem where a map abstract data type (ADT) would be most appropriate. Why is a map the best choice for this problem? Keeping track of the number of times a set of words have each been seen in a large text file. The map ADT allows us to quickly find a given word in the collection and modify its value. d) Describe a problem where a set data type (ADT) would be most appropriate. Why is a set the best choice for this problem? Creating a spell checking program. The set would track words that are in a known dictionary of correctly spelled words. The set ADT allows us to determine if a given key is present or not in a collection. 6

5. Methods (12 points). Match the method description on the left with the best method on the right. Assume candidate methods appear inside a class named Book. The Book class knows things like the author, title, and all the text of a book (the exact details are not important). Not every letter will be used. Each letter will be used at most once. E A G O J K A method that can determine if a Book contains exactly the same text as another Book. You have an object c of type Book. This method is called implicitly by the following line: System.out.println(c); A method that can calculate how many times each word in a set of words appears in a Book. For example, the method might calculate that whale occurs 10 times and ocean appears 17 times. A copy constructor that creates a Book based on another Book. A method that can return the count of the number of words that overlap in two Book objects. A method that can return the set of possible misspelled words in a Book given a passed in dictionary of known good words. A. public String tostring() B. public static String tostring() C. public void tostring(string s) D. public boolean foo(string s) E. public boolean foo(book a) F. public boolean foo() G. public int [] foo(string [] s) H. public int foo(arraylist<string> s) I. public int foo(hashset<string> s) J. public int foo(book a) K. public HashSet<String> foo(hashset<string> s) L. public String [] foo() M. public void foo(string s) N. public Book() O. public Book(Book a) P. public Book(other) Q. private Book(Book a) 7

This page intentionally left blank. 8

9