Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Quw from KB book, chap 4 -> test Q 3

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me this code?

1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7. // insert code here
8. System.out.println("true");
9. }
10. }

And the following five code fragments:

F1. if (f1 == f2)
F2. if (f1 == f2[2][1])
F3. if (x == f2[0][0])
F4. if (f1 == f2 [1,1] )
F5. if (f3 == f2 [2] )

What is true?

A. One of them will compile, only one will be true.

B. Two of them will compile, only one will be true.

C. Two of them will compile, two will be true.

D. Three of them will compile, only one will be true.

E. Three of them will compile, exactly two will be true.

F. Three of them will compile, exactly three will be true.


the answer is D
but i dont understand the reason given in the answer.
please explain...
thanks
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which part you don't understand?


F1. if (f1 == f2)// wont compile because f1 is a float variable and f2 is 2d array of floats
F2. if (f1 == f2[2][1])//will compile comparing f1 value with f2[2][1] results in false
F3. if (x == f2[0][0])// will compile and results in true
F4. if (f1 == f2 [1,1] )// syntax error
F5. if (f3 == f2 [2] )// will compile comparing 2 array objects results in false

so 3 will compile and only one will result in true
[ April 10, 2008: Message edited by: sridhar row ]
 
Sanket Modi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnks. i understood.
i was confused with F3...
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't get this

F3. if (x == f2[0][0])// will compile and results in true


Please explain
 
Let's get him boys! We'll make him read this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic