Flylib.com

Books Software

 
 
 

Review Questions


Review Questions

graphics/rq_icon.gif
2.7

Which of the following lines are valid declarations?

Select the three correct answers.

  1. char a = '\u0061';

  2. char 'a' = 'a';

  3. char \u0061 = 'a';

  4. ch\u0061r a = 'a';

  5. ch'a'r a = 'a';

2.8

Given the following code within a method, which statement is true?

int a, b;
b = 5;

Select the one correct answer.

  1. Local variable a is not declared.

  2. Local variable b is not declared.

  3. Local variable a is declared but not initialized .

  4. Local variable b is declared but not initialized.

  5. Local variable b is initialized but not declared.

2.9

In which of these variable declarations will the variable remain uninitialized unless explicitly initialized?

Select the one correct answer.

  1. Declaration of an instance variable of type int .

  2. Declaration of a static variable of type float .

  3. Declaration of a local variable of type float .

  4. Declaration of a static variable of type Object .

  5. Declaration of an instance variable of type int[] .


2.5 Java Source File Structure

The structure of a skeletal Java source file is depicted in Figure 2.2. A Java source file can have the following elements that, if present, must be specified in the following order:

  1. An optional package declaration to specify a package name . Packages are discussed in Section 4.6.

  2. Zero or more import declarations. Since import declarations introduce class and interface names in the source code, they must be placed before any type declarations. The import statement is discussed in Section 4.6.

  3. Any number of top-level class and interface declarations. Since these declarations belong to the same package, they are said to be defined at the top level , which is the package level.

    The classes and interfaces can be defined in any order. Class and interface declarations are collectively known as type declarations . Technically, a source file need not have any such definitions, but that is hardly useful.

    The Java 2 SDK imposes the restriction that at the most one public class definition per source file can be defined. If a public class is defined, the file name must match this public class. If the public class name is NewApp , then the file name must be NewApp.java .

    Classes are discussed in Section 4.2, and interfaces are discussed in Section 6.4.

Figure 2.2. Java Source File Structure

graphics/02fig02.gif

Note that except for the package and the import statements, all code is encapsulated in classes and interfaces. No such restriction applies to comments and white space.


Review Questions

graphics/rq_icon.gif
2.10

What will be the result of attempting to compile this class?

import java.util.*;

package com.acme.toolkit;

public class AClass {
    public Other anInstance;
}
class Other {
    int value;
}

Select the one correct answer.

  1. The class will fail to compile, since the class Other has not yet been declared when referenced in class AClass .

  2. The class will fail to compile, since import statements must never be at the very top of a file.

  3. The class will fail to compile, since the package declaration can never occur after an import statement.

  4. The class will fail to compile, since the class Other must be defined in a file called Other.java .

  5. The class will fail to compile, since the class Other must be declared public .

  6. The class will compile without errors.

2.11

Is an empty file a valid source file?

Answer true or false.