InterviewSolution
| 1. |
Why Does Not The Following Code Give The Desired Result? Int X = 3000, Y = 2000 ; Long Int Z = X * Y ; |
|
Answer» Here the multiplication is CARRIED out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. HOWEVER, to GET the correct output, we should use an explicit cast to force long ARITHMETIC as shown below: long int z = ( long int ) x * y ; Note that (long int) (x * y) would not give the DESIRED effect. Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below: long int z = ( long int ) x * y ; Note that (long int) (x * y) would not give the desired effect. |
|