1 What value will be the result of evaluating the following

1. What value will be the result of evaluating the following expressions is x is 5, y is 6, and z is 8:

2. Write either IF statements or SWITCH statements for the following (NOTE: you need only provide the code for the selection statements, you do not need to write complete programs):

Assign a value of 100 to x when a boolean variable named doIT is true and assigns 50 to x otherwise.

A nested IF that checks to see if x is greater than 0 and then checks to see if the string aString is equal to \"this is my string\". If both conditions hold, print \"it\'s alive\"

A selection statement that will print \"First\", \"Second\", or \"Third\" depending upon whether the integer variable k is equal 1, 2, or 3 respectively.

Solution

1) x is 5, y is 6, and z is 8
a) (x == 4) && (y>2)
(x==4) evaluates to False [because 5!=4]
(y>2) evaluates to True [because 6>2]
False && True
Final Result : False

b) (7 <= x) || (z>4)
(7 <= x) evaluates to False [because 5 is not greater than 7]
(z>4) evaluates to True [because 8>4]
False || True
Final Result : True

c) (y != 2) && (++x == 5)
(y != 2) evaluates to True [because 6 != 2]
(++x == 5) evaluates to False [because 6 != 5]
True && False
Final Result : False

2)
a)
boolean doIT=false;
int x;
if(doIT==true)
{
   x=100;
}
else
{
   x=50;
}

Output:
50

b)
String aString=\"Hello\";   //
int x=5;   //Randomly taken value as 5
if(x>0)           //It goes inside outer if block
{
   if(aString==\"this is my string\")       // Doesn\'t goes inside this inner if block since aString\'s value is different
   {
       System.out.println(\"it\'s alive\");
   }      
}

c)
int k=1;       //Randomly taken value as 1
switch(k)
{
   case 1: System.out.println(\"First\");
   break;
   case 2: System.out.println(\"Second\");
   break;
   case 3: System.out.println(\"Third\");
   break;
   default : System.out.println(\"Invalid choice\");
}

Output :
First

1. What value will be the result of evaluating the following expressions is x is 5, y is 6, and z is 8: 2. Write either IF statements or SWITCH statements for t
1. What value will be the result of evaluating the following expressions is x is 5, y is 6, and z is 8: 2. Write either IF statements or SWITCH statements for t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site