Monday, 20 July 2009

Java Certification Question 0040

Given the following code

class Base{
static int oak=99;
}

public class Doverdale extends Base{

public static void main(String argv[]){
Doverdale d = new Doverdale();
d.amethod();
}
public void amethod(){
//Here
}
}

Which of the following if placed after the comment //Here, will compile and modify the value of the variable oak?
1) super.oak=1;
2) oak=33;
3) Base.oak=22;
4) oak=50.1;



Answer :
1) super.oak=1;
2) oak=33;
3) Base.oak=22;
Because the variable oak is declared as static only one copy of it will exist. Thus it can be changed either through the name of its class or through the name of any instance of that class. Because it is created as an integer it canot be assigned a fractional component without a cast.

No comments: