aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes two questions,help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "two questions,help" Watch "two questions,help" New topic
Author

two questions,help

andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
i have got two questions from the mock exam JOP,and i hope for your help
question 1:
What will happend when you attempt to compile and run the following code?
public class T extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
T t = new T();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){

for(int i=0;i < 4; i++){
sName = sName + " " + i;

}
}
}
1) Compile time error
2) Compilation and output of "vandeleur wiggy"
3) Compilation and output of "vandeleur wiggy 0 1 2 3"
4) Compilation and probably output of "vandeleur" but possible output of "vandeleur 0 1 2 3"

The answer is : 4. Why?
Question 2:
public class MyClass{
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}
What modifiers would be legal at XX in the above code?
1) public
2) private
3) static
4) friend
The answer is:1,2,3. Why?
waitting for your help!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by andy lau:
The answer is : 4. Why?

Well, the reason it could be one or the other is that you don't know for sure which thread is going to get to use the processor when start is called. In one case, you could simply finish execution of main, printing out "vandeleur". On the other hand, if the new thread gets to use the processor right away, the run method may get executed, which changes the member variable sName. Then, when main is completed, "vandeleur 0 1 2 3" would be printed.
Actually, if you look closely at the answers, B and C can't possible be true - those cases could never happen. And, since there are no syntax errors, A is not correct, either. That only leaves you with choice D.

The answer is:1,2,3. Why?

When you declare and inner or nested class, you are free to use public private or static. Of course, if you use the keyword static, you have a nested class, not an inner class. The word "friend", however, isn't a keyword in Java - that's a C++ thing.
I hope that helps. If you have any more questions, just ask.
Corey


SCJP Tipline, etc.
Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
Just want to add something to the first question. B and C can't possibly be true because the scope of locale variable sName in Pigg() method, it doesn't change the sName of t object. For more info on related subject, see the following thread:
http://www.coderanch.com/t/237705/java-programmer-SCJP/certification/StringBuffer
wang jie
Greenhorn

Joined: May 01, 2002
Posts: 8
what is the difference between the inner class and nested class?
thanks!
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
Corey McGlone :
you said :Actually, if you look closely at the answers, B and C can't possible be true - those cases could never happen.
Can you explain why? thanks a lot!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by wang jie:
what is the difference between the inner class and nested class?
thanks!

Wang, this question really has no relation to the initial question of this post. If you'd like to post such a question, either reply to a thread that discusses inner and nested classes or start a brand new thread. Actually, there is a lot of information in this forum already on these topics, so you can probably just do a search and find what you're looking for.
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by andy lau:
you said :Actually, if you look closely at the answers, B and C can't possible be true - those cases could never happen.
Can you explain why?

Notice that the actual output is generated in the method main. From there, you're printing the contents of the static member sName.
However, in the definition of piggy, there is a parameter named sName, just like the static member variable. Therefore, any changes to sName within the method piggy change the local variable (the parameter), not that static member variable, because the local variable hides the member variable.
Therefore, the method piggy can not modify the member variable sName. Hence, the word "wiggy" can never appear in the output.
I hope that helps,
Corey
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
hooo! i am so careless!!
thank you very very much!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: two questions,help