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

Java Object Oriented Design. CSC207 Fall 2014

Practice for Chapter 11

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

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

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

Implements vs. Extends When Defining a Class

Programming Exercise 14: Inheritance and Polymorphism

C08: Inheritance and Polymorphism

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

What is Inheritance?

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

Midterm Exam CS 251, Intermediate Programming March 6, 2015

15CS45 : OBJECT ORIENTED CONCEPTS

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

Java for Non Majors Spring 2018

CS-202 Introduction to Object Oriented Programming

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

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

C08: Inheritance and Polymorphism

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

Full file at Chapter 2 - Inheritance and Exception Handling

Introduction to Programming Using Java (98-388)

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

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

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

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

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

COP 3330 Final Exam Review

Questions Answer Key Questions Answer Key Questions Answer Key

CMSC 331 Second Midterm Exam

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

C++ Important Questions with Answers

1.00 Lecture 13. Inheritance

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

Declarations and Access Control SCJP tips

Overriding Variables: Shadowing

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

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

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

CS 251 Intermediate Programming Inheritance

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

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

Index COPYRIGHTED MATERIAL

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

CLASS DESIGN. Objectives MODULE 4

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

INHERITANCE. Spring 2019

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

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

Class, Variable, Constructor, Object, Method Questions

JAVA Programming Language Homework I - OO concept

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

C++ Inheritance and Encapsulation

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

JAVA OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

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)

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

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

Fundamentals of Object Oriented Programming

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

Java Classes, Inheritance, and Interfaces

Distributed Systems Recitation 1. Tamim Jabban

QUESTIONS FOR AVERAGE BLOOMERS

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

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

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

JAVA MOCK TEST JAVA MOCK TEST II

Selected Java Topics

Questions Answer Key Questions Answer Key Questions Answer Key

Week 5-1: ADT Design

Polymorphism and Interfaces. CGS 3416 Spring 2018

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

INSTRUCTIONS TO CANDIDATES

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

COMP 524 Spring 2018 Midterm Thursday, March 1

CMSC 132: Object-Oriented Programming II

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

Inheritance and Polymorphism

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Distributed Systems Recitation 1. Tamim Jabban

Outline. Purpose. Useful Definitions. Pattern Overview

CS1150 Principles of Computer Science Objects and Classes

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 12

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

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

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 12

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) Page 6 of 12

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) b) foo(2) c) foo(3) d) foo(4) Page 7 of 12

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!"); Page 8 of 12

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 12

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 12

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 12

Page 12 of 12