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