Menu
Topics Index
...
`

creating datatypes


If you need explanation Read this topic

If you need Answer Take test on this topic

creating datatypes
What will be the output of the following program?
public class DemoOnFloats
{
    public static void main(String args[])
    {
        float first = 5.5f;
        long l = 25L, m;
        long m = l + first;
        System.out.print(m);
    }
}

A. 30.5L
B. 25
C. 33
D. 55
E. Compilation Error or Runtime Error
Topic: Type Conversion In Java

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.

Ans is E.
It will give compilation error as we are declaring variable 'm' twice.. So it will give error as "variable m is already defined"

Posted by Gopinath Manchikanti    2015-01-09 13:53:12


ANS IS E (Compilation Error)

There will be 2 Erorrs in program:
1. variable m is already defined in method main
        long m=l+f;
             ^
2. incompatible types: possible lossy conversion from float to long
        long m=l+f;
                ^
2 errors

--Here Concept of "type conversion" is used.Here float variable is declared with value 5.5f and long is declared to 25.
Then 5.5 and 25 are added.
Result=3.5
But here Destination variable is long so there will be loss of floating point value.

so,either "type conversion" should be used or destination variable should be float.

1st Approach:-->
m=(long)l+f

2nd Approach:-->
float m
m=l+first

So,2 Errors will be there in program one for duplicate name declaration and loss of precision value.

Posted by Mânïshå Mùlchåndânï    2015-01-09 15:48:38


ans e. compilation error. we can change the value that a variable holds, or reassign it , as such: int x; x = 3; System.out.println(x); // x equals 3 here x = 4; System.out.println(x); // x equals 4 here! Compile and run this code. See how it prints out 3 on one line and 4 on the next?  This is because we changed the value assigned to x. So a variable can hold one value for a while, and then it can hold another value for a while. Now, we can still declare and initialize x on one line, like this: int x = 3; System.out.println(x); // x equals 3 here x = 4; System.out.println(x); // x equals 4 here! But we cannot declare the variable x twice. So this is an error: int x = 3; System.out.println(x); int x = 4; // <-- Error! System.out.println(x);coming to this program here we declared m at 1st time without initialization, thn again we declare and initialize same m variable, this re declaration throws error

Posted by Maheshwari Natarajan    2015-01-09 19:16:09


ans is e..

Posted by Shubham Bansal    2015-01-10 03:25:46


Ans is E after execution starts variable first value became 5.5 and l value became 25 and next statement having compilation error because here we are trying to add float value and long value and that is giving to  the variable 'l' of type long so here we  cannot convert from float to long and also here we are declaring variable m 2 times and  we won't have  duplicate local variables in a single method  so it will throw compilation error so Ans is E

Posted by Bhagi Bhagyasri    2015-01-12 05:14:09


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Bhagi Bhagyasri,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-12 05:24:56


© meritcampus 2019

All Rights Reserved.

Open In App