Declaring Methods  «Prev  Next»


Objects, Methods, and Initializers - Quiz

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

1. Which lines are displayed by the following program?
class SuperClass {

  String s = "super";

  void display(String s) {
    System.out.println("super: " + s);
  }
}

class Question extends SuperClass {

  String s = "this";
  public static void main(String[] args) {
    new Question();
  }

  Question() {
    display(super.s);
    super.display(s);
  }
  void display(String s) {
    System.out.println("this: " + s);
  }
}

Please select all the correct answers.
  A. this: this
  B. this: super
  C. super: super
  D. super: this

2. Which of the following modifiers apply to methods?
Please select all the correct answers.
  A. protected
  B. transient
  C. final
  D. static

3. What is the output of the following program?
class Question {

  public static void main(String[] args) {
     new Question();
  }

  Question() {
    StringBuffer s = new StringBuffer("abc");
    replace(s, "xyz");
    System.out.println(s);
  }

  void replace(StringBuffer s1, String s2) {
    s1 = new StringBuffer(s2);
  }

}

Please select the best answer.
  A. null
  B. abc
  C. xyz
  D. abcxyz

4. Which of the following are included in the output of the following program?
class Question {

  static {
    System.out.println("1");
  }

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

  Question() {
    System.out.println("3");
  }

  static {
    System.out.println("4");
  }

}

Please select all the correct answers.
  A. 1
  B. 2
  C. 3
  D. 4