0% found this document useful (0 votes)
202 views

Answer Key Exercise On Lesson 9

The document provides example code and problems for exercises on lessons about Java programming concepts like conditional statements, loops, and data types. It includes sample code to demonstrate concepts like if/else statements, for loops, switch statements, and more. The problems ask the reader to analyze code snippets, write code to demonstrate various concepts, and determine the output of provided code examples.

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

Answer Key Exercise On Lesson 9

The document provides example code and problems for exercises on lessons about Java programming concepts like conditional statements, loops, and data types. It includes sample code to demonstrate concepts like if/else statements, for loops, switch statements, and more. The problems ask the reader to analyze code snippets, write code to demonstrate various concepts, and determine the output of provided code examples.

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 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);
3. Write a switch structure that uses the character myChar. It should increment the integer
variable y if myChar is either a capital or small letter G. It should decrement y if myChar is
either a capital or a small letter M. If myChar is anything else, add 100 to y.

4. What is output by the following code? String s = “Green”;


q = 0; switch(s)
{
case “Red”:
q++; case “Green”:
q++; case “Blue”: q++;
case “Yellow”: q++;
default: q++;
} System.out.println(--q);

5. Write a line of code that declares the variable chr as a character type and assigns the letter z to
it.

6. What is output by the following? int x = 10, y = 12;


System.out.println( “The sum is ” + x + y ); System.out.println( “The sum is ” + (x + y) );

7. Convert the following code into a switch statement.


10-5
if(speed = = 75)
{
System.out.println(“Exceeding speed limit”);
}
else if( (speed = = 69) || (speed = = 70) )
{
System.out.println(“Getting close”);
}
else if(speed = = 65)
{
System.out.println(“Cruising”);
} else {
System.out.println(“Very slow”); }

8. Is default a mandatory part of a switch structure?

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

You might also like