CIS 1068 Program Design and Abstraction Fall 2013 Final Exam

Similar documents
CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

java Features Version April 19, 2013 by Thorsten Kracht

Object-Oriented Programming in Java

Sample CSE8A midterm Multiple Choice (circle one)

AP Computer Science Java Subset

AP Computer Science Java Mr. Clausen Program 9A, 9B

Course Intro Instructor Intro Java Intro, Continued

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

CS 111 Classes I 1. Software Organization View to this point:

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Part I. Multiple Choice Questions (2 points each):

Computer Programming I

The following program is aiming to extract from a simple text file an analysis of the content such as:

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Introduction to Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Basics: Data Types, Variables, and Loops

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Chapter 2 Introduction to Java programming

J a v a Quiz (Unit 3, Test 0 Practice)

Lab Experience 17. Programming Language Translation

Week 1: Review of Java Programming Basics

Java Interview Questions and Answers

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Crash Course in Java

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

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

public static void main(string[] args) { System.out.println("hello, world"); } }

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Part 3: GridWorld Classes and Interfaces

Inheritance, overloading and overriding

Building Java Programs

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

CS170 Lab 11 Abstract Data Types & Objects

Introduction to Programming

Basic Object-Oriented Programming in Java

AP Computer Science Static Methods, Strings, User Input

Introduction to Programming System Design. CSCI 455x (4 Units)

Programming Languages

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

COMPUTER SCIENCE. Paper 1 (THEORY)

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Unit 6. Loop statements

Computing Concepts with Java Essentials

Explain the relationship between a class and an object. Which is general and which is specific?

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Java Crash Course Part I

INPUT AND OUTPUT STREAMS

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Iteration CHAPTER 6. Topic Summary

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Software Engineering Techniques

CS 106 Introduction to Computer Science I

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

LAB4 Making Classes and Objects

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Semantic Analysis: Types and Type Checking

Chapter 13 - Inheritance

CS193j, Stanford Handout #10 OOP 3

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

C# and Other Languages

Java CPD (I) Frans Coenen Department of Computer Science

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Java Programming Fundamentals

CS 1302 Ch 19, Binary I/O

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Habanero Extreme Scale Software Research Project

Event-Driven Programming

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Creating a Simple, Multithreaded Chat System with Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Inside the Java Virtual Machine

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

CMSC 202H. ArrayList, Multidimensional Arrays

Programming Languages CIS 443

An Incomplete C++ Primer. University of Wyoming MA 5310

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Chapter 2 Elementary Programming

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Limitations of Data Encapsulation and Abstract Data Types

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

2 The first program: Little Crab

Project 2: Bejeweled

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Transcription:

Fall 2013 Name: Instructor s Name: Page Points Score 1 13 2 7 3 12 4 9 5 4 6 6 7 10 8 12 9 7 10 8 11 3 12 9 Total: 100 Instructions The exam is closed book. You may not use a calculator, cell phone, etc. You may bring to the exam your own cheat sheet : one 8.5 x 11 sheet of paper with notes that you have written. You may use both sides of the sheet. If a question asks you to answer briefly, it means just that. A sentence or two at most should be sufficient. Unless otherwise specified, you may use functions from the standard Java library. Good luck. i

Some Reminders about Syntax Miscellaneous static boolean Arrays.equals(int[ ] a1, int[ ] a2); static void Arrays.sort(int[ ] a); static int Arrays.binarySearch(int[ ] a, int key); static String Arrays.toString(int[ ] a); static double Math.max(double d1, double d2); Object class boolean equals(object obj); // indicates if some other object is "equal to" this one String tostring( ); // returns a string representation of the object String class int indexof(string s); // returns position of s, -1 if not there int indexof(string s, int k); //returns position of s at or past position k char charat(int k); // returns character at position k String substring(int k); //substring starting at k up to end String substring(int k, int h); //substring starting at k ending at h excluded int compareto(string anotherstring); String touppercase( ); boolean equalsignorecase(string s); Files Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(new File(filename)); boolean sc.hasnext( ); boolean sc.hasnextline( ); String sc.next( ); String sc.nextline( ); int sc.nextint( ); PrintWriter pw = new PrintWriter(new File(filenameString)); void close( ); // works for Scanner, PrintWriters

Short Answer Questions 1. Please provide a brief answer to each of the following: (1 point) (a) How many bytes are in a gigabyte? How many bits? (1 point) (b) In what component of a desktop computer is most of the computation done? (1 point) (c) About how fast can the component you mentioned in question (b) compute? (1 point) (d) What s the primary job of the Java compiler? (Hint: it s not to find the errors in my syntax.) (1 point) (e) Write the single line of code which declares an int x so that x is only directly accessible within its own class. (1 point) (f) What does it mean to override a method? (1 point) (g) What does it mean to say that a method is abstract? (2 points) 2. If it takes me 3 seconds to sort 1000 data points using a bubble sort (which is an O(n 2 ) algorithm), roughly how long should it take me to sort 3000 data points using the same bubble sort on the same machine? (4 points) 3. For the array below, circle each of the cells that is checked when a program executes a binary search to find the value 27. -29-12 -10-1 2 12 23 24 36 54 What is the return value from the binary search? 3. out of a possible 13 1 of 12 exam continues...

Trace Me Questions (3 points) 4. What are the values of variables a, b, c, and d at Reference Point 1? 1 public class BooleanExpressions 2 3 public static void main(string [] args) 4 5 boolean P = true; 6 boolean Q = false; 7 boolean R = P!Q; 8 int X = 17; 9 10 boolean a = P && Q; 11 boolean b = (P && Q) (X > 10) 12 boolean c = P && (Q!R) 13 boolean d =!!Q; 14 // Reference Point 1 15 16 (4 points) 5. Answer the question below about this method definition: 1 public static void display(double x, char c) 2 3 int y = x * x; 4 5 if (y > x) 6 System.out.println(c); 7 else if (y < x) 8 9 if(c== A ) 10 System.out.println("found an A "); 11 System.out.println("" + y + " is less than " + x); 12 13 For each method call below, indicate what the method will display on the screen. Method Call display(2.0, A ); Displays on the screen display(0.5, 8 ); display(0.1, A ); display(0.0, D ); out of a possible 7 2 of 12 exam continues...

(3 points) 6. What s printed when we execute the following: 1 public class ForTest 2 public static void main(string [] args) 3 int x = 1; 4 for(; x < 0; x++) 5 System.out.println("x = " + x); 6 7 (3 points) 7. What s printed when we execute the following: 1 public class DoWhileTest 2 public static void main(string [] args) 3 int x = 1; 4 do 5 6 System.out.println("x = " + x); 7 x++; 8 while (x < 0); 9 10 (3 points) 8. What s printed when we execute the following: 1 public class WhatsPrinted00 2 public static void func(int x,int y,int z) 3 4 x *= 2; 5 y += x; 6 z = (x + y) % 2; 7 System.out.println(x + " " + 8 y + " " + z); 9 10 11 12 13 public static void main(string args[]) 14 15 int x = 10, y = 20, z = 30; 16 func(z, x, y); 17 18 (3 points) 9. What s printed when we execute the following: 1 public class WhatsPrinted01 2 public static void func(int x,int y,int z) 3 4 x *= 2; 5 y += x; 6 z = (x + y) % 2; 7 8 9 public static void main(string args[]) 10 11 int x = 10, y = 20, z = 30; 12 func(z, x, y); 13 System.out.println(x + " " + y + " " + z); 14 15 16 out of a possible 12 3 of 12 exam continues...

