Menu
Topics Index
...
`

Count coins


If you need explanation Read this topic

If you need Answer Take test on this topic

Count coins
What will be the output of the following program?
public class Coins {
    public static void main(String[] args) {
        String input = "50";
        Short coins = Short.parseShort(input + 10);
        short total = Short.parseShort(60, 16);
        System.out.println(coins + " " + total);
    }
}

A. 5010 96
B. 60 96
C. Compilation Error
D. Runtime Error
Topic: Creating Objects for Primitive Data Types (Byte, Short)

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

answer is c...compilation error..becoz..method Short.parseShort(String,int) is not applicable...(actual argument int cannot be converted to String by method invocation conversion)

Posted by Sneha Rapool    2014-03-31 13:58:23


Answer is C. This program results in a compilation error as there is no parseShort() method in the wrapper class "Short" that takes an integer as the first parameter (Line 5). It expects the first parameter (could be the only parameter) to be a string. To fix the error, pass 60 as a string. Then the value of the argement input + 10 is 5010 ( + acts as a concatenation operator since one of the operands is a string). Then parseShort("60", 16) parses 60 in hexadecimal form and equivalent decimal value is 96. Therefore, SOP statement prints out 5010  96.

Posted by Shaileshwar Sharma    2014-03-31 14:01:38


Congratulations Sneha Rapool. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-01 05:24:44


© meritcampus 2019

All Rights Reserved.

Open In App