Constructors

Card Set Information

Author:
wgaynor
ID:
285124
Filename:
Constructors
Updated:
2014-10-13 18:30:08
Tags:
Java
Folders:
Java
Description:
Studying for OCA
Show Answers:

Home > Flashcards > Print Preview

The flashcards below were created by user wgaynor on FreezingBlue Flashcards. What would you like to do?


  1. Under what situations does a class get a default constructor?

    A: All classes get a default constructor
    B: You have to define at least one constructor to get the default constructor
    C: If the class does not define any constructors explicitly
    D: All classes get default constructor from Object class.
    E: None of the above
    C: If the class does not define any constructors explicitly
  2. Which 2 of these statements are true?
    A: All classes must explicitly define a constructor
    B: A constructor can be declared private
    C: A constructor can declare a return value
    D: A constructor must initialize all the member variables of a class
    E: A constructor can access the non-static members of a class
    • B and E
    • A constructor can be declared private
    • A constructor can access the non-static members of a class
  3. What will be the result of attempting to compile this;

    public class TestClass {
      long l1;
      public void TestClass(long pLong) { l1 = plong; } //1
      public static void main(String args[]) {
        TestClass a, b;
        a = new TestClass(); //2
        b = new TestClass(5); //3
      }
    }

    A: A compilation error will be encountered at (1), since constructors should not specify a return value.

    B: A compilation error will be encountered at (2), since the class does not have a default constructor.

    C: A compilation error will be encountered at (3).

    D: The program will compile correctly.

    E: It will not compile because parameter type of the constructor is different than the type of value passed to it.
    • C: A compilation error will be encountered at (3).
    • Because (1) is a method and not a constructor. So there is no constructor that take a parameter.
  4. Which 3 are a valid constructor for
    public class TestClass {
      A: public TestClass(int a, int b) { }
      B: public void TestClass(int a) { }
      C: public TestClass(String s);
      D: private TestClass(String s, int a) { }
      E: public TestClass(String s1, String s2) { }
    • A: public TestClass(int a, int b) { }
    • D: private TestClass(String s, int a) { }
    •   You can apply public, private, protected to a constructor. But not static, final, synchronized, native and abstract.
    • E: public TestClass(String s1, String s2) { }
    •   The compiler ignores the extra semi-colon.

What would you like to do?

Home > Flashcards > Print Preview