Declaring Methods  «Prev  Next»


Java Review - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 

1. Which of the following declare an array of String objects?
Please select all the correct answers.
  A. String[] s = {"1","2","3"};
  B. String[]s;
  C. String s[];
  D. []String s;

2. What does the void return type indicate about a method?
Please select all the correct answers.
  A. The method does not return a value.
  B. The method is invalid and may not be used.
  C. The method may not be accessed outside of the class in which it is declared.
  D. The method may not be overridden.

3. Which of the following are true about static variables?
Please select all the correct answers.
  A. They may not be modified once they have been assigned a value.
  B. They may not be serialized.
  C. They may not be accessed outside of the class in which they are declared.
  D. They are shared among all instances of the class in which they are declared.

4. What is wrong with the following program?

class Question {

  String s = "abc";

  public static void main(String[] args) {
    System.out.println(s);
  }

}


Please select all the correct answers.
  A. Question should be declared as static.
  B. Since main() is static it may not access non-static s without a reference to an instance of Question.
  C. main() should not be declared as static.
  D. args should be declared as static.

5. Which of the following may be declared as static?
Please select all the correct answers.
  A. Field variables
  B. Local variables
  C. Methods
  D. Initializers

6. Which of the following is true about overloaded methods?
Please select the best answer.
  A. They have the same name.
  B. They have the same parameter list.
  C. They have the same return type.
  D. They must be declared as final.

7. Which of the following restrictions apply to method overriding?
Please select all the correct answers.
  A. The overridden method must not be final.
  B. The overriding and overridden methods must have different return types.
  C. The overriding and overridden methods must have the same access modifiers.
  D. The overriding and overridden methods must throw the same exceptions.

8. Which lines of output are displayed by the following program?
class Question {

  public static void main(String[] args) {
    Class2 v2 = new Class2();
    v2.display();
  }
}
class Class1 {

  String s = "Class1";

  void display() {
    System.out.println(s);
  }
}

class Class2 extends Class1 {

  String s = "Class2";
}

Please select all the correct answers.
  A. Class1
  B. Class2
  C. Class1Class2
  D. Class2Class1