The entire exam is worth 96 points.

 

Multiple choice:___________________________________________________

 

13:_____________________________________________________________

 

14:_____________________________________________________________

 

15:_____________________________________________________________

 

16:_____________________________________________________________

 

17:_____________________________________________________________

 

 

 

 

TOTAL:__________________________________________________________________

 

Which of the following are methods in the class JOptionPane ?.

 

>>a. showInputDialog

b. showOutputDialog

c. showOptionDialog

d. all of the above.

e. none of the above.

 

  1. Suppose D is a derived class of the B . Suppose d is an object of type D and b is an object of type B . Which one of the following is true?

>>a. d is of type D and of type B .

b. b is of type D and of type B .

c. d is of type D but not of type B .

d. b is of type B but not of type D .

e. none of the above

 

  1. Suppose D is a derived class of the B . Suppose a method named doStuff() is defined in B and has its definition overridden (changed) in the definition of class D . Suppose d is an object of type D , what definition of doStuff is used in d.doStuff() :

 

a. The one given in the definition of B .

>>b. The one given in the definition of D .

c. It depends on the type of the variable used to name d .

d. You need more information to decide which definition of doStuff() is used.

e. none of the above.

 

  1. Suppose D is a derived class of the B . Suppose d is an object of type D and b is an object of type B . Which one of the following is legal?

 

>>a. d = b; (but not the other way around)

b. b = d; (but not the other way around)

c. Both a and b are legal.

d. Neither a nor b is legal.

e. none of the above.

 

  1. What kind of thing is an exception?

 

a. an identifier

b. a string

>>c. an object

d. an integer

e. none of the above.

 

 

 

 

 

  1. Normally when an exception is thrown it must be either caught in a catch block or declared in the throws clause of the method (in which the throw occurs). Some classes are exempt from this rule. Which of the following classes is exempt from this rule?

 

a. IOException

b. EOFException

c. FileNotFoundException

d. all of the above.

>>e. none of the above.

 

  1. Suppose a method in a base class has a throws clause. Suppose the method is overwritten in a derived class. Which of the following is true?

 

a. The throws clause in the method definition in the derived class must be exactly the same as in the base class.

b. The throws clause in the derived class can be anything (including not there) without regard to what the throws clause was in the base class.

c. The throws clause in the derived class can have more but not fewer classes listed in the throws clause.

>>d. The throws clause in the derived class can have fewer but not more classes listed in the throws clause.

e. None of the above.

 

  1. Which of the following classes have a constructor that accepts a file name as an argument?

 

a. PrintWrite r

b. BufferedReader

>>c. FileReader

>>d. FileOutputStream .

e. All of the above.

f. none of the above.

 

  1. If a is an array, then the number of indexed variables in a is

 

a. a.size .

b. a.size()

>>c. a.length

d. a.length()

e. None of the above.

 

  1. If a and b are array variables (that name some arrays) then the effect of

a = b;

 

a. is to copy the elements in the array b to the indexed variables of the array a .

b. is to copy the elements in the array a to the indexed variables of the array b .

>>c. is to make a and b two names for the same array,

d. nothing. It is illegal to use = with arrays.

e. none of the above.

 

 

 

 

 

 

  1. A two dimensional array is

 

a. an object of a class named Array .

b. an array whose indexed variables are written as illustrated by a[i,j] .

>>c. an array of arrays.

d. all of the above.

e. none of the above.

 

  1. If a and b are array variables (that name some arrays) then
    a = = b returns true if

 

a. a and b have the same length and contain the same elements in the same order.

b. a and b have the same length and the same base type.

c. It never returns true , because a = = b is an illegal expression.

>>d. none of the above.

Short Answer
*In many cases the amount of space allowed for answer is larger than you need. Do not take that as a guide to how long the answer is.
*Style will count.

 

  1. Write a complete Java program that will fill an array a with 20 values of type double read in from the keyboard, and that will output the numbers in the array as well as indicate how much each number differs from the last number in the input list. For example, if an array element is 2.0 and the last value in the input list is 5.0 , the difference is 3.0 . If an array element is 7.0 and the last value in the input list is 5.0 , the difference is − 2.0 . Assume the users enter 20 numbers, one per line, from the keyboard. You need not give elaborate instructions to the user. Be sure to use the length instance variable. You need not include any comments, but your code should be clear without comments.
    ANSWER: (10 points)

public class Exercise
{
    public static void main(String[] args)
    {
        double[] a = new double[20];

        int index;
        System.out.println("Enter 20 numbers:");
        for (index = 0; index < a.length; index++)
            a[index] = SavitchIn.readLineDouble();

        System.out.println(
           "The numbers and differences from last number are:");
        for (index = 0; index < a.length; index++)
            System.out.println(a[index]
             + " differs from last by "
             + (a[a.length - 1] - a[index]));
    }
}

 


  1. Describe two uses for the reserved word super . These are uses for super within the definition of a derived class.

ANSWER: (10 points) Hint; answer is ABOUT 5 to 10 lines .

 

1. It is used as the name for a constructor of the base class.

2. When used as a "calling object" as in

super .writeOutput();

it is used to indicate that the method definition of the given name in the base class should be used, as opposed to the method definition in the derived class.

 

 

  1. Which of the following lines are legal and which are illegal? ( Student is a derived class of Person , and Undergraduate is a derived class of Student .)

ANSWER (10 points. Minus 1 for each missing or extra circle. No negative points.)

CIRCLE ALL LINES THAT ARE LEGAL

 
Person p1 = new Student();//Legal. A Student is a Person.
Person p2 = new Undergraduate();//Legal.
                              //An Undergraduate is a Person.
Student s1 = new Person(); //ILLEGAL!
Student s2 = new Undergraduate(); //Legal.
                              //An Undergraduate is a Student.
Undergraduate ug1 = new Person(); //ILLEGAL!
Undergraduate ug2 = new Student(); //ILLEGAL!
Object ob = new Student(); //Legal. A Student is an Object.
Student s3 = new Object();//ILLEGAL!
  • Student s1 = new Person(); // ILLEGAL!
  • Student s2 = new Undergraduate(); // Legal.
  • //An Undergraduate is a Student.
  • Undergraduate ug1 = new Person(); // ILLEGAL!
  • Undergraduate ug2 = new Student(); // ILLEGAL!
  • Object ob = new Student(); // Legal. A Student is an Object.
  • Student s3 = new Object();// ILLEGAL!
    </tt>
    1. Define an exception class called CoreBreachException . The class should have a constructor with no parameters. If an exception is thrown with this zero argument constructor, then getMessage should return "Core Breach! Evacuate Ship!" The class should also have a constructor with a single parameter of type String . If an exception is thrown with this constructor, then getMessage returns the value that was used as an argument to the constructor.

    Answers (10 points.)

    public class CoreBreachException extends Exception
    {
        public CoreBreachException()
        {
            super("Core Breach! Evacuate Ship!");
        }
    
        public CoreBreachException(String message)
        {
            super(message);
        }
    }
    
    1. What happens when the method readLine in the class BufferedReader attempts to read beyond the end of a file? How can you use this to test for the end of a file?

     

    ANSWER (5 points.) Hint: the answer is a ABOUT 2 to 6 lines.

     

    When the method readLine tries to read beyond the end of a file, it returns the value null . Thus, you can test for the end of a file by testing for null .

     

     

    1. Write a complete Java program that will ask the user for a file name, test if the file exists, and if the file exists, will ask the user if it should be deleted or not, and then delete it or not as the user requests. If the program cannot delete the file, it should report this fact.
      ANSWER (15 points)
    2. import java.io.*;
      
      public class FileClassExercise
      {
          public static void main(String[] args)
          {
              String name = null;
              File fileObject = null;
      
              System.out.print("Enter a file name and I will ");
              System.out.println("tell you if it exists.");
              name = SavitchIn.readLineWord();
              fileObject = new File(name);
              if (fileObject.exists())
              {
                  System.out.println("I found the file " + name);
                  System.out.println("Delete the file? (y/n)");
                  char ans = SavitchIn.readLineNonwhiteChar();
      
                  if ((ans == ’y’) || (ans == ’Y’))
                  {
                      System.out.println(
                                   "If you delete the file " + name);
                      System.out.println(
                                    "all data in the file will");
                      System.out.println("be lost. Delete? (y/n)");
                      ans = SavitchIn.readLineNonwhiteChar();
                      if ((ans == ’y’) || (ans == ’Y’))
                      {
                          if (fileObject.delete())
                                System.out.println("File deleted.");
                           else
                                System.out.println(
                                            "Cannot delete file.");
                      }
                      else
                          System.out.println("File not deleted.");
                  }
                  else
                      System.out.println("File not deleted.");
              }
              else
                  System.out.println("I cannot find " + name);
          }
      }