SampleExam07 Ch10 11

Document Sample
SampleExam07 Ch10 11 Powered By Docstoc
					 Name:_______________________ CSCI 1302 OO Programming
                              Armstrong Atlantic State University
 (50 minutes)                 Instructor: Y. Daniel Liang

Covers Chapters 10-11

Part I: Multiple Choice Questions:

1.    A subclass inherits _____________ from its superclass.

a.      private method
b.      protected method
c.      public method
d.      a and c
e.      b and c

2. Show the output of running the class Test in the following code:

        interface A {
          void print();
        }

        class C {}

        class B extends C implements A {
          public void print() { }
        }

        public class Test {
          public static void main(String[] args) {
            B b = new B();
            if (b instanceof A)
              System.out.println("b is an instance of A");
            if (b instanceof C)
              System.out.println("b is an instance of C");
          }
        }

a.      Nothing.
b.      b is an instance of A.
c.      b is an instance of C.
d.      b is an instance of A followed by b is an instance of C.

3.    When you implement a method that is defined in a superclass, you
        __________ the original method.

a.   overload
b.   override
c.   copy
d.   call

4. What is the output of running the class C.

        public class C {
          public static void main(String[] args) {
            Object[] o = {new A(), new B()};
            System.out.print(o[0]);
            System.out.print(o[1]);
          }
        }



                                                                         1
        class A extends B {
          public String toString() {
            return "A";
          }
        }

        class B {
          public String toString() {
            return "B";
          }
        }

a.   AB
b.   BA
c.   AA
d.   BB
e.   None of above

5. What is the output of running class C?

        class A {
          public A() {
            System.out.println(
              "The default constructor of A is invoked");
          }
        }

        class B extends A {
          public B(String s) {
            System.out.println(s);
          }
        }

        public class C {
          public static void main(String[] args) {
            B b = new B("The constructor of B is invoked");
          }
        }

a.      none
b.      "The   constructor of B is     invoked"
c.      "The   default constructor     of A is invoked"
        "The   constructor of B is     invoked"
d.      "The   default constructor     of A is invoked"

6. Analyze the following code:

        public class Test1 {
          public Object max(Object o1, Object o2) {
            if ((Comparable)o1.compareTo(o2) >= 0) {
              return o1;
            }
            else {
              return o2;
            }
          }
        }




                                                              2
a.    The program has a syntax error because Test1 does not have a main
method.
b.    The program has a syntax error because o1 is an Object instance
      and it does not have the compareTo method.
c.    The program has a syntax error because you cannot cast an Object
instance o1 into Comparable.
d.    The program would compile if ((Comparable)o1.compareTo(o2) >= 0)
      is replaced by (((Comparable)o1).compareTo(o2) >= 0).
e.    b and d are both correct.

7.    The method _____ overrides the following method:

      protected double xMethod(int x) {…};

a.    private double xMethod(int x) {…}
b.    protected int xMethod(double x) {…}
c.    public double xMethod(double x) {…}
d.    public double xMethod(int x) {…}

8. Which of the following possible modifications will fix the errors in
this code?

      public class Test {
        private double code;

          public double getCode() {
            return code;
          }

          protected abstract void setCode(double code);
      }

a.    Remove abstract in the setCode method declaration.
b.    Change protected to public.
c.    Add abstract in the class declaration.
d.    b and c.

9. Analyze the following code.

      class Test {
        public static void main(String[] args) {
          Object x = new Integer(2);
          System.out.println(x.toString());
        }
      }

a.    The program has syntax errors because an Integer object is
assigned to x.
b.    When x.toString() is invoked, the toString() method in the Object
class is used.
c.    When x.toString() is invoked, the toString() method in the
Integer class is used.
d.    None of the above.

10. Analyze the following code.

      class Test {
        public static void main(String[] args) {
          Object x = new Integer(2);
          System.out.println(x.doubleValue());
        }



                                                                          3
         }

a.    The program has syntax errors because an Integer object is
assigned to x.
b.    The program has syntax errors because doubleValue() is not a
method in the Object class.
c.    The program compiles and runs fine.
d.    None of the above.

11. Analyze the following code.

         class Test {
           public static void main(String[] args) {
             Inner inner = new Inner();
             System.out.println(inner.k);
           }

             private class Inner {
               protected int k;
             }
         }

a.    The program has a syntax error because the Inner class does not
have a constructor and you cannot create an object from it.
b.    The program has a syntax error because the Inner class is private
and it cannot be accessed in the main method .
c.    The program has a syntax error because k is protected in the
Inner class and it cannot be accessed in the main method.
d.    The program has a syntax error because the Inner class is not
static and it cannot be used to create an object in the main method.

12.      Analyze the following code.

Number numberRef = new Integer(0);
Double doubleRef = (Double)numberRef;

a. You cannot assign an Integer object into a variable of the Number
type.
b. The compiler detects that numberRef is not an instance of Double.
c. A runtime class casting exception occurs, since numberRef is not an
instance of Double.
d. The program runs fine, since Integer is a subclass of Double.
e. You can convert an int to double, so you can cast an Integer
instance to a Double instance.

13. The __________ class is inherited by every Java class.

a.    Class
b.    Object
c.    Number
d.    Comparable


Part II: Trace Programs

(2 pts) The java.util.Date class implements
java.lang.Cloneable and overrides the equals method to



                                                                          4
return true if two objects have the same date and time.
Show the output of the following code.

import java.util.*;
public class Test extends Object {
  public static void main(String[] args) {
    Date d1 = new Date();
    Date d2 = new Date(349324);
    Date d3 = d1;
    System.out.println("(1) " + (d1 == d2));
    System.out.println("(2) " + (d1 == d3));
    System.out.println("(3) " + d1.equals(d2));
    System.out.println("(4) " + d1.equals(d3));
  }
}

Part III: Write Programs

(5 pts) Write a method to find the max in an array of comparable
objects (Assume that the objects in the argument are instances of
Comparable). The method signature is as follows:

public static Object max(Object[] a)




(5 pts) Write a class named Hexagon that extends GeometricObject and
      implements the Comparable interface. Assume all six sides of the
      hexagon are of equal size. The Hexagon class is defined as
      follows:

  public class Hexagon extends GeometricObject implements Comparable {
    private double side;

    /** Construct a Hexagon with the specified side */
    public Hexagon(double side) {
      // Implement it




                                                                         5
    }

    /** Implement the abstract method findArea in
       GeometricObject */
    public double findArea() {
        // Implement it ( area  3 *   3 * side * side )



    }

    /** Implement the abstract method findPerimeter in
       GeometricObject */
    public double findPerimeter() {
      // Implement it



    }

    /** Implement the compareTo method in
       the Comparable interface to */
    public int compareTo(Object obj) {
      // Implement it (compare two Hexagons based on their areas)




    }
}




                                                                    6
Key

Part I: Multiple Choice Questions: (1 pts each)

    1.     e
    2.     d
    3.     b
    4.     a
    5.     c
    6.     e
    7.     d
    8.     c
    9.     c
    10.    b
    11.    d
    12.    c
    13.    b



Part II:

               (1)   false
               (2)   true
               (3)   false (because they are created at different times)
               (4)   true




                                                                           7

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:8/31/2012
language:Unknown
pages:7