Final Exam COMP 401 Fall 2014

Similar documents
Final Exam COMP 401 Fall 2014

COMP 401 Spring 2013 Midterm 2

COMP 401 Spring 2013 Midterm 2

COMP 401 Fall 2014 Midterm 2

COMP 401 Fall 2014 Midterm 2

COMP 401 Spring 2013 Midterm 1

COMP 401 Spring 2014 Midterm 1

Java Object Oriented Design. CSC207 Fall 2014

ECE 122. Engineering Problem Solving with Java

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CIS 110: Introduction to computer programming

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CMSC 132: Object-Oriented Programming II

CS211 Prelim Oct 2001 NAME NETID

Questions Answer Key Questions Answer Key Questions Answer Key

Exam Duration: 2hrs and 30min Software Design

Chapter 5 Object-Oriented Programming

Final Exam 90 minutes Eng. Mohammed S. F. Abdual Al

15CS45 : OBJECT ORIENTED CONCEPTS

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Introduction to Programming Using Java (98-388)

Practice for Chapter 11

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CMSC 132: Object-Oriented Programming II

OVERRIDING. 7/11/2015 Budditha Hettige 82

Distributed Systems Recitation 1. Tamim Jabban

Objects and Classes Lecture 2

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

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

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

Inheritance, part 2: Subclassing. COMP 401, Spring 2015 Lecture 8 2/3/2015

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

5/23/2015. Core Java Syllabus. VikRam ShaRma

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

C08: Inheritance and Polymorphism

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

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

Java SE 7 Programming

Java SE7 Fundamentals

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

Questions Answer Key Questions Answer Key Questions Answer Key

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Java SE 7 Programming

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Distributed Systems Recitation 1. Tamim Jabban

CS-202 Introduction to Object Oriented Programming

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

A final method is a method which cannot be overridden by subclasses. A class that is declared final cannot be inherited.

More on Inheritance. Interfaces & Abstract Classes

Subtype Polymorphism

Software and Programming 1

CSE wi Final Exam 3/12/18 Sample Solution

Interview Questions I received in 2017 and 2018

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

CSE wi Final Exam 3/12/18. Name UW ID#

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

What is Inheritance?

CSE 70 Final Exam Fall 2009

CS 116 Week 8 Page 1

Inheritance and Polymorphism

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

CS1004: Intro to CS in Java, Spring 2005

Chapter 4: Writing Classes

Index COPYRIGHTED MATERIAL

CMSC 132: Object-Oriented Programming II. Inheritance

C++ Important Questions with Answers

Inheritance. Chapter 7. Chapter 7 1

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

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

Programming in C# Inheritance and Polymorphism

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Java SE 8 Programming

Exercise: Singleton 1

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

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

JAVA MOCK TEST JAVA MOCK TEST IV

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

CS Programming I: Inheritance

CLASSES AND OBJECTS IN JAVA

Object-Oriented Programming

Week 5-1: ADT Design

University of Massachusetts Amherst, Electrical and Computer Engineering

Introduction to Inheritance

Static, Final & Memory Management

Transcription:

Final Exam COMP 401 Fall 2014 I have not received nor given any unauthorized assistance in completing this exam. Signature: Name: PID: The exam has 4 parts for a total of 135 points.

This page intentionally left blank. Page 2 of 18

Part I: Multiple choice (75 points total, 5 points per question) Directions: For each multiple choice question below, indicate all correct answers among the choices provided. More than one answer may be correct and at least one answer will be correct. Each question is worth 5 points. Selecting all of the choices or selecting none of the choices will result in no points awarded. 1. Which of these statements about Java code organization and syntax are true? a) A block of statements can be used anywhere an expression is needed. * b) A statement ends in a semicolon. * c) Classes organized into a package are visible to each other without an import statement. * d) A Java source file can only contain one public class or interface. e) Local variables declared within a loop are accessible outside of the loop body. 2. Which of these statements about Java arrays are true? a) Arrays are indexed starting with 1. * b) Arrays are objects. c) An array can hold elements of different types. * d) An array can not be resized once created. e) An array may not be passed to a method as a parameter. 3. Which of these statements about encapsulation are true? * a) Using private instance fields is related to the principle of encapsulation. b) An object with one or more setters is immutable. c) A getter must be associated with a specific instance field. * d) An advantage of setters is the ability to validate the new value of a property. e) Proper encapsulation requires all methods to be marked public. 4. Which of these statements about polymorphism are true? * a) Overloaded methods are an example of polymorphism. b) Using the JavaBeans conventions for getters and setters is an example of polymorphism. c) Polymorphism requires that at most one class will implement a particular interface. d) Using the same local variable name in different methods is an example of polymorphism. * e) The "is- a" relationship between a class and the interfaces that the class implements is an example of polymorphism. Page 3 of 18

5. Which of these statements about subclassing are true? a) A subclass can override private methods in its parent class. * b) A subclass may only have one parent class. c) Two subclasses of the same parent class have an is- a relationship with each other. d) A subclass has direct access to private fields in its parent class. e) A subclass must redeclare any protected fields declared in its parent class. 6. Which of these statements about interfaces and subinterfaces are true? * a) An enumeration can be declared within an interface. * b) A subinterface may have multiple parent interfaces. c) An interface may include one or more private methods. * d) An extended interface adds one or more additional methods to those declared by its parent. * e) A public interface should be defined in a Java source file with the same name. 7. Which of these statements about overriding methods are true? a) Every method of a parent class should be overridden by a method in a subclass. b) An overridden method is not able to call the version of the method as defined in the parent class. * c) An overridden method in a subclass has the same method signature as a method in the parent class. * d) Overridden methods are virtual by definition in Java. * e) A class can simultaneously overload and override the same method. 8. Which of these statements about overloading methods are true? a) An overloaded method may not call (i.e., invoke) a different version of the overloaded method. * b) An overloaded method is one that is defined more than once within a class with a unique method signature associated with each definition. * c) A subclass can overload a method defined in its parent class. * d) All versions of an overloaded method must have the same access modifier (e.g., public, protected, private). e) All versions of an overloaded method must have the same return type. Page 4 of 18

9. Which of these statements about the iterator design pattern are true? * a) A for- each loop can be used with any collection that implements the java.util.iterable interface. b) An existing iterator must iterate over the entire collection before another iterator can be created for that collection. * c) The iterator design pattern provides an abstraction for iterating over a collection without needing to understand how the collection is maintained. * d) An iterator can be passed to a method as a parameter. e) An iterator must be a subclass of the iterable collection. 10. Which of these statements about the factory design pattern are true? a) The factory design pattern uses delegation. * b) A class using the factory design pattern prevents direct instantiation of objects. c) The factory method in the factory design pattern is usually an instance method. * d) The factory design pattern can be used for dynamic subclass binding. * e) The factory design pattern can be used to create a singleton. 11. Which of these statements about the decorator design pattern are true? * a) The decorator design pattern extends the functionality of an obejct without subclassing. b) A class that implements a decorated interface will not also implement the original interface. * c) The decorator design pattern uses delegation. * d) The decorator design pattern relies on subinterfacing. e) The constructor of a class implementing a decorated interface will not generally need any parameters. 12. Which of these statements about the MVC design pattern are true? a) The model component is responsible for invoking controller methods when the user iteracts with the application. * b) The view component can be an observer of state changes within the model component. * c) The MVC design pattern is an application- level pattern that separates the concerns of maintaining application state, rendering a current subset of the application state, and implementing application- level logic. d) The controller component is usually a subclass of the view component. * e) MVC is often used for interactive applications with user interfaces. Page 5 of 18

13. Which of these statements about composition and aggregation are true? * a) Composition and aggregation are two ends of a design spectrum for modeling an object as a "whole" comprised of many "parts". b) A composition is characterized by a constructor that is provided references to its component parts as parameters. c) The components of an aggregation are not likely to be useful as independent objects or as part of other aggregations. * d) Delegation may be used within both compositions and aggregations. * e) An aggregation is more likely to provide getters and setters for its components than a composition. 14. Which of these statements about multithreaded programming are true? a) All objects of the same class share a single lock. b) Two different threads may be simultaneously executing within different methods of an object that are marked as synchronized. * c) A synchronized method may call a different synchronized method of the same object because it has already obtained the object lock. d) A thread can only obtain one object lock at a time. * e) Multithreaded programming is the key to building software that can take advantage of advances in modern hardware. 15. Which of these statements about exceptions are true? a) As a general principle, one should catch exceptions are early as possible. * b) An exception is an event that occurs during the execution of a program that signals a disruption in the normal flow of the program. c) Every exception is subject to Java's catch- or- specify rule. * d) It is possible to nest a try- catch block within a catch block. * e) The finally block will always execute even if an exception is not thrown. Page 6 of 18

Part II: Evaluating and Understanding Code 20 points total (5 points each for subparts a and b, 10 points each for subpart c). a) Given the following definition of class Foo: public class Foo { public int x; public int y; public Foo(int a) { x = a; y = 1; public int bar(int a) { x += a; y += 1; return (x+y); public void bar(foo f) { if (f.x < x) { f.bar(x); else { bar(f.x); Fill in the table to the right, providing the value of the expressions f1.x, f1.y, f2.x, and f2.y after each of the lines 3 through 7 of the following code snippet is executed (note, line numbers provided for reference and are NOT part of the code). 1: Foo f1 = new Foo(1); 2: Foo f2 = new Foo(2); 3: f1.bar(3); 4: f2.bar(4); 5: f1.bar(f2.bar(5)); 6: f2.bar(f1); 7: f1.bar(new Foo(6)); Line # f1.x f1.y f2.x f2.y 3 4 2 2 1 4 4 2 6 2 5 18 3 11 3 6 18 3 29 4 7 18 3 29 4 Page 7 of 18

b) Given the following definition of the class Foo: public class Foo { private int[] vals; public Foo(int[] vals) { this.vals = vals; public int[] getvals() { return vals; public int getindex(int i) { return vals[i]; public void addothervals(int[] other_vals) { for (int i=0; i<vals.length; i++) { vals[i] += other_vals[i]; public void swapbiggervals(int[] other_vals) { for (int i=0; i<vals.length; i++) { if (vals[i] > other_vals[i]) { int tmp = other_vals[i]; other_vals[i] = vals[i]; vals[i] = tmp; What are the values of the variables r1, r2, r3, r4, and r5 after the following code executes: Foo f1 = new Foo(new int[] {1, 4, 5); Foo f2 = new Foo(new int[] {2, 3, 6); f1.swapbiggervals(f2.getvals()); f2.addothervals(f1.getvals()); f2.swapbiggervals(f1.getvals()); int r1 = f1.getindex(0); int r2 = f1.getindex(1); int r3 = f1.getindex(2); int r4 = f2.getindex(1); int r5 = f2.getindex(2); Answers: r1 = 3 r2 = 7 r3 = 11 r4 = 3 r5 = 5 Page 8 of 18

c) Given the following class and interface definitions (actual bodies of these definitions are not important and are represented by ellipses as a placeholder): public interface InterA extends InterB {... public interface InterB {... public interface InterC extends InterA, InterD {... public interface InterD {... public class A implements InterA {... public class B implements InterB {... public class C extends B implements InterC {... public class D extends B implements InterD {... public class E extends D implements InterA {... Suppose the following variables are defined: A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D(); E e1 = new E(); Indicate whether each of the following lines is legal or illegal by circling the appropriate word in the comment that trails each line. InterA ia1 = (InterA) c1; // Legal InterB ib1 = (InterB) d1; // Legal A a2 = (A) e1; // Illegal InterC ic1 = (InterC) a1; // Illegal B b2 = (B) e1; // Legal InterC ic2 = (InterC) d1; // Illegal InterA ia2 = (InterA) c1; // Legal D d2 = (D) c1; // Illegal InterB ib2 = (InterA) ((B) c1); // Legal InterD id1 = (D)((B) e1); // Legal Page 9 of 18

Part III: Inheritance 20 points total (credited at discretion of grader) Given the following definitions for classes Student, Faculty, and Staff, reorganize the code to employ inheritance as most appropriate. Your new version of the code should include a parent class called Person. public class Student { public enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR; private String name; private int pid; private Status status; public Student(String name, int pid, Status status) { this.name = name; this.pid = pid; this.status = status; public String getname() { return name; public int getpid() { return pid; public Status getstatus() { return status; public String speak() { return Yo! ; Page 10 of 18

public class Faculty { public enum Status {ASSISTANT, ASSOCIATE, FULL; private String name; private int pid; private Status status; public Faculty(String name, int pid, Status status) { this.name = name; this.pid = pid; this.status = status; public String getname() { return name; public int getpid() { return pid; public Status getstatus() { return status; public String speak() { return Greetings! ; Page 11 of 18

public class Staff { public enum Status {PART_TIME, FULL_TIME; private String name; private int pid; private Status status; public Student(String name, int pid, Status status) { this.name = name; this.pid = pid; this.status = status; public String getname() { return name; public int getpid() { return pid; public Status getstatus() { return status; public String speak() { return Cheers! ; Page 12 of 18

// Put your part III code here. // public modifier below is optional public abstract class Person { private String name; private int pid; // protected below is optional protected Person (String name, int pid) { this.name = name; this.pid = pid; public String getname() { return name; public int getpid() { return pid; abstract public String speak(); public class Student extends Person { public enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR; private Status status; public Student(String name, int pid, Status status) { super(name, pid); this.status = status; public Status getstatus() {return status; @Override public String speak() { return Yo! ; Page 13 of 18

// Continue your part III code here if necessary. public class Faculty extends Person { public enum Status {ASSISTANT, ASSOCIATE, FULL; private Status status; public Faculty(String name, int pid, Status status) { super(name, pid); this.status = status; public Status getstatus() {return status; @Override public String speak() { return Greetings! ; public class Staff extends Person { public enum Status {PART_TIME, FULL_TIME; private Status status; public Staff(String name, int pid, Status status) { super(name, pid); this.status = status; public Status getstatus() {return status; @Override public String speak() { return Cheers! ; Page 14 of 18

Part IV: Observer/Observable 20 points (credited at discretion of grader) Consider the following classes: class DicePair { private int die1; private int die2; public DicePair() { roll(); public void roll() { // The following sets each field // to a random number from 1 to 6. die1 = (((int) (Math.random() * 6.0)) + 1); die2 = (((int) (Math.random() * 6.0)) + 1); public int getdie1() { return die1; public int getdie2() { return die2; class DoublesCounter { private int num_doubles; public DoublesCounter() { num_doubles = 0; public int getnumdoubles() { return num_doubles; public void detectdoubles(int v1, int v2) { if (v1 == v2) { num_doubles++; class SevenDetector { private int num_sevens; public SevenDetector() { num_sevens = 0; public int getnumsevens() { return num_sevens; public void detectseven(int value) { if (value == 7) { num_sevens++; Page 15 of 18

Rewrite these classes in order to apply the Observer/Observable design pattern so that DoublesCounter and SevensDetector objects can be used as observers of DicePair objects. A DicePair object should inform any observers when the roll() method is called. DoublesCounter objects should keep track of the number of times doubles is rolled by any DicePair objects observed. SevenDetector objects should keep track of the number of times the sum of the dice of any observed DicePair objects is equal to seven. You are free to create any new interfaces and/or classes you need in order to appropriately apply the Observer/Observable design pattern. Do NOT rely on java.util.observer and/or java.util.observable. You may rely on ArrayList<> as a collection class. Below is an ArrayList cheat sheet for your reference: To create an ArrayList with items of type E: ArrayList<E> alist = new ArrayList<E>(); To find the number of items in an ArrayList: int num_items = alist.size(); To add to the ArrayList: alist.add(item) To remove an item from the ArrayList: alist.remove(item); Page 16 of 18

// Continue your code for Part IV here if necessary. interface DiceObserver { observedicepair(dicepair dp); class DicePair { private int die1; private int die2; private List<DiceObserver> observers; public DicePair() { observers = new ArrayList<DiceObserver>(); roll(); public void roll() { // The following sets each field // to a random number from 1 to 6. die1 = (((int) (Math.random() * 6.0)) + 1); die2 = (((int) (Math.random() * 6.0)) + 1); notifyobservers(); public void addobserver(diceobserver o) { observers.add(o); public void removeobserver(diceobserver o) { observers.remove(o); public void notifyobservers() { for (DiceObserver o : observers) { o.observedicepair(this); public int getdie1() { return die1; public int getdie2() { return die2; Page 17 of 18

// Continue your code for Part IV here if necessary. class DoublesCounter implements DiceObserver { private int num_doubles; public DoublesCounter() { num_doubles = 0; public int getnumdoubles() { return num_doubles; public void detectdoubles(int v1, int v2) { if (v1 == v2) { num_doubles++; public void observerdicepair(dicepair dp) { detectdoubles(dp.getdie1(), dp.getdie2()); class SevenDetector implements DiceObserver { private int num_sevens; public SevenDetector() { num_sevens = 0; public int getnumsevens() { return num_sevens; public void detectseven(int value) { if (value == 7) { num_sevens++; public void observerdicepair(dicepair dp) { detectseven (dp.getdie1() + dp.getdie2()); Page 18 of 18