Looking for Interesting/Tricky Java questions

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
I'm looking for interesting/tricky java questions to make quizzes for students (small quizzes at the beginning of labs). Anyone has some on top of your head or knows some place that has them, can you share it with me? Any Java level is welcome
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
What level?


I do remember a polymorphism question on the AP test that I thought was good.


 

ArmchairAthlete

Diamond Member
Dec 3, 2002
3,763
0
0
Do something relating to comparing two floats or doubles for equality and the possibility that two you think should be equal will not be (because of how computers approximate).

Where you have to use something like subtracting the floats and then making sure what's left is less than 0.0000001 or so.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Amdfanboy's question about stack overflow might be a good warmup

Stack Overflow Blues

It would really demonstrate an understanding of inheritence and the problems you can run into when overriding some functions but not others.

Or why this code prints:
lalala
lalala
lalala
S1!=S2
S1=S3
S2!=S3
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
I assume that you will then call myMethod(o) in main. I'm pretty sure the result would be "You passed an Object"
It was a good question.
Thanks
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: Kilrsat
Amdfanboy's question about stack overflow might be a good warmup

<a target=_blank class=ftalternatingbarlinklarge href="http://forums.anandtech.com/me...eyword1=java">Stack Overflow Blues</a>

It would really demonstrate an understanding of inheritence and the problems you can run into when overriding some functions but not others.

Or why this code prints:
lalala
lalala
lalala
S1!=S2
S1=S3
S2!=S3

Interesting one. I was thinking that S1 would not equal to S3 as well, since the operation was checking if the two pointers were pointing to the same object.

S1 = new String("lalala");
S3 = new String("lalala");
S1 == S3 would yield false.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: vtqanh
<blockquote>quote:
<hr><i>Originally posted by: <b>Kilrsat</b></i>
Amdfanboy's question about stack overflow might be a good warmup

<a target=_blank class=ftalternatingbarlinklarge href="<a target=_blank class=ftalternatingbarlinklarge href="http://forums.anandtech.com/me...va">Stack</a> Overflow Blues</a>

It would really demonstrate an understanding of inheritence and the problems you can run into when overriding some functions but not others.

Or why this code prints:
lalala
lalala
lalala
S1!=S2
S1=S3
S2!=S3<hr></blockquote>

Interesting one. I was thinking that S1 would not equal to S3 as well, since the operation was checking if the two pointers were pointing to the same object.

S1 = new String("lalala");
S3 = new String("lalala");
S1 == S3 would yield false.

Exactly, == check if they are the same object (of course they are equal) and .equals method from object checks for equality. It is important to note that the defualt implementation of equal is to check if they are the same object. You need to override it with your own way to check if they are equal.

 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: vtqanh
<blockquote>quote:
<hr><i>Originally posted by: <b>Kilrsat</b></i>
Amdfanboy's question about stack overflow might be a good warmup

<a target=_blank class=ftalternatingbarlinklarge href="<a target=_blank class=ftalternatingbarlinklarge href="http://forums.anandtech.com/me...va">Stack</a> Overflow Blues</a>

It would really demonstrate an understanding of inheritence and the problems you can run into when overriding some functions but not others.

Or why this code prints:
lalala
lalala
lalala
S1!=S2
S1=S3
S2!=S3<hr></blockquote>

Interesting one. I was thinking that S1 would not equal to S3 as well, since the operation was checking if the two pointers were pointing to the same object.

S1 = new String("lalala");
S3 = new String("lalala");
S1 == S3 would yield false.
It gets into the Java String pool. Even though S1 and S3 are different objects according to the code both S1 and S3 actually point to the same object in the String pool. Its the String pool that creates issues with comparing strings because sometimes == does produce the expected results.
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: Kilrsat
<blockquote>quote:
<hr><i>Originally posted by: <b>vtqanh</b></i>
<blockquote>quote:
<hr><i>Originally posted by: <b>Kilrsat</b></i>
Amdfanboy's question about stack overflow might be a good warmup

<a target=_blank class=ftalternatingbarlinklarge href="<a target=_blank class=ftalternatingbarlinklarge href="<a target=_blank class=ftalternatingbarlinklarge href="http://forums.anandtech.com/me...ck</a></a> Overflow Blues</a>

It would really demonstrate an understanding of inheritence and the problems you can run into when overriding some functions but not others.

Or why this code prints:
lalala
lalala
lalala
S1!=S2
S1=S3
S2!=S3<hr></blockquote>

Interesting one. I was thinking that S1 would not equal to S3 as well, since the operation was checking if the two pointers were pointing to the same object.

S1 = new String("lalala");
S3 = new String("lalala");
S1 == S3 would yield false.<hr></blockquote>
It gets into the Java String pool. Even though S1 and S3 are different objects according to the code both S1 and S3 actually point to the same object in the String pool. Its the String pool that creates issues with comparing strings because sometimes == does produce the expected results.

This makes sense now. So i guess if we insert this statement s2 = s2.intern(), then the check (s2==s3) would yield 'true'.

Keep them coming guys, everything posted in this thread has been very interesting.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Here's a tricky little sob that I ran into the other day at work. :beer: to anyone who can predict it correctly without actually running it.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
null and null? DescendantData needs to call its init() in its constructor. Better yet, just ditch init() and use the constructor to initialize; that's its purpose.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: BingBongWongFooey
null and null? DescendantData needs to call its init() in its constructor. Better yet, just ditch init() and use the constructor to initialize; that's its purpose.

1 null

the base's constructor still gets called implicitly
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: dighn
<blockquote>quote:
<hr><i>Originally posted by: <b>BingBongWongFooey</b></i>
null and null? DescendantData needs to call its init() in its constructor. Better yet, just ditch init() and use the constructor to initialize; that's its purpose.<hr></blockquote>

1 null

the base's constructor still gets called implicitly

Aha. Tricky indeed! :)
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: BingBongWongFooey
<blockquote>quote:
<hr><i>Originally posted by: <b>dighn</b></i>
<blockquote>quote:
<hr><i>Originally posted by: <b>BingBongWongFooey</b></i>
null and null? DescendantData needs to call its init() in its constructor. Better yet, just ditch init() and use the constructor to initialize; that's its purpose.<hr></blockquote>

1 null

the base's constructor still gets called implicitly<hr></blockquote>

Aha. Tricky indeed! <img src="i/expressions/face-icon-small-smile.gif" border="0">

Ok, you've got the first trick but you're still wrong ;) Well, which one do you mean to be null?



Edit: crap, the quote functionality blows... did that change since they ditched JRun?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Whoops, dighn, you are correct, sorry. Can you explain why it's not:
1
2

Hint: DescendantData.init() does get called.

BingBong: I know the constructor should be used for initialization. There was a reason why we had a situation like this that is not apparent in this example because it's been stripped down to just demonstrate the tricky part.
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: kamper
Whoops, dighn, you are correct, sorry. Can you explain why it's not:
1
2

Hint: DescendantData.init() <b>does</b> get called.

BingBong: I know the constructor should be used for initialization. There was a reason why we had a situation like this that is not apparent in this example because it's been stripped down to just demonstrate the tricky part.

Here's the reason: (and yes, DescentdantData.init() DOES get called)
When this line DescendantData data = new DescendantData(); is excecuted, these things will occur (in this order):
the constructor of the BASE class gets called, which then calls init(); However, the function init() has been overriden by DescendantData, this function will set descendantInteger to 2 (however, note that at this point, descendantInteger is not initialized yet!!!)
After this, the initialization takes place, which means this line private Integer descentdantInteger = null will be executed, this will set descendantInteger to null.
After this, the constructor of DescentdantData will be executed.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
:beer::beer: Good work. That seemed so counter-intuitive to me the first time I heard it. I had to go write the code and do a step-through.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
On the non-technical side you could ask them design things like why you shouldn't expose data members of a class publicly (more of a beginner question).

You could also maybe ask something about how protected actually means package-access.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
On the non-technical side you could ask them design things like why you shouldn't expose data members of a class publicly (more of a beginner question).

You could also maybe ask something about how protected actually means package-access.

Yes, ask them what classes they would use to make a web server with a GUI. Pass the results on, I would really like to detangle my code :(