• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

two questions,help

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
https://coderanch.com/t/237705/java-programmer-SCJP/certification/StringBuffer
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between the inner class and nested class?
thanks!
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hooo! i am so careless!!
thank you very very much!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic