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

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

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

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

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

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

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

CSCI 135 Exam #2 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 2012

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

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

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Introduction to Programming Using Java (98-388)

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

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

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

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

CS163/164 Final Exam Study Session

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS 113 PRACTICE FINAL

UMBC CMSC 331 Final Exam

Conversions and Overloading : Overloading

Excep&ons and file I/O

JAVA MOCK TEST JAVA MOCK TEST II

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

COMP 401 Spring 2014 Midterm 1

EXCEPTIONS. Fundamentals of Computer Science I

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

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

Final Exam CS 251, Intermediate Programming December 13, 2017

IT 313 Advanced Application Development

Java Foundations Certified Junior Associate

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

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

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

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

CS159 Midterm #1 Review

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

Array. Prepared By - Rifat Shahriyar

Full file at

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

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Super-Classes and sub-classes

CSCI 135 Final Fall Problem Points Score Total 100

ENCAPSULATION AND POLYMORPHISM

public class Q extends P { private int num; private int i; P(int n) { public Q() { this.num = n;

Final Exam Practice. Partial credit will be awarded.

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

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

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

CSC 222: Object-Oriented Programming. Fall Object-oriented design

Give one example where you might wish to use a three dimensional array

Midterm Exam 2 CS 455, Fall 2013

Questions Answer Key Questions Answer Key Questions Answer Key

Exam Duration: 2hrs and 30min Software Design

Midterm Exam 2 CS 455, Spring 2015

IT 313 Advanced Application Development

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

More on collec)ons and sor)ng

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Csci 102: Sample Exam

Name: CS 159 Practice Final Fall 2015

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Selected Questions from by Nageshwara Rao

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

CS 113 MIDTERM EXAM 2 SPRING 2013

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

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

COMP 401 Spring 2013 Midterm 1

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS 1331 Fall 2016 Exam 2

CS 1331 Exam 3 Practice

CO Java SE 8: Fundamentals

Java Review: Objects

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

Pace University. Fundamental Concepts of CS121 1

Computer Programming, I. Laboratory Manual. Final Exam Solution

CSE 1223: Exam II Autumn 2016

CS 314 Midterm 2 Fall 2012

CLASS DESIGN. Objectives MODULE 4

CS 251, Intermediate Programming Midterm Exam October 9, 2013

CSC 222: Object-Oriented Programming. Fall 2015

PROGRAMMING FUNDAMENTALS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Self-test Java Programming

CS 132 Midterm Exam Spring 2004

Input-Output and Exception Handling

Name: CS 159 Practice Final Fall 2015

CS 1331 Exam 3 Practice ANSWER KEY

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

CS1083 Week 2: Arrays, ArrayList

Examination Questions Midterm 1

Distributed Systems Recitation 1. Tamim Jabban

INFO1113. Calum Baird. October 5, Stuff Always check null Inline declare array Switch statement...

Transcription:

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 6 problems on the following 7 pages. You may use your single-sided handwritten 8 ½ x 11 note sheet during the exam. No calculators, computers, mobile devices, cell phones, or other electronic devices 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 8 3 10 4 8 5 16 6 6 Total 56 1

1. Loops, Input (8 points). Consider the following program: public class Prob1 public static void main(string[] args) final int N = Integer.parseInt(args[0]); int [] a = new int[n]; for (int i = 1; i < args.length; i++) int j = Integer.parseInt(args[i]); a[j] = 1; for (int i : a) System.out.print(i + " "); Below are five example executions of the program. Give the output produced by the program. If the given input would cause a runtime exception, write "error". Command line Output % java Prob1 error % java Prob1 4 0 0 0 0 % java Prob1 5 0 2 4 1 0 1 0 1 % java Prob1 5 2.3 error % java Prob1 6 2 2 3 2 3 2 0 0 1 1 0 0 2

2. File I/O, Collections (8 points). a) Consider the following method that is supposed to print out each word contained in the filename passed to it. Currently it compile and runs, but it just prints the filename and not the words in the actual file. 00 public static void printwords(string filename) 01 02 Scanner scanner = null; 03 04 scanner = new Scanner(filename); 05 06 while (!scanner.hasnext()); 07 System.out.println(scanner.next()); 08 09 scanner.close(); 10 Circle the four things that must be done to make this method work as intended. A. Line 00: Remove the static keyword B. Line 00: Change void to String C. Line 02: Change null to filename. D. Line 04: Change filename to new File(filename)) E. Surround line 04 with a try-catch block, catching an exception of type FileNotFoundException F. Surround line 04 with a try-catch block, catching an exception of type ArrayNotFoundException G. Line 06: Change hasnext() to next() H. Line 06: Remove the! symbol at the front of the condition I. Line 06: Remove the ; symbol at the end of the line J. Line 07: Change println to printf K. Line 09: Change scanner to filename b) Add the missing code to the following method so that all the values in the nums list are output to the specified filename. Each number should be on a separate line and rounded to two decimal places. public static void outputnums(string filename, ArrayList<Double> nums) PrintWriter writer = null; try writer = new PrintWriter(filename); catch (FileNotFoundException e) return; for (double d: nums) writer.printf("%.2f\n", d); writer.close(); 3

3. Multiple choice (10 points). Circle the single best answer: a) You want to declare and instantiate a variable that can be used to store both the name of a color, e.g. "BrickRed" and a Java Color object that represents that color. The variable should be able to grow automatically as colors are added. Which of the following compiles and meets your requirements? I. ArrayList<Color> colors = new ArrayList<Color>(); II. ArrayList<String, Color> colors = new ArrayList<String, Color>(); III. String [] colors = new String[Color.length()]; IV. HashMap<String, Color> colors = new HashMap<String, Color>(); V. HashMap<Color, String> colors = new HashMap(); b) You are declaring a concrete class Employee that is a child class of the abstract base class Person. You would like to make it so a collection of Employee objects can easily be sorted (say by salary) using the Java static methods Collections.sort or Arrays.sort. Which of the following class declarations meets your requirements? I. public class Employee extends Person implements Comparable<Employee> II. public class Employee extends abstract Person implements Comparable<Integer> III. public class Employee implements Person extends Comparable<Employee> IV. public class Employee extends Person, Comparable<Employee> V. public class Employee extends Person<Integer> c) All the following statements about Java exceptions are true EXCEPT: I. If a method declares that it throws a particular type of checked exception, exceptions of that type in the method do not need to be enclosed in a try-catch block. II. Multiple catch blocks are possible, each catching a different type of exception. III. Including a try-catch block for unchecked exceptions such as NullPointerException is optional. IV. Program execution always terminates after the last line of a catch block. V. Opening a file for reading can cause a checked exception of type FileNotFoundException. d) A class has the following method: public double foo(int a, double b) return 0.0; All the following methods would be valid overloaded versions of the method foo EXCEPT: I. public int foo(int a, int b) return 0; II. public int foo(int a, double b) return 0; III. public double foo(string a, int b) return 0.0; IV. public double foo(double a, int b) return 0.0; V. public double foo(int a, int b, int c) return 0.0; e) Which of the following is a boolean expression that is a valid and reliable way to test if the String variable str (which you can assume is non-null) is equal to "Duck": I. (str == "Duck") II. (str!= "Duck") III. ("Duck" = str) IV. (str.equals(duck)) V. (str.equals("duck")) 4

4. Short answer (8 points). a) Recall in the predictive keyboard assignment, we had an instance variable declared as follows: private HashMap<String, Integer> wordtocount = new HashMap<String, Integer>(); Consider if we had instead used two parallel ArrayList variables to track the words and their counts: private ArrayList<String> words = new ArrayList<String>(); private ArrayList<Integer> counts = new ArrayList<Integer>(); Briefly describe the performance advantage of the HashMap version compared to the ArrayList version. The HashMap version can find the count for a given word in constant time. The ArrayList version would need to search through the list in order to find the word, thus taking time linear in the number of words. b) Briefly describe the fundamental difference between a concrete class and an abstract class in Java. Be sure to describe what you can do with one type of class but not the other. A concrete class has all of its methods implemented and none are declared with the abstract keyword. An abstract class may have methods declared abstract and these methods defer implementation to its children. You can instantiate (using the new operator) an object of a concrete class type, but you cannot instantiate an object of an abstract class type. c) Briefly describe the fundamental difference between an abstract class and an interface in Java. An abstract class can contain implemented methods and/or instance variables. Implemented methods and instance variables are not allowed in an interface (with the exception of default methods added in Java 1.8 and public static final instance variables). Interfaces are simply a list of abstract methods that all classes that implement the interface must provide implementations of. d) Briefly describe two useful things we have seen interfaces used for in Java: Sorting objects of a certain data type by declaring the data type implements Comparable. Iterating over all the objects in a collection by declaring the collection data type implements Iterable. 5

5. Inheritance (16 points) a) Consider the following class: public abstract class Person private String name = ""; public Person(String name) this.name = name; public String tostring() return name; Fill in the missing super and this keywords into the underlined areas to correctly implement this child class: public class Employee extends Person private int salary = 0; private boolean retired = false; public Employee(String name, int salary) super (name); this.salary = salary; public Employee(String name, int salary, boolean retired) this (name, salary); this.retired = retired; // Return a string representation of this person, for example: John 23000 true public String tostring() return String.format("%s %d %b", super.tostring(), salary, retired); 6

b) Given the Person and Employee classes from part a, fill in the missing code in the class declaration and instance variables for the class Student. Student is-a Person, but has additional state such as a student ID and grade point average (GPA). Be sure to demonstrate proper data encapsulation. You also need to complete the method ishonorroll which returns true if a student's GPA is greater than or equal to 3.0, false otherwise. For full credit, do this using a single statement in ishonorroll. public class Student extends Person private long id = 0; private double gpa = 0.0; public Student(String name, long id, double gpa) /* code */ public boolean ishonorroll() return (gpa >= 3.0); c) Provide a code snippet that declares an instantiates an array named people that contains an Employee named "Bob" and a Student named "Carol". You can choose any valid values for the other parameters required to construct the Employee and Student objects. Person[] people = new Person[2]; people[0] = new Employee("Bob", 100); people[1] = new Student("Carol", 123, 4.0); d) Given the Person, Employee, and Student classes declared previously, give just the class declaration (no need for instance variables or methods) for a class StudentEmployee that is-a Student and is-a Employee (that is it inherits from both). If this is not possible, briefly explain why. Not possible, Java does not support multiple inheritance (can't extend more than one class). 7

6. Methods (6 points). Match the method description on the left with the best method on the right. Not every letter will be used. Each letter will be used at most once. J E L D A A method that given a twodimensional array of integers and the index of a row in that array, returns the specified row of values. A method that given a twodimensional array of integers and the low and high value of an integer range, returns the percentage of integers in the array that are inside the specified range. A method that given the size of each dimension, creates and returns a three-dimensional array of integers, initializing all elements to 1. A method that given two sets of integers, returns all the integer values that are in common between the sets. A method that given a list of integers, converts them to a list of the spelled version of each integer (e.g. converting integer 42 to "forty two"). A. public static String [] foo(arraylist<integer> a) B. public static boolean foo(string a) C. public static boolean foo(hashset<integer> a, HashSet<Integer> b) D. public static int [] foo(hashset<integer> a, HashSet<Integer> b) E. public static double foo(int [][] a, int b, int c) F. public static int [] foo(string [] a) G. public static int foo(arraylist<string> a) H. public static int foo(hashset<string> a) I. public static ArrayList<Boolean> foo() J. public static int [] foo(int [][] a, int b) K. public static String [] foo() L. public static int [][][] foo(int a, int b, int c) M. public static int[3] foo(int a, int b, int c) C A method that given two sets of integers, returns true if the two sets are exactly the same size, false otherwise. 8