• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Six java questions: Could anyone answer

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> 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()
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]


 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well for the first question, If a method is synhronized then how come to threads simultaneously execute it.
Yasir
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)");
}
}
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic