Which of the following will result in a compile-time error? float f = 1.3; char c = 'A'; byte b = 10; boolean b = true;
Question
Which of the following will result in a compile-time error?
float f = 1.3; char c = 'A'; byte b = 10; boolean b = true;
Solution
The first line float f = 1.3; will result in a compile-time error. In Java, by default, any decimal number is treated as a double. So, 1.3 is considered a double value and we are trying to assign it to a float variable which leads to a precision loss. To fix this, you should write it as float f = 1.3f; or float f = (float) 1.3;.
The other lines of code are correct. The second line char c = 'A'; is correct because 'A' is a valid character literal in Java. The third line byte b = 10; is correct because 10 is within the range of byte (-128 to 127). The fourth line boolean b = true; is correct because true is a valid boolean value in Java.
Similar Questions
This code produces an error message. Where will the error occur, and why?1 bool a = 1;2 int b = 2 + 4;3 float c = 4;4 cout<<a + b<<endl;
Line 1 : int x = Integer.parseInt("5"); Line 2 : float pi=Float.parseFloat("3.14"); Line 3 : boolean status=Boolean.parseBoolean("TRUE");Line 4 : char c = new Character("X");Which of the above lines can cause compilation error?Select one:a.Line 2b.Line 3c.Line 4d.Line 1
What will be the output of the following C code?#include <stdio.h> struct p { int k; char c; float f; }; int p = 10; int main() { struct p x = {1, 97}; printf("%f %d\n", x.f, p); }a.Compile time errorb.0.000000 10c.Somegarbage value 10d.0 10
Which of the following data types is not supported by C but is supported by C++?a.floatb.intc.doubled.bool
Which of the following declaration is not supported by C language?Question 13Answera.String str;b.char *str;c.float str = 3e2;d.Both String str; & float str = 3e2;
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.