Programming Principles I Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

-25 % 5 is _____

0

25 % 1 is _____

0

Suppose x is 1. What is x after x -= 1?

0

What is the output from System.out.println((int)Math.random() * 4)?

0

Which of the following is a possible output from invoking Math.random()? 323.4 0.5 34 1.0 0.0 0.234

0.5 0.0 0.234

If today is Tuesday, what will be the day in 100 days?

(2 + 100) % 7 = 4. So it is Thursday.

Which of the following statements are the same? (A) x -= x + 4 (B) x = x + 4 - x (C) x = x - (x + 4)

(A) and (C) are the same

What is wrong with the following programs? (a) 1 public class ShowErrors { 2 public static void main(String[] args) { 3 int i = 0; 4 do { 5 System.out.println(i + 4); 6 i++; 7 } 8 while (i < 10) 8 } 9 } (b) 1 public class ShowErrors { 2 public static void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 System.out.println(i + 4); 5 } 6 }

(a) Line 8: Missing ; at the end of this line. (b) Line 3: The ; at the end of for loop should be removed. for (int i = 0; i < 10; i++);

Rewrite the following conditional expressions using if-else statements. (a) score = (x > 10) ? 3 * scale : 4 * scale; (b) tax = (income > 10000) ? income * 0.2 : income * 0.17 + 1000; (c) System.out.println((number % 3 == 0) ? i : j);

(a) if (x > 10) score = 3 * scale; else score = 4 * scale; (b) if (income > 10000) tax = income * 0.2; else tax = income * 0.17 + 1000; (c) if (number % 3 == 0) System.out.println(i); else System.out.println(j);

a. How do you generate a random integer i such that 0 <= i < 20 ? b. How do you generate a random integer i such that 10 <= i < 20 c. How do you generate a random integer i such that 10 <= i <= 50 d. Write an expression that returns 0 or 1 randomly.

(a) (int)(Math.random() * 20) (b) 10 + (int)(Math.random() * 10) (c) 10 + (int)(Math.random() * 41) (d) (int)(Math.random() * 2)

(a) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100. (b) 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.

(a) (num > 1) && (num < 100) (b) (num > 1) && (num < 100) || num < 0

(a) Write a Boolean expression for |x - 5| < 4.5. (b) Write a Boolean expression for |x - 5| > 4.5.

(a) (x - 5) < 4.5 && (x - 5) > -4.5 (b) (x - 5) > 4.5 || (x - 5) < -4.5

How would you write the following arithmetic expression in Java? a. 4/(3(r + 34) - 9(a + bc) + (3 + d(2 + a) / (a + bd) b. 5.5 * (r + 2.5)^(2.5 + t)

(a) 4.0 / (3 * (r + 34)) - 9.0 * (a + b * c) + (3 + d * (2 + a)) / (a + b * d) (b) 5.5 * Math.pow(r + 2.5, 2.5 + t)

How many times are the following loop bodies repeated? What is the output of each loop? (a) int i = 1; while (i < 10) if (i % 2 == 0) System.out.println(i); (b) int i = 1; while (i < 10) if (i % 2 == 0) System.out.println(i++); (c) int i = 1; while (i < 10) if ((i++) % 2 == 0) System.out.println(i);

(a) Infinite number of times. (b) Infinite number of times. (c) The loop body is executed nine times. The output is 3, 5, 7, 9 on separate lines.

(a) Is (x > 0 && x < 10) the same as (x > 0 && x < 10)? (b) Is (x > 0 || x < 10) the same as (x > 0 || x < 10)? (c) Is (x > 0 || x < 10 && y < 0) the same as (x > 0 || (x < 10 && y < 0))?

(a) Yes. Since the relational operators have higher precedence than the && operator (see Table 3.8), x > 0 && x < 10 is the same as x > 0 && x < 10. (b) Yes. Since the relational operators have higher precedence than the || operator (see Table 3.8), (x > 0 || x < 10) is the same as (x > 0 || x < 10). (c) Yes. Since the && operator has higher precedence than the || operator (see Table 3.8), (x > 0 || x < 10 && y < 0) is the same as (x > 0 || (x < 10 && y < 0)).

Count the number of iterations in the following loops. (a) int count = 0; while (count < n) { count++; } (b) for (int count = 0; count <= n; count++) { } (c) int count = 5; while (count < n) { count++; } (d) int count = 5; while (count < n) { count = count + 3; }

(a) n times (b) n+1 times (c) n-5 times (d) The ceiling of (n-5)/3 times

How would you write the following arithmetic expression? (-b + squarerootof b^2 - 4ac) / 2a

(b + Math.pow(b * b - 4 * a * c, 0.5)) / (2 * a)

Write conditional expression that returns -1 or 1 randomly.

(int)(Math.random() * 2) == 0 ? -1 : 1;

Assuming that x is 1, show the result of the following Boolean expressions. (true) && (3 > 4) !(x > 0) && (x > 0) (x > 0) || (x < 0) (x != 0) || (x == 0) (x >= 0) || (x < 0) (x != 1) == !(x == 1)

(true) && (3 > 4) is false !(x > 0) && (x > 0) is false (x > 0) || (x < 0) is true (x != 0) || (x == 0) is true (x >= 0) || (x < 0) is true (x != 1) == !(x == 1) is true

The command to exit JShell is __________

/exit

Suppose, when you run the following program, you enter the input 2 3 6 from the console. What is the output? public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); System.out.println("(x < y && y < z) is " + (x < y && y < z)); System.out.println("(x < y || y < z) is " + (x < y || y < z)); System.out.println("!(x < y) is " + !(x < y)); System.out.println("(x + y < z) is " + (x + y < z)); System.out.println("(x + y > z) is " + (x + y > z)); } }

(x < y && y < z) is true (x < y || y < z) is true !(x < y) is false (x + y < z) is true (x + y > z) is false

The order of the precedence (from high to low) of the operators binary + * && || ^ is:

* + ^ && ||

What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10;

-10

-24 % -5 is

-4

-24 % 5 is _____

-4

The command to view all variables in JShell is __________

/vars

Which of the following is a valid identifier? Please select all that apply.

1. $343 2. radius

Which of the following is a constant, according to Java naming conventions? Please select all that apply.

1. MAX_VALUE 2. COUNT

According to Java naming convention, which of the following names can be variables? Please select all that apply.

1. findArea 2. totalLength

Which of the following assignment statements is incorrect? Please select all that apply.

1. i = 1 = j = 1 = k = 1; 2. i == j == k == 1;

Which of the following are correct ways to declare variables? Please select all that apply.

1. int length; int width; 2. int length, width;

Which of the following are correct names for variables according to Java naming conventions? Please select all that apply.

1. radius 2. findArea

Math.pow(4, 1 / 2) returns

1.0

Suppose m and r are integers. Write a Java expression for mr^2 to obtain a floating-point result.

1.0 * m * (r * r)

Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10).

10

Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)?

10

Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x++ > 10)

10

Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x-- > 10).

10

What is the result of 45 / 4?

11

Show the output of the following code: double a = 6.5; a += a + 1; System.out.println(a); a = 6; a /= 2; System.out.println(a);

14.0 3.0 Hint: a += a + 1 is a += 6.5 + 1, a += 7.5, a = a + 7.5, a = 6.5 + 7.5. Therefore, a is 14.0.

What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; }

2

Show the result of the following remainders. 56 % 6 78 % -4 -34 % 5 -34 % -5 5 % 1 1 % 5

2 2 -4 -4 0 1

If you enter 1 2 3, when you run this program, what will be the output? import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); double number1 = input.nextDouble(); double number2 = input.nextDouble(); double number3 = input.nextDouble(); // Compute average double average = (number1 + number2 + number3) / 3; // Display result System.out.println(average); } }

2.0

Math.pow(4, 1.0 / 2) returns

2.0

What is the value of (double)(5/2)?

2.0

What is the value of (double)5/2?

2.5

Show the output of the following code: double amount = 5; System.out.println(amount / 2); System.out.println(5 / 2);

2.5 2

The expression 4 + 20 / (3 - 1) * 2 is evaluated to

24

What is the result of 25 / 4? How would you rewrite the expression if you wished the result to be a floating-point number?

25 / 4 is 6. If you want the quotient to be a floating-point number, rewrite it as 25.0 / 4.0, 25.0 / 4, or 25 / 4.0.

How many days in the February of a leap year? Which of the following is a leap year? 500 1000 2000 2016 2020

29 days. 500 & 1000 are not leap years. 2000, 2016, & 2020 are leap years.

Suppose x is 1. What is x after x += 2?

3

24 % 5 is _____

4

What is i printed in the following code? public class Test { public static void main(String[] args) { int j = 0; int i = j++ + j * 5; System.out.println("What is i? " + i); } }

5

Which of the following are the same as 52.534? 5.2534e+1, 0.52534e+2, 525.34e-1, 5.2534e+0

5.2534e+1, 0.52534e+2, 525.34e-1 are the same as 52.534.

Which of the following are correct literals? 5_2534e+1, _2534, 5_2, 5_

5_2534e+1 and 5_2 are correct.

What is i printed? public class Test { public static void main(String[] args) { int j = 0; int i = ++j + j * 5; System.out.println("What is i? " + i); } }

6

Show the output of the following code: int a = 6; int b = a++; System.out.println(a); System.out.println(b); a = 6; b = ++a; System.out.println(a); System.out.println(b);

7 6 7 7

The expression (int)(76.0252175 * 100) / 100 evaluates to

76

Show the result of the following code: System.out.println(2 * (5 / 2 + 5 / 2)); System.out.println(2 * 5 / 2 + 2 * 5 / 2); System.out.println(2 * (5 / 2)); System.out.println(2 * 5 / 2);

8 10 4 5

Math.pow(2, 3) returns

8.0

List six relational operators.

<, <=, ==, !=, >, >=

The "less than or equal to" comparison operator in Java is

<=

the Java assignment operator

=

The equal comparison operator in Java is

==

How many accurate digits are stored in a float or double type variable?

A float value has 7-8 number of accurate digits and a double value has 15-17 number of accurate digits.

What is a round-off error? Can integer operations cause round-off errors? Can floating-point operations cause round-off errors?

A round-off error, also called a rounding error, is the difference between the calculated approximation of a number and its exact mathematical value. Integer operations will not cause rounding error. Floating-point operations may cause rounding error.

Which of the following is equivalent to x != y? Please select all that apply. A. ! (x == y) B. x > y && x < y C. x > y || x < y D. x >= y || x <= y

A. ! (x == y) C. x > y || x < y

Which of the following are so called short-circuit operators? Please select all that apply. A. && B. & C. || D. |

A. && C. ||

Which of the Boolean expressions below is incorrect? Please select all that apply. A. (true) && (3 => 4) B. !(x > 0) && (x > 0) C. (x > 0) || (x < 0) D. (x != 0) || (x = 0) E. (-10 < x < 0)

A. (true) && (3 => 4) D. (x != 0) || (x = 0) E. (-10 < x < 0)

What is the possible output from System.out.println((int)(Math.random() * 4))? Please select all that apply. A. 0 B. 1 C. 2 D. 3 E. 4

A. 0 B. 1 C. 2 D. 3

Which of the following are the same as 1545.534? Please select all that apply. A. 1.545534e+3 B. 0.1545534e+4 C. 1545534.0e-3 D. 154553.4e-2

A. 1.545534e+3 B. 0.1545534e+4 C. 1545534.0e-3 D. 154553.4e-2

Which of these data types requires the most amount of memory? A. long B. int C. short D. byte

A. long

Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? Please select all that apply. I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");

All are correct

Which of the following are correct literals for floating-point numbers? 12.3, 12.3e+2, 23.4e-2, -334.4, 20.5, 39F, 40D

All can be used as literals for floating-point numbers.

Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0;

All three are correct, but Code 3 is preferred.

The __________ method immediately terminates the program. A. System.terminate(0); B. System.halt(0); C. System.exit(0); D. System.quit(0); E. System.stop(0);

C. System.exit(0);

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? A. 1 < x < 100 && x < 0 B. ((x < 100) && (x > 1)) || (x < 0) C. ((x < 100) && (x > 1)) && (x < 0) D. (1 > x > 100) || (x < 0)

B. ((x < 100) && (x > 1)) || (x < 0)

Which of the following expression results in 45.37? A. (int)(45.378 * 100) / 100 B. (int)(45.378 * 100) / 100.0 C. (int)(45.378 * 100 / 100) D. (int)(45.378) * 100 / 100.0

B. (int)(45.378 * 100) / 100.0

Which of the following is a possible output from invoking Math.random()? Please select all that apply. A. 3.43 B. 0.5 C. 0.0 D. 1.0

B. 0.5 C. 0.0

Which of the following expressions will yield 0.5? Please select all that apply. A. 1 / 2 B. 1.0 / 2 C. (double) (1 / 2) D. (double) 1 / 2 E. 1 / 2.0

B. 1.0 / 2 D. (double) 1 / 2 E. 1 / 2.0

Assume x = 4 and y = 5, which of the following is true? A. !(x == 4) ^ y != 5 B. x != 4 ^ y == 5 C. x == 5 ^ y == 4 D. x != 5 ^ y != 4

B. x != 4 ^ y == 5

To add a value 1 to variable x, you write ______________. Please select all that apply. A. 1 + x = x; B. x += 1; C. x := 1; D. x = x + 1; E. x = 1 + x;

B. x += 1; D. x = x + 1; E. x = 1 + x;

Assume x = 4 and y = 5, which of the following is true? A. x < 5 && y < 5 B. x < 5 || y < 5 C. x > 5 && y > 5 D. x > 5 || y > 5

B. x < 5 || y < 5

Given |x| >= 4, which of the following is true? A. x >= 4 && x <= -4 B. x >= 4 || x <= -4 C. x >= 4 && x < -4 D. x >= 4 || x < -4

B. x >= 4 || x <= -4

Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; boolean even = (number % 2 == 0);

Both Code 1 and Code 2 are correct, but Code 2 is better.

Are the following statements correct? Which one is better? (a) if (age < 16) System.out.println ("Cannot get a driver's license"); if (age >= 16) System.out.println ("Can get a driver's license"); (b) if (age < 16) System.out.println ("Cannot get a driver's license"); else System.out.println ("Can get a driver's license");

Both are correct. (b) is better.

Evaluate the following expressions: 2 * 2 - 3 > 2 && 4 - 2 > 5 2 * 2 - 3 > 2 || 4 - 2 > 5

Both are false

Which of the following code displays the area of a circle if the radius is positive? A. if (radius != 0) System.out.println(radius * radius * 3.14159); B. if (radius >= 0) System.out.println(radius * radius * 3.14159); C. if (radius > 0) System.out.println(radius * radius * 3.14159); D. if (radius <= 0) System.out.println(radius * radius * 3.14159);

C. if (radius > 0) System.out.println(radius * radius * 3.14159);

Analyze the following code: public class Test { public static void main(String[] args) { int n = 10000 * 10000 * 10000; System.out.println("n is " + n); } } A. The program displays n is 1000000000000. B. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program is aborted. C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow. D. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program is aborted. E. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an underflow and the program continues to execute because Java does not report errors on underflow.

C. The result of 10000 * 10000 * 10000 is too large to be stored in an int variable n. This causes an overflow and the program continues to execute because Java does not report errors on overflow.

Which of the following is incorrect? A. int x = 9; B. long x = 9; C. float x = 1.0; D. double x = 1.0;

C. float x = 1.0;

Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true? A. if (isPrime = true) B. if (isPrime == true) C. if (isPrime) D. if (!isPrime = false) E. if (!isPrime == false)

C. if (isPrime)

Given |x| <= 4, which of the following is true? A. x <= 4 && x >= 4 B. x <= 4 && x > -4 C. x <= 4 && x >= -4 D. x <= 4 || x >= -4

C. x <= 4 && x >= -4

What are the naming conventions for class names, method names, constants, and variables? Which of the following items can be a constant, a method, a variable, or a class according to the Java naming conventions? MAX_VALUE, Test, read, readDouble

Class names: Capitalize the first letter in each name. Variables and method names: Lowercase the first word, capitalize the first letter in all subsequent words. Constants: Capitalize all letters. Constant: MAX_VALUE Method or variable: read , readDouble Class name: Test

What is wrong in the following code? if (score >= 60.0) System.out.println("D"); else if (score >= 70.0) System.out.println("C"); else if (score >= 80.0) System.out.println("B"); else if (score >= 90.0) System.out.println("A"); else System.out.println("F");

Consider score 90, what will be the grade? It will be D.

Which of the following statements is false? A. (x > 0 && x < 10) is same as ((x > 0) && (x < 10)) B. (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) C. (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) D. (x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0)

D. (x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0)

Which of the following expression results in a value 1? A. 2 % 1 B. 15 % 4 C. 25 % 5 D. 37 % 6

D. 37 % 6

Which of the following is incorrect? A. 1_2 B. 0.4_56 C. 1_200_229 D. _4544

D. _4544

Which of the following assignment statements is illegal? A. float f = -34; B. int t = 23; C. short s = 10; D. int t = 4.5;

D. int t = 4.5;

To add number to sum, you write __________. (Note: Java is case-sensitive) Please select all that apply. A. number += sum; B. number = sum + number; C. sum = Number + sum; D. sum += number; E. sum = sum + number;

D. sum += number; E. sum = sum + number;

Assume x = 4, which of the following is true? A. !(x == 4) B. x != 4 C. x == 5 D. x != 5

D. x != 5

Which of the following operators are right-associative. A. * B. + (binary +) C. % D. && E. =

E. =

The following code fragment reads in two numbers. What is the incorrect way to enter these two numbers? Scanner input = new Scanner(System.in); int i = input.nextInt(); double d = input.nextDouble();

Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.

Show the output in Listing 2.10 with the input value 1.99. Why does the program produce an incorrect result for the input 10.03?

Enter an amount in double, for example 11.56: 1.99 Your amount 1.99 consists of: 1 dollars 3 quarters 2 dimes 0 nickels 4 pennies Due to possible loss of precision, the program may produce incorrect results. See the last paragraph at the end of the section for the discussion on the input 10.03. Also for the input 2.01, the program will produce an incorrect result.

What is the output of the following code if number is 14, 15, or 30? (a) if (number % 2 == 0) System.out.println (number + " is even"); if (number % 5 == 0) System.out.println (number + " is multiple of 5"); (b) if (number % 2 == 0) System.out.println (number + " is even"); else if (number % 5 == 0) System.out.println (number + " is multiple of 5");

For (a) if number is 14, the output is: 14 is even if number is 15, the output is: 15 is multiple of 5 if number is 30, the output is: 30 is even 30 is multiple of 5 For (b) if number is 14, the output is: 14 is even If number is 15, the output is: 15 is multiple of 5 if number is 30, the output is: 30 is even

Find the largest and smallest byte, short, int, long, float, and double. Which of these data types requires the least amount of memory?

For byte, from -128 to 127, inclusive. For short, from -32768 to 32767, inclusive. For int, from -2147483648 to 2147483647, inclusive. For long, from -9223372036854775808 to 9223372036854775807. For float, the smallest positive float is 1.40129846432481707e-45 and the largest float is 3.40282346638528860e+38. For double, the smallest positive double is 4.94065645841246544e-324 and the largest double is 1.79769313486231570e+308d.

Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is the best? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");

II (2)

What is the output of the code in (a) and (b) if number is 30? What if number is 35? (a) if (number % 2 == 0) System.out.println(number + " is even."); System.out.println(number + " is odd."); (b) if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd.");

If number is 30, (a) displays 30 is even 30 is odd (b) displays 30 is even If number is 35, (a) displays 35 is odd (b) displays 35 is odd

What is the value of the expression x >= 50 && x <= 100 if x is 45, 67, or 101?

If x is 45, the expression is false. If x is 67, the expression is true. If x is 101, the expression is false.

Suppose income is 4001, what is the output of the following code? if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); }

Income is greater than 3000

What happens if you enter an integer as 05?

It will be the same as entering 5.

What is wrong if guess is initialized to 0 in line 11 in Listing 5.3?

It would be wrong if it is initialized to a value between 0 and 100, because it could be the number you attempt to guess.

Identify and fix the errors in the following code: 1 public class Test { 2 public void main(string[] args) { 3 double i = 50.0; 4 double k = i + 50.0; 5 double j = k + 1; 6 7 System.out.println("j is " + j + " and 8 k is " + k); 9 } 10 }

Line 2: Missing static for the main method. Line 2: string should be String. Lines 7 to 8: The string cannot be broken into two lines.

Identify and fix the errors in the following code: 1 public class Test { 2 public void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 sum += i; 5 6 if (i < j); 7 System.out.println(i) 8 else 9 System.out.println(j); 10 11 while (j < 10); 12 { 13 j++; 14 } 15 16 do { 17 j++; 18 } while (j < 10) 19 } 20 }

Line 2: missing static. Line 3: The semicolon (;) at the end of the for loop heading should be removed. Line 4: sum not defined. Line 6: the semicolon (;) at the end of the if statement should be removed. Line 6: j not defined. Line 7: Missing a semicolon for the first println statement. Line 11: The semicolon (;) at the end of the while heading should be removed. Line 18: Missing a semicolon at the end of the do-while loop.

Identify and fix the errors in the following code: 1 public class Test { 2 public static void main(String[] args) { 3 int i = k + 2; 4 System.out.println( i ); 5 } 6 }

Line 3: k is undefined

Write a statement to display the result of 2^3.5

Math.pow(2, 3.5)

How do you write 2.5 ^ 3.1 in Java?

Math.pow(2.5, 3.1)

The __________ method returns a raised to the power of b.

Math.pow(a, b)

What does REPL stand for? How do you launch JShell?

REPL stands for Read-Evaluate-Print Loop. You can launch JShell from the command window and type jshell.

Are there any performance differences between the following two import statements? import java.util.Scanner; import java.util.*;

No

Can you declare a variable as int and later redeclare it as double?

No

Will overflow cause a runtime error?

No

Can the following conversions involving casting be allowed? Write a test program to verify it. boolean b = true; i = (int)b; int i = 1; boolean b = (boolean)i;

No. Boolean values cannot be cast to other types.

If a variable is declared in a for loop control, can it be used after the loop exits?

No. The scope of the variable is inside the loop.

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? if (x > 2) if (y > 2) { int z = x + y; System.out.println("z is " + z); } else System.out.println("x is " + x);

Note that the else pairs with the most recent if. In this case, the else pairs with the second else. So code is same as if (x > 2) if (y > 2) { int z = x + y; System.out.println("z is " + z); } else System.out.println("x is " + x); and same as if (x > 2) { if (y > 2) { int z = x + y; System.out.println("z is " + z); } else System.out.println("x is " + x); } No output if x = 2 and y = 3. Output is "x is 3" if x = 3 and y = 2. Output is "z is 6" if x = 3 and y = 3.

Identify and fix the errors in the following code: 1 public class Test { 2 public static void main(String[] args) { 3 int i = j = k = 2; 4 System.out.println(i + " " + j + " " + k); 5 } 6 }

Note that the statement int i = j = k = 2 in line 3 only declares i. j and k are not declared. The following line would declare i, j, and k: int i, j, k; To fix the error, change line 3 to int j, k; int i = j = k = 2; or int i = 2; int j = 2; int k = 2; or int i = 2, j = 2, k = 2;

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

Note: else matches the first if clause. No output if x = 3 and y = 2. Output is "z is 7" if if x = 3 and y = 4. Output is "x is 2" if if x = 2 and y = 2. Click on the Flowchart to the right

What is an integer overflow? Can floating-point operations cause overflow?

Numbers are stored with a limited numbers of digits. When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. Overflow is for integer operations. Floating-point operations will not cause overflow.

Write an expression that rounds down a double value in variable d to an integer. Write an expression that rounds a double value in variable d to an integer.

Rounding down d to an integer: (int)(d) Rounding d to an integer: (int)(d + 0.5) Correction: In the last paragraph in Section 2.16 in the book, replace "rounded up" with "rounded". Note: In mathematics, roundup(7.1) is 8. round(7.1) is 7. rounddown(7.6) is 7. round(7.6) is 8.

Do the following two loops result in the same value in sum? (a) for (int i = 0; i < 10; ++i) { sum += i; } (b) for (int i = 0; i < 10; i++) { sum += i; }

Same. When the i++ and ++i are used in isolation, their effects are same.

Revise the code using the System.nanoTime() to measure the time in nano seconds?

Simply replace System.currentTimeMillis() using System.nanoTime().

Suppose that, when you run the following program, you enter the input 2 3 6 from the console. What is the output? public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); System.out.println((x < y && y < z) ? "sorted" : "not sorted"); } }

Sorted

What data types are required for a switch variable? If the keyword break is not used after a case is processed, what is the next statement to be executed? Can you convert a switch statement to an equivalent if statement, or vice versa? What are the advantages of using a switch statement?

Switch variables must be of char, byte, short, int, or String types. If a break statement is not used, the next case statement is performed. You can always convert a switch statement to an equivalent if statement, but not an if statement to a switch statement. The use of the switch statement can improve readability of the program in some cases. The compiled code for the switch statement is also more efficient than its corresponding if statement.

To obtain the current second, use _________

System.currentTimeMillis() / 1000 % 60

To obtain the current minute, use ________.

System.currentTimeMillis() / 1000 / 60 % 60

To obtain the current hour in UTC, use _________.

System.currentTimeMillis() / 1000 / 60 / 60 % 24

If you change (int)(tax * 100) / 100.0 to (int)(tax * 100) / 100 in line 11 in Listing 2.8, what will be the output for the input purchase amount of 197.556?

The answer is 11 Here is the reason: tax = purchaseAmount * 0.06 = 197.556 * 0.06 = 11.85336 tax * 100 = 1185.336 (int)(tax * 100) = 1185 1185 / 100 = 11

Analyze the following code. boolean even = false; if (even) { System.out.println("It is even!"); }

The code displays nothing

What are the differences between a while loop and a do-while loop? Convert the following while loop into a do-while loop. Scanner input = new Scanner(System.in); int sum = 0; System.out.println("Enter an integer " + "(the input ends if it is 0)"); int number = input.nextInt(); while (number != 0) { sum += number; System.out.println("Enter an integer " + "(the input ends if it is 0)"); number = input.nextInt(); }

The difference between a do-while loop and a while loop is the order of evaluating the continuation-condition and executing the loop body. In a while loop, the continuation-condition is checked and then, if true, the loop body is executed. In a do-while loop, the loop body is executed for the first time before the continuation-condition is evaluated. Scanner input = new Scanner(System.in); int sum = 0; int number; do { System.out.println("Enter an integer " + "(the input ends if it is 0)"); number = input.nextInt(); sum += number; } while (number != 0);

What does an explicit casting from a double to an int do with the fractional part of the double value? Does casting change the variable being cast?

The fractional part is truncated. Casting does not change the variable being cast.

What does the following statement do? for ( ; ; ) { // Do something }

The loop keeps doing something indefinitely.

What is wrong in the following code? if radius >= 0 { area = radius * radius * PI; System.out.println("The area for the circle of " + " radius " + radius + " is " + area); }

The parentheses is required for the condition radius >= 0.

List the precedence order of the Boolean operators. Evaluate the following expressions: true || true && false true && true || false

The precedence order for boolean operators is !, ^, &&, and || true || true && false is true true && true || false is true

Analyze the following code. public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } }

The program has a syntax error, because 09 is an incorrect literal value.

Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even"); }

The program runs fine and displays It is even.

Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100");

The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses.

Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; }

The switch control variable cannot be double.

What are the three parts of a for loop control? Write a for loop that prints the numbers from 1 to 100.

The three parts in a for loop control are as follows: -The first part initializes the control variable. -The second part is a Boolean expression that determines whether the loop will repeat. -The third part is the adjustment statement, which adjusts the control variable. for (int i = 1; i <= 100; i++) System.out.println(i);

What are the benefits of using constants? Declare an int constant SIZE with value 20.

There are three benefits of using constants: (1) you don't have to repeatedly type the same value; (2) the value can be changed in a single location, if necessary; (3) the program is easy to read. final int SIZE = 20;

What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?

There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.

Show the output of the following programs. (Hint: Draw a table and list the variables in the columns to trace these programs.) (a) public class Test { public static void main(String[] args) { for (int i = 1; i < 5; i++) { int j = 0; while (j < i) { System.out.print(j + " "); j++; } } } } (b) public class Test { public static void main(String[] args) { int i = 0; while (i < 5) { for (int j = i; j > 1; j--) System.out.print(j + " "); System.out.println("****"); i++; } } } (c) public class Test { public static void main(String[] args) { int i = 5; while (i >= 1) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "xxx"); num *= 2; } System.out.println(); i--; } } } (d) public class Test { public static void main(String[] args) { int i = 1; do { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "G"); num += 2; } System.out.println(); i++; } while (i <= 5); } }

Tip for tracing programs: Draw a table to see how variables change in the program. Consider (a) for example. i j output 1 0 0 1 1 2 0 0 2 1 1 2 2 3 0 0 3 1 1 3 2 2 3 3 4 0 0 4 1 1 4 2 2 4 3 3 4 4 (a). 0 0 1 0 1 2 0 1 2 3 (b). **** **** 2 **** 3 2 **** 4 3 2 **** (c). 1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx (d). 1G 1G3G 1G3G5G 1G3G5G7G 1G3G5G7G9G

Every letter in a Java keyword is in lowercase?

True

True or false? All the binary operators except assignment operators (including augmented assignment operators) are left associative.

True

How do you write a statement to let the user enter a double value from the keyboard? What happens if you entered 5a when executing the following code? double radius = input.nextDouble( );

Use Scanner input = new Scanner(System.in); double value = input.nextDouble( ); A runtime error will occur if you entered 5a when executing the following code: double radius = input.nextDouble( );

Which of the following identifiers are valid? Which are Java keywords? miles, Test, a++, --a, 4#R, $4, #44, apps, class, public, int, x, y, radius

Valid identifiers: miles, Test, $4, apps, x, y, radius Invalid identifiers: a++, --a, 4#R, #44, class, public, int Keywords: class, public, int

How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j)

When i is 0, the println statement is not executed. When i is 1, the println statement is executed once. When i is 2, the println statement is executed two times. When i is 3, the println statement is executed three times. When i is 9, the println statement is executed nine times. So, the total is 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

Are the following four statements equivalent? number += 1; number = number + 1; number++; ++number;

Yes

Are the following two expressions the same? (a) x % 2 == 0 && x % 3 == 0 (b) x % 6 == 0

Yes

Are the following two statements equivalent? (a) if (income <= 10000) tax = income * 0.1; else if (income <= 20000) tax = 1000 + (income - 10000) * 0.15; (b) if (income <= 10000) tax = income * 0.1; else if (income > 10000 && income <= 20000) tax = 1000 + (income - 10000) * 0.15;

Yes

Are the following statements correct? If so, show the output. System.out.println("25 / 4 is " + 25 / 4); System.out.println("25 / 4.0 is " + 25 / 4.0); System.out.println("3 * 2 / 4 is " + 3 * 2 / 4); System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);

Yes, the statements are correct. The output is 25 / 4 is 6 25 / 4.0 is 6.25 3 * 2 / 4 is 1 3.0 * 2 / 4 is 1.5

Can you always convert a while loop into a for loop? Convert the following while loop into a for loop. int i = 1; int sum = 0; while (sum < 10000) { sum = sum + i; i++; }

Yes. for (int i=1; sum < 10000; i++) sum = sum + i;

Can you convert a for loop to a while loop? List the advantages of using for loops.

Yes. When using a while loop, programmers often forget to adjust the control variable such as i++. Using for loop can avoid this error.

Can different types of numeric values be used together in a computation?

Yes. Different types of numeric values can be used in the same computation through numeric conversions referred to as casting.

In Java, the word true is

a Boolean literal

requirements specification

a formal process that seeks to understand the problem and document in detail what the software system needs to do.

Which of the following statements are equivalent? Which ones are correctly indented? (a) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; (b) if (i > 0) { if (j > 0) x = 0; else if (k > 0) y = 0; } else z = 0; (c) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; (d) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0;

a, c, and d are the same. (B) and (C) are correctly indented.

Write a Boolean expression that evaluates to true if age is greater than 13 and less than 18.

age > 13 && age < 18

What is the exact output of the following code? double area = 3.5; System.out.print("area"); System.out.print(area);

area3.5

Which of these statements are true? a. Any expression can be used as a statement. b. The expression x++ can be used as a statement. c. The statement x = x + 5 is also an expression. d. The statement x = y = x = 0 is illegal.

b and c are true.

To improve readability and maintainability, you should declare a _________ for PI instead of using literal values such as 3.14159.

constant

Analyze the following code. Is count < 100 always true, always false, or sometimes true or sometimes false at Point A, Point B, and Point C? int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C

count < 100 is always true at Point A. count < 100 always false at Point C. count < 100 is sometimes true or sometimes false at Point B.

If you attempt to add an int, a byte, a long, and a double, the result will be a(n) __________ value.

double

Translate the following algorithm into Java code: Step 1: Declare a double variable named miles with initial value 100. Step 2: Declare a double constant named KILOMETERS_PER_MILE with value 1.609. Step 3: Declare a double variable named kilometers, multiply miles and KILOMETERS_PER_MILE, and assign the result to kilometers. Step 4: Display kilometers to the console. What is kilometers after Step 4?

double miles = 100; final double KILOMETERS_PER MILE = 1.609; double kilometers = KILOMETERS_PER MILE * miles; System.out.println(kilometers); The value of kilometers is 160.9.

Show the output of the following code: float f = 12.5F; int i = (int)f; System.out.println("f is " + f); System.out.println("i is " + i);

f is 12.5 i is 12

What is the output of the following code? boolean even = false; System.out.println(even ? "true" : "false");

false

To declare a constant MAX_LENGTH inside a method with value 99.98, you write:

final double MAX_LENGTH = 99.98;

Write an if statement that increases pay by 3% if score is greater than 90.

if (score > 90) pay *= 1.03;

Write an if statement that increases pay by 3% if score is greater than 90, otherwise increases pay by 1%.

if (score > 90) pay *= 1.03; else pay *= 1.01;

Write an if statement that assigns 1 to x if y is greater than 0.

if (y > 0) x = 1;

Suppose a Scanner object is created as follows, what method do you use to read a real number? Scanner input = new Scanner(System.in);

input.nextDouble( );

To declare an int variable number with initial value 2, you write:

int number = 2;

Rewrite the switch statement in Listing 3.8 using an if-else statement.

int remainder = year % 12; if (remainder == 0) System.out.println("monkey"); else if (remainder == 1) System.out.println("rooster"); else if (remainder == 2) System.out.println("dog"); else if (remainder == 3) System.out.println("pig"); else if (remainder == 4) System.out.println("rat"); else if (remainder == 5) System.out.println("ox"); else if (remainder == 6) System.out.println("tiger"); else if (remainder == 7) System.out.println("rabbit"); else if (remainder == 8) System.out.println("dragon"); else if (remainder == 9) System.out.println("snake"); else if (remainder == 10) System.out.println("horse"); else System.out.println("sheep");

The following code displays ___________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right");

just right

How do you obtain the current second, minute, and hour?

long totalMills = System.currentTimeMillis() returns the milliseconds since Jan 1, 1970. long totalSeconds = totalMills / 1000 returns the total seconds. long totalMinutes = totalSeconds / 60 returns the total minutes. totalSeconds % 60 returns the current second. totalMinutes % 60 returns the current minute. totalMinutes / 60 % 24 returns the current hour.

Suppose the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number = input.nextInt(); int max = number; do { number = input.nextInt(); if (number > max) max = number; } while (number != 0); System.out.println("max is " + max); System.out.println("number " + number); } }

max is 5 number 0

Suppose the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number = input.nextInt(); int max = number; while (number != 0) { number = input.nextInt(); if (number > max) max = number; } System.out.println("max is " + max); System.out.println("number " + number); } }

max is 5 number 0

Rewrite the following statement using a Boolean expression: if (count % 10 == 0) newLine = true; else newLine = false;

newLine = (count % 10 == 0);

When assigning a literal to a variable of the byte type, if the literal is too large to be stored as a byte value, it

receives a compile error

analysis

seeks to analyze the data flow and to identify the systems input and output. When you do analysis, it helps to identify what the output is first, and then figure out what input data you need in order to produce the output.

Suppose the input is 2 3 4 5 0. What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number, sum = 0, count; for (count = 0; count < 5; count++) { number = input.nextInt(); sum += number; } System.out.println("sum is " + sum); System.out.println("count is " + count); } }

sum is 14 count is 5

Write a switch statement that displays Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, if day is 0, 1, 2, 3, 4, 5, 6, accordingly.

switch (day) { case 0: System.out.println("Sunday"); break; case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thurday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; }

Pseudocode

the code with natural language mixed with Java code.

The System.currentTimeMillis( ) returns

the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time)

Rewrite the following if statements using the conditional operator. if (ages >= 16) ticketPrice = 20; else ticketPrice = 10;

ticketPrice = (ages >= 16) ? 20 : 10;

What is 1 + 1 + 1 + 1 + 1 == 5?

true

What is the value of the following expression? true || true && false

true

Assuming that x is 1, show the result of the following Boolean expressions: x > 0 x < 0 x != 0 x >= 0 x != 1

true false true true false

Write a Boolean expression that evaluates to true if weight is greater than 50 pounds and height is greater than 60 inches.

weight > 50 && height > 60.

Write a Boolean expression that evaluates to true if either weight is greater than 50 pounds or height is greater than 60 inches, but not both.

weight > 50 ^ height > 60.

Write a Boolean expression that evaluates to true if weight is greater than 50 pounds or height is greater than 60 inches.

weight > 50 || height > 60.

Convert the following for loop statement to a while loop and to a do-while loop: long sum = 0; for (int i = 0; i <= 1000; i++) sum = sum + i;

while loop: long sum = 0; int i=0; while (i <= 1000) { sum += i++; } do-while loop: long sum = 0; int i = 0; do { sum += i++; } while (i <= 1000);

Suppose x = 1, y = -1, and z = 1. What is the output of the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");

x < 0 and z > 0;

To assign a double variable d to a float variable x, you write:

x = (float)d;

To assign a value 1 to variable x, you write

x = 1;

Assume that x and y are int type. Which of the following are legal Java expressions? x > y > 0 x = y && y x /= y x or y x and y (x != 0) || (x = 0)

x > y > 0 is incorrect x = y && y is incorrect x /= y is correct x or y is incorrect x and y is incorrect (x != 0) || (x = 0) is incorrect on x = 0. It should be x == 0.

What is the output of the following code? Explain the reason. int x = 80000000; while (x > 0) x++; System.out.println("x is " + x);

x is -2147483648 The reason: When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. 2147483647 + 1 is actually -2147483648

What is the output of the following code? int x = 0; if (x < 4) { x = x + 1; } System.out.println("x is " + x);

x is 1

What is x after the following if-else statement is executed? Use a switch statement to rewrite it and draw the flowchart for the new switch statement. int x = 1, a = 3; if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34;

x is 17 switch (a) { case 1: x += 5; break; case 2: x += 10; break; case 3: x += 16; break; case 4: x += 34; } Click on flowchart to the right

What is x after the following statements? int x = 1; x *= x + 1;

x is 2

Suppose int x = 3264, what is the output of the following code? int y = x % 10; x = x / 10; System.out.println("x is " + x + " and y is " + y);

x is 326 and y is 4

What is x after the following statements? int x = 2; int y = 1; x *= y + 1;

x is 4

What is the output of the following code: double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y);

x is 5.5 and y is 5

What is y displayed? public class Test { public static void main(String[] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } }

y is 2

What is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } }

y is 2 because x + 1 is assigned to x and then x is assigned to y.

What is y after the following switch statement is executed? Rewrite the code using an if-else statement. x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; }

y is 2. x = 3; y = 3; if (x + 3 == 6) { y = 1; } y += 1;

What is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x; System.out.println("y is " + y); } }

y is 3


Set pelajaran terkait

FIN 4610 Corporate Finance Chapter 11

View Set

Quiz Bowl: Mountains of the World

View Set

Ch. 5 Dosage Calculation of IV Solutions and Drugs

View Set

Nutrition in Health: Chapter 7: Amino Acids

View Set

Chapter 15 Inquizitive: The Era of Reconstruction

View Set

Bible: Quiz 1: Glorious Christ and His People

View Set

Foreign Service Officer Test - World Affairs

View Set

Chapter 07: Nursing Management of Pain During Labor and Birth

View Set

Unit 2: Cell Structure, function, and membrane transport AP biology

View Set