Sunday, 26 July 2009

Java Certification Question 0045

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

public class Sandys{

private int court;

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



Answer 4:

4) Compilation and run with an output of 99

The fact that the variable court is declared as private does not stop the constructor from being able to initialise it.

No comments: