Techniques of Java Programming

Similar documents
Java Programming. Final Examination on December 13, 2015 Fall 2015

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Extensibility Patterns: Extension Access

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

Welcome to. Session

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Student s Printed Name: KEY_&_Grading Guidelines_CUID:

Math 1020 ANSWER KEY TEST 3 VERSION B Spring 2018

Student s Printed Name:

Version A QP1-14,18-24, Calc ,App B-D

MATH 1040 Test 2 Spring 2016 Version A QP 16, 17, 20, 25, Calc 1.5, 1.6, , App D. Student s Printed Name:

Solar Open House Toolkit

Student s Printed Name: _ Key _&_Grading Guidelines CUID:

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

Student s Printed Name:

Principles of Math 12 - Geometric Series Practice Exam 1

Student s Printed Name:

Version B QP1-14,18-24, Calc ,App B-D

GRADE 11 NOVEMBER 2014 MATHEMATICAL LITERACY P2

You have 3 hours to complete the exam. Some questions are harder than others, so don t spend too long on any one question.

Math 1020 TEST 3 VERSION A Spring 2017

Test 2 Electrical Engineering Bachelor Module 8 Signal Processing and Communications

MATH 360. Probablity Final Examination December 21, 2011 (2:00 pm - 5:00 pm)

Department of Economics ECO 204 Microeconomic Theory for Commerce (Ajaz) Test 1 Solutions

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

Conceptual Explanations: Simultaneous Equations Distance, rate, and time

Page Points Score Total: 100

MthSc 107 Test 1 Spring 2013 Version A Student s Printed Name: CUID:

ON SITE SYSTEMS Chemical Safety Assistant

Chemistry 122-XXX Prof. Mines. Practice Exam 1b (From an Exam 1 that was originally 100 points) (Exam from many years ago!)

Mathematics Level D: Lesson 2 Representations of a Line

EECS 126 Probability and Random Processes University of California, Berkeley: Spring 2018 Kannan Ramchandran February 14, 2018.

Student s Printed Name: _Key

INF Models of concurrency

Motion II. Goals and Introduction

Data Structures and Algorithms Winter Semester

WeatherHawk Weather Station Protocol

Flight Utilities Metar Reader version 3.1 by Umberto Degli Esposti

Math 1020 ANSWER KEY TEST 3 VERSION A Fall 2016

BoardGame: A MiniDraw extension

MATH 1020 TEST 1 VERSION A SPRING Printed Name: Section #: Instructor:

Discrete Event Systems Exam

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

A Model of GIS Interoperability Based on JavaRMI

Sampling Distributions of the Sample Mean Pocket Pennies

Week Unit/Lesson Learning Objectives Reporting Categories - Welcome

1 Java Night Countdown (30%)

Real Time Operating Systems

Conservation of Mechanical Energy Activity Purpose

Good luck! School of Business and Economics. Business Mathematics. Date: 25 October, Time: 12:00. Calculator allowed:

Design Patterns and Refactoring

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

Student s Printed Name: _KEY Grading Guidelines CUID:

Page Points Score Total: 100

KING DAVID HIGH SCHOOL LINKSFIELD MATHEMATICS PAPER 1 GRADE 11 NOVEMBER EXAMINATION 2017

Exam 1 MATH 142 Summer 18 Version A. Name (printed):

Stat 2300 International, Fall 2006 Sample Midterm. Friday, October 20, Your Name: A Number:

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

Data and Error Analysis

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

MAT 2377C FINAL EXAM PRACTICE

ALGEBRA 1 FINAL EXAM 2006

PHP-Einführung - Lesson 4 - Object Oriented Programming. Alexander Lichter June 27, 2017

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

ST-Links. SpatialKit. Version 3.0.x. For ArcMap. ArcMap Extension for Directly Connecting to Spatial Databases. ST-Links Corporation.

WARMUP. Draw the Lewis dot diagram and determine the VSEPR shape for the following compounds. This is practice for the quiz. OF2

THANK YOU FOR YOUR PURCHASE!

Factory method - Increasing the reusability at the cost of understandability

INSTRUCTION MANUAL. Laser Diode Sarcomere Length. Aurora Scientific Inc. 360 Industrial Parkway South, Unit 4 Aurora, Ontario, Canada L4G 3V7

Student s Printed Name:

Designing Information Devices and Systems I Summer 2017 D. Aranki, F. Maksimovic, V. Swamy Midterm 2. Exam Location: 100 Genetics & Plant Bio

You must show your student ID when you hand in your exam!

FCE 3900 EDUCATIONAL RESEARCH LECTURE 8 P O P U L A T I O N A N D S A M P L I N G T E C H N I Q U E

Family Name (Please print Given Name(s) Student Number Practical Group in BLOCK LETTERS) as on student card Code (eg. FxA)

Chemistry: Introduction to Chemistry

Introduction to Computing II (ITI1121) FINAL EXAMINATION

Question: Total Points: Score:

Information System Design IT60105

Discrete Mathematics and Probability Theory Summer 2015 Chung-Wei Lin Midterm 1

MATH 1070 Test 1 Spring 2014 Version A Calc Student s Printed Name: Key & Grading Guidelines CUID:

1.1 Linear Equations and Inequalities

Test 2 - Answer Key Version A

MthSc 107 Test 1 Spring 2013 Version A Student s Printed Name: CUID:

DO NOT OPEN THE EXAMINATION PAPER UNTIL YOU ARE TOLD BY THE SUPERVISOR TO BEGIN. Math 9. Sample Final June Value: 50 Points Duration: 2 Hours

Bachelor s Degree Programme Operations Research (Valid from 1st January, 2012 to 30th November, 2012.)

Quiz. Good luck! Signals & Systems ( ) Number of Problems: 19. Number of Points: 19. Permitted aids:

An object-oriented design process. Weather system description. Layered architecture. Process stages. System context and models of use

Lab Exercise 03: Gauss Law

Incline Plane Activity

12/14/2010. Directions

Electric Fields. Goals. Introduction

How to find Lecture Room 3 in Lecture Block A on the Sidgwick Site

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

FSA Algebra I End-of-Course Review Packet

CHEM311 FALL 2005 Practice Exam #3

---- WITH SOLUTIONS ----

B264B MATHEMATICS B (MEI) MONDAY 2 JUNE 2008 GENERAL CERTIFICATE OF SECONDARY EDUCATION. Paper 4 Section B (Higher Tier) Afternoon Time: 1 hour

Limiting Reactants An analogy and learning cycle approach

Bloomsburg University Weather Viewer Quick Start Guide. Software Version 1.2 Date 4/7/2014

Transcription:

Legi-Nr.:... Techniques of Java Programming ETH Zurich Date: 9 May 008 Family name, first name:... Student number:... I confirm with my signature, that I was able to take this exam under regular circumstances and that I have read and understood the directions below. Signature:... Directions: Exam duration: 90 minutes. Use a pen (not a pencil)! Please write your student number onto each sheet. All solutions can be written directly onto the exam sheets. If you need more space for your solution ask the supervisors for a sheet of official paper. You are not allowed to use other paper. You should answer all questions (no questions are optional). All personal documents are authorized. Exchanging documents during the examination would mean failing the examination. Electronic equipment (laptops, cell phones, PDA, etc.) are not authorized. Using them would mean failing the examination. Only one solution can be handed in per question. Invalid solutions need to be crossed out clearly. Please write legibly! We will only correct solutions that we can read. Manage your time carefully (take into account the number of points for each question). Please immediately tell the supervisors of the exam if you feel disturbed during the exam. Good luck! 1

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam Question Number of possible points Points 1 9 10 3 13 10 5 1 TOTAL 5

Legi-Nr.:... 1 Java Fundamentals (9 Points) All questions below refer to the Java language version 5.0 as taught in the course. Put checkmarks in the checkboxes corresponding to the correct answers. Multiple correct answers are possible; there is at least one correct answer per question. A correctly set checkmark is worth 1 point, an incorrectly set checkmark is worth -1 point. If the sum of your points is negative, you will receive 0 points. 1. Consider the following classes: 3 public class CustomInteger { 5 private int anint ; 6 7 public CustomInteger ( int i ) { 8 anint=i ; 9 } 10 11 public int getanint ( ) { 1 return anint ; 13 } 1 } 3 public class Main { 5 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 6 CustomInteger c i 1 = new CustomInteger ( 6 ) ; 7 CustomInteger c i = new CustomInteger ( 6 ) ; 8 I n t e g e r i = new I n t e g e r ( 6 ) ; 9 System. out. p r i n t ( c i 1. e q u a l s ( c i ) ) ; 10 System. out. p r i n t ( c i 1==c i ) ; 11 System. out. p r i n t ( c i 1. e q u a l s ( i ) ) ; 1 } 13 } Which of the following result from the print statements? a. truefalsefalse. b. falsefalsefalse. c. truefalsetrue. d. falsetruetrue.. How could you declare the visibility of a method that has to be visible both in its own class package and by descendant classes? a. The defaut (package) visibility best addresses the need. b. Both the defaut (package) and the protected visibility can be used. c. The protected visibility best addresses the need. d. In Java doesn t exist a visibility level that addresses the mentioned specific need, so I have to declare the method public. 3

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam 3. Consider the following classes: 3 public class Creature { 5 public void a t t a c k ( int i ){ 6 System. out. p r i n t ( Creature a t t a c k s : + i + ) ; 7 } 8 } 3 public class Orc extends Creature { 5 public void a t t a c k ( int i ){ 6 System. out. p r i n t ( Orc a t t a c k s : + i + ) ; 7 } 8 } 3 public class Goblin extends Creature { 5 public void a t t a c k ( long i ){ 6 System. out. p r i n t l n ( Goblin a t t a c k s : + i + ) ; 7 } 8 } 3 public class GameTest { 5 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 6 Creature c1 = new Orc ( ) ; 7 Creature c = new Goblin ( ) ; 8 Goblin g = new Goblin ( ) ; 9 c1. a t t a c k ( 7 ) ; 10 c. a t t a c k ( 5 ) ; 11 g. a t t a c k ( 3 ) ; 1 g. a t t a c k (L ) ; 13 } 1 } Which of the following result from the print statements? a. Orc attacks:7 Creature attacks:5 Goblin attacks:3 Goblin attacks: b. Creature attacks:7 Creature attacks:5 Creature attacks:3 Goblin attacks: c. Orc attacks:7 Creature attacks:5 Goblin attacks:3 Creature attacks: d. Orc attacks:7 Creature attacks:5 Creature attacks:3 Goblin attacks:

Legi-Nr.:.... Consider the following classes: 3 public class Creature { 5 protected Weapon weapon=new Weapon( Sword ) ; 6 7 public Creature ( Weapon w){ 8 weapon = w; 9 } 10 11 public Weapon getweapon ( ) { 1 return weapon ; 13 } 1 15 } 3 public class Goblin extends Creature { 5 public Goblin ( Weapon w) { 6 super (w) ; 7 } 8 9 public MeleeWeapon getweapon ( ) { 10 return ( MeleeWeapon ) weapon ; 11 } 1 } 3 public class Weapon { 5 private S t r i n g name= Name not a s s i g n e d ; 6 7 public Weapon( S t r i n g name ){ 8 this. name=name ; 9 } 10 11 public S t r i n g getname ( ) { 1 return name ; 13 } 1 } 3 public class MeleeWeapon extends Weapon { 5 public MeleeWeapon ( S t r i n g name ) { 6 super ( name ) ; 7 } 8 } 5

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam 3 public class WeaponTest { 5 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 6 Creature c=new Goblin (new MeleeWeapon ( Axe ) ) ; 7 System. out. p r i n t l n ( c. getweapon ( ). getname ( ) ) ; 8 } 9 } Which of the following are true? a. The code does not compile. The method getweapon() is duplicated. b. The code compiles correctly and prints Name not assigned. c. The code compiles correctly and prints Axe. d. The code compiles correctly and prints Sword. 5. Consider the following class: 3 public class ExceptionTest { 5 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 6 ExceptionTest e t = new ExceptionTest ( ) ; 7 try{ 8 e t. method ( ) ; 9 } 10 catch ( Exception e ){ 11 System. out. p r i n t l n ( I n s i d e catch ) ; 1 } 13 f i n a l l y { 1 System. out. p r i n t l n ( I n s i d e f i n a l l y ) ; 15 } 16 System. out. p r i n t l n ( A f t e r f i n a l l y ) ; 17 } 18 19 private void method ( ) { 0 1 throw new RuntimeException ( Runtime Exception ) ; } 3 } What happens when trying to compile/execute the code? a. The code compiles correctly. b Exactly three messages are printed. c. The code does not compile. The method method must throws the exception. d. Exactly two messages are printed. e. The code does not compile. The compiler signals unreachable code after the finally clause. 6

Legi-Nr.:... 6. Consider the following class: import java. u t i l. ArrayList ; 3 import java. u t i l. C o l l e c t i o n ; 5 public class GenericsTest { 6 7 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 8 // ArrayList<Creature> c r i t A r r a y=n u l l ; 9 // ArrayList <?> c r i t A r r a y=n u l l ; 10 ArrayList <Goblin> goblinarray = 11 new ArrayList <Goblin >(); 1 // g o blinarray. add ( new Goblin ( ) ) ; 13 // c r i t A r r a y = g o blinarray ; 1 System. out. p r i n t ( c r i t A r r a y. get ( 0 ). t o S t r i n g ( ) ) ; 15 } 16 } Which line(s) would you uncomment to compile and execute the following code without raising exceptions? a. Line 8. b Line 9. c. Line 1. d. Line 13. 7

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam Exceptions (10 Points) Discuss advantages and disadvantages of checked and unchecked exceptions, with particular respect to data exceptions.......................................................................... 8

Legi-Nr.:... 3 Threads (13 Points) Suppose we want to simulate a simple connection pool which can handle a maximum of 10 connections. We have a connection factory that produces connections, and a client that consumes them. Because we are writing a simulation, you don t have to create any real connection object; instead it is sufficient to only keep track of the number of currently active connections making use of the attribute and constants in class ConnectionPool. The situations in which the maximum or minimum number of available connections is reached should be handled by letting the connection factory or the client wait alternatively, rather than by raising an exception. Provide the missing code for the following classes, filling the dots where indicated. Methods or attributes may be missing. The elaboration will start from the ConnectionPoolTest class. There may be more dotted lines then needed. 3 public class ConnectionPoolTest { 5 private ConnectionFactory c o n n e c t i o n F a c t o r y ; 6 private C l i e n t c l i e n t ; 7 8 public s t a t i c void main ( S t r i n g [ ] a r g s ) 9 { 10 ConnectionPoolTest cpt = new ConnectionPoolTest ( ) ; 11 cpt. s t a r t ( ) ; 1 System. out. p r i n t l n ( End main thread ) ; 13 } 1 15 private void s t a r t ( ) { 16 ConnectionPool c o n n e c t i o n P o o l= new ConnectionPool ( ) ; 17 c o n n e c t i o n F a c t o r y = 18 new ConnectionFactory ( c o n n e c t i o n P o o l ) ; 19 c l i e n t = new C l i e n t ( c o n n e c t i o n P o o l ) ; 0 1 // c r e a t e and s t a r t t h e two t h r e a d s here 3 //..................................... 5 //..................................... 6 7 //..................................... 8 9 //..................................... 30 } 31 } 9

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam 3 public class ConnectionFactory implements Runnable { 5 private ConnectionPool c o n n e c t i o n P o o l ; 6 7 //..................................... 8 9 //..................................... 10 11 //..................................... 1 13 //..................................... 1 15 //..................................... 16 17 //..................................... 18 19 //..................................... 0 1 //..................................... 3 //..................................... 5 //..................................... 6 7 //..................................... 8 9 //..................................... 30 31 //..................................... 3 33 3 / 35 Simulates connection r e q u e s t s 36 / 37 public void run ( ) 38 { 39 for ( int i =0; i <100; i ++) 0 { 1 c r e a t e C o n n e c t i o n ( ) ; } 3 } } 10

Legi-Nr.:... 3 public class C l i e n t implements Runnable { 5 private ConnectionPool c o n n e c t i o n P o o l ; 6 7 8 //..................................... 9 10 //..................................... 11 1 //..................................... 13 1 //..................................... 15 16 //..................................... 17 18 //..................................... 19 0 //..................................... 1 //..................................... 3 //..................................... 5 6 //..................................... 7 8 //..................................... 9 30 //..................................... 31 3 //..................................... 33 3 / 35 Simulates connection r e l e a s e s 36 / 37 public void run ( ) 38 { 39 for ( int i =0; i <100; i ++) 0 { 1 r e l e a s e C o n n e c t i o n ( ) ; } 3 } } 3 public class ConnectionPool { 5 6 private int activeconnectionscounter ; 7 private f i n a l int MAX NUMBER CONNECTIONS=10; 8 private f i n a l int MIN NUMBER CONNECTIONS=0; 9 // t h e a c t u a l r e f e r e n c e to a c o n t a i n e r o f c o n n ections 10 // can be omitted here, because i t i s not r e l e v a n t 11 // to understand t h e whole mechanism 1 13 //..................................... 1 15 //..................................... 16 11

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam 17 //..................................... 18 19 //..................................... 0 1 //..................................... 3 //..................................... 5 //..................................... 6 7 //..................................... 8 9 //..................................... 30 31 //..................................... 3 33 //..................................... 3 35 //..................................... 36 37 //..................................... 38 39 //..................................... 0 1 //..................................... 3 //..................................... 5 //..................................... 6 7 //..................................... 8 9 //..................................... 50 51 //..................................... 5 53 //..................................... 5 55 //..................................... 56 57 //..................................... 58 59 //..................................... 60 61 //..................................... 6 63 //..................................... 6 65 //..................................... 66 67 //..................................... 68 69 //..................................... 70 71 //..................................... 7 73 //..................................... 7 75 //..................................... 76 77 //..................................... 78 } 1

Legi-Nr.:... Reflection (10 Points) Suppose we are dealing with an application that draws images embedded in documents. In terms of resources, we judge too expensive to draw all the images when a document is opened for the first time, thus we want to draw them lazily (on demand), as soon as their visualization is needed. In particular, when the method draw() in class Graphics is invoked, we want to load the image first and then pass the invocation to the actual Graphics object. We believe that the Java dynamic proxy facility can be useful to solve this problem. Analyze the code of the following four classes, filling the dots where indicated. There may be more dotted lines than needed. / 3 The i n t e r f a c e a l l t h e images have to conform to 5 / 6 public interface I m a g e H a n d l i n g I n t e r f a c e { 7 8 / 9 Draws an image 10 / 11 void draw ( ) ; 1 / 13 Loads an image 1 / 15 void load ( ) ; 16 } 3 public class Graphics implements I m a g e H a n d l i n g I n t e r f a c e { 5 public void draw ( ) { 6 System. out. p r i n t l n ( Drawing image... ) ; 7 } 8 9 public void load ( ) { 10 System. out. p r i n t l n ( Loading image... ) ; 11 } 1 } 13

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam 3 import java. lang. r e f l e c t. I n v o c a t i o n H a n d l e r ; import java. lang. r e f l e c t. Method ; 5 6 public class ImageInvocationHandler implements InvocationHandler { 7 8 private I m a g e H a n d l i n g I n t e r f a c e t a r g e t ; 9 10 public ImageInvocationHandler ( Object obj ) { 11 1 // s e t s t h e t a r g e t image o b j e c t 13 //............................... 1 } 15 16 public Object invoke ( Object proxy, Method method, 17 Object [ ] a r g s ) throws Throwable { 18 19 0 //............................... 1 //............................... 3 //............................... 5 6 //............................... 7 8 //............................... 9 30 //............................... 31 3 } 33 3 } import java. lang. r e f l e c t. Proxy ; 3 public class ProxyTest { 5 6 public s t a t i c Object createproxy ( Object obj ) 7 { 8 //............................................... 9 10 //............................................... 11 1 //............................................... 13 1 //............................................... 15 } 16 17 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 18 I m a g e H a n d l i n g I n t e r f a c e image =new Graphics ( ) ; 19 I m a g e H a n d l i n g I n t e r f a c e image proxy = 0 ( I m a g e H a n d l i n g I n t e r f a c e ) createproxy ( image ) ; 1 //draw t h e image here, to t e s t t h e program 3 //............................................... } 5 } 1

Legi-Nr.:... 5 Design Patterns (1 Points) 1. Consider the following extract from an informal meeting: We have to program an application for a POS (Point Of Sale). The stakeholders tell us that that we should be taking into account that for every purchase the price of the comprised items needs to be computed according to particular details regarding the purchase and the purchaser. More precisely, a discount has to be offered in case the purchase total price is above a certain amount, or a certain number of identical items are bought. Also, there are certain categories of customers eligible for discounts. Finally, the conditions affecting the final purchase price are mutually exclusive and might change in time. Which design pattern(s) would you take into consideration to model the described situation and why? 15

ETHZ D-INFK Prof. Dr. B. Meyer Techniques of Java Programming Exam. Suppose you have to re-engineer an e-commerce application. Every time a customer registers to use the application, the application processes the customer data and assigns him/her to a certain profile, saves the data into a database, and sends a welcome email. At the moment, the implementation is centralized in class Customer, which has a method adddatatodb(), that processes the customer data, adds them to the database and sends a welcome email, using objects of classes DBHandler, ProfileHandler and WelcomeEmail. Which design pattern(s) would you take into consideration to refactor the described situation and why? 16