GoConqr has a new look! Let us know what you think.

Quiz

01-Objective-Wise-Constructors

Wong Fei Hung 10 questions

Take the quiz

Questions in this quiz (10)

  1. 1Which line contains a valid constructor in the following class definition?
    public class TestClass{
    int i, j ;
    public TestClass getInstance( ) { return new TestClass( ) ; } //1
    public void TestClass( int x, int y) { i = x; j = y; } //2
    public TestClass TestClass( ) { return new TestClass( ) ; } //3
    public ~TestClass( ) { } //4
    }
    1. ALine 1
    2. BLine 2
    3. CLine 3
    4. DLine 4
    5. ENone ofthe above.
  2. 2What will be the result ofattempting to compile the following program?
    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)
    }
    }
    1. AA compilation error will be encountered at (1), since constructors should not
      specify a return value.
    2. BA compilation error will be encountered at (2), since the class does not have a
      default constructor.
    3. CA compilation error will be encountered at (3).
    4. DThe program will compile correctly.
    5. EIt will not compile because parameter type ofthe constructor is different than the
      type ofvalue passed to it
  3. 3Which ofthese statements are true?
    1. AAll classes must explicitly define a constructor.
    2. BA constructor can be declared private.
    3. CA constructor can declare a return value.
    4. DA constructor must initialize all the member variables ofa class.
    5. EA constructor can access the non-static members ofa class.
  4. 4Note: This question may be considered too advanced for this exam. Given:
    class MySuper{
    public MySuper( int i) { }
    }a
    bstract class MySub extends MySuper{
    public MySub( int i) { super( i) ; }
    public abstract void m1( ) ;
    }c
    lass MyTest{
    public static void main( String[ ] args) {
    MySub ms = new MySub( ) {
    public void m1( ) { System. out. println( " In MySub. m1( ) " ) ; }
    } ;
    ms. m1( ) ;
    }
    }Wha
    t will be the output when the above code is compiled and run?
    1. AIt will not compile.
    2. BIt will throw an exception at run time.
    3. CIt will print In MySub. m1( )
    4. DIt will compile and run without any exception but will not print anything.
  5. 5Which ofthe following are true about the "default" constructor?
    1. AIt is provided by the compiler only ifthe class and any ofits super classes does not
      define any constructor.
    2. BIt takes no arguments
    3. CA default constructor is used to return a default value.
    4. DTo define a default constructor, you must use the default keyword.
    5. EIt is always public.
  6. 6Which lines contain a valid constructor in the following code?
    public class TestClass{
    public TestClass( int a, int b) { } // 1
    public void TestClass( int a) { } // 2
    public TestClass( String s) ; // 3
    private TestClass( String s, int a) { } //4
    public TestClass( String s1, String s2) { } ; //5
    }
    1. ALine // 1
    2. BLine // 2
    3. CLine // 3
    4. DLine // 4
    5. ELine // 5
  7. 7Under what situations does a class get a default constructor?
    1. AAll classes in Java get a default constructor.
    2. B. You have to define at least one constructor to get the default constructor.
    3. CIfthe class does not define any constructors explicitly.
    4. DAll classes get default constructor from Object class.
    5. ENone ofthe above.
  8. 8You can call only public and protected constructors ofthe super class from a subclass
    ifthe subclass is not in the same package because only those are inherited.
    1. ATrue
    2. BFalse
  9. 9Given a class named Test, which ofthese would be valid definitions for the
    constructors for the class?
    1. ATest( Test b) { }
    2. BTest Test( ) { }
    3. Cprivate final Test( ) { }
    4. Dvoid Test( ) { }
    5. Epublic static void Test( String args[ ] ) { }
  10. 10Given the following code, which ofthese constructors can be added to class B without
    causing a compile time error?
    class A{
    int i;
    public A( int x) { this. i = x; }
    }c
    lass B extends A{
    int j ;
    public B( int x, int y) { super( x) ; this. j = y; }
    }
    1. AB( ) { }
    2. BB( int y ) { j = y; }
    3. CB( int y ) { super( y*2 ) ; j = y; }
    4. DB( int y ) { i = y; j = y*2; }
    5. EB( int z ) { this( z, z) ; }

Information

Description
OCA-SE-7 Objective-Wise-Ethuware-OCA-SE-7 Quiz on 01-Objective-Wise-Constructors, created by Wong Fei Hung on 30/07/2017.
Language
Português
Views
0
Rating
No ratings yet
Rate this quiz
Rate this quiz

Feedback

What is it about?

What do you think of the new look?

How do you like it?