cs116 exam 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

How do you display the characters \ and "?

'\\' and '\"'

Math.asin(Math.sin(Math.PI / 6))?

0.5235987755982988

What is Math.exp(Math.log(5.5))?

5.5

What would be wrong if lines 6-7 in Listing 4.5 is replaced by the following code?String lottery = "" + (int)(Math.random() * 100);

lottery may have only one digit.

Which of the following are correct literals for characters? '1', '\u345dE', '\u3fFa', '\b', '\t'

'\u345dE' is wrong. It must have exactly four hex numbers in the Unicode.

Write an expression that obtains a random integer between 34 and 55. Write an expression that obtains a random integer between 0 and 999. Write an expression that obtains a random number between 5.5 and 55.5.

(a) 34 + (int)(Math.random() * (55 - 34)) (b) (int)(Math.random() * 1000) (c) 5.5 + (Math.random() * (55.5 - 5.5))

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.

Show the output of the following statements. (a) System.out.printf("amount is %f %e\n", 32.32, 32.32); (b) System.out.printf("amount is %5.2f%% %5.4e\n", 32.327, 32.32); (c) System.out.printf("%6b\n", (1 > 2)); (d) System.out.printf("%6s\n", "Java"); (e) System.out.printf("%-6b%s\n", (1 > 2), "Java"); (f) System.out.printf("%6b%-8s\n", (1 > 2), "Java"); (g) System.out.printf("%,5d %,6.1f\n", 312342, 315562.932); (h) System.out.printf("%05d %06.1f\n", 32, 32.32);

(a) amount is 32.320000 3.233000e+01 (b) amount is 32.33% 3.2330e+01 (c) *false // * denote a space (d) **Java // * denote a space (e) false*Java (f) *falseJava****(g) 312,342 315,562.9 (h) 00032 0032.3

Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements: a. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual. b. Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual. c. Compare s1 with s2 and assign the result to an int variable x. d. Compare s1 with s2, ignoring case, and assign the result to an int variable x. e. Check whether s1 has the prefix AAA and assign the result to a Boolean variable b. f. Check whether s1 has the suffix AAA and assign the result to a Boolean variable b. g. Assign the length of s1 to an int variable x. h. Assign the first character of s1 to a char variable x. i. Create a new string s3 that combines s1 with s2. j. Create a substring of s1 starting from index 1. k. Create a substring of s1 from index 1 to index 4. l. Create a new string s3 that converts s1 to lowercase. m. Create a new string s3 that converts s1 to uppercase. n. Create a new string s3 that trims whitespaces on both ends of s1. o. Assign the index of the first occurrence of the character e in s1 to an int variable x. p. Assign the index of the last occurrence of the string abc in s1 to an int variable x.

(a) boolean isEqual = s1.equals(s2); (b) boolean isEqual = s1.equalsIgnoreCase(s2); (c) int x = s1.compareTo(s2); (d) int x = s1.compareToIgnoreCase(s2); (e) boolean b = s1.startsWith("AAA"); (f) boolean b = s1.endsWith("AAA"); (g) int x = s1.length(); (h) char x = s1.charAt(0); (i) String s3 = s1 + s2; (j) String s3 = s1.substring(1); (k) String s3 = s1.substring(1, 5); (l) String s3 = s1.toLowerCase(); (m) String s3 = s1.toUpperCase(); (n) String s3 = s1.trim(); (o) int x = s1.indexOf('e'); (p) int x = s1.lastIndexOf("abc");

What is wrong in the following statements? (a) System.out.printf("%5d %d\n", 1, 2, 3); (b) System.out.printf("%5d %f\n", 1); (c) System.out.printf("%5d %f\n", 1, 2); (d) System.out.printf("%.2f\n%0.3f\n", 1.23456, 2.34); (e) System.out.printf("%08s\n", "Java");

(a) the last item 3 does not have any specifier. (b) There is not enough items.(c) The data for %f must a floating-point value. (d) %0.3f is wrong. Width cannot be zero.(e) %08s is wrong. 0 should be removed.

Write one statement to return the number of digits in a double value d.

(d + "").length()

Write one statement to return the number of digits in an integer i.

(i + "").length()

Write the code that generates a random lowercase letter.

(int)(Math.random() * 26 + 'a')

What is Math.sin(Math.asin(Math.PI / 6))?

0.5235987755982988

Evaluate the following expressions (write a program to verify your results): 1 + "Welcome " + 1 + 1 1 + "Welcome " + (1 + 1) 1 + "Welcome " + ('\u0001' + 1) 1 + "Welcome " + 'a' + 1

1 + "Welcome " + 1 + 1 is 1Welcome 11. 1 + "Welcome " + (1 + 1) is 1Welcome 2. 1 + "Welcome " + ('\u0001' + 1) is 1Welcome 2 1 + "Welcome " + 'a' + 1 is 1Welcome a1

If you run Listing 4.3 GuessBirthday.java with input 1 for Set1, Set3, and Set4 and 0 for Set2 and Set5, what will be the birthday?

13

What is Math.log(Math.exp(5.5))?

5.5

Revise the code to increase the count only when the user gets a correct answer.

In the while loop, change the loop continuation condition to (correctCount < NUMBER_OF_QUESTIONS).

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.

Suppose that s1 and s2 are two strings. Which of the following statements or expressions are incorrect? String s = "Welcome to Java"; String s3 = s1 + s2; String s3 = s1 - s2; s1 == s2; s1 >= s2; s1.compareTo(s2); int i = s1.length(); char c = s1(0); char c = s1.charAt(s1.length());

