aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to deal such Q on thread?? 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 "how to deal such Q on thread??" Watch "how to deal such Q on thread??" New topic
Author

how to deal such Q on thread??

Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93

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


SCJP1.4(91%)
Levente Szekrenyes
Greenhorn

Joined: Mar 08, 2002
Posts: 29
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

Joined: Feb 04, 2002
Posts: 231
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

Joined: Nov 12, 2002
Posts: 47
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


SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
Ben Ritchie
Ranch Hand

Joined: Nov 18, 2002
Posts: 98

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>


SCJP1.4, SCJD, SCEA (in progress)
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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 ]

    - Jess
    Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
    Levente Szekrenyes
    Greenhorn

    Joined: Mar 08, 2002
    Posts: 29
    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

    Joined: Aug 14, 2002
    Posts: 279
    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...


    Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
    Levente Szekrenyes
    Greenhorn

    Joined: Mar 08, 2002
    Posts: 29
    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

    Joined: Jul 02, 2002
    Posts: 1865
    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.


    Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
    Arpana Rai
    Ranch Hand

    Joined: Nov 12, 2002
    Posts: 93
    Thanx Dan, nowonwars i will read Q more carefully.
    regds
    Arpana
    Barkat Mardhani
    Ranch Hand

    Joined: Aug 05, 2002
    Posts: 787
    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

    Joined: Jul 02, 2002
    Posts: 1865
    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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: how to deal such Q on thread??