1. Write an if-else statement that outputs the word "High" if the value of the variable score is greater than 100 and outputs "Low" if the value of score is at most 100. The variable score is of type int.

 

 

2. Suppose savings and expenses are variables of type double that have been given values. Write an if-else statement that outputs the word "Solvent", decreases the value of savings by the values of expenses, and sets the value of expenses to zero, provided that savings is larger than expenses. If, however, savings is less than or equal to expenses, the if-else statement should simply output the word "Bankrupt" without changing the values of any of the variables.

 

 

3. Suppose number is of type int. Write an if-else statement that outputs the word "Positive" if the value of the variable number is greater than 0 and outputs the words "Not positive" if the value of number is less than or equal to 0.

 

 

4. Suppose salary and deductions are variables of type double that have been given values. Write an if-else statement that outputs the word "Crazy" if salary is less than deductions; otherwise, it should output "OK" and set the variable net equal to salary minus deductions.

 

 

5. What output will be produced by the following code?

int extra = 2;
if (extra < 0)
System.out.println("small");
else if (extra ==0)
System.out.println("medium");
else System.out.println("large");

6. What would the output be in Exercise 5 if the assignment were changed to the following?

int extra = -37;

7. What would the output be in Exercise 5 if the assignment were changed to the following?

int extra = 0;

8. Write a multiway if-else statement that classifies the value of an int variable n into one of the following categories and writes out an appropriate message:

n<0 or 0<=n<100 or n>=100

Hint: remember the boolean expressions are checked in order

 

 

 

 

9. What is the output 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;
}

10. What is the output produced by the following code?

int key = 1;
switch (key + 1)
{
case 1: System.out.println("Apples"); break;
case 2: System.out.println("Oranges"); break;
case 3: System.out.println("Peaches");
case 4: System.out.println("Plums"); break;
default: System.out.println("Fruitless");
}

11. What would be the output in Exercise 10 if the first line were changed to the following?

int key = 3;

12. What would be the output in Exercise 10 if the first line were changed to the following?

int key = 5;

13. Suppose n1 and n2 are two int variables that have been given values. Write a Boolean expression that returns true if the value of n1 is greater than or equal to the valiue of n2; otherwise, it should return false.

 

 

14. Suppose n1 and n2 are two int variables that have been given values. Write an if-else statement that outputs "n1" if n1 is greater than n2 and outputs "n2" otherwise.

 

 

15. Suppose variable1 and variable2 are two variables that have been given values. How do you test whether they are equal when the variables are of type int?

 

 

16. Assume that nextWord is a String variable that has been given a String value consisting entirely of letters. Write some Java code that outputs the message "First half of the alphabet", provided nextWord precedes "N" in alphabetic ordering. If nextWord does not precede "N" in alphabetic ordering, the code should output "Second half of the alphabet" (Note that "N" uses double quotes to produce a String value, as opposed to using single quotes to produce a char value.)

 

 

17. Wr;iter an if-else statement that outputs the word "Passed" provided the value of the variable exam is greater than or equal to 60 and also the value of the variable programsDone is greater than or equal to 10. Otherwise, the if-else statement should output the word "Failed". The variables exam and programsDone are both of type int.

 

 

18. Write an if-else statement that outputs the word "Emergency" provided the value of the variable presure is greater than 100 or the value of the variable temperature is greater than or equal to 212. Otherwise the if-else statement should output the word "OK". The variables pressure and temperature are both of type int.

 

 

19. Determine the value, true or false, of each of hte following Boolean expressions, assuming that the value of the variable count is 0 and the value of the variable limit is 10.

a. (count == 0) && (limit < 20)

b. count ==0 && limit < 20

c. (limit > 20) || (count<5)

d. !(count==12)

e. (count ==1) || (x < y)

f. (count < 10) || (x < y)

g. !(((count < 10) || (x < y)) && (count >=0) )

h. ((limit/count) > 7) || (limit < 20))

i. (limit < 20) || ((limit/count) > 7)

j. ((limit/count) > 7) && (limit < 0)

k. (limit < 0) && ((limit/count) > 7)