Chapter 10 - Reading and Understanding Code Flashcards

1
Q

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" );
  }
}
  1. Draw the UML diagram for the class hierarchy.
A

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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" );
  }
}
  1. What fields and methods are inherited by which class?
A

B inherits from A: name, price (foo2 and foo3 are overridden)

C inherits from B: name, price, foo2, foo3 (foo1 is overridden)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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" );
  }
}
  1. What fields and methods are not inherited?
A

B does not inherit from A: number, foo1, foo2 and foo3

C does not inherit from B: service, foo1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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" );
  }
}
  1. What is the output of the following code sequence?

B b1 = new B( );

A

A( ) called

B( ) called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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" );
  }
}
  1. What is the output of the following code sequence?
B b2 = new B( );
b2.foo1( );
A

A( ) called
B( ) called
B version of foo1( ) called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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" );
  }
}
  1. What is the output of the following code sequence?
B b3 = new B( );
int n = b3.foo2( );
A

A( ) called
B( ) called
A version of foo2( ) called
B version of foo2( ) called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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" );
  }
}
  1. What is the output of the following code sequence?
// b4 is a B object reference
System.out.println( b4.foo3( ) );
A

A version of foo3( ) called
B version of foo3( )
Hi foo3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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" );
  }
}
  1. What is the output of the following code sequence?

C c1 = new C( );

A

A( ) called
B( ) called
C( ) called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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" );
  }
}
  1. What is the output of the following code sequence?
// c2 is a C object reference
c2.foo1( );
A

C version of foo1( ) called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly