CSE 413 Winter 2001 Midterm Exam

Similar documents
CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Software Design and Analysis for Engineers

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

Full file at Chapter 2 - Inheritance and Exception Handling

CSE413 Midterm. Question Max Points Total 100

Software Practice 1 - Error Handling

CSE P 501 Exam 8/5/04

This exam is open book. Each question is worth 3 points.

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

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

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

Handout 8 Classes and Objects Continued: Static Variables and Constants.

Lecture 19 Programming Exceptions CSE11 Fall 2013

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

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

CSE 331 Midterm Exam 2/13/12

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Implementing Classes (P1 2006/2007)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Software Practice 1 - Error Handling Exception Exception Hierarchy Catching Exception Userdefined Exception Practice#5

Vectors in Scheme. Data Structures in Scheme

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Implementing Classes

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

Windows and Events. created originally by Brian Bailey

Lab 2: File Input and Output

Title. Java Just in Time. John Latham. February 8, February 8, 2018 Java Just in Time - John Latham Page 1(0/0)

CHAPTER 7 OBJECTS AND CLASSES

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

CS 101 Fall 2006 Midterm 3 Name: ID:

Inheritance and Subclasses

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

CSE 331 Final Exam 12/9/13

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

Arrays Classes & Methods, Inheritance

ICOM 4015: Advanced Programming

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

Options for User Input

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CSE 333 Midterm Exam 2/14/14

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

CSE P 501 Exam 12/1/11

CS 1331 Fall 2016 Exam 3 Part 1 ANSWER KEY

Structure and Interpretation of Computer Programs

Practice problem on defining and using Class types. Part 4.

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

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CHAPTER 7 OBJECTS AND CLASSES

More on Exception Handling

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

CS112 Lecture: Working with Numbers

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Scheme Quick Reference

Homework 6. What To Turn In. Reading. Problems. Handout 15 CSCI 334: Spring, 2017

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

More on Exception Handling

CS159. Nathan Sprague

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

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

2. (True/False) All methods in an interface must be declared public.

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Exceptions and Error Handling

Defensive Programming

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)

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Chapter 6 Introduction to Defining Classes

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

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

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

XC Total Max Score Grader

Fundamentos de programação

Building Java Programs

Type Systems, Type Inference, and Polymorphism

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

Handout 9 OO Inheritance.

COMP-202 Unit 9: Exceptions

Scheme Quick Reference

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

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CS 61B Discussion 5: Inheritance II Fall 2014

Imperative Languages!

ESc101 : Fundamental of Computing. Object Oriented programming

CSE 143 Au04 Midterm 1 Page 1 of 9

CS159. Nathan Sprague

Character Stream : It provides a convenient means for handling input and output of characters.

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Recursive Data and Recursive Functions

Transcription:

Name ID # Score 1 2 3 4 5 6 7 8 There are 8 questions worth a total of 75 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the textbook (Budd s Understanding Object-Oriented Programming using Java) the Scheme report, and clean copies of the course handouts only. No other books or notes are allowed. Please wait to turn the page until you are told to begin. Page 1 of 11

Question 1. (18 points, 2 points each part) Suppose we enter the following top-level definitions into a Scheme interpreter. (define a 17) (define b 42) (define c 100) (define n (list 1 (+ 1 1) (- c 1))) (define colors '(red green blue)) (define p '((1 2) (3 4))) (define (f b) (+ a b)) (define g (lambda (x y) (* x y))) What is the value of each of the following expressions, given that the above definitions are in effect? If evaluating the expression produces an error, explain what is wrong. a) (car p) b) (cdar p) c) (cddr p) d) (cddr g) e) n f) (cons (reverse (cdr colors)) p) g) (append (reverse (cdr colors)) p) h) (let ((a 10) (c (+ a 1))) (f c)) i) (let* ((a 10) (c (+ a 1))) (f c)) Page 2 of 11

Question 2. (10 points) Write a Scheme function divisors-of that returns a list of numbers that exactly divide its positive integer argument. Examples: (divisors-of 12) => (1 2 3 4 6) (divisors-of 28) => (1 2 4 7 14) (divisors-of 17) => (1) (divisors-of 1) => () Feel free to use appropriate Scheme library functions in your solution. You can also write additional auxiliary functions if that is helpful. Page 3 of 11

Question 3. (10 points) A perfect number is an integer that is the sum of its positive integer divisors. The first three perfect numbers are 6=1+2+3 28=1+2+4+7+14 496=1+2+4+8+16+31+62+124+248 WriteaSchemefunctionperfect-numbers that returns a list of the first n perfect numbers. Examples: (perfect-numbers 0) => () (perfect-numbers 1) => (6) (perfect-numbers 3) => (6 28 496) You should use function divisors-of from question 2 when you need to calculate the divisors of a number. (Assume that it works as specified, even if you re not sure that your solution to question 1 is entirely correct.) You can write additional helper functions if needed, and you should use functions from the standard Scheme library as needed. Page 4 of 11

Question 4. (6 points) Suppose we have defined a list of numbers at top level: (define nums '(2 6 4 0 9)) Write a single scheme expression involving nums that evaluates to a list whose elements are each three times the corresponding values in nums. Ifnums is defined as above, your expression should evaluate to (6 18 12 0 27) Here s the catch: the expression should produce the right answer regardless of the current length of nums or the integer values in it. In other words, the literal expression(6 18 12 0 27) is not the right answer. You may not define any new functions (i.e., no (define ) at top-level). Hint: Lambda and higher-order functions. Page 5 of 11

Question 5. (10 points) In one of the homework problems, we defined constructor and accessor functions to manipulate binary trees in Scheme. The definitions were (make-node value left right) => (value left right) (value-of '(value left right)) => value (left-child '(value left right)) => left (right-child '(value left right)) => right A tree node is an internal node if it is not a leaf of the tree. In other words, interior nodes have at least one child. Write a function (n-internal atree) that returns the number of internal nodes in atree. IfaTree is empty, n-internal should return 0. Use the accessor and constructor functions given above to manipulate the nodes in atree. Page 6 of 11

Question 6. (15 points, 3 each) A few short-answer questions about Java. a) Methods in a class may be declared protected or private (among other things). What s the difference between these two? b) A well-written Java class should contain a tostring method. What is the purpose of this method? When is it called? Page 7 of 11

Question 6 (cont). c) In Java, there is a fairly elaborate hierarchy of classes whose instances represent exceptions. Part of the hierarchy looks like this. class Exception class RuntimeException extends Exception class IndexOutOfBoundsException extends RuntimeException class NullPointerException extends RuntimeException class IOException extends Exception class FileNotFoundException extends IOException The question is, why does this hierarchy of exception classes exist? Why is it useful to have many different, related classes instead of a single class Exception? d) Several recent Java compilers have included an analysis stage that detects objects that are truly local variables inside methods. That is, they detect objects that are allocated during the execution of a method, are only used inside the method, and cannot be referenced after the method returns. Why bother to accumulate this information? How is this information used? Why is this effective? Page 8 of 11

Question 6 (cont). e) Suppose we have a class that represents the balance in a bank account. class BankAccount { private double balance; // account balance public BankAccount(double initialdeposit) { balance = initialdeposit; public double getbalance() { return balance; public void deposit(double amount) { balance = balance + amount; Recently, a program that uses these objects has been enhanced to use multiple threads so it can process several transactions at the same time. Unfortunately, the auditors have discovered that account balances are occasionally incorrect. What s wrong, and how could you fix it? (Getting rid of the multithreading is not an option for fixing it.) Page 9 of 11

Question 7. (3 points) What output does the following Java program produce when method babble is called? class That { // return class name public String name() { return "That"; class Thing extends That { public String name() { return "Thing"; public babble() { That ref = (That)this; System.out.println("this.name() = " + this.name()); System.out.println("ref.name() = " + ref.name()); System.out.println("super.name() = " + super.name()); Page 10 of 11

Question 8. (3 points) The following program attempts to track the coordinates of the mouse and print them to System.out. But it doesn t compile. What s wrong? (If there s more than one error, identify all of them.) import java.awt.event.*; public class Cat implements MouseMotionListener { showmousecoordinates(int x, int y) { System.out.println("mouse x = " + x + ", y = " + y); public void MouseMoved(MouseEvent e) { showmousecoordinates(e.getx(), e.gety()); public void MouseDragged(MouseEvent e) { showmousecoordinates(e.getx(), e.gety()); public static void main(string [] args) { addmousemotionlistener(this); Page 11 of 11