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

Similar documents
Exam 1 - (20 points)

Final Exam CS 251, Intermediate Programming December 10, 2014

CS Exam 1 Review Suggestions

CS 11 java track: lecture 3

Final Exam CS 251, Intermediate Programming December 13, 2017

Use the scantron sheet to enter the answer to questions (pages 1-6)

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

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

Introduction to Programming Using Java (98-388)

Declarations and Access Control SCJP tips

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

First Name: AITI 2004: Exam 2 July 19, 2004

CSC 1351 The Twelve Hour Exam From Hell

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

RAIK 183H Examination 2 Solution. November 10, 2014

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

CMSC 331 Second Midterm Exam

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

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

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Last name:... First name:... Department (if not D-INFK):...

Java: introduction to object-oriented features

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

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CS11 Java. Fall Lecture 3

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

First Name: AITI 2004: Exam 2 July 19, 2004

1. (5 points) In your own words, describe what an instance is.

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

CS11 Java. Fall Lecture 4

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

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

Midterm assessment - MAKEUP Fall 2010

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

CS 180 Final Exam Review 12/(11, 12)/08

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CSC Java Programming, Fall Java Data Types and Control Constructs

CIS 120 Final Exam 15 December 2017

CSE 331 Final Exam 6/10/14

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

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

UMBC CMSC 331 Final Exam

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

Java Help Files. by Peter Lavin. May 22, 2004

Come & Join Us at VUSTUDENTS.net

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

Building Java Programs

Lecture 9. Lecture

Chapter 4 Defining Classes I

Last name:... First name:... Department (if not D-INFK):...

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Program Correctness and Efficiency. Chapter 2

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

Here are the steps to get the files for this project after logging in on acad/bill.

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

Chapter 10 Classes Continued. Fundamentals of Java

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

H212 Introduction to Software Systems Honors

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

CS 251 Intermediate Programming GUIs: Event Listeners

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

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

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

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Programming Languages and Techniques (CIS120)

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

Programming Exercise. Scribbler

JAVA MOCK TEST JAVA MOCK TEST II

INSTRUCTIONS TO CANDIDATES

