CSCD 226       Exam #2                                               Name_________________________
100 points                                                                    Section_______

Short Answer (please attempt to write clearly)

1.        (5 points) How are private members of the base class treated with respect to the derived class? (Can they be accessed from the derived class?) Explain as clearly as possible for full credit.

 

 

 

 

 

2.        (9 points) (a) Why would you make a class abstract?  (b) Why would you make a method abstract? (c) Should an abstract class occur at the bottom of an inheritance hierarchy - explain?

 

 

 

 

 

 

3.        (6 points) Explain how method override promotes reuse of code and polymorphic behavior.

 

 

 

 

 

 

4.        (9 points) What are the three main components in exception handling in Java?  For full credit, describe what purpose each thing serves.

 


5.        Given the code below answer the questions that follow.  Use back of page as necessary to answer questions.

public class ExceptionScope
{
   public void m1()
   {
      System.out.println("m1 begin");
 
      try
      {
         m2();
      }
      catch (ArithmeticException problem)
      {
         System.out.println ();
         System.out.println ("exception is: " + problem.getMessage());
         System.out.println ();
         System.out.println ("Call stack trace:");
         problem.printStackTrace(); //lists order methods called to get to exception
         System.out.println ();
      }
 
      System.out.println("m1 end");
   }
 
   public void m2()
   {
      System.out.println("m2 begin");
      m3 ();
      System.out.println("m2 end");
   }
 
   public void m3 ()
   {
      int numerator, denominator;
      System.out.println(“Enter a numerator followed by a denominator”);
      numerator = Keyboard.readInt();
      denominator = Keyboard.readInt();
 
      System.out.println("m3 begin");
      int result = numerator / denominator;
      System.out.println("m3 end");
   }
}
 
public class Propagation
{
   public static void main (String[] args)
   {
      ExceptionScope demo = new ExceptionScope();
 
      System.out.println("main begin");
      demo.m1();
      System.out.println("main end");
   }
}

a)       (3 points) What will be printed if a denominator of 0 is entered? (note that the numerator value is irrelevant)

 

 

b)       (3 points) What will be printed if a denominator of 5 is entered? (note that the numerator value is irrelevant)

 

 

c)       (4 points) What will be printed if the main method has demo.level2() instead of demo.level1() and a denominator of 0 is entered?


6.        (16 Points) Show what will result from each of the methods being invoked in the InheritanceTester class in the code below.

public class BaseClass

{

      public void m1()

      {

            System.out.println("BC m1");

      }// end method1

 

      public void m2()

      {

            System.out.println("BC m2");

      }// end method2

}// end class

 

public class DerivedClass extends BaseClass

{

      public void m2()

      {

            System.out.println("DC m2");

      }// end method2

 

      public void m3()

      {

            System.out.println("DC m3");

      }// end method3

}// end class

 

import BaseClass;

import DerivedClass;

 

public class InheritenceTester

{

      public static void main(String [] args)

      {

            BaseClass ref1, ref3;

            DerivedClass ref2;

 

            ref1 = new BaseClass();

            ref2 = new DerivedClass();

            ref3 = ref2;

 

//SHOW WHAT WILL RESULT FROM EACH OF THE FOLLOWING METHODS BEING INVOKED.
//
àASSUME EACH OF THE FOLLOWING LINES IS INDEPENDENT OF THE OTHERSß
//
àSHOW YOUR ANSWER TO THE RIGHT OF EACH LINEß

            ref1.m1();

 

            ref1.m2();

 

            ref2.m1();

 

            ref2.m2();

 

            ref2.m3();

 

            ref3.m1();

 

            ref3.m2();

 

            ref3.m3();

      }// end main

}// end class

True or False (3 points each – all or nothing so BE CAREFUL!)

àCircle T or Fß

7.        T / F  The messages printed by a thrown exception indicate the nature of the problem and a call stack trace that shows the methods that were invoked.

8.        T / F  If an exception is not caught and handled where it occurs, an error is generated.

9.        T / F  Once an exception has been caught, no other catch blocks/phrases can catch that exception.

10.     T / F  An object of a derived class can be treated as an object of its corresponding base class.

11.     T / F  Assigning a derived class reference to a base class reference will generate a compiler error.

12.     T / F  For a derived-class object, the derived class constructor executes completely before the base class constructor is allowed to do anything.

13.     T / F  You are not allowed to create your own exception handling class in Java.  You can only use the classes provided in the exception handling hierarchy.

14.     T / F  You are required to call the base class constructor from a derived class constructor.

15.     T / F  All classes in Java are ultimately derived from a single parent (or grandparent) class.

16.     T / F  Every class in Java is part of an inheritance hierarchy.

 

17.     (15 points) Implement ONE of the next two problems.  If you implement both problems, the second is worth up to 5 points extra credit.  Be sure and CIRCLE which problem you want to count as the REGULAR problem and which is EXTRA CREDIT.  Use the back of this page if necessary to complete your answer.

(a) REGULAR / EXTRA CREDIT  Write a method called shiftColumnsLeft that will shift the columns of a two dimensional array of integers one to the left.  The leftmost column should be shifted “around” to the rightmost column.

 

 

 

 

 

 

 

(b) REGULAR / EXTRA CREDIT Given a two dimensional array of integers, write a method called sumRowProducts that calculates and returns the sum of the products of the rows in the array.