Which of the following are legal lines of Java code? 1. int ct1 = (int)888.8; 2. byte ct2 = (byte)100L; 3. long ct3 = (byte)100; 4. byte ct4 = (byte)100L;Options2 and 31 and 23 and 4All statements are correct
Question
Which of the following are legal lines of Java code? 1. int ct1 = (int)888.8; 2. byte ct2 = (byte)100L; 3. long ct3 = (byte)100; 4. byte ct4 = (byte)100L;Options2 and 31 and 23 and 4All statements are correct
Solution
All statements are correct.
Here's why:
-
int ct1 = (int)888.8; This is a legal line of Java code. It's casting a double value to an integer. The decimal part will be truncated.
-
byte ct2 = (byte)100L; This is also a legal line of Java code. It's casting a long value to a byte. As long as the value is within the range of byte (-128 to 127), it's legal.
-
long ct3 = (byte)100; This is a legal line of Java code. It's casting an integer to a byte and then implicitly casting it to a long. Java allows implicit casting from a byte to a long.
-
byte ct4 = (byte)100L; This is also a legal line of Java code. It's casting a long value to a byte. As long as the value is within the range of byte (-128 to 127), it's legal.
Similar Questions
The following Java statements give an error. What is the correct way to rectify this?byte b = 50; b = b * 2; Group of answer choicesb = byte(b*2);b = int(b*2);b = (int) (b*2);b = (byte) (b*2);
Which of the following lines of code will compile without error?Code 1: Code 2: Code 3: Code 4:int i=0;if(i) {System.out.println("Hello");}boolean b=true;boolean b2=true;if(b==b2) {System.out.println("So true");int i=1;int j=2;if(i=1|| j=2)System.out.println("OK");int i=1;int j=2;if(i==1 && j==2)System.out.println("OK");A. Code 1 B. Code 2 C. Code 3 D. Code 4 E. Code 2&4 both F. Code 2&3 both
rect answerWhat will be the output of the following Java code? class codetantra { public static void main(String args[]) { double m = 295.30; int n = 330; byte o = (byte) m; byte p = (byte) n; System.out.println(o + " " + p); } }
Which one is a valid statement in Java?I. double num = 2.718;II. double num = @718;III. double num = 2#7_1_8;
Which is an invalid keyword in Java?A) byteB) ShortC) intD) long
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.