• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question 19 from Green's Mock

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,



Here are the list of answers


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 output of either "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"




The answer is given as 4, but i dont really undersatnd how come wiggy is missing in the answer? where has it gone to??
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clue:how many variables are called sName in this program?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasad Tamirisa

In this code you have 2 sName variables.
1. static String sName = "vandeleur";
2. public void piggy(String sName){}

In the method piggy you are updating the local varible sName.

in run() method you accessing the static String sName ;
So the changes made in piggy() method won't reflect in public void run(){) method.

This is my explanation...
am i right....?
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops,

I think i got confused. Its pretty clear now. Thanks barry and suresh.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, what will be the exact answere??

I think the answere should be vandeleur 3.

Am I correct??
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Ok, I got the answere.
It should be "vandeleur 0 1 2 3"!!!
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi krishna,

the output depends on the timing of the two threads. So answer 4 is correct.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred,
you said:

It depends on the timing of two threads.


This question is pretty straight forward and is based on Shadowing concept...I think!
I dont find the question of threads here. Are you meaning two threeads as one main thread and the thread t?
please clarify..

Thanks
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Two things to consider here:
1- Shadowing the variable sName;
2- Two threads are working in the program
i) main thread
ii) thread t

The output of the program depends when does the line
System.out.println(sName); of the main() get executed.
If it is executed before loop starts, the output will be different,
if it executed in between any cycle of the loop or after the loop completes,
the output will be different.

Therefore no guaranteed output.

Thanks,
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic