Home > Articles

  • Print
  • + Share This
This chapter is from the book

review.jpg Review Questions

  • 3.1 Which one of these declarations is a valid method declaration?

    Select the one correct answer.

    1. void method1         { /* ... */ }

    2. void method2()       { /* ... */ }

    3. void method3(void)   { /* ... */ }

    4. method4()            { /* ... */ }

    5. method5(void)        { /* ... */ }

  • 3.2 Which statements, when inserted at (1), will not result in compile-time errors?

    public class ThisUsage {
      int planets;
      static int suns;
    
      public void gaze() {
        int i;
        // (1) INSERT STATEMENT HERE
      }
    }

    Select the three correct answers.

    1. i = this.planets;

    2. i = this.suns;

    3. this = new ThisUsage();

    4. this.i = 4;

    5. this.suns = planets;

  • 3.3 Given the following pairs of method declarations, which statements are true?

    void fly(int distance) {}
    int  fly(int time, int speed) { return time*speed; }
    
    void fall(int time) {}
    int  fall(int distance) { return distance; }
    
    void glide(int time) {}
    void Glide(int time) {}

    Select the two correct answers.

    1. The first pair of methods will compile, and overload the method name fly.

    2. The second pair of methods will compile, and overload the method name fall.

    3. The third pair of methods will compile, and overload the method name glide.

    4. The first pair of methods will not compile.

    5. The second pair of methods will not compile.

    6. The third pair of methods will not compile.

  • 3.4 Given a class named Book, which one of these constructor declarations is valid for the class Book?

    Select the one correct answer.

    1. Book(Book b) {}

    2. Book Book() {}

    3. private final Book() {}

    4. void Book() {}

    5. public static void Book(String[] args) {}

    6. abstract Book() {}

  • 3.5 Which statements are true?

    Select the two correct answers.

    1. A class must define a constructor.

    2. A constructor can be declared private.

    3. A constructor can return a value.

    4. A constructor must initialize all fields when a class is instantiated.

    5. A constructor can access the non-static members of a class.

  • 3.6 What will be the result of compiling the following program?

    public class MyClass {
      long var;
    
      public void MyClass(long param) { var = param; }  // (1)
    
      public static void main(String[] args) {
        MyClass a, b;
        a = new MyClass();                              // (2)
        b = new MyClass(5);                             // (3)
      }
    }

    Select the one correct answer.

    1. A compile-time error will occur at (1).

    2. A compile-time error will occur at (2).

    3. A compile-time error will occur at (3).

    4. The program will compile without errors.

  • + Share This
  • 🔖 Save To Your Account