Computer Applications

Explain whether the following assignments are correct or not:

(a) int m =155;

(b) float f = 0.002654132;

(c) String str = 'Computer';

(d) boolean p = false;

(e) String b = "true";

(f) char ch = "apps";

(g) String st= "Application";

(h) double n = 455.29044125;

Values & Data Types Java

82 Likes

Answer

(a) int m =155;

This assignment is correct as 155 is an integer literal and it is assigned to an int variable m.

(b) float f = 0.002654132;

This assignment is incorrect as data type of 0.002654132 is double but it is assigned to a float variable. The correct assignment will be float f = 0.002654132f;

(c) String str = 'Computer';

This assignment is incorrect as the String literal Computer is enclosed in single quotes. It should be in double quotes. The correct assignment will be String str = "Computer";

(d) boolean p = false;

This assignment is correct as false is a valid boolean literal and it is assigned to a boolean variable.

(e) String b = "true";

This assignment is correct as "true" is a string literal not a boolean literal as it is enclosed in double quotes. It is correctly assigned to a String variable.

(f) char ch = "apps";

This assignment is incorrect as "apps" is a string literal not a character literal and it is assigned to a variable ch of char data type.

(g) String st= "Application";

This assignment is correct as "Application" is a string literal and it is correctly assigned to a String variable.

(h) double n = 455.29044125;

This assignment is correct as 455.29044125 is a literal of double data type and it is correctly assigned to a double variable.

Answered By

45 Likes


Related Questions