COMP 401 Fall 2014 Midterm 2

Similar documents
COMP 401 Fall 2014 Midterm 2

COMP 401 Spring 2013 Midterm 2

COMP 401 Spring 2013 Midterm 2

Final Exam COMP 401 Fall 2014

Final Exam COMP 401 Fall 2014

COMP 401 Spring 2013 Midterm 1

COMP 401 Spring 2014 Midterm 1

Chapter 5 Object-Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

Practice for Chapter 11

Introduction to Programming Using Java (98-388)

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

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

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

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

Full file at Chapter 2 - Inheritance and Exception Handling

Object Oriented Programming: Based on slides from Skrien Chapter 2

Index COPYRIGHTED MATERIAL

15CS45 : OBJECT ORIENTED CONCEPTS

Object Oriented Programming. Java-Lecture 11 Polymorphism

Fundamentals of Object Oriented Programming

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

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

Polymorphism. return a.doublevalue() + b.doublevalue();

CS-202 Introduction to Object Oriented Programming

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

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

OVERRIDING. 7/11/2015 Budditha Hettige 82

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Inheritance -- Introduction

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

CMSC 331 Second Midterm Exam

Principles of Object Oriented Programming. Lecture 4

Questions Answer Key Questions Answer Key Questions Answer Key

CSE 70 Final Exam Fall 2009

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS100J Prelim I, 29 Sept. 2003

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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?

What is Inheritance?

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Declarations and Access Control SCJP tips

Programming II (CS300)

CLASS DESIGN. Objectives MODULE 4

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Lecture 2: Java & Javadoc

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

CO Java SE 8: Fundamentals

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CS 251 Intermediate Programming Inheritance

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

INSTRUCTIONS TO CANDIDATES

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

JAVA MOCK TEST JAVA MOCK TEST III

Assigned Date: August 27, 2014 Due Date: September 7, 2015, 11:59 PM

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Chapter 10 Classes Continued. Fundamentals of Java

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

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

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Object-Oriented Programming

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

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

Polymorphism and Interfaces. CGS 3416 Spring 2018

Abstract Classes and Interfaces

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

CMSC 132: Object-Oriented Programming II

Logistics. Final Exam on Friday at 3pm in CHEM 102

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

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Java Classes, Inheritance, and Interfaces

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Chapter 14 Abstract Classes and Interfaces

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

VIRTUAL FUNCTIONS Chapter 10

Object-Oriented Programming

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Inheritance and Polymorphism

JAVA MOCK TEST JAVA MOCK TEST IV

Object-Oriented Design. March 2005 Object Oriented Design 1

Classes and Inheritance Extending Classes, Chapter 5.2

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

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

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Programming II (CS300)

ECE Fall 20l2, Second Exam

Fall Problem Set 1

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

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

C08: Inheritance and Polymorphism

Transcription:

COMP 401 Fall 2014 Midterm 2 I have not received nor given any unauthorized assistance in completing this exam. Signature: Name: PID: Please be sure to put your PID at the top of each page.

This page left intentionally blank. Do not write on it. Page 2 of 14

Part I: Multiple choice (40 points total) 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 the relationship between a subclass and its parent class is true? a) A parent class constructor is called after the subclass constructor finishes executing. b) A subclass has direct access to protected fields defined in a parent class only if it is defined in the same package. c) A subclass can change the access modifier of a parent class field by redeclaring the field. * d) A subclass never has direct access to private fields declared in the parent class. * e) Two subclasses with a common parent class have direct access to each other s public fields. 2. Which of these statements about inheritance is true? a) A subclass must declare any interfaces implemented by its parent class. * b) A subclass can inherit from only one parent class. * c) All classes inherit from Object. d) A subclass can be legally cast to any sibling subclass (i.e., a subclass that also inherits from the same parent class). e) A subclass does not inherit private fields from its parent class. 3. Which of these statements about overriding is true? a) An overriding method can be declared as non-virtual. * b) An overriding method defined in a subclass must have the same method signature as declared in the parent class. c) A subclass can only override methods declared in its immediate parent class. * d) Overriding is related to the principle of polymorphism. * e) A method can be simultaneously overridden and overloaded. 4. Which of these statements about Composition and Aggregation are true? * a) Providing encapsulated objects as parameters to a constructor is a characteristic of aggregation. * b) Providing getters and setters for its encapsulated objects is a characteristic of aggregation. * c) An object can exhibit characteristics of both composition and aggregation. d) Encapsulated objects of a composition will also be compositions. e) Composition and aggregation are design techniques that rely on polymorphism. Page 3 of 14

5. Which of these statements about exceptions are true? * a) A user-defined exception that is a direct subclass of Exception is subject to the catch-or-specify policy. * b) The finally block of a try-catch-finally structure is executed if a method returns within a try block without causing an exception. c) If more than one catch block has an is-a relationship with a thrown exception, all of them will be executed. * d) A catch block handling an exception may throw a different exception while executing. * e) A try-catch-finally structure can be nested within a try block. 6. Which of these statements about the Factory design pattern are true? * a) A class employing the Factory design pattern will usually not have any public constructors. * b) A class employing the Factory design pattern will usually have at least one public static method. c) Dynamic subclass binding is not a use case for the Factory design pattern. d) The Factory design pattern relies on inheritance. * e) A class using the Factory design pattern prevents direct instantiation of new instances. 7. Which of these statements about Observer/Observable are true? a) An observable class usually provides a public static method for registering observers. * b) Java user interface components are observable. c) The Observer/Observable design pattern requires an observer class to be immutable. d) All observer classes of a particular observable class will extend a common parent class. * e) An observable object notifies any registered observers when its state changes. 8. Which of these statements about the Decorator design pattern are true? * a) The Decorator design pattern uses inheritance to add additional state or functionality to an existing interface. * b) The Decorator design pattern relies on delegation. c) A class that implements a decorated interface must be a subclass of a class that implements the original interface. * d) The constructor of a class using the Decorator design pattern requires an instance of the original interface to be provided as a parameter. e) Once a decorated instance is created, the original instance cannot be retrieved. Page 4 of 14

Part II: Is-A Relationships (10 points total) Suppose the following interfaces and classes are declared (the actual definitions are unimportant) public interface I1 public interface I2 public interface I3 extends I4 public interface I4 extends I1 public class A extends B implements I4 public class B public class C extends B implements I3 public class D extends E implements I3 public class E extends F public class F implements I2 public class G extends E implements I1, I2 Suppose the following variables are defined: A obj_a = new A(); B obj_b = new B(); C obj_c = new C(); D obj_d = new D(); E obj_e = new E(); F obj_f = new F(); G obj_g = new G(); For each of the following statements, draw a line through any of the statements that are not legal (i.e., would either not compile or cause a runtime exception). I1 ref_i1 = (I1) obj_d; I2 ref_i2 = (I2) obj_d; I3 ref_i3 = (I3) obj_a; I4 ref_i4 = (I4) obj_a; A ref_a = (A) obj_d; B ref_b = (B) obj_c; C ref_c = (C) obj_d; D ref_d = (D) obj_g; E ref_e = (E) obj_d; G ref_g = (G) obj_e; Page 5 of 14

Part III: Evaluating Code (3 points for each part a-e, 15 points total) Given the following class definitions: class Doh { public int m(string s, int i) { if (i < 20) { return m(s, i+1); else { return m(s); public int m(string s) { return 100; class Ray extends Doh { public int m (String s) { return m(s, s.length()); public int m (int i, String s) { return m(s, i); public int m (String s, int i) { if (i < 10) { return super.m(s, i); else { return i - s.length(); class Mee extends Ray { public int m (String s, int i) { return s.length() + i; public int m (int i1, int i2) { return super.m("hello", i1+i2); Page 6 of 14

Part III continued Suppose the following variables are defined: Doh d = new Doh(); Ray r = new Ray(); Mee m = new Mee(); What is the value of the following expressions: a) m.m(1, 2) 9 b) r.m( Turing ) 4 c) m.m(4, UNC ) 7 d) ((Doh) r).m( Brooks ) 4 e) d.m( Brooks ) 100 Page 7 of 14

Part IV: Exceptions (3 points for each part a-e, 15 points total) Given the following code defining four exception classes and two functions: class ExceptionA extends RuntimeException { class ExceptionB extends ExceptionA { class ExceptionC extends ExceptionA { class ExceptionD extends ExceptionB { public static int bar(int x) { switch (x) { case 1: throw new ExceptionA(); case 2: throw new ExceptionB(); case 3: throw new ExceptionC(); case 4: throw new ExceptionD(); default: return x*x; public static int foo(int y) { int x = 0; try { try { x += bar(y); catch (ExceptionD ex_d) { x = -3; catch (ExceptionC ex_c) { x += bar(y-1); catch (ExceptionA ex_a) { try { x += bar(y+3); catch (ExceptionB ex_b) { x += 3; finally { x += 3; catch (RuntimeException e) { x += 5; finally { x += 3; return x; Page 8 of 14

Part IV continued What is the value of the following expressions: a) foo(1) 9 b) foo(2) 31 c) foo(3) 8 d) foo(4) 0 e) foo(5) 28 Page 9 of 14

Part V: Inheritance (20 points) Refactor the following classes DumbPhone, SmartPhone, and SatellitePhone employing inheritance as appropriate given the following guidelines: DumbPhone and SmartPhone should be subclasses of a common parent class called CellPhone CellPhone and SatellitePhone should be subclasses of a common abstract parent class called Phone Write your code on pages 13 and 14. class DumbPhone { private String number; private String model; private Network net; public enum Network {GSM, CDMA, LTE; public DumbPhone(String number, String model, Network net) { this.number = number; this.model = model; this.net = net; public String getnumber() { return number; public int getmemorysize() { return 1; public String getmodel() { return model; public Network getnetwork() { return net; public String getname() { return getmemorysize() + "GB " + getmodel() + "(" + getnetwork().tostring() + ")"; Page 10 of 14

class SmartPhone { private String number; private String model; private int memory_size; private Network net; private String processor; public enum Network {GSM, CDMA, LTE; public SmartPhone(String number, String model, int memory_size, String processor, Network net) { this.number = number; this.model = model; this.memory_size = memory_size; this.processor = processor; this.net = net; public String getnumber() { return number; public String getmodel() { return model; public int getmemorysize() { return memory_size; public Network getnetwork() { return net; public String getname() { return getmemorysize() + "GB " + getmodel() + "(" + getnetwork().tostring() + ")"; public String getprocessor() { return processor; Page 11 of 14

class SatellitePhone { private String number; private String model; private int memory_size; public SatellitePhone(String number, String model, int memory_size) { this.number = number; this.model = model; this.memory_size = memory_size; public String getnumber() { return number; public String getmodel() { return model; public int getmemorysize() { return memory_size; public String getradioband() { return "L"; public String getname() { return "Satellite Phone"; Page 12 of 14

Model Answer: abstract class Phone { private String number; private String model; public Phone(String number, String model) { this.number = number; this.model = model; public String getnumber { return number; public String getmodel { return model; abstract public int getmemorysize(); abstract public String getname(); class SatellitePhone extends Phone { private int memory_size; public SatellitePhone(String number, String model, int memory_size) { super(number, model); this.memory_size = memory_size; public int getmemorysize() { return memory_size; public String getradioband() { return "L"; public String getname() { return "Satellite Phone"; Page 13 of 14

abstract class CellPhone extends Phone { private Network net; public enum Network {GSM, CDMA, LTE; public CellPhone(String number, String model, Network net) { super(number, model); this.net = net; public Network getnetwork() { return net; public String getname() { return getmemorysize() + "GB " + getmodel() + "(" + getnetwork().tostring() + ")"; class DumbPhone extends CellPhone { public DumbPhone(String number, String model, Network net) { super(number, model, net); public int getmemorysize() { return 1; class SmartPhone extends CellPhone { private int memory_size; private String processor; public SmartPhone(String number, String model, int memory_size, String processor, Network net) { super(number, model, net); this.memory_size = memory_size; this.processor = processor; public int getmemorysize() { return memory_size; public String getprocessor() { return processor; Page 14 of 14