473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Bit tricky question for ==

Please finish the declaration of variable x in the bold line below so that the next line prints “false”.

public class Reflexive {
public static void main(String[] args) throws Exception {

<typeX> x = <valueX>;

System.out.println(x == x);
}
}
Dec 20 '07 #1
11 1846
r035198x
13,262 8TB
Please finish the declaration of variable x in the bold line below so that the next line prints “false”.

public class Reflexive {
public static void main(String[] args) throws Exception {

<typeX> x = <valueX>;

System.out.println(x == x);
}
}
<spoiler>
Suppose x is NaN ...
</spoiler>


Edit: Shouldn't the println say
System.out.println(x == <valueX>);
Dec 20 '07 #2
JosAH
11,448 Expert 8TB
<spoiler>
Suppose x is NaN ...
</spoiler>


Edit: Shouldn't the println say
System.out.println(x == <valueX>);
Nope, according to the IEEE definition NaN should compare unequal even to itself.
NaN == NaN should result in false.

kind regards,

Jos
Dec 20 '07 #3
r035198x
13,262 8TB
Nope, according to the IEEE definition NaN should compare unequal even to itself.
NaN == NaN should result in false.

kind regards,

Jos
That's why I suggested it for the println to print false but it wont print false with x == x.
As in
Expand|Select|Wrap|Line Numbers
  1. Double x = Double.NaN; 
  2. System.out.println(x == x);
  3. System.out.println(x == Double.NaN);
  4.  
prints
true
false
Dec 20 '07 #4
BigDaddyLH
1,216 Expert 1GB
That's why I suggested it for the println to print false but it wont print false with x == x.
As in
Expand|Select|Wrap|Line Numbers
  1. Double x = Double.NaN; 
  2. System.out.println(x == x);
  3. System.out.println(x == Double.NaN);
  4.  
prints
true
false
Very cheeky. Try:
Expand|Select|Wrap|Line Numbers
  1. double x = Double.NaN; 
  2. System.out.println(x == x);
  3. System.out.println(x == Double.NaN);
  4.  
prints:
false
false
Dec 20 '07 #5
BigDaddyLH
1,216 Expert 1GB
Edit: Shouldn't the println say
System.out.println(x == <valueX>);
If you're going to do that, you could also play games with Integer pooling:

Expand|Select|Wrap|Line Numbers
  1. Integer x = Integer.valueOf(0);
  2. System.out.println(x == x);
  3. System.out.println(x == Integer.valueOf(0));
  4.  
  5. Integer y = Integer.valueOf(5000);
  6. System.out.println(y == y);
  7. System.out.println(y == Integer.valueOf(5000));
  8.  
This prints:
true
true
true
false
Dec 20 '07 #6
JosAH
11,448 Expert 8TB
Bugger, that autoboxing thingie again ... where's the guy/gal who invented it? The
window on the attic is wide open; please climb those fine stairs now and wave towards
the general direction of the hungry peasants.

kind regards,

Jos
Dec 20 '07 #7
r035198x
13,262 8TB
... where's the guy/gal who invented it? T...
kind regards,

Jos
Probably enjoying their holiday in the Bahamas with the money he got for inventing it.
Dec 20 '07 #8
JosAH
11,448 Expert 8TB
Probably enjoying their holiday in the Bahamas with the money he got for inventing it.
Do you remember this thread?

kind regards,

Jos
Dec 20 '07 #9
r035198x
13,262 8TB
Do you remember this thread?

kind regards,

Jos
It came to my mind when I saw this thread.
Dec 20 '07 #10
shauank, I gave you this test for *you* to solve by yourself and did not ask for a public help, didn't I?

ALL: shauank is an applicant for a job ad and he received a Java Quiz test. Just another cheater, I guess.

Please finish the declaration of variable x in the bold line below so that the next line prints “false”.

public class Reflexive {
public static void main(String[] args) throws Exception {

<typeX> x = <valueX>;

System.out.println(x == x);
}
}
Jan 12 '08 #11
BigDaddyLH
1,216 Expert 1GB
shauank, I gave you this test for *you* to solve by yourself and did not ask for a public help, didn't I?

ALL: shauank is an applicant for a job ad and he received a Java Quiz test. Just another cheater, I guess.
Didn't I see this fellow posting several of these interview questions on Sun's Java forums in order to elicit solutions?
Jan 12 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
0
by: dracolytch | last post by:
Good day all, Ok, I have a pretty tricky problem that I need some help with. I pass around search query information a fair amount (specifically WHERE statements). Normally, I just rawurlencode()...
1
by: JZ | last post by:
Oracle 9iR2 I have a table: SQL> select * from test; A B C ------------------- ---------- ---------- 01/01/2004 10:00:00 1 1...
4
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column...
4
by: Angel Cat | last post by:
I have 2 tables joined together by the IDs, People and the pets they own PEOPLE ID NAME 1 JohnSMith 2 JaneDoe PETS ID PET
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
5
by: Danny | last post by:
Hi there I need help with a tricky problem. I have a 2 dimensional array with qualities such as ball size, ball color, ball weight. Now I have to print out all the possible combinations of...
8
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char...
7
by: NileshKorpe | last post by:
Can you please send me link of some c++ tricky (confusing) questions usually asked in the c++ technical interview. Thank You
1
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.