0% found this document useful (0 votes)
150 views4 pages

Exercise On Lesson 10 - p1

The document contains sample code and exercises related to Java programming lessons on logical operators, conditional statements, and switch statements. Some key points: 1) There are examples evaluating boolean expressions using logical operators like &&, ||, and !. 2) Exercises test understanding of conditional statements like if-else and how to assign boolean values based on condition evaluations. 3) Questions cover primitive data types that can be used in switch statements and sample code to demonstrate switch statement behavior.

Uploaded by

myob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views4 pages

Exercise On Lesson 10 - p1

The document contains sample code and exercises related to Java programming lessons on logical operators, conditional statements, and switch statements. Some key points: 1) There are examples evaluating boolean expressions using logical operators like &&, ||, and !. 2) Exercises test understanding of conditional statements like if-else and how to assign boolean values based on condition evaluations. 3) Questions cover primitive data types that can be used in switch statements and sample code to demonstrate switch statement behavior.

Uploaded by

myob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Answer key

Exercise on Lesson 8
In problems1 – 5 assume the following: int z = 23, x = -109;
double c = 2345.19, v = 157.03; boolean a = false, s = true;

1. booleangus=(x>0)&&(c==v); System.out.println(!gus);

2. System.out.println(a | | s);

3. System.out.println( ( (-1 * x) > 0) && !a );

4. booleanr=z==x; System.out.println( r | | false );

5. 5. System.out.println( z!=x );

6. 6. Fill in the following charts.


a b false false false true true false true true (!a && b) a
b (a||!b) false false false true true false true true

In order given on the book: (!a &&b) false, true, false, false,
(a||!b) False, true, true, true

7. Assume b, p, and q are booleans. Write code that will assign to b the result of AND- ing p and
q.
b=p&&q;
boolean w=(x>0) || (y==z);

8. Assign to the boolean variable w the result of OR-ing the following two things: A test to see
if x is positive: A test to see if y equals z:

9. What are the two possible values of a boolean variable?


False and true

10. Write a test that will return a true if a is not equal to b. Assume a and b are integers.
Store the result in boolean kDog.

Boolean kDog=a!=b
1. Write the answer to #10 another way. (?? L’ho scritto io, da verificare )

if (a!=b) {
kDog = true
}
else {
kDog = false
}
2. What is the Java operator for boolean AND-ing?

||

3. What is the Java operator for boolean OR-ing?

4. System.out.println( (true && false) | | ( (true && true) | | false ) );

5. System.out.println(true && true || false);

6. System.out.println(true || true && false);

7. System.out.println(false || true && false);

8. System.out.println(false && true || false);

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;

1. true_false = (j > i);

2. true_false = (i > j);

3. true_false = (i= = j);


4. true_false=((j<=i)||(j>=i) );

5. true_false=( (i>j)&&(j==0) );

6. true_false=( (j<50)||(j!=33) );

7. true_false=( !(j>=0)||(i<=50) );

8. true_false = ( !(! (!true)) );

9. true_false = (5 < = 5);


10. true_false = (j != i);

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) )

In problem 14 – 16 what is the output?


14. String s1 = “school BUS”;
if ( s1.equals(“school bus”) )
System.out.println(“Equal”); else
System.out.println(“Not equal”);

15. String s1 = “school BUS”;


if ( s1.equalsIgnoreCase(“school bus”) ) System.out.println(“Equal”);
else
System.out.println(“Not equal”);
9-3

16. int j = 19, m = 200; if (j= =18)


m++;
j++; System.out.println(m);
System.out.println(j);

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) {

if (k<60) {System.out.println(“One”);} else


{ System.out.println(“Two”);}

} 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){ . .
.}

2. What is the output of the following code? int x = 3, p = 5, y = -8;


switch(x)
{
case 2:
p++;
case 3: case 4:
y+=(--p);
break; case 5:
y+=(p++); }
System.out.println(y);

You might also like