Chapter 3 Java

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

int key =1; switch (key + 1) { case 1: System.out.println("Cake"); break; case 2: System.out.println("Pie"); break; case 3: System.out.println("Ice cream"); case 4: System.out.println("Cookies"); break; default: System.out.println("Diet time"); break; } Suppose you change the first line of the code above to int key = 3; What output would be produced?

Cookies

int key =1; switch (key + 1) { case 1: System.out.println("Cake"); break; case 2: System.out.println("Pie"); break; case 3: System.out.println("Ice cream"); case 4: System.out.println("Cookies"); break; default: System.out.println("Diet time"); break; } Suppose you change the first line of the code above to int key = 5; What output would be produced?

Diet time

int code = 2; switch (code) { case 1: System.out.println("Hello"); case 3: System.out.println("Good-bye."); break; default: System.out.println("Till we meet again."); break; } Suppose you change the code above so that the first line is the following: int code = 1; What output would be produced?

Hello Goodbye

Suppose x1 and x2 are two variables that have been given values are equal when the variables are of type int? How about when the variables are of type String.

If they are type int, you use x1 == x2. If they are of type String, you use x1.equals(x2).

What output is produced by the following code? int key =1; switch (key + 1) { case 1: System.out.println("Cake"); break; case 2: System.out.println("Pie"); break; case 3: System.out.println("Ice cream"); case 4: System.out.println("Cookies"); break; default: System.out.println("Diet time"); break; }

Pie

What output is produced by the following statements? int number = 7; boolean isPositive = (number > 0); if (number >0); number = -100; if (isPostitive) System.out.println("Postitive."); else System.out.println("Not postitive. "); System.out.println(number);

Postive -100

What output is produced by the following code? char letter = 'B'; switch (letter) { case 'A': case 'a': System.out.println("Some kind of A."); case 'B': case 'b': System.out.println("Some kind of B."); break; default: System.out.println("Something else."); break; }

Some kind of B.

Assume that nextWord is a Sring variable that has been given a value consisting entirely of letters.Write some java code that displays the message First half of the alphabet, provided that nextWord precedes the letter N in alphabetic ordering, your code should display Second half of the alphabet. Be careful when you represent the letter N. You will need a String value, as opposed a char value.

String upperWord = nextWord.toUpperCase(); if(upperWord.compareTo("N") < 0) System.out.println("First half of the alphabet"); else System.out.println("Second half of the alphabet");

What output is produced by the following statements? System.out.println(true && false); System.out.println( true || false); System.out.println( false && (x>0)); System.out.println( true || (x>0));

The output produced is false true false true Because of short-circuit evaluation, you do not need to know the value of x.

What output is produced by the following statements? System.out.println(false); System.out.println(7 < 0); System.out.println(7 > 0); int n = 7; System.out.println(n > 0);

The output produced is false false true true

What is the output produced by the following code? int code = 2; switch (code) { case 1: System.out.println("Hello"); case 3: System.out.println("Good-bye."); break; default: System.out.println("Till we meet again."); break; }

Till we meet again.

What output is produced by the following code? int time = 2, tide =3; if (time + tide > 6) System.out.println("Time and tide wait for no one."); else if ( time + tide > 5) System.out.println(" Time and tide wait for someone."); else if ( time + tide > 4) System.out.println(" Time and tide wait for everyone."); else System.out.println("Time and tide wait for me.");

Time and tide wait for everyone.

What is the output produced by the following code? int time = 2, tide =3; if (time + tide > 6) System.out.println("Time and tide wait for no one."); else System.out.println("Time and tide wait for me.");

Time and tide wait for me.

if (time + tide > 6) System.out.println("Time and tide wait for no one."); else System.out.println("Time and tide wait for me."); What is the output when you change the following: int time = 4, tide = 3; What output would be produced?

Time and tide wait for no one.

Suppose salary and bonus are variables of type double. Write an if-else statement that displays the word OK provided that either salary is greater than or equal to MIN_SALARY or bonus is greater than or equal to MIN_BONUS. Otherwise, it should display Toolow.MIN_SALARY and MIN_BONUS are named constants.

if ((salary >= MIN_SALARY) || (bonus >= MIN_BONUS)) System.out.println("OK"); else System.out.println("Too low");

Suppose speed and visibility are variables of type int. Write an if statement that sets the variable speed equal to 25 and displays the word Caution provided the value of speed is greater than 25 and the value of visibility is under 20. There is no else part.

if ((speed > 25) && (visibility < 20)) { speed =25; System.out.println( "Caution"); }

Suppose goal is a variable of type int. Write an if-else statement that displays the word Wow if the value of variable goals is greater than 10 and displays the words Oh Well if the value of goal is at most 10.

if (goals >10) System.out.println("Wow"); else System.out.println("Oh Well");

Suppose salary and deductions are variables of type double that have been given values. Write an if-else statement that displays Ok and sets the variable net equal to salary minus deductions, provided that salary is at least as large as deductions. If,however, salary is less than deductions, the if-else statement should simply display the words No Way but not change the value of any variables.

if (salary >= deductions) { System.out.println("OK"); net = salary - deductions; } else { System.out.println(" No Way"); }

Suppose goals and errors are variables of type int. Write an if-else statement that displays the word Wow if the value of the variable goals is greater than 10 and the value of errors is 0. Otherwise, the if-else statement should display the words Oh Well.

if((goals > 10) && (errors==0)) System.out.println("Wow"); else System.out.println("Oh Well");

Suppose number is a variable of type int that has been given a value. Write a multibranch if-else statement that displays the word High if number is greater than 10, Low if number is less than 5, and So-so if number is anything else.

if(number > 10) System.out.println("High"); else if ( number < 5) System.out.println("Low"); else System.out.println("So-so");


Ensembles d'études connexes