Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Books

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 

Bookshelf Index

The Complete Java 2 Certification Study Guide
Chapter 6: Objects and Classes

by Simon Roberts, Philip Heller, and Michael Ernest

Introduction | Chapter 6: Objects and Classes| Test Yourself
<< BACK >>


Test your knowledge on Classes and Objects.

Note: Many of the questions have more than one correct answer. You must check each of the correct answers to receive a point for the question and a perfect score on the quiz.


  1. Consider this class:
    1. public class Test1 {
    2.   public float aMethod(float a, float b) {
    3.   }
    4.
    5. }
    
    Which of the following methods would be legal if added (individually) at line 4?

     A. public int aMethod(int a, int b) { }
     B. public float aMethod(float a, float b) { }
     C. public float aMethod(float a, float b, int c) throws _Exception { }
     D. public float aMethod(float c, float d) { }
     E. private float aMethod(int a, int b, int c) { }

  2. Consider these classes, defined in separate source files:
    1. public class Test1 {
    2.   public float aMethod(float a, float b)
           throws IOException {
    3.   }
    4. }
    
    1. public class Test2 extends Test1 {
    2.
    3. }
    
    Which of the following methods would be legal (individually) at line 2 in class Test2?

     A. float aMethod(float a, float b) { }
     B. public int aMethod(int a, int b) throws Exception { }
     C. public float aMethod(float a, float b) throws _Exception { }
     D. public float aMethod(float p, float q) { }

  3. You have been given a design document for a veterinary registration system for implementation in Java technology. It states:

    "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a textual description of its markings."

    Given that the Pet class has already been defined, which of the following fields would be appropriate for inclusion in the Cat class as members?
     A. Pet thePet;
     B. Date registered;
     C. Date vaccinationDue;
     D. Cat theCat;
     E. boolean neutered;
     F. String markings;

  4. You have been given a design document for a veterinary registration system for implementation in Java. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined and you expect the Cat class to be used freely throughout the application, how would you make the opening declaration of the Cat class, up to but not including the first opening brace? Use only these words and spaces: boolean, Cat, class, Date, extends, Object, Owner, Pet, private, protected, public, String.

  5. Consider the following classes, declared in separate source files:
     1. public class Base {
     2.   public void method(int i) {
     3.     System.out.println("Value is " + i);
     4.   }
     5. }
    
     1. public class Sub extends Base {
     2.   public void method(int j) {
     3.     System.out.println("This value is " + j);
     4.   }
     5.   public void method(String s) {
     6.     System.out.println("I was passed " + s);
     7.   }
     8.   public static void main(String args[]) {
     9.     Base b1 = new Base();
    10.     Base b2 = new Sub();
    11.     b1.method(5);
    12.     b2.method(6);
    13.   }
    14. }
    
    What output results when the main method of the class Sub is run?
     A. Value is 5
    Value is 6
     B. This value is 5
    This value is 6
     C. Value is 5
    This value is 6
     D. This value is 5
    Value is 6
     E. I was passed 5
    I was passed 6

  6. Consider the following class definition:
    1. public class Test extends Base {
    2.   public Test(int j) {
    3.   }
    4.   public Test(int j, int k) {
    5.     super(j, k);
    6.   }
    7. }
    
    Which of the following are legitimate calls to construct instances of the Test class?

     A. Test t = new Test();
     B. Test t = new Test(1);
     C. Test t = new Test(1, 2);
     D. Test t = new Test(1, 2, 3);
     E. Test t = (new Base()).new Test(1);

  7. Consider the following class definition:
    1. public class Test extends Base {
    2.   public Test(int j) {
    3.   }
    4.   public Test(int j, int k) {
    5.     super(j, k);
    6.   }
    7. }
    
    Which of the following forms of constructor must exist explicitly in the definition of the Base class?

     A. Base() { }
     B. Base(int j) { }
     C. Base(int j, int k) { }
     D. Base(int j, int k, int l) { }

  8. Which of the following statements are true?
     A. An inner class may be declared private.
     B. An inner class may be declared static.
     C. An inner class defined in a method should always be anonymous.
     D. An inner class defined in a method can access all the method local variables.
     E. Construction of an inner class may require an instance of the outer class.

  9. Consider the following definition:
     1. public class Outer {
     2.   public int a = 1;
     3.   private int b = 2;
     4.   public void method(final int c) {
     5.     int d = 3;
     6.     class Inner {
     7.       private void iMethod(int e) {
     8.
     9.       }
    10.     }
    11.   }
    12. }
    
    Which variables may be referenced correctly at line 8?

     A. a
     B. b
     C. c
     D. d
     E. e

  10. Which of the following statements are true?
     A. Given that Inner is a non-static class declared inside a public class Outer, and appropriate constructor forms are defined, an instance of Inner may be constructed like this:
    new Outer().new Inner()
     B. If an anonymous inner class inside the class Outer is defined to implement the interface ActionListener, it may be constructed like this:
    new Outer().new ActionListener()
    
     C. Given that Inner is a non-static class declared inside a public class Outer and appropriate constructor forms are defined, an instance of Inner may be constructed in a static method like this:
    new Inner()
    
     D. An anonymous class instance that implements the interface MyInterface may be constructed and returned from a method like this:
    1. return new MyInterface(int x) {
    2.   int x;
    3.   public MyInterface(int x) {
    4.     this.x = x;
    5.   }
    6. };
    

Page 1 2 3 4 5 Test Yourself
BACK

See also:Sun Educational Services


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.