Java 101 study hall

Brush up on Java terms, learn tips and cautions, and enter the first Java 101 reader challenge

Glossary of terms

polymorphism

The quality or state of assuming different forms (or shapes)

Tips and cautions

These tips and cautions will help you write better programs and save you from agonizing over error messages produced by the compiler.

Tips

  • Don't call class methods via object reference variables. Doing so clouds the fact that those methods are class methods.

Cautions

  • If a class declares an abstract method, the class signature must include the abstract keyword. Otherwise, the compiler reports an error.
  • If a subclass inherits an abstract method from an abstract superclass and does not provide a code body for that method, the compiler regards the subclass as abstract. Attempts to create objects from that subclass will cause the compiler to report errors.

Miscellaneous notes and thoughts

Just as it is unwise to add new constants and/or method signatures to an interface, it is also unwise to remove those members. To see why, consider the following example:

Create an X.java source file that contains Listing 1's source code. That source file holds an interface declaration named X:

Listing 1. X.java

// X.java
interface X
{
   void foo ();
}

Create a UseX.java source file that contains Listing 2's source code. That source file holds a class declaration that implements X:

Listing 2. UseX.java

// UseX.java
class UseX implements X
{
   public static void main (String [] args)
   {
      X x = new UseX ();
      x.foo ();
   }
   public void foo ()
   {
      System.out.println ("foo");
   }
}

Compile both source files. Assuming you use the SDK tools for Windows, type javac UseX at the command line to create X.class and UseX.class.

Run UseX (by typing java UseX) and the program should run correctly.

Comment out void foo (); in X.java and recompile that source file (i.e., javac X.java).

Attempt to run the previously generated UseX.class file. This time, you will receive a runtime error message stating that a method -- foo(), to be exact -- cannot be found.

Homework

Now that the object-oriented language basics series has ended, it is time to test your knowledge of series material. To add some incentive for completing the quiz below, I've fashioned the questions into a reader challenge. Challenge winners will receive JavaWorld sweatshirts and t-shirts.

Here are the rules:

  • All entries must be submitted no later than 12:00 p.m. Central Daylight Time on October 15, 2001.
  • Please email your answers to Java 101 Challenge (no attachments). You can include either a copy of the questions with your answers or just the answers. However, I must be able to associate your answers with the respective questions.
  • You must answer all questions.
  • I will sort entries by submission date/time stamp. The first three individuals to achieve a score of 100 percent will receive sweatshirts. If only one person gets 100 percent, the first two individuals who achieve 99 percent will each receive a t-shirt. If no one achieves 100 percent, the first three individuals who achieve 99 percent will receive t-shirts. This process will continue until I find the first three individuals with the highest scores.
  • Entries will not be returned.
  • Winners will be contacted.
  • The names of the three winners will appear in the November Java 101 column.
  • This challenge is open to anyone.
  • Neither JavaWorld nor Jeff Friesen will be held liable for any misunderstanding of challenge rules.

