Java Questions 31 - 40  «Prev  Next»

Java Questions 37

  1. If a superclass defines its own constructor with parameters, what happens when the subclass tries to invoke super(); without using arguments?
    Answer:
    The subclass will receive an error when it tries to invoke super(); without using arguments.
    a) Arguments are used when you call a method and b) parameters are the elements in the method's signature.
  2. Are constructors inherited?
    Answer:
    Constructors are never inherited.
  3. Can constructors be overridden?
    Answer:
    Constructors cannot be overridden because they are not methods and only instance methods can be overridden.
  4. What type of polymorphism can occur with constructors?
    Answer:
    Static polymorphism can occur with constructors.
    Static Binding
    is known as method overloading (in the same class).
    Although constructors can not be overridden, constructors can be overloaded.
  5. What happens if you write your own constructor in a class (i.e. a constructor that takes a parameter)?
    Answer:
    If you write your own constructor in a class ( a constructor that takes a parameter), the compiler will not supply a default constructor.

  6. Can you inovke an instance method before the super constructor has run?
    Answer:
    You cannot inovke an instance method until after the super constructor has run.
  7. How do you create a String array in Java?
    Answer:
    String name = new String [] {"Alpha", "Beta", "Tau", "Sigma"};
  8. What must be the first line in a constructor?
    Answer:
    The first line in a constructor must be a call to super() or a call to this().
  9. Can a constructor make both a 1) call to super() and 2) a call to this()?
    Answer:
    A constructor can never have both a
    1. call to super and ()
    2. a call to this().
  10. Can you legally use both 1) this and 2) super() in the same constructor?
    Answer:
    You cannot legally use both 1) this and 2) super() in the same constructor. It is a requirements of the language that both appear on the first line of the constructor.