Test time left: 20:37Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { int value = 121; String var = (String)value; //line 1 String temp = "122"; int data = (int)temp; //line 2 System.out.println(data + var); }}OptionsCompilation error due to line 1243Compilation error due to line 2Compilation error due to line 1 and line 2
Question
Test time left: 20:37Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { int value = 121; String var = (String)value; //line 1 String temp = "122"; int data = (int)temp; //line 2 System.out.println(data + var); }}OptionsCompilation error due to line 1243Compilation error due to line 2Compilation error due to line 1 and line 2
Solution
The correct answer is "Compilation error due to line 1 and line 2".
In Java, you cannot directly cast an integer to a String or a String to an integer. On line 1, the code attempts to cast an integer to a String, which is not allowed. On line 2, the code attempts to cast a String to an integer, which is also not allowed.
To convert an integer to a String, you can use Integer.toString(int) or String.valueOf(int). To convert a String to an integer, you can use Integer.parseInt(String) or Integer.valueOf(String).
Similar Questions
elect the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { int value = 121; String var = (String)value; //line 1 String temp = "122"; int data = (int)temp; //line 2 System.out.println(data + var); }}OptionsCompilation error due to line 2Compilation error due to line 1243Compilation error due to line 1 and line 2
Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { double data = 222.365; int value = data; System.out.println(data); }}Options222222.365Runtime errorCompilation error
Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { System.out.println((125/50.0)*Integer.parseInt("10") + 70); }}Options80Compilation error95.0Runtime error
Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { double data = 222.423; int sum = 3; float value = 2.1f; System.out.println(data + sum + value); }}Options222222.423227.52299222.5
Time left 0:13:05Question 1Not yet answeredMarked out of 1.00Flag questionTipsQuestion textWhat will be the output of the program?public class Sample{ public static void main(String [] args) { int i = 10; do while ( i < 10 ) System.out.print("The value of i is " + i); while ( i > 10 ) ; }}Select one:a.No output is produced.b.The value of i is 10 The value of i is 10c.Compilation errord.The value of i is 10Clear my choiceQuestion 2Not yet answeredMarked out of 1.00Flag questionTipsQuestion textWhich of the following options remain true for case constants in a switch construct?Select one:a.The code with the switch construct gives a compilation error when there is a duplicate case label b.If no case matches but there is a default label, then all statements after the matching default label in the switch block are executed in sequence. c.If no case matches and there is no default label, then no further action is taken and the switch statement completes abnormallyd.If any one of the case constants has a match with the expression, then all statements after the matching case label in the switch block are executed in sequencee.If no case matches and there is no default label, then it will result in a compilation error
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.