Friday 10 May 2013

Constructor - 12 -


Consider the following class declaration:Which of the following calls are legal to construct an instance of the Test class?


package net.dharmaraj;

public class Test
{
    public Test(double i)
    {
    }

    public void Test()
    {
    }

    public Test(int i, int j)
    {
    }

    public static void main(String[] x)
    {

        // here

    }
}




Choose answer(s)
a)Test t = new Test(1,2);  
b)Test t = new Test(1);  
c)Test t = new Test(1.0);  
d)Test t = new Test();

Constructor - 11 -


What is the result of executing the following code: Select the one right answer. 


package net.dharmaraj;

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.test(1.0, 2L, 3);
    }

    void test(double a, double b, /* float */short c)
    {
        System.out.println("1");
    }

    void test(float a, byte b, byte c)
    {
        System.out.println("2");
    }

    void test(double a, double b, double c)
    {
        System.out.println("3");
    }

    void test(int a, long b, int c)
    {
        System.out.println("4");
    }

    void test(long a, long b, long c)
    {
        System.out.println("5");

    }
}



a) 1 
b) 2 
c) 3 
d) 4 
e) 5 


Answer :-- C

Constructor - 10 -

 What will appear in the standard output when you run the Tester class? 


package net.dharmaraj;

public class Tester

{
    int var;

    Tester(double var)

    {
        // this.var;
        this.var = (int) var;
        System.out.println(this.var);
        System.out.println(var);
    }

    Tester(int var)

    {
        this("hello");
    }

    Tester(String s)

    {
        this();
        System.out.println(s);
    }

    Tester()

    {
        System.out.println("good-bye");
    }

    public static void main(String[] args)

    {
        Tester t = new Tester(5.0);
        // Tester t = new Tester(5);
    }
}



a) nothing 


b) "hello" 
c) 5 
d) "hello" followed by "good-bye" 
e) "good-bye" followed by "hello" 


Answer:--


5
5.0


Constructor - 9 -

What will happen if you compile/run the following code?


package net.dharmaraj;

public class Q21

{
    int maxElements;

    void Q21()// it takes as method , if we write blank constructor

    {
        maxElements = 100;
        System.out.println(maxElements);
    }

    /*

     * Q21() { maxElements = 100; System.out.println(maxElements); }
     */

    Q21(int i)

    {
        maxElements = i;
        System.out.println(maxElements);
    }

    public static void main(String[] args)

    {
        Q21 a = new Q21();
        Q21 b = new Q21(999);
    }
}




A) Prints 100 and 999.
B) Prints 999 and 100.
C) Compilation error at line 3, variable maxElements was not initialized.
D) Compillation error at line 19.



Constructor - 8 -


Given the following code how could you invoke the Base constructor that will print out the string "base constructor";


package net.dharmaraj;
class Base
{
    Base(int i)
    {
        System.out.println("base constructor");
    }

    Base()

    {
    }
}

public class Sup extends Base

{
    public static void main(String argv[])
    {
        Sup s = new Sup();

        // One

    }

    Sup()

    {
        super(10);
        // Two
    }

    public void derived()

    {

        // Three

    }
}




1) On the line After //One put Base(10); 
2) On the line After //One put super(10); 
3) On the line After //Two put super(10); 
4) On the line After //Three put super(10);


Answer:--

base constructor

Constructor - 7 -


Given the following class definition which of the following can  be legally placed after the comment line? 


package net.dharmaraj;

class Base


{

    public Base(int i)
    {
    }
}

public class Derived extends Base

{
    public static void main(String arg[])
    {
        Derived d = new Derived(10);
    }

    Derived(int i)

    {
        super(i);
    }

    Derived(String s, int i)

    {
        this(i);
        // Here

    }

}




A) Derived d = new Derived();  

B) super();  
C) this("Hello",10);  
D) Base b = new Base(10);  


Answer:--


D) Base b = new Base(10);  
Explanation: 
A) This is wrong because there is no matching constructor defined in Derived class. 
B) The super keyword suppose to be the first line in the constructor. 
C) The this keyword suppose to be first line in the constructor. 
D) This is correct because there is matching constructor in Base class. 


Constructor - 6 -

What will be printed when you execute the code?


package net.dharmaraj;

class A

{
    A()
    {
        System.out.println("Class A Constructor");
    }
}

public class B extends A

{
    B()
    {
        System.out.println("Class B Constructor");
    }

    public static void main(String args[])

    {
        B b = new B();
    }
}





A) Class A Constructor followed by Class B Constructor  
B) Class B Constructor followed by Class A Constructor
C) Compile time error
D) Run time error



Answer:- a

Class A Constructor
Class B Constructor