Friday, 10 May 2013

Constructor - 5 -


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 MyOver extends Base

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

    MyOver(int i)

    {
        super(i);
    }

    MyOver(String s, int i)

    {
        this(i);
        // Here

    }


}






1)MyOver m = new MyOver();

2)super();
3)this("Hello",10);
4)Base b = new Base(10);

Constructor - 4 -

Given the following code what will be the output?



package net.dharmaraj;

class ValHold

{
    public int i = 10;
}

public class ObParm

{
    public static void main(String argv[])
    {
        ObParm o = new ObParm();
        o.amethod();
    }

    public void amethod()

    {
        int i = 99;
        ValHold v = new ValHold();
        v.i = 30;
        System.out.println("1st-->    " + i);
        another(v, i);

        System.out.println("2nd-->    " + v.i);

    }// End of amethod

    public void another(ValHold v, int i)// 

    {
        i = 0;
        v.i = 20;
        ValHold vh = new ValHold();
        v = vh;
        System.out.println(v.i + " " + i);
    }// End of another
}






1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20



Answer:--


1st-->    99
10 0
2nd-->    20

Constructor - 3 -

What will happen when you attempt to compile and run the following code



package net.dharmaraj;

class Base {

protected int i = 99;
}

public class Ab {

private int i = 1;

public static void main(String argv[]) {

Ab a = new Ab();
System.out.println(a.i);
a.hallow();
}

void hallow() {

System.out.println("Claines " + i);
}
}





1) Compile time error
2) Compilation and output of Claines 99
3) Compilation and output of Claines 1
4) Compilation and not output at runtime


Answer:--
1
Claines 1


Constructor - 2 -

What will happen when you attempt to compile and run the following code?


package net.dharmaraj;

public class Sandys {

public int court = 100;

public static void main(String argv[]) {


Sandys s = new Sandys(99);

System.out.println(s.court);
}

Sandys(int ballcount) {

court = ballcount;
}
}





1) Compile time error, the variable court is defined as private
2) Compile time error, s is not initialized when the System.out method is called
3) Compilation and execution with no output
4) Compilation and run with an output of 99

Constructor - 1 -


You have a public class called myclass with the main method defined as follows 

package net.dharmaraj;


public class dev {

public static void main(String parm[]) {
System.out.println(parm.length);
System.out.println(parm[1]);

}

}



If you attempt to compile the class and run the program as follows 
java myclass hello 
What will happen? 
1) Compile time error, main is not correctly defined 
2) Run time error, main is not correctly defined 
3) Compilation and output of java 
4) Compilation and output of hello