File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes Jxam #47 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "Jxam #47" Watch "Jxam #47" New topic
Author

Jxam #47

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
"Consider the following piece of code, and select the statements which are true."
class Test{
int x = 5;
static String s = "abcd";
public static void method(){
System.out.println(s + x);
}
}
1)The code compiles and displays "abcd5".
2)The code compiles, but throws an exception at runtime, because variable x isn't declared as static.
3)The code fails to compile because you can't make a static reference to a non-static variable.
4)The code will compile and display "abcd5" if x is declared to be static.
5)The code will compile by removing the static keyword from the declaration of method().
Correct Answers: 3,4,5
I didn't select 4) .
There's no call to method().
So eventhough a correction of the source would be made, with x declared static, nothing is displayed.
JRoch
VVed
Greenhorn

Joined: Apr 07, 2000
Posts: 3
hi JRoch
class Test{
int x = 5;
static String s = "abcd";
public static void method(){
System.out.println(s + x);
}
}
in the above code method() is static hence it will be executed once. there is no need for a call to method(). and the answer 4 is perfectly right if int x is made static then the above code will display "abcd5"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
There are in current JAVA only 2 sort of static blocks of code that get executed without explicit calls:
1/ static initializers:
static {
..
}
2/ and the main method:
public static void main(String args[])
The static 'method()' of our Mock question is none of them.
You might have thought of it as the real 'main' method.
(As there is no such main in this code, it compiles once variable x is made static, but does not run when called from the cmd line)
JRoch

[This message has been edited by JRoch (edited April 07, 2000).]
VVed
Greenhorn

Joined: Apr 07, 2000
Posts: 3
JRoch u are right.
chandra sekhar
Greenhorn

Joined: Apr 21, 2000
Posts: 13
I think the correct answers are 3 and 4 but not 5 bcoz if we make that method non static still sting type is static ,so 3 and 4 are corrct .Can any one match their opinion with my answer?


chandu
suren r
Greenhorn

Joined: Sep 01, 2000
Posts: 7
3,4,5 are right...
if you remove static from the method it will compile fine but it won't give any result...
as per the question option 5 is right...
R Whelan
Greenhorn

Joined: Oct 18, 2000
Posts: 1
Just a clarification to what chandra suggested -- you can't make a static reference to a non-static variable, because it isn't initialized until the instance is created, but it doesn't work the other way around. After all, static variables are initialized when the class is loaded, so they will definitely be accessable to instances (i.e., code in a non-static context).
krishna kumar
Greenhorn

Joined: Nov 18, 2000
Posts: 4
answers 3 and 5 are right..
with out main u cant get the out put.
Only 2 types of static methods are executed with out calling.
Those are
1) static{........}
2) public static void main(String []args)
The method "method()" will not execute without calling.....
So answers 3 & 5 are correct...but not 4.


Krishna Kumar R<BR>CBSI,6 th Floor<BR>B Towers<BR>Airport Rd<BR>Bangalore<P>U can reach me at yourskk@usa.net
 
 
subject: Jxam #47
 
Threads others viewed
going bananas with this one
Static Initializer Code
Why the variable should be declared as final?
Questions from Jxam
Static Reference
Two Laptop Bag