Introduction to Computing II (ITI 1121) Midterm Examination

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

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

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

Lecture 12: Abstract data types

Java Interview Questions and Answers

DATA STRUCTURE - STACK

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Contents. 9-1 Copyright (c) N. Afshartous

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Data Structures in the Java API

Stacks. Linear data structures

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

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

CmpSci 187: Programming with Data Structures Spring 2015

7.1 Our Current Model

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

Inside the Java Virtual Machine

AP Computer Science Java Subset

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Software Engineering Techniques

CMSC 202H. ArrayList, Multidimensional Arrays

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Introduction to Java

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

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

Introduction to Java. CS 3: Computer Programming in Java

Stack Allocation. Run-Time Data Structures. Static Structures

Object-Oriented Programming Lecture 2: Classes and Objects

Algorithms and Data Structures Written Exam Proposed SOLUTION

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Lecture J - Exceptions

The Interface Concept

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:

What is Java? Applications and Applets: Result of Sun s efforts to remedy bad software engineering practices

Software Development with UML and Java 2 SDJ I2, Spring 2010

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

Chapter 3: Restricted Structures Page 1

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Object Oriented Software Design

CompSci-61B, Data Structures Final Exam

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Introduction to Programming

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

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

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

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Semantic Analysis: Types and Type Checking

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Data Structures and Algorithms Lists

COMPUTER SCIENCE. Paper 1 (THEORY)

HW3: Programming with stacks

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

El Dorado Union High School District Educational Services

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

Fundamentals of Java Programming

Java CPD (I) Frans Coenen Department of Computer Science

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

CSE 308. Coding Conventions. Reference

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

Computer Programming I

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

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

CS 106 Introduction to Computer Science I

13 Classes & Objects with Constructors/Destructors

Tutorial on C Language Programming

Glossary of Object Oriented Terms

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

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

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

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

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

Chapter 7D The Java Virtual Machine

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

C++ INTERVIEW QUESTIONS

Description of Class Mutation Mutation Operators for Java

Java (12 Weeks) Introduction to Java Programming Language

The C Programming Language course syllabus associate level

Under the Hood: The Java Virtual Machine. Lecture 24 CS 2110 Fall 2011

Understanding class definitions

C++FA 5.1 PRACTICE MID-TERM EXAM

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

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

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

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

Rigorous Software Development CSCI-GA

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

Chapter 2: Elements of Java

Sources: On the Web: Slides will be available on:

Assignment # 2: Design Patterns and GUIs

STACKS,QUEUES, AND LINKED LISTS

Habanero Extreme Scale Software Research Project

14 Stacks, Queues, And Linked Lists

Java Program Coding Standards Programming for Information Technology

Transcription:

Introduction to Computing II (ITI 1121) Midterm Examination Instructor: Marcel Turcotte February 2011, duration: 2 hours Identification Student name: Student number: Signature: Instructions 1. 2. 3. 4. 5. 6. This is a closed book examination; No calculators or other aids are permitted; Write comments and assumptions to get partial marks; Beware, poor hand writing can affect grades; Do not remove the staple holding the examination pages together Write your answers in the space provided. Use the back of pages if necessary. You may not hand in additional pages. Marking scheme Question 1 2 3 Total Maximum 15 32 23 70 Result

February 2011 ITI 1121 Page 2 of 13 Question 1: (15 marks) For this question, n items are stored into k boxes. Given the weight of the items (weights) and their box assignment (map), in Java, write a static method that calculates the weight difference between the heaviest and the lightest box. There are n items, numbered 0, 1... n 1. There are k boxes, numbered 0, 1... k 1. The formal parameter weights designates an array of size n of integers where weights[i] is the weight of the ith item. Hence, weights[0] is the weight of item 0, weights[1] is the weight of item 1, etc. The formal parameter map designates an array of size n of integers such that map[i] is the number of the box where item i is stored. Hence, map[0] indicates the box number of item 0, map[1] indicates the box number of item 1, etc. public static int g e tweightdifference ( int [ ] weights, int [ ] map, int k ) { If the above method is declared in the class Q1, the program below would display 70. int [ ] weights = { 10, 50, 30, 10, 40, 20, 10, 10 ; int [ ] map = { 2, 0, 1, 0, 0, 1, 2, 2 ; System. out. p r i n t l n ( Q1. g etweightdifference ( weights, map, 3 ) ) ; You can assume that weights and map are not null, and the content of both arrays is valid.

February 2011 ITI 1121 Page 3 of 13 (blank space)

February 2011 ITI 1121 Page 4 of 13 Question 2: (32 marks) A tuple holds two Integer numbers (objects of the class Integer). All the tuples have a method getfirst, as well as a method getsecond, returning a reference to the first, and second, number of the tuple, respectively. A tuple has a method isincreasing that returns true if the first element is smaller than the second element, and false otherwise. AbstractTuple <<interface>> Tuple +getfirst() : Integer +getsecond() : Integer +isincreasing() : boolean -value : int Integer +intvalue() : in t Pair ArrayPair For this question there is an interface named Tuple, an abstract class named AbstractTuple, and two concrete implementations, called Pair and ArrayPair. Their complete description can be found on the next pages. An object of the class Integer has a method intvalue that returns the value of the object as an int. The execution of the statements below produces the following output: 7 is less than 17. I n t e g e r n1, n2, n3 ; n1 = new I n t e g e r ( 7 ) ; n2 = new I n t e g e r ( 17 ) ; n3 = new I n t e g e r ( 2 ) ; Tuple t1, t2 ; t1 = new Pair ( n1, n2 ) ; t2 = new ArrayPair ( n2, n3 ) ; i f ( t1. i s I n c r e a s i n g ( ) ) { System. out. p r i n t l n ( t1. g e t F i r s t ( ) + " is less than " + t1. getsecond ( ) ) ; i f ( t2. i s I n c r e a s i n g ( ) ) { System. out. p r i n t l n ( t2. g e t F i r s t ( ) + " is less than " + t2. getsecond ( ) ) ; Make sure to include the constructors and access methods that are necessary for the execution of the above statements.

February 2011 ITI 1121 Page 5 of 13 A. Implement the interface Tuple. The interface declares 3 methods. There are two access methods, named getfirst and getsecond, and both return a reference of type Integer. Finally, the interface also declares a method called isincreasing that returns a boolean value. (6 marks) B. Write the abstract class named AbstractTuple, which implements the interface Tuple. The class AbstractTuple has a concrete implementation of the method isincreasing, which returns true if the first element of the tuple is less than the second element, and false otherwise. (8 marks)

February 2011 ITI 1121 Page 6 of 13 C. Write a concrete implementation of the class AbstractTuple called Pair. The implementation has two instance variables that are references to the first and second element of this tuple. Add all the necessary constructors and access methods. (8 marks)

February 2011 ITI 1121 Page 7 of 13 D. Write a concrete implementation of the class AbstractTuple called ArrayPair. The implementation uses an array of size 2 to store references to the first and second element of this tuple. Add all the necessary constructors and access methods. (10 marks)

February 2011 ITI 1121 Page 8 of 13 Question 3: (23 marks) A. Following the guidelines presented in class, as well as the lecture notes, draw the memory diagrams for all the objects and all the local variables of the method Q3.test following the execution of the statement line = new Line( origin, new Point( 11, 21 ) ). public class Point { private int x = 0 ; private int y = 0 ; public Point ( int x, int y ) { this. x = x ; y = y ; public class Line { private Point a ; private Point b ; public Line ( Point a, Point b ) { this. a = a ; this. b = b ; public class Q3 { public static void t e s t ( ) { int zero ; Point o r i g i n ; Line l i n e ; zero = 0 ; o r i g i n = new Point ( zero, 0 ) ; l i n e = new Line ( o r i g i n, new Point ( 11, 21 ) ) ; // Here! Answer :

February 2011 ITI 1121 Page 9 of 13 B. The class DynamicArrayStack implements the interface Stack using the dynamic array technique presented in class. This allows a data structure (here a stack) to increase its physical size according to the needs of the application. For the partial implementation of the class DynamicArrayStack below, implement the method void trimtosize() that reduces the physical size of the stack to the maximum of mincapacity and its logical size. public c l a s s DynamicArrayStack<E> implements Stack<E> { // The minimum p h y s i c a l s i z e o f the s t a c k private f i n a l int mincapacity ; // A d e f a u l t v a l u e f o r the increment t h a t i s used to i n c r e a s e // the c a p a c i t y o f the s t a c k private static f i n a l int DEFAULT INCREMENT = 2 5 ; // Each time the s t a c k becomes f u l l, a l a r g e r array i s created, // increment i s t he number o f c e l l s to add to the s i z e o f the // p r e v i o u s array in order to o b t a i n the s i z e o f the new array. private f i n a l int increment ; // A r e f e r e n c e to an array t h a t h o l d s the elements o f the s t a c k private E [ ] elems ; // An i n s t a n c e v a r i a b l e t h a t keeps t r a c k o f the p o s i t i o n o f the // top element o f the s t a c k w i t h i n the array. For an empty // s t a c k the v a l u e i s 1. private int top = 1; // Implement t he method void trimtosize ( ) t h a t reduces the // p h y s i c a l s i z e o f the s t a c k to the maximum o f mincapacity and i t s // l o g i c a l s i z e. public void trimtosize ( ) {

February 2011 ITI 1121 Page 10 of 13 C. Give the result that will be printed on the standard output when the following main method is executed. public static void main ( S t r i n g [ ] args ) { Stack<Integer > s, t ; s = new DynamicArrayStack<Integer >( 100 ) ; for ( int i =1; i <5; i ++) { s. push ( new I n t e g e r ( i ) ) ; I n t e g e r x = null ; i f (! s. isempty ( ) ) { x = s. pop ( ) ; t = new DynamicArrayStack<Integer >( 100 ) ; while (! s. isempty ( ) ) { t. push ( s. pop ( ) ) ; t. push ( x ) ; while (! t. isempty ( ) ) { System. out. p r i n t ( t. pop ( ) ) ; i f (! t. isempty ( ) ) { System. out. p r i n t ( "," ) ; System. out. p r i n t l n ( ) ; Answer : D. If a subclass s constructor does not make an explicit call to a superclass s constructor: (a) a run-time error will result (b) a compile-time error will result (c) the constructor will be called anyway (d) the class will be implicitly declared as abstract (e) none of the above Answer:

February 2011 ITI 1121 Page 11 of 13 E. All Java classes are subclasses of the class. (a) String (b) java.lang (c) Java (d) Class (e) Object Answer: F. Consider the following line of code. Comparable s = new String(); Which of the following statements is true about this line? (a) It will result in a compile-time error. (b) It will result in a run-time error. (c) It will create a String object pointed to by a Comparable reference. (d) Although it is perfectly valid Java, it should be avoided due to confusion. (e) none of the above are true Answer: G. Give the result that will be printed on the standard output when the following main method is executed. public class Ticket { private int nextserialnumber = 100; private int serialnumber ; public Ticket ( ) { serialnumber = nextserialnumber ; nextserialnumber = nextserialnumber + 1 ; public int getserialnumber ( ) { return serialnumber ; public static void main ( S t r i n g [ ] args ) { Ticket t1, t2, t3 ; t1 = new Ticket ( ) ; t2 = new Ticket ( ) ; t3 = new Ticket ( ) ; System. out. p r i n t ( t1. getserialnumber ( ) + "," ) ; System. out. p r i n t ( t2. getserialnumber ( ) + "," ) ; System. out. p r i n t l n ( t3. getserialnumber ( ) ) ; Answer :

February 2011 ITI 1121 Page 12 of 13 H. Give the result that will be printed on the standard output when the following main method is executed. public class Ticket { private static int nextserialnumber = 100; private static int serialnumber ; public Ticket ( ) { serialnumber = nextserialnumber ; nextserialnumber = nextserialnumber + 1 ; public int getserialnumber ( ) { return serialnumber ; public static void main ( S t r i n g [ ] args ) { Ticket t1, t2, t3 ; t1 = new Ticket ( ) ; t2 = new Ticket ( ) ; t3 = new Ticket ( ) ; System. out. p r i n t ( t1. getserialnumber ( ) + "," ) ; System. out. p r i n t ( t2. getserialnumber ( ) + "," ) ; System. out. p r i n t l n ( t3. getserialnumber ( ) ) ; Answer : I. Give the result that will be printed on the standard output when the following main method is executed. public class Ticket { private static int nextserialnumber = 100; private int serialnumber ; public Ticket ( ) { serialnumber = nextserialnumber ; nextserialnumber = nextserialnumber + 1 ; public int getserialnumber ( ) { return serialnumber ; public static void main ( S t r i n g [ ] args ) { Ticket t1, t2, t3 ; t1 = new Ticket ( ) ; t2 = new Ticket ( ) ; t3 = new Ticket ( ) ; System. out. p r i n t ( t1. getserialnumber ( ) + "," ) ; System. out. p r i n t ( t2. getserialnumber ( ) + "," ) ; System. out. p r i n t l n ( t3. getserialnumber ( ) ) ; Answer :

February 2011 ITI 1121 Page 13 of 13 (blank space)