Question: Find the errors in the following code: 1. // Warning! This code contains ERRORS! if (x == 1); y = 2; else if (x ==
Find the errors in the following code:
1. // Warning! This code contains ERRORS! if (x == 1);
y = 2; else if (x == 2);
y = 3; else if (x == 3);
y = 4;
2. // if (average = 100)
Warning! This code contains an ERROR!
System.out.println("Perfect Average!");
3. // if (num2 == 0)
Warning! This code contains ERRORS!
System.out.println("Division by zero is not possible."); System.out.println("Please run the program again "); System.out.println("and enter a number besides zero.");
else Quotient = num1 / num2; System.out.print("The quotient of " + Num1); System.out.print(" divided by " + Num2 + " is "); System.out.println(Quotient);
Review Questions and Exercises 181
182 Chapter 3
Decision Structures
4.
// Warning! This code contains ERRORS! switch (score) {
case (score > 90):
grade = 'A';
break;
case(score > 80):
grade = 'b';
break;
case(score > 70):
grade = 'C';
break;
case (score > 60):
grade = 'D';
break;
default: grade = 'F';
}
5. 6. 7. 8. 9.
10.
The following statement should determine whether x is not greater than 20.
What is wrong with it? if (!x > 20)
The following statement should determine whether count is within the range of 0 through 100.
What is wrong with it?
if (count >= 0 || count <= 100)
The following statement should determine whether count is outside the range of 0 through 100.
What is wrong with it? if (count < 0 && count > 100)
The following statement should assign 0 to z if a is less than 10; otherwise, it should assign 7 to z.
What is wrong with it?
z = (a < 10) : 0 ? 7;
Assume that partNumber references a String object. The following if statement should
perform a case-insensitive comparison. What is wrong with it?
if (partNumber.equals("BQ789W4")) available = true;
What is wrong with the following code?
double value = 12345.678;
System.out.printf("%.2d", value);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
