• Tidak ada hasil yang ditemukan

CPCS202-Chapter3-Question answers

N/A
N/A
Protected

Academic year: 2024

Membagikan "CPCS202-Chapter3-Question answers"

Copied!
11
0
0

Teks penuh

(1)

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?

(2)

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

(3)
(4)
(5)

Input x,y

Is x >2

Is y >2

Zx+y

Print "z is", z Print "x is", x

N

N Y

Y

(6)

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

(7)
(8)

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;

(9)

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;

(10)

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

(11)

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

Referensi

Dokumen terkait

(In this case, Yolanda can call any whole number from 40 to 49, and in any of these cases Xavier can call 50, since 50 is no more than 10 greater than any of these numbers.) In

On the face of it, Theorem 2 is quite remarkable: given the axioms, the expected mean (transformed) probability estimates of true events are greater than (the transformation of)

Foursquare food and shop- and- service venues in Kansas City, Missouri, by the number of users in census tracts where the median age is either less than twenty- five or greater

b The worst case scenario average accuracy using only the worst 10% of the resamples Table 2 Number of individuals needed to achieve greater than 95% accuracy for either 50% or 90% of

In statistical analyses, which one of the following statements is true about the mean, median and mode of a normal distribution?. A The mean is always greater than the median and mode

Or, The time for travel the maximum height is half of the time for maximum distance for a throwing object.. Or, The double of the time for a throwing object is for maximum distance than

Write a query to return a list of department id’s and average salaries where the department’s average salary is greater than Ernst’s salary.. SELECT department_id,

From the observed parameters, the provision of 50 ppm GA3 had optimal growing the number of leaves, plant’s height, leaf’s area, fresh weight, dry weight, and this effect is inversely