Prajakta Vaidya

Greenhorn
+ Follow
since Oct 10, 2007
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 Prajakta Vaidya



This code compiles fine, but at runtime gives ClassCastException.

Is it because the class Person does not have equals() and hashcode() methods defined ?
Given:


Answer: B

I guessed the answer as B and it is correct, but I don't know the reasoning behind it.

Can anyone please explain ?
I am preparing for SCJP, I think I am good at other topics (this conclusion is from the mock tests I have solved) but I am not very comfortable with questions on "Threads" topics. Is there any tutorial available online that explains the concepts thoroughly.

I have gone through the chapter on threads in many books, I understand the theory but when it comes to code-based questions I am not able to guess the o/p
here is a sample code from 1 of the mock exams , the start() method of the Thread class in not static, here the method is called just as start() . so the code should give compiler error, is that rite ?


public class Tux extends Thread
{
static String sName = "Techno";
public static void main(String argv[])
{
Tux t = new Tux();
t.name(sName);
System.out.println(sName);
}
public void name(String sName)
{
sName = sName + " park";
start();
}
public void run()
{
for(int i=0;i < 4; i++)
{
sName = sName + " " + i;
}
}
}
Thnx

Actually I thought only from the no-args constructor of the subclass the super() gets called, and in this case no object of Sub class is created like Sub sb = new Sub(), compiler won't complain
Here is 1 Q in 1 of the SCJP mock tests on the net -

class Super{

String name;

Super(String s){

name =s ;

} }

class Sub extends Super{

String name;

Sub(String s){

name=s;

}

public static void main(String args[]){

Super sup = new Super("First");

Sub sub = new Sub("Second");

System.out.println(sub.name + " " + sup.name);

} }

I tried to run the code it gives compiler error for Super() constructor.

When I added no-args constructors for both the classes it worked fine, I don't get the reason behind the error.

Can anyone please help me ?

Thanks,

- Prajakta