2.3 What is the difference between the following two statements? a. if (n > 2) {
ID: 641156 • Letter: 2
Question
2.3 What is the difference between the following two statements?
a. if (n > 2) { if (n < 6) System.out.println("Done!"); }
else System.out.println("Error");
b. if (n > 2) { if (n < 6) System.out.println("Done!");
else System.out.println("Error"); }
2.4 What is the difference between the result of the following two statements?
a. int cents = (int)(100 * price + 0.5);
b. int cents = (100 * price + 0.5);
3.2 Use string concatenation3.2 prints the message "the quick white tiger jumps over the small dog". public class ConcatDemo { public static void main(String[] args) { String animal1 = "quick white tiger"; String animal2 = "small dog";
String article = "the"; String action = "jumps over"; /* Your work goes here */
System.out.println(message); } }
3.3 Find the output of the following code when the user enters 100 and when the user enters AQ:
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter somthing: ");
String str = in.next();
int count = Integer.parseInt(str);
System.out.println("Input is " + count);
}
Explanation / Answer
2.3
a. Here the else condition is of outer if, If n>2 and n<6 then "Done!" will prints, otherwise no output. and if n<=2 then "error"prints.
On second part , the else is of nested if, Here if n>2 and n<6 the "Done!" prints, otherwise when n>2 but not n<6 then it prints "error" , but if n<=2 then no output will print.
2.4 expression a. is correct because expression contains float value and float value is first downcast explicitly before it inserts into int datatype. Second expressions is not valid.
3.2
String animal1 = "quick white tiger";
String animal2 = "small dog";
String article = "the";
String action = "jumps over";
String message=article+" "+animal1+" "+action+" "+article+" "+animal2;
System.out.println(message);
3.3 when user enters 100, the output is:-
Enter somthing: 100
Input is 100
and when user enters aq, the output is:-
java.lang.NumberFormatException: For input string: "aq"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.