I've divided the quiz into four sections:

  • Fill in the blanks (1-20): To submit answers in this section, simply specify each question's number followed by either a single word/phrase or a comma-delimited list of word/phrases (for those questions that have more than one blank).
  • Multiple choice (21-25): To submit answers in this section, simply specify each question's number followed by the letter a, b, c, or d.
  • True or false (26-50): To submit answers in this section, simply specify each question's number followed by the word true or false.
  • Short answer (51-65): To submit answers in this section, simply specify each question's number followed by a brief paragraph or a few words.
  1. The object-oriented programming ________________ principle promotes the integration of state with behavior.
  2. A ________________ is a source code blueprint.
  3. Another name for a class instance is ________________.
  4. Java supports ________________ access levels for fields and methods. (Enter a number.)
  5. Another name for a read-only variable is ________________.
  6. Values passed to a method during a method call are known as ________________.
  7. ________________ passes a value to a method, and ________________ passes a reference.
  8. When only a single object can be created from a class, that class is known as a ________________ class.
  9. ________________ is a synonym for composition.
  10. The object-oriented programming ________________ principle promotes layered objects.
  11. Composition promotes ________________ relationships.
  12. A ________________ inherits fields and methods from a ________________.
  13. If class A extends class B, and class A declares a method that has the same name, return type, and parameter list as class B's method, class A's method is said to ________________ class B's method.
  14. ________________ is Java's ultimate superclass.
  15. Arrays are ________________ cloned.
  16. The object-oriented programming ________________ principle promotes many forms.
  17. Class methods are ________________ to classes, and instance methods are ________________ to objects.
  18. Classes situated near the top of a class hierarchy represent ________________ entities, and classes lower in the class hierarchy represent ________________ entities.
  19. The equals() method defaults to comparing object ________________.
  20. The clone() method throws a ________________ if it cannot clone an object.
  21. When declaring a field, which of the following access-level specifier keywords would you use so that only classes in the same package as the class that declares the field can access that field?

    a) public

    b) private

    c) protected

    d) none of the above

  22. Which keyword has something to do with object serialization?

    a) transient

    b) volatile

    c) synchronized

    d) native

  23. Which of the following keywords do you use to achieve implementation inheritance?

    a) implements

    b) extends

    c) super

    d) this

  24. Which method returns an object locked (behind the scenes) by static synchronized methods?

    a) toString()

    b) finalize()

    c) getClass()

    d) wait()

  25. According to Sun's Java 2 SDK, how many public classes can be declared in a source file?

    a) 1

    b) 0

    c) as many as desired

    d) no more than 1 public class and 1 public interface

  26. True or false: Object-oriented programming emphasizes separating a program's data from its functionality.
  27. True or false: You must declare class fields with the static keyword.
  28. True or false: The integration of state and behaviors into objects is known as information hiding.
  29. True or false: When the JVM creates an object, it zeroes the memory assigned to each instance field.
  30. True or false: You can access local variables prior to specifying their declarations.
  31. True or false: You must initialize local variables before accessing them.
  32. True or false: Subclasses can override a superclass's final methods.
  33. True or false: An enumerated type is a reference type with an unrestricted set of values.
  34. True or false: When returning a value from a method, that method must not have a void return type.
  35. True or false: A class method cannot access an object's instance fields.
  36. True or false: The keyword this can be used in any method to call a constructor that resides in the same class as that method.
  37. True or false: If a class declares no constructors, the compiler generates an empty no-argument constructor.
  38. True or false: You cannot extend final classes.
  39. True or false: You can use the keyword super to call a superclass constructor from any method.
  40. True or false: You cannot make a field or method's access level more restrictive in a subclass.
  41. True or false: Java supports multiple implementation inheritance.
  42. True or false: A subclass can directly access a superclass's private fields.
  43. True or false: You can legally place code ahead of a constructor call (via either this or super) in a constructor.
  44. True or false: Arrays are objects.
  45. True or false: You can declare read/write variables in an interface.
  46. True or false: All method signatures in an interface have a public access level.
  47. True or false: A class that inherits an abstract method from a superclass and does not override that method is also abstract.
  48. True or false: You can declare field variables in methods.
  49. True or false: Java's new keyword allocates memory for an object, and Java's delete keyword releases that memory.
  50. True or false: The Object class declares 11 methods.
  51. If a subclass constructor does not include a call to a superclass constructor (via super) or another subclass constructor (via this), what happens?
  52. What is wrong with the following code fragment?

    class Sup
    {
       Sup (int x)
       {
       }
    }
    class Sub extends Sup
    {
    }
    
  53. Is there anything wrong with the following code fragment?

    abstract void hello () 
    {
       System.out.println ("Hello")
    }
    
  54. Why would you use interfaces?
  55. List the four polymorphism categories.
  56. Suppose you create an object from a superclass and assign that object's reference to a superclass variable. Suppose you cast that variable's type from the superclass type to a subclass type before accessing a subclass field or calling a subclass method. What happens?
  57. Must a subclass constructor always call a superclass constructor?
  58. Explain two uses for the super keyword.
  59. A class can only extend one superclass. Is an interface subject to the same restriction (that is, can an interface only extend a single superinterface)?
  60. By default, what does an object's toString() method return?
  61. What are hash codes?
  62. When does method overloading fail?
  63. Describe the difference between shallow cloning and deep cloning.
  64. Why can't a class signature include both the abstract and final keywords?

  65. If you do not declare a class to be public, must you declare it in a file whose filename matches the class name? For example, must you declare class Account {} in Account.java?

Part 6's homework answers

Below you will find the homework questions for "Object-Oriented Language Basics, Part 6," followed by their answers:

  1. Why is it unwise to place the NOT_STARTED and STARTED constants and the isstarted() method in the StartStop interface? Placing the NOT_STARTED and STARTED constants in the StartStop interface can lead to compiler error messages. Those error messages describe ambiguous constant references when a class implements multiple interfaces and one interface already declares NOT_STARTED and STARTED, but with different types and/or initial values. If you recompile legacy classes that implement the new StartStop interface instead of the old StartStop interface, and one class implements an additional interface with different NOT_STARTED and STARTED constants, you have a problem.

    Don't place the isStarted() method in the StartStop interface because recompilations of source code to legacy classes that implement StartStop will fail. The compiler will complain about those classes not providing code bodies for isStarted(). Attempting to fix the problem by adding to each affected class an isStarted() method that returns a default value only adds redundancy to those legacy classes -- and might require you to distribute new versions of the legacy classes. Furthermore, you might find that future source code/class maintenance will prove more difficult.

  2. If an interface introduces a type into source code, and if a type consists of a set of data items in addition to a set of methods, where are the interface's data items? (Hint: constants are not the data items.)

    The interface's data items are those objects whose classes implement the interface. A program applies the interface's methods to objects, just as a program applies a primitive type's operations to values of that type -- such as the integer type's addition operation to integer values.

Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more