janki tangeda

Ranch Hand
+ Follow
since Jun 07, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by janki tangeda

Can static nested classes have static members?
Is it possible to declare any modifier for anonymous inner class?
If not, does it have any implicit modifiers?

For a static nested class what other modifiers can be specified?
Hi Niranjan,

Congrats!Its really good score for 10 days preparation.Please let me know
how you prepared for the exam.I have my exam one week later.I started preparing just 4 days ago.What are the important things we need to focus on for such short duration preparation?

Janki
16 years ago
I didn't notice that misplaced throws clause.Thankyou.
I read on this topic from K&B.From what i understood, RuntimeExceoptions are unchecked (and need not be thrown).If we throw a RuntimeException the compiler won't check if we have caught it or declared it.
i don't understand what's the problem with the code below.It's not getting compiled.

public class RuntimeExceptionTest throws RuntimeException{

public static void main(String[] args) {


throw new RuntimeException();
//}
//catch(RuntimeException re){
//re.printStackTrace();
//}

}
}
what are the legal ways to use a RuntimeException in your code?.Can we throw it without declaring it?Please explain.
if(myMap instanceof List) //ok - line 1 System.out.println("OK");
if(myMap instanceof Collection) //ok - line 2 System.out.println("OK");

Why does line1 and line 2 donot give any error..the List and Collection interface are in a different hierarchy when compared to myMap??
F is incorrect because the expression in F evaluates to two different values when a=o.b & b=o.a.that means different hashcodes for object that are equal according to equals().thus it violates the equals() contract.
thanks Sergei Iakhnin.now it's clear to me.

Scenario 3 - Lastly, if main method starts, creates t1, acquires lock on sa, changes sa[0] to "Done" and then gets switched out of the CPU and the run method of t1 gets switched in for execution it will never be able to execute the line System.out.print(sa[1] + sa[2] + sa[3]); because that line occurs inside a block synchronized on sa and the lock for sa would be held by main at that point. The only time it would give up the lock would be after it exits its own synchronized block.



In the above scenario what will happen when main menthod comes to line
sa.notify();
t1 can starting running only after main method finishes the execution of synchronized block but how will the main method execute notify() when t1 is not in waiting state.
Note that this is not a synchronized method, but a synchronized(...){} statement

Why this statement?what would happen if it were a synchronized method?
class A extends Thread {
String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}}
System.out.print(sa[1] + sa[2] + sa[3]);
}}
class B {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new A(sa); t1.start();
synchronized (sa) {
sa[0] = "Done"; //1
sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
sa.notify();
}}}

The given output for this program is ABC.The explanation is that thread t1 enters wait state and by the time notify gets called from main method all values are changed.
But by the time t1 gets to exeute run() method the main thread may execute line 1.Thus with sa[0]= "Done" t1 may not enter into wait state at all.Is there no such possibility?If no, why?
class A extends Thread {
String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {System.out.print(sa[0] + sa[1] + sa[2]);}
}}
class B {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {
Thread t1 = new A(sa); t1.start();
sa[0] = "A"; sa[1] = "B"; sa[2] = "C";
}}}

The output here is ABC and the explanation is that the t1 thread will be blocked until main method execution completes.I agree that the t1 thread is invoking a block synchronised on sa but that block is in a different class i.e class A???
Hi Joseph,

I understood why an assertion error will not be thrown from your explanation, but why the answers c & d are correct?How do we get these values as outputs?
got it..thanks Anurag
class B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}

Why are the first two implementations of hashcode not appropriate though it won't violate the hashcode error and won't give any error?