public int calculatedamage() { // Generate and return the damage inflicted on ship }

CSC Advanced Object Oriented Programming, Spring Specification

Midterm Exam (REGULAR SECTION)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Checked and Unchecked Exceptions in Java

Transcription:

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

Name: CSC143 Exam 1 2 Multiple Choice Questions (30 points) Answer all of the following questions. READ EACH QUESTION CAREFULLY. Fill the correct bubble on your scantron sheet. Each correct answer is worth 1 point. Each question has EXACTLY one correct answer. 1. Consider public class A { public void foo() {/* code */ public class B extends A { public void foo() { /* code */ When the following lines of code are executed A a = new B(); a.foo(); what is the static type of this within the foo method called by the statement a.foo()? A. A B. B C. Object D. Can t tell without having the code of foo. E. The above code is illegal.

Name: CSC143 Exam 1 3 2. Consider public class A { public void foo() {/* code */ public class B extends A { public void foo() { /* code */ When the following lines of code are executed A a = new B(); a.foo(); what is the dynamic type of this within the foo method called by the statement a.foo()? A. A B. B C. Object D. Can t tell without having the code of foo. E. The above code is illegal.

Name: CSC143 Exam 1 4 3. Consider the following class definitions public class Vehicle { public Vehicle() { public class Car extends Vehicle implements Rentable { public Rentable getrentable() { return /*??? */ What could be written in place of /*??? */ so that the above code compiles correctly? A. this; B. new Vehicle(); C. new Rentable(); D. Either A or B E. Either A or B or C

Name: CSC143 Exam 1 5 4. Consider the following inheritance hierarchy public class OopsException extends Exception{ public class MoreOopsException extends OopsException { public class A { public void foo() throws OopsException { public class B extends A { What are possible signatures of an implementation of an override of foo in the B class? A. public void foo() throws OopsException B. public void foo() throws MoreOopsException C. public void foo() throws MoreOopsException, RuntimeException D. A and B E. A, B and C 5. Which statements are true? I. A try-catch block can only be used for checked exceptions II. You can have multiple catch blocks for one try block III. return statements and throw statements are legal in a catch block A. I only B. II only C. I, II, & III D. I & III E. II, & III

Name: CSC143 Exam 1 6 6. Consider the following two class definitions written within the same file package disco; public class Boogie{ public void setcolor (Color c) { void display(){ protected Color color; class Woogie extends Boogie{ public void setcolor(color c) { color = c; // A public void show() { display(); // B System.out.println(text); private String text; Are statements A and/or B legal? (Will they compile?) A. Both statements are legal B. Neither statement is legal C. B is legal, A is not D. A is legal, B is not E. Can't tell. There is not enough information. I would need to read through the implementation of display. 7. Using these classes one more time... which is true? Assume that any possibly illegal statement (A and/or B) has been removed. A. Any client can instantiate disco.woogie, but not disco.boogie B. Any client can instantiate disco.boogie, but not disco.woogie C. Any client can instantiate objects of type disco.boogie and disco.woogie D. As long as the client imports disco, then it can instantiate objects of type disco.boogie and disco.woogie E. None of the above (A, B, C and D are all false).

Name: CSC143 Exam 1 7 8. To create a top level container (e.g. a graphics window), which javax.swing class(es) could you instantiate? A. JPanel B. JFrame C. JTextArea D. A and B E. A, B and C 9. In Java, which of the following is (are) true? (I) An event listener can listen to more than one component. (II) A component can send an event to more than one event listener A. I B. II C. I and II D. None are true 10. When using an MVC framework to program a game (e.g. a chess game), which component would know about the rules of the game (e.g. the motion of the pieces)? A. Model B. View C. Controller D. Rules are programmed within the View and Controller with no clear separation E. Rules are programmed within the View, Controller and Model with no clear separation

Name: CSC143 Exam 1 8 Short answers (50 points) 1) [10 points] Fill out the following table with Y for Yes and N for No. interface Enables support for multiple inheritance Supports abstract methods Implementation allowed (i.e. can you write code within the methods?). Create an instance of Partial implementation allowed (i.e. can you write code within some of the methods?). abstract class concrete class 2) [10 points] Consider the following code: public class Student { private double gpa; // must be between 0.0 and 4.0 /** * Updates the gpa of this student and returns the new gpa. * @param coursegrade the grade received for a course * (between 0.0 and 4.0). * @param credits the number of credits (greater than 0). * @return new gpa value. */ public double updategpa(double coursegrade, int credits) { /* code */ a) _ Give a class invariant _ How would you use the assert keyword to check that invariant? b) _ Give one precondition of the setgpa method (which is not a class invariant): _ Write a statement with the throw keyword to check that precondition. c) Give a postcondition of the setgpa method (other than any condition already mentioned).

Name: CSC143 Exam 1 9 3) [5 points] In homework 2, you created an abstract class MovingThing that had the two methods public void movecenterby (int dx, int dy) { // use movecenterto // x and y are the coordinates of the center // of this MovingThing (declared as protected instance // fields) int newx = x + dx; int newy = y + dy; movecenterto(newx, newy); public abstract void movecenterto (int x, int y); MoveCenterTo was implemented in the derived classes (e.g. Spider). However this approach had the potential danger of generating a StackOverflowError exception. Write an implementation of MoveCenterTo that illustrates this problem: public void movecenterto (int x, int y) { 4) [5 points] What is the difference between an adapter and a listener (for example MouseAdapter and MouseListener)? Explain the difference for a developer using one or the other.

Name: CSC143 Exam 1 10 5) [5 points] The ActionListener interface is defined as follows ( actually this is not true, but it is not relevant for this question). public interface ActionListener { public void actionperformed(actionevent e); A JButton can send its click event to an action listener using the method with the following signature: public void addactionlistener(actionlistener listener) Complete the code below that creates a listener for a JButton mybutton. The listener should print Button has been clicked whenever the button is clicked. Use an anonymous inner class. JButton mybutton = new JButton( Click me! ); // Add your code below

Name: CSC143 Exam 1 11 6) Consider the following classes [15 points] public abstract class Vehicle { public boolean startengine() {/* code */ public abstract int getmaxspeed(); public abstract double getvehiclevalue(); public interface Rentable { public double getweeklyrate(); public interface UsesPremiumGas { public void filltankwithpremiumgas(double gallons); public class Car extends Vehicle implements Rentable {/*code*/ public class FancyCar extends Car implements UsesPremiumGas { /* code */ public class Bus extends Vehicle { /* code */ a) Answer the following questions (must means so that the code compiles). Which methods must be implemented in the Car class? Which methods must be implemented in the FancyCar class? Which methods must be implemented in the Bus class?

Name: CSC143 Exam 1 12 b) A company has a fleet of vehicles. Complete the method below that prints the cheapest weekly rate for the vehicles of the fleet that can be rented? In no vehicle can be rented, the method should state so. Use an iterator in your implementation. You can assume that v is not null. /** * Prints the cheapest weekly rate available among a fleet * of vehicles, or prints that no vehicle can be rented. * @param v the list of vehicles */ public void printcheapestweeklyrate(list v) {

Name: CSC143 Exam 1 13 7) Programming question [20 points] Consider a network of streets laid out in a rectangular grid; for example, y 1 2 x In a northeast path from one point in the grid to another, one may walk only to the north (up) and to the east (right). For example, there are four northeast paths from 1 to 2 in the preceding grid: Write a recursive method to count the number of northeast paths from one point (x1, y1) to another point (x2, y2) in a rectangular grid. Take that the number of northeast paths of a point to itself is 0. public int numberofnepaths(int x1, int y1, int x2, int y2) {