aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Six java questions: Could anyone answer 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 "Six java questions: Could anyone answer" Watch "Six java questions: Could anyone answer" New topic
Author

Six java questions: Could anyone answer

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
How could you coordinate two threads that execute simultaneously inside synchronized methods of the same object?
Choice 1
place the threads in the same ThreadGroup and let the ThreadGroup acquire the object's monitor
Choice 2
dynamically control the priorities of the threads within the method
Choice 3
use Thread.yield() to switch between threads at set points
Choice 4
use Thread.wait() and Thread.notify()
Choice 5
use Thread.sleep()

Question 2.
int x=3, y=4, z=5;
switch(y) {
case 2:
x *= y;
case 4:
y = (x + z)/y;
case 6:
z += y++;
break;
case 8:
z++;
default:
x -= y;
}
What are the values of x, y, and z after execution?
Choice 1
x=12, y=5, z=10
Choice 2
x=12, y=5, z=9
Choice 3
x=3, y=3, z=7
Choice 4
x=0, y=3, z=8
Choice 5
x=3, y=3, z=8
Question 3
double dble;
byte bt = 99;
What is the correct way to cast a byte to a double?
Choice 1
dble (double) = bt
Choice 2
dble = bt(double)
Choice 3
you can not cast a byte to a double
Choice 4
dble = double(bt)
Choice 5
dble = (double) bt
Question 4
String readString() {
char buf[] = new char[80];
Reader in = new InputStreamReader(System.in);
in.read(buf, 0, 80);
return new String(buf);
}
What is wrong with method readString()?
Choice 1
String objects can not be instantiated with a character array
Choice 2
an InputStreamReader object can not be bound to standard input
Choice 3
all IO methods must be declared public
Choice 4
logic to catch exceptions for the InputStream statements is missing
Choice 5
you can not cast InputStreamReader into Reader
Question 5
class A {
int i, j, k;
public A() { i=3; }
public A(int i1) { i = i1; }
public A(int i1, int k1) {
this(i1);
k = k1;
}
}
What is wrong with class A?
Choice 1
methods can not invoke other methods in their own class
Choice 2
there are multiple methods named A()
Choice 3
nothing
Choice 4
all methods must declare return datatypes
Choice 5
a method can not have the same name as its class
Question 6
If memory may be running low, what could free some up?
Choice 1
call destroy() on any objects you no longer need
Choice 2
make more methods synchronized
Choice 3
invoke System.gc()
Choice 4
reduce the priority of less important threads
Choice 5
invoke System.garbageCollection()
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
The answer to Question 2 :
1. Switch statement starts executing with y=4
2. The case 4: block gets executed . Here
x=3
z=5
y=2 ( by expression : (3+5)/2)
3. As no break statement case 6: also gets executed
x=3
z=(5+2)=7
y=3 ( as y++ )
So choice3 is correct
Output is x=3, y=3,z=7

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
>> The answer to Question 5
class A {
int i, j, k;
public A() { i=3; }
public A(int i1) { i = i1; }
public A(int i1, int k1) {
this(i1);
k = k1;
}
}
What is wrong with class A?
Choice 1
methods can not invoke other methods in their own class
Choice 2
there are multiple methods named A()
Choice 3
nothing
Choice 4
all methods must declare return datatypes
Choice 5
a method can not have the same name as its class
Choice 3
Nothing is wrong with the above code
======================================================================
>> The answer to Question 6
If memory may be running low, what could free some up?
Choice 1
call destroy() on any objects you no longer need
Choice 2
make more methods synchronized
Choice 3
invoke System.gc()
Choice 4
reduce the priority of less important threads
Choice 5
invoke System.garbageCollection()
Choice 3 invoke System.gc()
Farhan Tariq
Ranch Hand

Joined: May 14, 2001
Posts: 54
Hi chiran!!
Answer to question no:1 is Choice#1
Because

Choice 2
dynamically control the priorities of the threads within the method

Change of priorities won't result on the lock

Choice 3
use Thread.yield() to switch between threads at set points

same the case yield method is for the execution of the thread which have same priority level but again lock will not be lost.
and the other thread waits for the lock.
and same is the case with the rest options. The first option clears that ThreadGroup have the lock so threads related to that threadgroup only acquire the lock and run simultaneously.
In question 3 option no 5 is correct
However we don't need to cast byte into double.
In question 4 option no 4 is correct
Because read() of InputStreamReader class throws IOException which is checked exception and must be caught or define in the throws clause of readString method as "String readString() throws java.io.IOException{"
hope the above answers clear to u
tell me if i'm wrong
Regards
Farhan
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
Answer to 2:
Choice 3
x=3, y=3, z=7
Answer to 3:
Choice 5
dble = (double) bt
Answer to 5:
Choice 3
nothing
Answer to 6:
Choice 3
invoke System.gc()
(only suggests the gc to run ...)
get back if u have anydoubts in anything specific
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
Answer to 2:
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
On question #1, shouldn't the correct answer be choice #4?
If I remember correctly, wait is supposed to let go of any locks, so all other threads are allowed in
Am I wrong?
/Mike
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
i think answer to first question is choice 1.otherwise it may not be possible for two threads to execute a synchronised method symultaneously.correct me if i am wrong!!!
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94

Q2 - 3
Q3 - 5
Q4 - 4
Q5 - 3
Q6 - 3 (I think option 1 should also be correct)
Please correct me if i am wrong
Jyotsna

[This message has been edited by Jyotsna Umesh (edited June 08, 2001).]
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Jyotsna
I don't think Java has a destroy() method unless you explicitly create your own.
Originally posted by Jyotsna Umesh:

Q2 - 3
Q3 - 5
Q4 - 4
Q5 - 3
Q6 - 3 (I think option 1 should also be correct)
Please correct me if i am wrong
Jyotsna
[This message has been edited by Jyotsna Umesh (edited June 08, 2001).]


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
Posts: 64
I think Mikael is correct - the answer to 1 is 4. When a thread enters synchronized code it aquires the object's monitor. No other thread can enter ANY synchronized code in that object until the monitor is relinquished. When the original thread calls wait() it gives up the monitor until notified.

[This message has been edited by Tod Tryk (edited June 08, 2001).]
Yasir Qureshi
Ranch Hand

Joined: Apr 27, 2001
Posts: 44
Well for the first question, If a method is synhronized then how come to threads simultaneously execute it.
Yasir
Farhan Tariq
Ranch Hand

Joined: May 14, 2001
Posts: 54
Hi yasir!
That's why I select option no 1 for the first question, because two threads can't share simultaneously one synchronized method unless they are part of ThreadGroup and that ThreadGroup acquires the lock. There is no point of getting lock by one thread and releasing lock by wait().
Regards
Farhan
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
i also suggested choice 1 for question#1.but if a thread enters a synchronised method and call its wait() method its lock is released and another thread can enter the same method.when the second thread call notify(),then all the threads waiting can try for lock.
Denis Anoykin
Greenhorn

Joined: Jun 11, 2001
Posts: 8
Jeena and Farhan
For question #1 I can't understand what you mean when you suggest to use ThreadGroup object. I created sample where each thread is in different ThreadGroup and these threads execute synchronized method simultaneously. Why answer #4 for question #1 is incorrect?
public class Test {
public Test() {
}
public static void main(String strs[]) throws Exception {
Test t = new Test();
Thread t1 = t.new ThreadWait("Thread 1");
Thread t2 = t.new ThreadWait("Thread 2");
Thread t3 = t.new ThreadWait("Thread 3");
Thread t4 = t.new ThreadNotifyAll("Thread 4");
System.out.println ("Thread Groups for threads:");
System.out.println (t1.getThreadGroup());
System.out.println (t2.getThreadGroup());
System.out.println (t3.getThreadGroup());
System.out.println (t4.getThreadGroup());

t1.start();
t2.start();
t3.start();
Thread.currentThread().sleep(200);
t4.start();
}
public class ThreadNotifyAll extends Thread {
public ThreadNotifyAll(String str) {
super(new ThreadGroup(str), str);
}
public void run() {
threadNotifyAllMethod();
}
}
public class ThreadWait extends Thread {
public ThreadWait(String str) {
super(new ThreadGroup(str), str);
}
public void run() {
threadWaitMethod();
}
}
public synchronized void threadNotifyAllMethod() {
System.out.println(Thread.currentThread().getName() + " notifyAll - (1)");
this.notifyAll();
System.out.println(Thread.currentThread().getName() + " notifyAll - (2)");
}
public synchronized void threadWaitMethod() {
System.out.println(Thread.currentThread().getName() + " wait - (1)");
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " wait - (2)");
}
}
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
I believe that the reason #4 is incorrect for question 1 is that the wait() method needs to be called by the object with the synchronized method, not by calling a static "Thread.wait()" -- in fact, there is no such method listed in the API. A thread object will have a wait() instance method inherited from Object, but that method will not cause a thread executing an object's synchronized method to release the object's lock. You would need to call this.wait() or simply wait() within the synchronized code -- which is exactly what Denis's code is doing.
[This message has been edited by Scott Appleton (edited June 11, 2001).]
Denis Anoykin
Greenhorn

Joined: Jun 11, 2001
Posts: 8
I agree that #4 answer on question #1 isn't right formulated. But in this answer I see keywords 'wait()' and 'notify()'.
I can't understand how we can use other methods (in other answers)?
I see that some persons suggest that #1 answer is right. Would they explain this more detailed?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Six java questions: Could anyone answer