• 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

Another Whizlabs Question...

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one is from Whizlabs Diagnostic exam

According to the official answer this code compiles and prints "ABC".

I said that I did not think it would compile and here is why.

Down in the synchronized block in main, there is a Thread.sleep(4000) that is not enclosed in a try/catch block. I checked my API and it says that Thread.sleep() throws an InterruptedException which is not a RuntimeException and I would think that it would have to be caught.

Opinions?

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look closely. there is a throws in the main method...
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, thanks a bunch. You are catching the little things that I miss.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are we sure that it prints ABC?

Why not 123?
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you are confused about


s[0] refers to the first index of array s, it is NOT an assignment.
s[0] gets assigned "A" on Line 20


Hope this helps.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aakash,
My understanding is that only one string object was created on line 14 (new). It was passed to constructor of MyRunnable by reference, so s in MyRunnable refers to the same object on the heap.
So after main thread slept for 4 seconds it continue to put letters into this one array object and then released lock.
At that time t1 started executing run and printed ABC
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Danilou wrote:Hi Aakash,
My understanding is that only one string object was created on line 14 (new). It was passed to constructor of MyRunnable by reference, so s in MyRunnable refers to the same object on the heap.
So after main thread slept for 4 seconds it continue to put letters into this one array object and then released lock.
At that time t1 started executing run and printed ABC



On line 14, it is not a single String object being created, it is an array object that holds String objects. I think that each element placed in the array is its own String object.
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Danilou wrote:Hi Aakash,
...after main thread slept for 4 seconds it continue to put letters into this one array object and then released lock.
At that time t1 started executing run and printed ABC



Thanks, thats what I wanted to confirm.
 
Alexander Danilou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
W. Joe Smith - Correct! I should have said "one Array object" instead of "one string object". It refered by s
reply
    Bookmark Topic Watch Topic
  • New Topic