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

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

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Introduction to Computing II (ITI1121) FINAL EXAMINATION

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Instance Methods and Inheritance (1/2)

Elementary Sorts 1 / 18

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

Lecture 5: Sep. 23 &25

DNHI Homework 2 Solutions Recursion

Lecture 5: Jun. 10, 2015

CS Java. Introduction to Java. Andy Mroczkowski Department of Computer Science Drexel University

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Introduction to Algorithmic Complexity. D. Thiebaut CSC212 Fall 2014

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Lecture 4: Stacks and Queues

56 CHAPTER 3. POLYNOMIAL FUNCTIONS

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

1 Java Night Countdown (30%)

Lecture 14: Nov. 11 & 13

ITI Introduction to Computing II

Introduction to Computing II (ITI 1121) MIDTERM EXAMINATION

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

Compiling Techniques

Lab Exercise 6 CS 2334

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Java Programming. Final Examination on December 13, 2015 Fall 2015

ITI Introduction to Computing II

Growth of Functions. As an example for an estimate of computation time, let us consider the sequential search algorithm.

Algorithms and Their Complexity

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

Solutions. Prelim 2[Solutions]

SFWR ENG/COMP SCI 2S03 Principles of Programming

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

Binary Search Trees. Motivation

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

SSJ User s Guide. Package randvarmulti Generating Random Vectors

Computer Science Introductory Course MSc - Introduction to Java

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

A Java introduction SDK, FC 15/12/2009 UMR AMAP. SDK, FC (UMR AMAP) A Java introduction 15/12/ / 50

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Asymptotic Running Time of Algorithms

Python. chrysn

Programming Languages Fall 2013

Design Pattern. Observer Pattern

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

More on Methods and Encapsulation

CS 61B Asymptotic Analysis Fall 2017

6.001 Recitation 22: Streams

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

INF Models of concurrency

SFWR ENG 3S03: Software Testing

Computer Science Introductory Course MSc - Introduction to Java

An Invitation to Mathematics Prof. Sankaran Vishwanath Institute of Mathematical Sciences, Chennai. Unit I Polynomials Lecture 1A Introduction

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

Tou has been released!

Voortgezet Programmeren

Chapter 4 Number Representations

Techniques of Java Programming

Liquid or Gas. New Mexico Super Computing Challenge Final Report April 1, 2006 Team #74 Oñate High School

Int Math 3 Midterm Review Handout (Modules 5-7)

Notes from Yesterday s Discussion. Big Picture. CIS 500 Software Foundations Fall November 1. Some lessons.

Data Structures. Outline. Introduction. Andres Mendez-Vazquez. December 3, Data Manipulation Examples

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Algorithms 2/6/2018. Algorithms. Enough Mathematical Appetizers! Algorithm Examples. Algorithms. Algorithm Examples. Algorithm Examples

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Lists, Stacks, and Queues (plus Priority Queues)

Übung Informatik I - Programmierung - Blatt 7

7. The Recursion Theorem

Compiling Techniques

MATHEMATICAL OBJECTS in

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

mith College Computer Science CSC231 - Assembly Week #7 Dominique Thiébaut

CS1083 Week 4 : Polymorphism

Two-Digit Number Times Two-Digit Number

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CSE 311: Foundations of Computing. Lecture 26: Cardinality

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Math 5a Reading Assignments for Sections

Fibonacci (Leonardo Pisano) ? Statue in Pisa Italy FIBONACCI NUMBERS AND RECURRENCES

Writing Bots for Maize

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Solution suggestions for examination of Logic, Algorithms and Data Structures,

CC Algebra Quadratic Functions Test Review. 1. The graph of the equation y = x 2 is shown below. 4. Which parabola has an axis of symmetry of x = 1?

Intro Vectors 2D implicit curves 2D parametric curves. Graphics 2011/2012, 4th quarter. Lecture 2: vectors, curves, and surfaces

JChartLib ver 1.0. Silvio Schneider, June 7, Introduction 3. 2 Chart and Dataset 4. 3 Adding data to a chart 4

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

CSEN202: Introduction to Computer Programming Spring Semester 2014 Final Exam

Design Patterns and Refactoring

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. (b) T F : A child class can modify the private fields of the class that it extends. (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. (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. (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.) 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 method constructor destructor builder for an object. (b) An interface is a standardized set of. 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. stream Lambda Expression static method Comparator (d) 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 JLabel JComponent JPanel JContainer class. Page 1 of 8

(f) The 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) (h) A public method may be accessed from anywhere, a from within its own class. static protected private package protected method can only be accessed 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? (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. 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 ) ) ; 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 ) ; 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? (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

(c) Does the m2 method in BaseGrandChild override the m2 method in the BaseChild class? If so, why? If not, why not? (d) What will get printed if you run the main method in the BaseGrandChildTester class? Page 8 of 8