Friday, 10 May 2013

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.



No comments:

Post a Comment