int i = 1;
boolean b = (boolean)i;
3.3 What is the printout of the code in (a) and (b) if number is 30 and 35, respectively?
3.4 Suppose x = 3 and y = 2; show the output, if any, of the following code.
What is the output if x = 3 and y = 4?
What is the output if x = 2 and y = 2?
Draw a flow chart of the code:
if (x > 2) {
if (y > 2) {
z = x + y;
System.out.println("z is " + z);
} } else
System.out.println("x is " + x);
Input x,y
Is x >2
Is y >2
Zx+y
Print "z is", z Print "x is", x
N
N Y
Y
3.5 Which of the following statements are equivalent? Which ones are correctly indented?
b is different, b is correctly indented.
3.6 Suppose x = 2 and y = 3. Show the output, if any, of the following code. What is the output if x = 3 and y = 2? What is the output if x = 3 and y = 3?
(Hint: Indent the statement correctly first.)
3.7 Are the following 2 statements equivalent?
Yes
3.8 Which of the following is a possible output from invoking Math.random()?
323.4, 0.5, 34, 1.0, 0.0, 0.234
3.9 How do you generate a random integer i such that 0 <= i < 20
int x = (int)(Math.random() *20);
How do you generate a random integer i such that 10 <= i < 20
int x = (int)(Math.random() *10.999)+ 10; How do you generate a random integer i such that 10 <= i <= 50
int x = (int)(Math.random() *40.999)+ 10;
3.11 (a) Write an if statement that increases pay by 3% if score is greater than 90.
If(score>90) pay+=pay*3/100 ;
(b) Write an if statement that increases pay by 3% if score is greater than 90, otherwise increases pay by 1%.
If(score>90)
pay+=pay*3/100 ; else
pay+=pay*1/100 ;
3.12 What is wrong in the following code?
Let score = 100
If (score>=60.0) is true then grade=D this is true for all scores from 60.0 to ---
It should be : if (score <=60), if(score<=70.0)---
3.13 Rewrite the following statement using a Boolean expression:
if (count % 10 == 0) newLine = true;
else
newLine = false;
3.14 Assuming that x is 1, show the result of the following Boolean expressions.
3.15 Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100.
(num>=1)&&(num<=100)
3.16 Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.
((num>=1)&&(num<=100))||(num<0)
3.20 Write a Boolean expression that evaluates true if age is greater than 13 and less than 18.
(age>13)&&(age<18)
3.22 Write a Boolean expression that evaluates true if weight is greater than 50 or height is greater than 160.
(weight>50)||(height>160)
3.23 Write a Boolean expression that evaluates true if weight is greater than 50 and height is greater than 160.
(weight>50)&&(height>160)
3.24 Write a Boolean expression that evaluates true if either weight is greater than 50 or height is greater than 160, but not both.
(weight>50) ^ (height>160) 3.26 What is y after the following switch statement is executed?
x = 3; y = 3;
switch (x + 3) { case 6: y = 1;
default: y += 1;
}
(true) && (3 > 4) False
!(x > 0) && (x > 0) False
(x > 0) || (x < 0) True
(x != 0) || (x == 0) True
(x >= 0) || (x < 0) True or False True
(x != 1) == !(x == 1) False = False True
x 3
y 1 x 3
y 2
x 3
y 3
switch(day(
{
case 0: dayName="Sunday" ; break;
case 1 : dayName="Monday";
break;
case 2: dayName="Tuesday" ; break;
case 3: dayName="Wednesday" ; break;
case 4: dayName="Thursday" ; break;
case 5: dayName="Friday" ; break;
case 6: dayName="Saturday" ; break;
default:
dayName="error" ; }
3.32 What is wrong in the following statements?
(a) System.out.printf("%5d %d", 1, 2, 3);
+ is missing , Extra number 3 (b) System.out.printf("%5d %f", 1);
+ is missing, Missing number for float (c) System.out.printf("%5d %f", 1, 2);
+ is missing,2 is not float