(3 points) 10. What s printed when we execute the following: 1 public class WhatsPrinted02 2 public static void func(int A[]) 3 for (int i=1; i<a.length; i++) 4 A[i]+=A[i-1]; 5 6 7 public static void main(string args[]) 8 int A[] = 10,20,30; 9 func(a); 10 System.out.println(A[2]); 11 12 (3 points) 11. What s printed when we execute the following: 1 public class WhatsPrinted03 2 3 public static int func(int A[], int B[]) 4 5 A = B; 6 return A[1]; 7 8 9 public static void main(string args[]) 10 int A[] = 10,20,30; 11 int B[] = 40,50,60; 12 13 int x = func(a, B); 14 System.out.println(x + " " + A[1]); 15 16 (3 points) 12. What s printed when we execute the following: 1 class Stuff 2 int x; 3 4 public Stuff(int x) 5 this.x = x; 6 7 public void move() 8 x++; 9 10 11 12 13 14 15 public class WhatsPrinted05 16 public static void func(stuff s) 17 s.move(); 18 19 20 public static void main(string args[]) 21 Stuff a = new Stuff(10); 22 Stuff b = a; 23 24 func(a); 25 b.move(); 26 System.out.println(a.x); 27 28 out of a possible 9 4 of 12 exam continues...

(4 points) 13. The following two classes together have 9 different compile-time (syntax) errors. Circle the parts of the code where 4 of them occur (do not circle more than 4, you do not need to find them all). For each error that you circled, write a brief explanation of what the error is. 1 public class WillNotCompile 2 3 private int myfield = 3; 4 5 public static WillNotCompile(double initval) 24 public static void main(string [] args) 6 7 myfield = this.initval; 8 9 10 public void getfield() 11 12 return myfield; 13 14 15 public static int setfield(int newval) 16 17 this.myfield = newval; 18 19 20 21 22 public class AlsoWillNotCompile 23 25 26 WillNotCompile obj = 27 new WillNotCompile(7); 28 29 obj.setfield(10); 30 31 obj.myfield++; 32 33 int final = WillNotCompile.getField() + 5; 34 35 System.out.println(final); 36 37 out of a possible 4 5 of 12 exam continues...

(6 points) 14. Given the following class definitions: 1 class Lack extends Liatorp 2 public void b() 3 System.out.println("Lack b"); 4 5 6 class Liatorp 7 public void a() 8 System.out.println("Liatorp a"); 9 10 public void b() 11 System.out.println("Liatorp b"); 12 13 public String tostring() 14 return "Liatorp"; 15 16 17 class Ektorp extends Kvissle 18 public void a() 19 System.out.println("Ektorp a"); 20 21 22 class Kvissle extends Lack 23 public void a() 24 System.out.println("Kvissle a"); 25 26 public String tostring() 27 return "Kvissle"; 28 29 IfwehaveLiatorp[ ] theflurgs = new Ektorp(), new Kvissle(), new Liatorp(), new Lack(), what s printed when we execute: (a) System.out.println(theFlurgs[0]); (b) System.out.println(theFlurgs[1]); (c) System.out.println(theFlurgs[2]); (d) System.out.println(theFlurgs[3]); (e) theflurgs[0].a(); (f) theflurgs[1].a(); (g) theflurgs[2].a(); (h) theflurgs[3].a(); (i) theflurgs[0].b(); (j) theflurgs[1].b(); (k) theflurgs[2].b(); (l) theflurgs[3].b(); (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) (k) (l) out of a possible 6 6 of 12 exam continues...

Coding Questions (8 points) 15. (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters...). For example, if s is "Too bad Spongebob s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too bad..." If s is shorter than max characters, return the original String. If max is less than 3 and less than the length of s, return the empty String. (2 points) (b) Write a main method that calls the abbreviate method twice, and prints the result to the screen each time. The first call should abbreviate the String "How now, brown cow?" down to 10 characters. The second call should abbreviate the result of the first abbreviation down to 5 characters. out of a possible 10 7 of 12 exam continues...

Secret Message (12 points) 16. Below is a partially filled-out CipherDocument class that contains a String [] lines as a field. Each element of lines contains one line of text in the document. But there is a secret message encoded in the CipherDocument! Write an instance method called getsecretmessage() that returns a String containing the second character of each word in the document, or a space if the word is only one letter long. Hint: Recall that you can construct a Scanner object on a String. That way you can find all of the words inside of the String by repeatedly calling the next() method. After this code runs, it should display "easy problem" on the screen. public class CipherDocument private String [] lines; public CipherDocument(String [] doclines) this.lines = doclines; public static void main(string [] args) String [] doc = "He sat us by a spinning drum.", "You obstinate old eel, Emma!"; CipherDocument cd = new CipherDocument(doc); String secretmessage = cd.getsecretmessage(); System.out.println(secretMessage); // your definition of the getsecretmessage() method goes here out of a possible 12 8 of 12 exam continues...

GradeBook 17. Below is a partially filled-out GradeBook class, which stores the names and grades of students in a course. Names are stored in an array of Strings. Grades are stored in a two-dimensional array of doubles, one row for each student, and one column for each test. public class GradeBook private String [] names; private double [][] grades; public GradeBook(String [] names, double [][] grades) this.names = names; this.grades = grades; // additional methods that you will write (see below) will go here (2 points) (a) Write a getter (or accessor) method for the GradeBook class that takes an int i as argument, and returns the name of the i th student. (5 points) (b) Implement an instance method which is passed an int row as an argument. The method should return the average of the grades on that row. out of a possible 7 9 of 12 question 17 continues...

(5 points) (c) Implement an instance method that determines the row of grades with the highest average. The method should return the row number. You may call the method from part b in your answer to this question. (3 points) (d) Write a class called Teacher that has just a main method. The main method should construct a GradeBook for a class with two students, "Jack" and "Jill". Jack s grades on the three tests for the class are 33, 55, and 77. Jill s grades are 99, 66, and 33. After constructing the GradeBook object, main should use the instance methods of the GradeBook class to determine the name of the student with the highest average grades. Print the name of this student on the screen. out of a possible 8 10 of 12 exam continues...

18. Consider the CellPhone class below. public class CellPhone private String phonenumber; // stores e.g. "215-555-1234" private String carrier; // stores e.g. "Verizon" private boolean incall = false; // stores whether the phone is currently in a call private String connectedto = null; // the number of the other phone being called public CellPhone(String number, String carrier) this.phonenumber = number; this.carrier = carrier; public void initiatecall(string otherphone) incall = true; connectedto = otherphone; public void stopcall() incall = false; connectedto = null; (3 points) (a) Write a tostring method for the CellPhone class. It should return something like "Verizon, 215-555-1234". out of a possible 3 11 of 12 question 18 continues...

(5 points) (b) Write a subclass of the CellPhone class called SmartPhone that will allow the client class PhoneUser below to execute. SmartPhone should have two new fields: one that stores the name of the type of phone (either "ios" or "Android"), and one that stores an array of names for Apps that are stored on the phone. The SmartPhone class should also have a constructor that does everything that the superclass s constructor does, plus it should initialize the type of phone and the array of Apps. public class PhoneUser public static void main(string [] args) String [] apps = "SnapChat", "Doodle Jump"; SmartPhone phone = new SmartPhone("267-222-9876", "AT&T", "ios", apps); phone.initiatecall("610-999-0001"); phone.stopcall(); System.out.println(phone); (1 point) (c) Do you need to add a stopcall or initiatecall method to the SmartPhone class in order to make the PhoneUser class run properly? Why or why not? (1 point) (d) What does the PhoneUser class display on the screen when it is executed? (2 points) (e) What would we need to add (if anything) to the SmartPhone class in order for the command System.out.println(phone); to display everything that the CellPhone class s tostring method returns, plus the type of the SmartPhone object ("ios" or "Android")? out of a possible 9 12 of 12 end of exam