• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

valid assignment

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Which of the following statements are true or valid?
(Select two correct answers)
-----------------------------------------------------------------------
-----------------------------------------------------------------------
A: float v1 = 1.0;
B: char v2 = "a";
C: byte v3 = -128;
D: boolean v4 = null;
E: Float v5 = new Float(1.0);
I think ans should be
C,D,E
but as question says only two??
I have not seen the answer yet so plz .... which one of mine is wrong and why??
Thanks in advance
que is from : http://www.geocities.com/sun_guoqiao/scjp/mockexam2.html
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A: float v1 = 1.0;
Invalid 1.0 is Double and it will need a cast to float because it is narrowing conversion
B: char v2 = "a";
"a" is a String not a char like 'a'
C: byte v3 = -128;
this compiles because -128 is a constant expresion whith a value that is in the range for a byte (-128 , 127) Please read the JLS 5.2 because it is sure to be tested in the exam.
D: boolean v4 = null;
error null can be only asigned to reference variables, not to primitives
E: Float v5 = new Float(1.0);
ok, the constructor for Float can accept a float or a double
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree...
C,E
D:not a object reference can't set it to null
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
in
float v5 = new float(1.0);
you are assigning double to float constructor and it is also narrow conversioning and explicit cast is required. above line also causes an compilation error as well.
Error-->Invalid argument type float for new.
Clear me if i am wrong
Thanks

------------------
Regards
Farrukh Mahmud
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mahmud, it runs fine .Coz. the constructor of Float allows double value as a parameter.
Bye.
Viki
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everyone
I was thinking that null can be assigned to primitive types too.

------------------
Regards
Ravish
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by farrukh mahmud:
Hi
in
float v5 = new float(1.0);
you are assigning double to float constructor and it is also narrow conversioning and explicit cast is required. above line also causes an compilation error as well.
Error-->Invalid argument type float for new.
Clear me if i am wrong
Thanks


==============================================
farrukh mahmud,
Primitive data type cannot be created using new , in the given above code it is not float v5 = new float(1.0);
They have used wrapper class Float which takes float and double as arguments.
cheers
Monzy

 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i interpreted it wrong thanks for the ans

------------------
Regards
Farrukh Mahmud
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic