• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valid assignment

 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree...
C,E
D:not a object reference can't set it to null
 
farrukh mahmud
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Vikrama Sanjeeva
Ranch Hand
Posts: 756
  • 0
  • Mark post as helpful
  • send pies
  • 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: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone
I was thinking that null can be assigned to primitive types too.

------------------
Regards
Ravish
 
MONZY THARIAN
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i interpreted it wrong thanks for the ans

------------------
Regards
Farrukh Mahmud
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic