Which code segment should you insert at line 04?

You are implementing a method named Calculate that performs conversions between value types and
reference types. The following code segment implements the method. (Line numbers are included for reference
only.)
01 public static void Calculate(float amount)
02 {
03 object amountRef = amount;
04
05 Console.WriteLine(balance);
06 }You need to ensure that the application does not throw exceptions on invalid conversions. Which code segment
should you insert at line 04?

You are implementing a method named Calculate that performs conversions between value types and
reference types. The following code segment implements the method. (Line numbers are included for reference
only.)
01 public static void Calculate(float amount)
02 {
03 object amountRef = amount;
04
05 Console.WriteLine(balance);
06 }You need to ensure that the application does not throw exceptions on invalid conversions. Which code segment
should you insert at line 04?

A.
int balance = (int)(float)amountRef;

B.
int balance = (int)amountRef;

C.
int balance = amountRef;

D.
int balance = (int) (double) amountRef;

Explanation:
Only A has a valid cast, C would not even compile.



Leave a Reply 0

Your email address will not be published. Required fields are marked *