CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Similar documents
CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Binghamton University. CS-140 Fall Dynamic Types

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Inheritance, and Polymorphism.

CS-202 Introduction to Object Oriented Programming

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

Rules and syntax for inheritance. The boring stuff

First Name: AITI 2004: Exam 2 July 19, 2004

Chapter 14 Abstract Classes and Interfaces

Binghamton University. CS-140 Fall Chapter 9. Inheritance

What is Inheritance?

ITI Introduction to Computing II

Making New instances of Classes

ITI Introduction to Computing II

Class, Variable, Constructor, Object, Method Questions

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Java Object Oriented Design. CSC207 Fall 2014

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CS 251 Intermediate Programming Inheritance

C++ Important Questions with Answers

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

First Name: AITI 2004: Exam 2 July 19, 2004

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Framework Fundamentals

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

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

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

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

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

We are on the GUI fast track path

CS1150 Principles of Computer Science Objects and Classes

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Polymorphism CSCI 201 Principles of Software Development

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)

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Chapter 6: Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

The class Object. Lecture CS1122 Summer 2008

INHERITANCE. Spring 2019

Lecture 5: Inheritance

Advanced Placement Computer Science. Inheritance and Polymorphism

JAVA Programming Language Homework I - OO concept

CLASS DESIGN. Objectives MODULE 4

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Inheritance Motivation

Inheritance. The Java Platform Class Hierarchy

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

1 Shyam sir JAVA Notes

Abstract Classes and Interfaces

CS 11 java track: lecture 3

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

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

Programming: You will have 6 files all need to be located in the dir. named PA4:

Topic 7: Algebraic Data Types

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Give one example where you might wish to use a three dimensional array

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Programming Languages and Techniques (CIS120)

Chapter 9 Inheritance

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

Java Magistère BFA

Inheritance and Polymorphism

Practice for Chapter 11

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

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

Inheritance (continued) Inheritance

Index COPYRIGHTED MATERIAL

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CMSC 132: Object-Oriented Programming II

Subclassing for ADTs Implementation

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

CS 180 Final Exam Review 12/(11, 12)/08

CMSC 132: Object-Oriented Programming II

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS Week 13. Jim Williams, PhD

Handout 9 OO Inheritance.

CS Programming I: Inheritance

Lecture Notes Chapter #9_b Inheritance & Polymorphism

EECS 1001 and EECS 1030M, lab 01 conflict

Abstract classes are used to define a class that will be used only to build new classes. No objects will ever be instantiated from an abstract class.

Chapter 9 - Object-Oriented Programming: Polymorphism

Computer Science 210: Data Structures

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Transcription:

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If a child overrides its parent s method, it can still invoke the parent method with the super keyword, but if it does so, then it must invoke the parent method first. A parent class method can be invoked anywhere from the child method. It s only the constructor that has special requirements. (b) T F : A child class can modify the private fields of the class that it extends. A child can only modify public or protected fields - not private fields. (c) T F : The compiler will not compile a concrete class which implements an interface, but fails to implement all the methods in the interface. (d) T F : If you learn how to use the methods in an interface, you know something about how to use a class which implements that interface. (e) T F : We can think of a child object as having a virtual parent object inside the child object. In memory, a child object contains both the child fields and the parent fields. (f) T F : A protected field cannot be accessed by other classes in the same package unless the method accessing the field is in a child class of class that contains the field. A protected field is available either in a child class, or in another class in the same package. (g) T F : A GUI contains code that manipulates a rectangular array of pixels, where each pixel is represented by three color values. (h) T F : It is possible for instructions in the child constructor to run before the parent constructor for an extended object. Java requires that the parent constructor run first. The compiler enforces this restriction by calling any violation an error. (i) T F : Every object in Java refers to a (classname).class object which has static methods, data, and information about the class. (j) T F : An abstract class is used to prevent a child class from overriding specific methods (such as a checkpassword method.) The final keyword prevents a child class from overriding a method. 2. (10 points) Check the best answer to fill in the blank in the following statements: (a) In Java, the new keyword indicates that Java should invoke a constructor for an object. method constructor destructor builder (b) An interface is a standardized set of methods. variables classes constructors methods (c) If a class does not support the Comparable intreace, arrays of instances of that class can still be sorted using a Comparator. stream Lambda Expression static method Comparator Page 1 of 8

(d) Polymorphism is the concept that the same Java instruction may invoke different methods, depending on the data used in the instruction. Polymorphism Dynamic Dispatch Chaos Sub-typing (e) All displayable classes in Swing are derived from the JComponent class. JLabel JComponent JPanel JContainer (f) The instanceof operator allows programmers to check to see if a down-cast is valid, and avoid a run-time error. multiply issubclass getclass isntanceof (g) If O(f(x)) = g(x), then, as long as x > x 0, f(x) < c g(x). f(x) == c g(x) f(x) < c g(x) f(x) > c g(x) f(x)! = c g(x) (h) A public method may be accessed from anywhere, a private method can only be accessed from within its own class. static protected private package protected Page 2 of 8

3. (20 points) Given the following Java classes: public abstract class Shape { private int n ; // Number o f v e r t i c e s public Shape ( int n ) { this. n = n ; public int getn ( ) { return n ; public abstract double getarea ( ) ; public S t r i n g t o S t r i n g ( ) { return g e t C l a s s ( ). getsimplename ( ) + [ + n + v e r t i c e s area= + getarea ( ) + ] ; public class T r i a n g l e extends Shape { private double base ; private double h e i g h t ; public T r i a n g l e ( double base, double height ) { super ( 3 ) ; this. base = base ; this. h e i g h t = h e i g h t ; public double getarea ( ) { return ( base height ) / 2 ; public class Rectangle extends Shape { private double width ; private double h e i g h t ; public Rectangle ( double width, double height ) { super ( 4 ) ; this. width = width ; this. h e i g h t = h e i g h t ; public double getarea ( ) { return width height ; import java. u t i l. Arrays ; public class ShapeDriver { public static void main ( S t r i n g [ ] args ) { Shape [ ] shapes = { new T riangle ( 3. 0, 4. 0 ), new Rectangle ( 3. 3, 4. 7 ), new Rectangle ( 1. 0, 2. 0 ) ; System. out. p r i n t l n ( Arrays. t o S t r i n g ( shapes ) ) ; Page 3 of 8

(a) Assuming the state just before the System.out.println statement in the Shapedriver main method, complete the following graphical display of memory: (b) What would get printed if you ran java -cp. test2.shapedriver? [Triangle[ 3 vertices area=6.0 ], Rectangle[ 4 vertices area=15.51 ], Rectangle[ 4 vertices area=2.0 ]] (c) The Shapes class tostring method invokes the getarea method, eve though there is no code for getarea in the Shapes class. Explain why the Java compiler allows the invocation of a method which is not defined, and why at run time, there will always be getarea method available. Solution: Since getarea is an abstract method, it forces the Shape class to be abstract, and therefore, no object of class Shape can be instantiated. A concrete child of Shape must implement getarea to be concrete, and a class must be concrete before it can be instantiated. Therefore any instantiated object which is a sub-type of Shape must have an implemented getarea method. Page 4 of 8

4. (20 points) What output is produced by the main method in the Mapper class below: import java. u t i l. Arrays ; import java. u t i l. f u n c t i o n. Function ; public class Mapper { public double [ ] map( double [ ] rawdata, Function<Double, Double> f n ) { double [ ] r e s u l t=null ; i f ( rawdata!= null ) { r e s u l t = new double [ rawdata. length ] ; for ( int i =0; i <rawdata. length ; i++) { r e s u l t [ i ]= fn. apply ( rawdata [ i ] ) ; return r e s u l t ; public static void main ( S t r i n g [ ] args ) { Mapper f o r a l l = new Mapper ( ) ; double [ ] data = { 1. 5, 2. 5, 3. 5, 4. 5 ; double [ ] big = f o r a l l. map( data, d >{ i f (d>3.0) return d ; else return 3. 0 ; ) ; double [ ] l i t t l e = f o r a l l. map( data, d >(d<=3.0)?d : 3. 0 ) ; System. out. p r i n t l n ( Big : + Arrays. t o S t r i n g ( big ) ) ; System. out. p r i n t l n ( L i t t l e : + Arrays. t o S t r i n g ( l i t t l e ) ) ; Solution: Big : [3.0, 3.0, 3.5, 4.5] Little: [1.5, 2.5, 3.0, 3.0] Page 5 of 8

5. (20 points) What will get printed by the main method in the following Java code? public class LinkedList { private int value ; private LinkedList next ; public LinkedList ( int value ) { this. value = value ; public LinkedList add ( int value ) { i f ( next==null ) next=new LinkedList ( value ) ; else next. add ( value ) ; return this ; public int get ( int n ) { i f ( n==0) return value ; i f ( next==null ) throw new IllegalArgumentException ( I l l e g a l index ) ; return next. get (n 1); public LinkedList r e v e r s e ( ) { i f ( next==null ) return this ; return next. r e v e r s e ( ). add ( value ) ; public S t r i n g t o S t r i n g ( ) { S t r i n g r e s u l t= + value ; i f ( next!= null ) r e s u l t = r e s u l t + > + next. t o S t r i n g ( ) ; return r e s u l t ; public static void main ( S t r i n g [ ] args ) { LinkedList l s t = new LinkedList ( 2 1 ) ; l s t. add ( 3 3 ) ; l s t. add ( 1 9 ) ; l s t. add ( 4 4 ) ; l s t. add ( 1 9 ) ; l s t. add ( 1 9 ) ; System. out. p r i n t l n ( The t h i r d element i s + l s t. get ( 2 ) ) ; System. out. p r i n t l n ( L i s t : + l s t ) ; l s t=l s t. r e v e r s e ( ) ; System. out. p r i n t l n ( The t h i r d element i s now + l s t. get ( 2 ) ) ; System. out. p r i n t l n ( L i s t : + l s t ) ; Solution: The third element is 19 List: 21 ->33 ->19 ->44 ->19 ->19 The third element is now 44 List: 19 ->19 ->44 ->19 ->33 ->21 Page 6 of 8

6. (20 points) Given the following Java classes: public class Base { public void m1( ) { System. out. p r i n t l n ( Base.m1 ) ; private void m2( ) { System. out. p r i n t l n ( Base.m2 ) ; public void m3( ) { m1 ( ) ; m2 ( ) ; System. out. p r i n t l n ( Base.m3 ) ; public class Base { public void m1( ) { System. out. p r i n t l n ( Base.m1 ) ; private void m2( ) { System. out. p r i n t l n ( Base.m2 ) ; public void m3( ) { m1 ( ) ; m2 ( ) ; System. out. p r i n t l n ( Base.m3 ) ; public c l a s s BaseChild extends Base { public void m1( ) { System. out. p r i n t l n ( BaseChild.m1 ) ; private void m2( ) { System. out. p r i n t l n ( BaseChild.m2 ) ; public void m3( ) { m1 ( ) ; m2 ( ) ; System. out. p r i n t l n ( BaseChild.m3 ) ; System. out. p r i n t l n ( Invoking super.m3( ) from BaseChild ) ; super.m3 ( ) ; public c l a s s BaseGrandChild extends BaseChild { public void m1( ) { System. out. p r i n t l n ( BaseGrandChild.m1 ) ; private void m2( ) { System. out. p r i n t l n ( BaseGrandChild.m2 ) ; public void m3( ) { m1 ( ) ; m2 ( ) ; System. out. p r i n t l n ( BaseGrandChild.m3 ) ; System. out. p r i n t l n ( Invoking super.m3( ) from BaseGrandChild ) ; super.m3 ( ) ; public c l a s s BaseGrandChildTester { public static void main ( S t r i n g [ ] args ) { BaseGrandChild basegrandchild = new BaseGrandChild ( ) ; basegrandchild.m3 ( ) ; (a) What is the dynamic type of the implicit this parameter in the m3 method of the BaseGrandChild class when invoked from the main method in BaseGrandChildTester? Solution: BaseGrandChild (b) What is the dynamic type of the implicit this parameter in the m3 method of the BaseChild class when invoked as super.m3() from the m3 method in the BaseGrandChildTester class, which was invoked by the main method in the BaseGrandChildTester class? Page 7 of 8

Solution: BaseGrandChild (c) Does the m2 method in BaseGrandChild override the m2 method in the BaseChild class? If so, why? If not, why not? Solution: The m2 method in BaseGrandChild does not override the m2 method in the BaseChild class because the m2 method is private in BaseChild, and cannot be overriden. (d) What will get printed if you run the main method in the BaseGrandChildTester class? Solution: BaseGrandChild.m1 BaseGrandChild.m2 BaseGrandChild.m3 Invoking super.m3() from BaseGrandChild BaseGrandChild.m1 BaseChild.m2 BaseChild.m3 Invoking super.m3() from BaseChild BaseGrandChild.m1 Base.m2 Base.m3 Question: 1 2 3 4 5 6 Total Points: 10 10 20 20 20 20 100 Bonus Points: 0 0 0 0 0 0 0 Page 8 of 8