Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jxam #47

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JRoch u are right.
 
chandra sekhar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
suren r
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic