COMP 401 Spring 2013 Midterm 2

Similar documents
COMP 401 Spring 2013 Midterm 2

COMP 401 Fall 2014 Midterm 2

COMP 401 Fall 2014 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

C08: Inheritance and Polymorphism

C08: Inheritance and Polymorphism

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Java Object Oriented Design. CSC207 Fall 2014

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

Practice for Chapter 11

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

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

Implements vs. Extends When Defining a Class

Week 5-1: ADT Design

What is Inheritance?

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Programming Exercise 14: Inheritance and Polymorphism

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

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

15CS45 : OBJECT ORIENTED CONCEPTS

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

Introduction to Programming Using Java (98-388)

CMSC 132: Object-Oriented Programming II. Inheritance

Java for Non Majors Spring 2018

CS-202 Introduction to Object Oriented Programming

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

Inheritance and Polymorphism

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

Full file at Chapter 2 - Inheritance and Exception Handling

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

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

C++ Important Questions with Answers

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

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

Questions Answer Key Questions Answer Key Questions Answer Key

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

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

COP 3330 Final Exam Review

Java Fundamentals (II)

Declarations and Access Control SCJP tips

CMSC 331 Second Midterm Exam

CS 251 Intermediate Programming Inheritance

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

COE318 Lecture Notes Week 8 (Oct 24, 2011)

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Class, Variable, Constructor, Object, Method Questions

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

Software and Programming 1

More on Inheritance. Interfaces & Abstract Classes

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CLASS DESIGN. Objectives MODULE 4

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

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)

Index COPYRIGHTED MATERIAL

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

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

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

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

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

QUESTIONS FOR AVERAGE BLOOMERS

Classes and Inheritance Extending Classes, Chapter 5.2

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

Aggregation and Composition. [notes Chapter 4]

JAVA OBJECT-ORIENTED PROGRAMMING

Informatik II (D-ITET) Tutorial 6

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Distributed Systems Recitation 1. Tamim Jabban

Polymorphism. Arizona State University 1

1.00 Lecture 13. Inheritance

Abstract Classes and Interfaces

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Java Classes, Inheritance, and Interfaces

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

CH. 2 OBJECT-ORIENTED PROGRAMMING

CMSC 132: Object-Oriented Programming II

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

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

Questions Answer Key Questions Answer Key Questions Answer Key

Distributed Systems Recitation 1. Tamim Jabban

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

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

Overriding Variables: Shadowing

ENCAPSULATION AND POLYMORPHISM

CS1150 Principles of Computer Science Objects and Classes

Inheritance (Outsource: )

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

Transcription:

COMP 401 Spring 2013 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 13

Part I: True/False (2 points per question, 40 points total) T A class that implements a subinterface must also implement the parent interface. T A subinterface can extend more than one parent interface. F If a class implements an interface, you must define subinterfaces for any subclasses of the class. F A parent class has direct access to protected fields within a subclass. T Defining a method in a subclass with the same signature as a method in the parent class is known as overriding. T Overloading is an example of polymorphism. F A subclass inherits only private instance variables from the parent class. T A method defined by a subclass is able to invoke a protected method defined in the parent class. T A call to super can be invoked only as the first statement of a subclass constructor. T If a class contains an abstract method, it must be an abstract class. F Casting a reference to an object from a subclass type to a parent class type is an example of contravariance. Page 3 of 13

T Composition and Aggregation are design techniques for defining objects as an encapsulation of other objects. F Component objects within an aggregation are not typically useful outside of the aggregation. T Encapsulated objects of an aggregation are usually provided as parameters to the constructor. T Composition objects generally do not provide getters and setters for encapsulated objects. F The Factory design pattern employs delegation. F The Factory design pattern is useful when a class needs to provide a number of polymorphic public constructors. T Dynamic subclass binding is an example of the Factory design pattern. F An exception that is a subclass of RuntimeException is subject to the catch or specify requirement. F An abstract class can be instantiated directly. Page 4 of 13

Part II: Is-A Relationships (10 points total) Suppose the following interfaces and classes are declared (the actual definitions are unimportant) public interface InterA public interface InterB extends InterA public interface InterC extends InterA public interface InterD extends InterC public interface InterE public interface InterF extends InterE public class A implements InterA public class B implements InterB public class C extends A implements InterF public class D extends B implements InterD public class E extends A implements InterE public class F implements InterF 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(); F f1 = new F(); 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). InterA ia1 = (InterA) d1; InterB ib1 = (InterB) e1; InterC ic1 = (InterC) c1; InterD id1 = (InterD) b1; InterE ie1 = (InterE) c1; InterF if1 = (InterF) f1; A a2 = (A) b1; B b2 = (B) d1; C c2 = (C) f1; F f2 = (F) e1; Page 5 of 13

Part III: Evaluating Code (2 points for each part a-e, 10 points total) Given the following class definitions: public class Foo { public int m(int x) { return x*2; public class Bar extends Foo { public double m(double x) { return x*4.0; public class Doh extends Bar { public int m(int y) { return y + super.m(y); Suppose the following variables are defined: Foo f = new Foo(); Bar b = new Bar(); Doh d = new Doh(); What is the value of the following expressions: a) f.m(5) b) b.m(5) c) b.m(1.5) d) d.m(2.0) e) d.m (2) 10 10 6.0 8.0 6 Page 6 of 13

Part IV: Exceptions (2 points for each part a-d, 4 points for part e, 10 points total) Given the following code: class ExceptionA extends RuntimeException { class ExceptionB extends ExceptionA { class ExceptionC extends RuntimeException { public int bar(int x) { if (x == 1) { throw new ExceptionA(); if (x == 2) { throw new ExceptionB(); if (x == 3) { throw new ExceptionC(); return 2*x; public int foo(int y) { int x = 0; try { x += bar(y); x += 4; catch (ExceptionB e) { x += 5; catch (ExceptionA e) { x += 6; catch (RuntimeException e) { x += 7; finally { x += 8; return x; What is the value of the following expressions: a) foo(1) 14 b) foo(2) 13 c) foo(3) 15 d) foo(4) 20 Page 7 of 13

e) What will the following program print? class ExceptionA extends Exception { class ExceptionB extends ExceptionA { public class Test { public static void main (String[] args) { boolean exit = false; try { throw new ExceptionB(); catch (ExceptionA e) { System.out.println("Go Heels!"); exit =!exit; catch (Exception e) { System.out.println("Beat Duke!"); exit =!exit; finally { if (exit) { return; System.out.println("COMP 401 rocks!"); Go Heels! Page 8 of 13

Part V: Inheritance (20 points) Refactor the following classes Undergrad, GradStudent, and Professor employing inheritance as much as possible by making them all subclasses from a common parent class called Person. Write your code on page 12. public class Undergrad { private String name; private int PID; private String major; public Undergrad(String name, int PID, String major) { this.name = name; this.pid = PID; this.major = major; public String getname() { return name; public int getpid() { return PID; public String getmajor() { return major; public String getgreeting() { return "What's up?"; Page 9 of 13

public class GradStudent { private String name; private int PID; private boolean phd_intending; public GradStudent(String name, int PID, boolean phd_intending) { this.name = name; this.pid = PID; this.phd_intending = phd_intending; public String getname() { return name; public int getpid() { return PID; public boolean isphdintending() { return phd_intending; public String getgreeting() { return "Hello"; Page 10 of 13

public class Professor { private String name; private int PID; private String rank; public GradStudent(String name, int PID, String rank) { this.name = name; this.pid = PID; this.rank = rank; public String getname() { return name; public int getpid() { return PID; public String getrank() { return rank; public String getgreeting() { return "Live long and prosper"; Page 11 of 13

MODEL ANSWER: public abstract class Person { private String name; private int PID; public Person(String name, int PID) { this.name = name; this.pid = PID; public String getname() { return name; public int PID() { return PID; abstract public String getgreeting(); public class Undergrad extends Person { private String major; public Undergrad(String name, int PID, String major) { super(name, PID); this.major = major; public String getmajor() { return major; public String getgreeting() { return "What's up?"; public class GradStudent extends Person { private boolean phd_intending; public GradStudent(String name, int PID, boolean phd_intending) { super(name, PID); this.phd_intending = phd_intending; Page 12 of 13

public boolean isphdintending() { return phd_intending; public String getgreeting() { return "Hello"; public class Professor extends Person { private String rank; public Professor(String name, int PID, String rank) { super(name, PID); this.rank = rank; public String getrank() { return rank; public String getgreeting() { return "Live long and prosper"; Page 13 of 13