This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes valid assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "valid assignment" Watch "valid assignment" New topic
Author

valid assignment

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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


SCJP2. Please Indent your code using UBB Code
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Agree...
C,E
D:not a object reference can't set it to null
farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47
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


Regards<BR>Farrukh Mahmud
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
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!


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Thanks Everyone
I was thinking that null can be assigned to primitive types too.

------------------
Regards
Ravish
MONZY THARIAN
Ranch Hand

Joined: Feb 21, 2001
Posts: 63
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


Think Big . So shall you become big.
farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47
sorry i interpreted it wrong thanks for the ans

------------------
Regards
Farrukh Mahmud
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: valid assignment
 
Similar Threads
MockExam2 Question
Scjp help
About the data type
declaration float question!??
"valid declarations of a float" required