Java Chapter 3

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which expressions mean "x is greater than or equal to y"?

x >= y

Assume that a double variable holds the value 0.179. What format pattern would you use to display the number as .18?

#.00

Assume that one double variable number holds the value 459.6329, and another variable holds -459.6329. What format pattern would you use to display the numbers as 459.63 and 459.63-?

0.00;0.00-

Assume that a double variable holds the value 459.6329. What format pattern would you use to display the number as 4.596E02?

0.000E00

Assume that a double variable holds the value 0.4. What format pattern would you use to display the number as 40%?

#0%

(T/F) A constructor should initialize the object's attributes with appropriate data and perform any necessary setup operations.

True

(T/F) The keyword "new" creates an object in memory from a class.

True

Rewrite the following code as exactly as possible, using an if-else-if structure instead of the switch. Assume that the following code was taken from a working program (it was) and don't worry about what comes before or after this section. (I.e. don't worry about declarations, etc.) Your code should do whatever this code does. switch (choice) { case 1: name = "animal"; break; default: System.out.println("Invalid entry. Using default"); case 2: name = "vegetable"; break; case 3: name = "mineral"; }

if (choice == 1) name = "animal"; else if (choice == 2) name = "vegetable"; else if (choice == 3) name = "mineral"; else { System.out.println("invalid entry. using default"); // the original default case falls through to the next case // and sets name to "vegetable" name = "vegetable";

Which of the numbered lines will be executed in the following code? 1 int x = 10; 2 switch (x) 3 { 4 case 10: 5 x = x + 15; 6 case 12: 7 x = x - 5; 8 break; 9 default: 10 x = x * 3; 11 } 5 7 10 more than one of the above

more than one of the above

Assume that a double variable holds the value 0.04. What format pattern would you use to display the number as 04.0%?

00.0%

Assume that a double variable holds the value 459.6329. What format pattern would you use to display the number as 00459.633?

00000.000

Which of the numbered lines is executed in the following code? 1 int bonus, sales = 85000; 2 char dept = 'S'; 3 if (sales > 100000) 4 if (dept == 'R') 5 bonus = 2000; 6 else 7 bonus = 1500; 8 else if (sales > 75000) 9 if (dept == 'R') 10 bonus = 1250; 11 else 12 bonus = 1000; 13 else 14 bonus = 0; 5 7 10 12 14

12

What is the value of x after the following code has been executed? int x = 75; int y = 90; if (x != y) x += y;

165 since x doesn't equal y, you would calculate x + y = 75 + 90 = 165

In the code fragment below, which value will be assigned to discountRate? double a=22, b=24, c=27, d=29, e=31; double discountRate = 0.0; int purchase = 1250; char cust = 'N'; if (purchase > 1000) if (cust == 'Y') discountRate = a; else discountRate = b; else if (purchase > 750) if (cust == 'Y') discountRate = c; else discountRate = d; else discountRate = e;

24.0

Which of the numbered lines in the following code are executed? 1 int ans = 35, x = 50, y = 50; 2 if (x >= y) 3 { 4 ans = x + 10; 5 x = x - y; 6 } 7 else 8 { 9 ans = y + 10; 10 y = y + x; 11 } 4 9 both 4 & 9 neither 4 nor 9

4

Given the following code, is line 4 executed? Why or why not? 1 int f = 7, g = 8; 2 if (f > g) 3 f = 8; 4 g = 7;

Yes. Regardless of the condition, line 4 is not part of the if statement - it will always be executed. The indentation doesn't matter.

What will be the values of ans, x, and y after the following statements are executed? int ans = 0, x = 15, y = 25; if (x >= y) { ans = x + 10; x -= y; } else { ans = y + 10; y += x; }

ans = 35, x = 15, y = 40 since x isn't greater than or equal to y, the else statement executes, so ans = 25 +10 = 35 and y = 25 + 15 = 40, and x = 15

Write an if statement that prints "OK" to the screen if the contents of the String variable userPassword is "Tr0ub4dor&3"

if (userPassword.equals("Tr0ub4dor&3")) System.out.println("OK");

Write an if statement that assigns 65 to y if x not equal to B? (65 and B are literals, x and y are variables.)

if (x != 'B') // char literal y = 65; // OR if (!x.equals("B")) // String literal y = 65;

Write an if statement that sets x to "temperature out of range" if the variable y is less than 18 or more than 25.

if (y < 18 || y > 25) x = "temperature out of range";

Write an if statement that assigns 100 to x when y is at least 0.

if (y >= 0) x = 100;

The expression in the ( )s of an if statement must evaluate to

true or false


Conjuntos de estudio relacionados

BSC2010 Chapter 43 The Immune System

View Set

Wordly Wise 3000® Level 7, Lesson 1

View Set

Pharmacology ATI Learning System Quiz

View Set

Chapter 14: Nervous System: Spinal Cord & Spinal Nerves

View Set

Ch 15: Portfolio and Market Analysis

View Set

NR 304 Neuro Assessment Flash Cards

View Set

Sociology final exam review- Unit 2 chapters 5,6,11,12

View Set

Corporate Finance: Discounted Cash Flow (DCF) Valuation

View Set