• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to deal such Q on thread??

 
Arpana Rai
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above Q is from Dan's exam.I answered 'g' as we cannot determine how the thread scheduler work.Giving the same reason correct ans given by Dan are "a,b,c,d"(all the possible outputs).

Someone plz tell me how to tackle such Qs.
thanx in advance
Arpana
 
Levente Szekrenyes
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Dan's right, all the variants are "possible" outputs of the program. By choosing g, you're basically saying that none of the variants given can ever occur as an output.
However, you're also right, that this question might be a little bit ambiguos. But rest assured, the real exam questions are very well tested linguistically, so you'll have no doubts there. The wording is very clear so you'll know exactly what you're asked to answer.
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiles and do not generate any runtime error so e) and f) options are incorrect.
As you can not determine how the thread will be scheduler you dont know the order of the execution of the following lines:

in class A, and,

in class B.
As a result:
- if you 1 is completly executed prior to 2, you get "XYZ". (option A)
- if you 1 is completly executed after 2, but before 3, you get "AYZ". (option B)
- if 1 is executed between 3 and 4 you get printed "ABZ". (option c)
- and if 1 is executed after 4 you obtain "ABC"
(option d)
So the correct answer is a,b,c,d.
HTH
 
Keen Chen
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
because u can't determine when the thread acturally to run after u invoked start() method.
in this question, it maybe run before the assignment, of after.
// maybe thread run
sa[0] = "A";
// maybe thread run
sa[1] = "B";
// maybe thread run
sa[2] = "C";
// maybe thread run
 
Ben Ritchie
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But rest assured, the real exam questions are very well tested linguistically, so you'll have no doubts there. The wording is very clear so you'll know exactly what you're asked to answer.

<offtopic>
This is generally true - however, there were several questions in my exam which I found poorly worded and one Collections-related question which was very unclear. I had to guess between two answers which were, to me, completely reasonable.
I picked the right one
</offtopic>
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads are a fun... they definitely tweak your mind to think about them.
  • Thread Schedulers are platform dependent -- the currently executing thread can be put from the "Running" state to "Ready-to-Run" at virtually anytime.
  • calls to the .start() method returns control immediately to the method that called it.
  • calls to the .run() method do not return immediately.
  • figure out during which situations an IllegalStateException can be thrown.

  • So... for the code snipped you included it's *possible* that in between the concantenation of the System.out.println() String... control could be returned to the main() method where the values of the sa[] array is changed.
    It's also *possible* that in between the calls for those values being changed -- control could be returned BACK to the printing thread -- and something like "AYZ" could be pritned.
    The main thing with threads quesitons is to figure out WHAT is possible.
    What would be the answer if you changed the call of t1.start() to t1.run() ??
    [ December 12, 2002: Message edited by: Jessica Sant ]
     
    Levente Szekrenyes
    Greenhorn
    Posts: 29
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ben,
    You took the 1.4 exam, while I took 1.2 . Should we conclude that the 1.2 version is much better designed and more precisely worded? Because I really meant what I said, I didn't have any interpretation problems during the exam (and I'm not a native English speaker)
     
    Alfred Kemety
    Ranch Hand
    Posts: 279
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What would be the answer if you changed the call of t1.start() to t1.run() ??

    Sure XYZ as you make a direct invocation to the method run, not inistanciate a new thread, so the method has to be executed and returned before the array elements are changed to ABC
    Levente, the 1.4 is new, still being revised, 1.2 has been there for quite a while... I think...
     
    Levente Szekrenyes
    Greenhorn
    Posts: 29
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Alfred Kemety:

    Levente, the 1.4 is new, still being revised, 1.2 has been there for quite a while... I think...

    Is the 1.4 still being revised? I mean, is it possible that the questions might change?
    There was a beta-testing phase for 1.4 (Sun even offered free vouchers for that), and I thought that after this phase the exam was final and nothing will ever change about it (question wording, format etc)
    But this is really off-topic, so perhaps we should open another topic...or just leave it to the bartenders to move these posts somewhere else

    [ December 12, 2002: Message edited by: Levente Szekrenyes ]
     
    Dan Chisholm
    Ranch Hand
    Posts: 1865
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Arpana Rai:

    The above Q is from Dan's exam.I answered 'g' as we cannot determine how the thread scheduler work.Giving the same reason correct ans given by Dan are "a,b,c,d"(all the possible outputs).

    Someone plz tell me how to tackle such Qs.
    thanx in advance
    Arpana

    Mock exams are usually intended to be more challenging than the real exam because a mock exam that is less challenging would be dangerously misleading. For that reason, mock exam authors usually do not specify how many correct answers are included in the answer options. The real exam is a little easier because the number of correct answers are specified.
    If this question appeared on an exam that specifies the number of correct answers for each question then it would be obvious that the four correct answers are a, b, c and d. For that reason, a question on the real exam must be written such that the specified number of correct answers does not make the correct answer set obvious.
    My question is worded as follows.

    What are the possible results of attempting to compile and run the program?
    a. Prints: XYZ.
    b. Prints: AYZ.
    c. Prints: ABZ.
    d. Prints: ABC.
    e. Compiler error.
    f. Run time error.
    g. None of the above.

    Since the questions asks for "the possible results" the reader knows that more than one answer option might be correct. Therefore, it is necessary to look at each option to determine if the result is "possible". There is no doubt that looking at each option is a lot of work, but that is part of the learning process.
    The real exam is not intended to be a part of the learning process so the questions will not be designed to offer a learning opportunity. Instead, the question are designed to determine what has already been learned. For that reason, the questions on the real exam are not necessarily like the questions on my exam.
    The real exam would be more likely to word the question as follows.

    What is the result of attempting to compile and run the program? (Select 1)
    a. Prints: XYZ.
    b. Prints: AYZ.
    c. Prints: ABZ.
    d. Prints: ABC.
    e. Compiler error.
    f. Run time error.
    g. Can not be determined.

    On the real exam you could select option g as soon as you find two answers that could be correct. On my exam you would still have to check each option. My question requires a little more thought and a little more time but I think that a mock exam should be a little more challenging.
     
    Arpana Rai
    Ranch Hand
    Posts: 93
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanx Dan, nowonwars i will read Q more carefully.
    regds
    Arpana
     
    Barkat Mardhani
    Ranch Hand
    Posts: 787
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have taken a break from Java for a while so this might sound very basic question:
    sa is local array in main(). It's value (ie. xyz) is passed to the constructor of A class. So t1 object's sa is set to xyz. So why changing sa locally in main() would effect the value of t1.sa?
    Thanks
     
    Dan Chisholm
    Ranch Hand
    Posts: 1865
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Barkat Mardhani:
    I have taken a break from Java for a while so this might sound very basic question:
    sa is local array in main(). It's value (ie. xyz) is passed to the constructor of A class. So t1 object's sa is set to xyz. So why changing sa locally in main() would effect the value of t1.sa?
    Thanks

    The array is passed by reference to the constructor of class A. The contents of the array are not copied to class A. When a change is made to the contents of the array the change is visible in both class A and class B.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic