Chapter 10 - Reading and Understanding Code Flashcards
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- Draw the UML diagram for the class hierarchy.
A
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What fields and methods are inherited by which class?
B inherits from A: name, price (foo2 and foo3 are overridden)
C inherits from B: name, price, foo2, foo3 (foo1 is overridden)
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What fields and methods are not inherited?
B does not inherit from A: number, foo1, foo2 and foo3
C does not inherit from B: service, foo1
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What is the output of the following code sequence?
B b1 = new B( );
A( ) called
B( ) called
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What is the output of the following code sequence?
B b2 = new B( ); b2.foo1( );
A( ) called
B( ) called
B version of foo1( ) called
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What is the output of the following code sequence?
B b3 = new B( ); int n = b3.foo2( );
A( ) called
B( ) called
A version of foo2( ) called
B version of foo2( ) called
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What is the output of the following code sequence?
// b4 is a B object reference System.out.println( b4.foo3( ) );
A version of foo3( ) called
B version of foo3( )
Hi foo3
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What is the output of the following code sequence?
C c1 = new C( );
A( ) called
B( ) called
C( ) called
For Questions 12 to 20, consider the following three classes:
public class A { private int number; protected String name; public double price; public A( ) { System.out.println( "A( ) called" ); } private void foo1( ) { System.out.println( "A version of foo1( ) called" ); } protected int foo2( ) { System.out.println( "A version of foo2( ) called" ); return number; } public String foo3( ) { System.out.println( "A version of foo3( ) called" ); return "Hi"; } } public class B extends A { private char service; public B( ) { super( ); System.out.println( "B( ) called" ); } public void foo1( ) { System.out.println( "B version of foo1( ) called" ); } protected int foo2( ) { int n = super.foo2( ); System.out.println( "B version of foo2( ) called" ); return ( n + 5 ); } public String foo3( ) { String temp = super.foo3( ); System.out.println( "B version of foo3( )" ); return ( temp + " foo3" ); } } public class C extends B { public C( ) { super( ); System.out.println( "C( ) called" ); } public void foo1( ) { System.out.println( "C version of foo1( ) called" ); } }
- What is the output of the following code sequence?
// c2 is a C object reference c2.foo1( );
C version of foo1( ) called