Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work...

17
Review TEST 2 Chapters 4,5,7

Transcript of Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work...

Page 1: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ReviewTEST 2

Chapters 4,5,7

Page 2: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION

• For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

Page 3: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

• int

Page 4: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION

• What will be printed when the following statements are executed?

Account acct1 = new Account(1000.00); Account acct2 = new Account(1000.00); if (acct1 == acct2)

System.out.println("Equal"); else

System.out.println("Not equal");

Page 5: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

• Not equal

Page 6: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION

• What will be printed when the following statements are executed?

int n = 1;

System.out.print(n + " donut"); if (n >= 2);

System.out.print("s");

Page 7: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

• 1 donuts

Page 8: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION

• Write a cascaded if statement that tests the value of the variable temperature. If temperature is less than or equal to 32, the message "Freezing" should be displayed (without the quotes). If temperature is greater than or equal to 212, "Boiling" should be displayed. Otherwise, "Normal" should be displayed

Page 9: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

if (temperature <= 32) System.out.println("Freezing"); else if (temperature >= 212) System.out.println("Boiling"); else

System.out.println("Normal");

Page 10: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION

• Show the values of the variables i and j after statements have been executed.

i = 7; j = i-- * 2 + 1;

Page 11: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

• Value of i: 6. Value of j: 15

Page 12: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION• Assume that a is an array of integers. The following

statements are supposed to search a for the number 7. Unfortunately, there is an error that prevents the statements from working. Describe the error and show how to fix it.

boolean found = false; for (int i = 0; i < a.length && !found; i++)

if (a[i] == 7) found = true;

if (found) System.out.println("Found 7 at position: " +

i);

Page 13: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

• The variable i cannot be used after the loop terminates. It should be declared prior to the for statement.

Page 14: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION

• Use a conditional expression to combine the following statements into a single return statement:

if (i >= 0) return i;

return -i;

Page 15: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

return i >= 0 ? i : -i;

Page 16: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

QUESTION• Show the output of the following program.

public class Problem54 { public static void main(String[] args) {

g(62.781); g(0.08736);

} private static double f(double x) {

while (x >= 10.0) x /= 10;

while (x < 1.0) x *= 10;

return x; } private static void g(double x) {

System.out.println("f(" + x + ") = " + f(x)); }

}

Page 17: Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?

ANSWER

f(62.781) = 6.2781 f(0.08736) = 8.736