String s = "Welcome to Java"; Answer: Correct String s3 = s1 + s2; Answer: Correct String s3 = s1 - s2; Answer: Incorrect s1 == s2 Answer: Correct s1 >= s2 Answer: Incorrect s1.compareTo(s2); Answer: Correct int i = s1.length(); Answer: Correct char c = s1(0); Answer: Incorrect char c = s1.charAt(s1.length()); Answer: Incorrect : it's out of bounds, even if the preceding problem is fixed.

Show the output of the following statements (write a program to verify your results): System.out.println("1" + 1); System.out.println('1' + 1); System.out.println("1" + 1 + 1); System.out.println("1" + (1 + 1)); System.out.println('1' + 1 + 1);

System.out.println("1" + 1); => 11 System.out.println('1' + 1); => 50 // (since the Unicode for 1 is 49 System.out.println("1" + 1 + 1); => 111 System.out.println("1" + (1 + 1)); => 12 System.out.println('1' + 1 + 1); => 51

Use print statements to find out the ASCII code for '1', 'A', 'B', 'a', and 'b'. Use print statements to find out the character for the decimal codes 40, 59, 79, 85, and 90. Use print statements to find out the character for the hexadecimal code 40, 5A, 71, 72, and 7A.

System.out.println((int)'1'); System.out.println((int)'A'); System.out.println((int)'B'); System.out.println((int)'a'); System.out.println((int)'b'); System.out.println((char)40); System.out.println((char)59); System.out.println((char)79); System.out.println((char)85); System.out.println((char)90); System.out.println((char)0X40); System.out.println((char)0X5A); System.out.println((char)0X71); System.out.println((char)0X72); System.out.println((char)0X7A);

Why does the Math class not need to be imported?

The Math class is in the java.lang package. Any class in the java.lang package is automatically imported. So there is no need to import it explicitly.

What are the format specifiers for outputting a Boolean value, a character, a decimal integer, a floating-point number, and a string?

The specifiers for outputting a boolean value, a character, a decimal integer, a floating-point number, and a string are %b, %c, %d, %f, and %s.

Suppose that s1, s2, and s3 are three strings, given as follows: String s1 = "Welcome to Java"; String s2 = "Programming is fun"; String s3 = "Welcome to Java"; What are the results of the following expressions? a. s1 == s2 b. s2 == s3 c. s1.equals(s2) d. s1.equals(s3) e. s1.compareTo(s2) f. s2.compareTo(s3) g. s2.compareTo(s2) h. s1.charAt(0) i. s1.indexOf('j') j. s1.indexOf("to") k. s1.lastIndexOf('a') l. s1.lastIndexOf("o", 15) m. s1.length() n. s1.substring(5) o. s1.substring(5, 11) p. s1.startsWith("Wel") q. s1.endsWith("Java") r. s1.toLowerCase() s. s1.toUpperCase() t. s1.concat(s2) u. s1.contains(s2) v. "\t Wel \t".trim()

a. false b. false c. false d. true e. a positive number f. a negative number g. 0 h. W i. -1 j. 8 k. 14 l. 9 m. 15 n. me to Java o. me to p. true q. true r. welcome to java s. WELCOME TO JAVA t. Welcome to JavaProgramming is fun u. false v. Wel

Show the output of the following program: public class Test { public static void main(String[] args) { char x = 'a'; char y = 'c'; System.out.println(++x); System.out.println(y++); System.out.println(x - y); } }

b c -2

Can the following conversions involving casting be allowed? If so, find the converted result. char c = 'A'; int i = (int)c; float f = 1000.34f; int i = (int)f; double d = 1000.34; int i = (int)d; int i = 97; char c = (char)i;

char c = 'A'; i = (int)c; // i becomes 65 float f = 1000.34f; int i = (int)f; // i becomes 1000 double d = 1000.34; int i = (int)d; // i becomes 1000 int i = 97; char c = (char)i; // c becomes 'a'

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.

Write a statement that converts π / 7 to an angle in degrees and assigns the result to a variable.

double degree = Math.toDegrees(Math.PI / 7);

Write a statement that converts 47 degrees to radians and assigns the result to a variable.

double radians = Math.toRadians(47);

Evaluate the following: int i = '1'; int j = '1' + '2' * ('4' - '3') + 'b' / 'a'; int k = 'a'; char c = 90;

i is 49, since the ASCII code of '1' is 49. j is 100. k is 97 since the ASCII code of 'a' is 97. c is character 'Z' since (int) 'Z' is 90.

What is wrong in the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int value = input.nextInt(); System.out.println("The value is " + value); System.out.print("Enter a line: "); String line = input.nextLine(); System.out.println("The line is " + line); } }

input.nextLine() is used after input.nextInt(). Don't use a line-beased input after a token-based input.

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, max; number = input.nextInt(); 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

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, max; number = input.nextInt(); 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

True or false? The argument for trigonometric methods is an angle in radians.

true

Show the output of the following statements: System.out.println('a' < 'b'); System.out.println('a' <= 'A'); System.out.println('a' > 'b'); System.out.println('a' >= 'A'); System.out.println('a' == 'a'); System.out.println('a' != 'b');

true false false true true true

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


Ensembles d'études connexes

Stereotypes Prejudice and Discrimination

View Set

Chap. 7 Buying an Existing Business

View Set

Arts 8.3; Arts of South, West, and Central Asia

View Set

Chapter 13: Drugs Used for Sleep

View Set

Nursing Care of the Child With an Infectious or Communicable Disorder

View Set

Water Quality Criteria and Treatment-Session 4

View Set

"Indonesia's 'Great Power' Aspirations: A critical view" by 20b0178 & 15b8808

View Set