Answer Key Exercise On Lesson 9
Answer Key Exercise On Lesson 9
Exercise on Lesson 9
Use the following code for problems 1 – 10 and give the value of true_false for each: int i = 10, j = 3;
boolean true_false;
5. true_false=( (i>j)&&(j==0) );
6. true_false=( (j<50)||(j!=33) );
7. true_false=( !(j>=0)||(i<=50) );
11. Write a statement that will store a true in boolean b if the value in the variable m is 44 or less.
12. Write a statement that will store a false in boolean b if the value in r is greater than 17.
13. What is returned by the following expression? (Recall that the precedence order of logical
operators is !, &&, and finally | |.)
!( (2>3) | | (5= =5) && (7>1) && (4<15) | | (35<=36) && (89!=34) )
17. Write a statement that will store a false in boolean b if the value in g is not equal to 34.
18. Write a statement that will store a true in boolean b if integer k is even, false if it is odd.
19. Write a program that inputs a String from the keyboard after the prompt, “Enter your
password”. If it’s entered exactly as “XRay”, printout “Password entered successfully.”;
otherwise, have it printout “Incorrect password.”
20. What is output by the following “nested ifs” code? int k = 79;
if (k>50) {
} else {
if (k>30) System.out.println(“Three”);
else
System.out.println(“Four”);
Exercise on Lesson 10
1. What are the primary three permissible data types to use for x in the following? switch (x){ . .
.}
5. Write a line of code that declares the variable chr as a character type and assigns the letter z to
it.
9. Write a line of code that converts String s = “X” into a character called chr.
Exercise on Lesson 11
In each problem below state what is printed unless directed otherwise.
1. intj=0;
for (int g = 0; g <5; g++)
j++; System.out.println(j);
2. ints=1;
for (int j = 3; j >= 0; j--) {
s = s + j; }
System.out.println(s);
3. intp=6;
int m = 20, j;
for (j = 1; j < p; j++); //Notice the semicolon on this line {
m = m + j * j; }
System.out.println(m);