Chapter 4 Mathematical Functions, Characters, and Strings
Section 4.2 Common Mathematical Functions
Section 4.2.1 Trigonometric Methods
1. To obtain the sine of 35 degrees, use .
a. Math.sin(35)
b. Math.sin(Math.toRadians(35))
c. Math.sin(Math.toDegrees(35))
d. Math.sin(Math.toRadian(35))
e. Math.sin(Math.toDegree(35))
2. To obtain the arc sine of 0.5, use .
a. Math.asin(0.5)
b. Math.asin(Math.toDegrees(0.5))
c. Math.sin(Math.toRadians(0.5))
d. Math.sin(0.5)
3. Math.asin(0.5) returns .
a. 30
b. Math.toRadians(30)
c. Math.PI / 4
d. Math.PI / 2
#
4.
Math.sin(Math.PI) returns .
a.
0.0
b.
1.0
c.
0.5
d.
0.4
#
5.
Math.cos(Math.PI) returns .
a.
0.0
b.
1.0
c.
1.0
d.
0.5
#
Section 4.2.2 Exponent Methods
1. Which of the following is correct to obtain the square of 5?
a. Math.sqrt(5)
b. Math.sqrt(5.0)
c. Math.pow(5, 0.5)
d. Math.pow(5.0, 0.5)
6.
What is Math.rint(3.6)?
a.
3.0
b.
3
c.
4.0
d.
5.0
#
Section 4.2.3 The Rounding Methods
#
5.
What is Math.round(3.6)?
a.
3.0
b.
3
c.
4
d.
4.0
#
7.
What is Math.rint(3.5)?
a.
3.0
b.
3
c.
4
d.
4.0
e.
5.0
#
8.
a.
b.
c.
d.
#
9.
What is Math.floor(3.6)?
a.
3.0
b.
3
c.
4
d.
5.0
#
Section 4.2.4 The min, max, and abs Methods
1. Which of the following is correct to obtain the min of x, y, z?
a. Math.min(x, Math.min(y, z))
b. Math.min(Math.min(x, y), z)
c. Math.min(Math.min(y, z), x)
d. Math.min(z, Math.min(x, y))
#
Section 4.2.5 The random Method
1. Which of the following is correct to obtain a random integer between 5 and 10?
a. 5 + Math.random() * 6
b. 5 + (int)(Math.random() * 6)
c. 5 + Math.random() * 5
d. 5 + (int)(Math.random() * 5)
#
Section 4.3 Character Data Type and Operations
Section 4.3.1 Unicode and ASCII Code
10. Which of the following is the correct expression of character 4?
a. 4
b. “4”
c. ‘\0004’
d. ‘4’
#
11. A Java character is stored in .
a. one byte
b. two bytes
c. three bytes
d. four bytes
#
12.
The Unicode of ‘a’ is 97. What is the Unicode for ‘c‘?
a.
96
b.
97
c.
98
d.
99
#
Section 4.3.2 Escape Sequences for Special Characters
13. Which of the following statement prints smith\exam1\test.txt?
a. System.out.println(“smith\exam1\test.txt”);
b. System.out.println(“smith\\exam1\\test.txt”);
c. System.out.println(“smith\”exam1\”test.txt”);
d. System.out.println(“smith”\exam1″\test.txt”);
#
Section 4.3.3 Casting between char and Numeric Types
14. Suppose x is a char variable with a value ‘b’. What is the output of the statement System.out.println(++x)?
a. a
b. b
c. c
d. d
#
15. Suppose i is an int type variable. Which of the following statements display the character whose Unicode is
stored in variable i?
a. System.out.println(i);
b. System.out.println((char)i);
c. System.out.println((int)i);
d. System.out.println(i + “);
#
16. Will System.out.println((char)4) display 4?
a. Yes
b. No
#
17. What is the output of System.out.println(‘z’ ‘a’)?
a. 25
b. 26
c. a
d. z
#
18. An int variable can hold .
a. ‘x’
b. 120
c. 120.0
d. “x”
e. “120”
#
19. Which of the following assignment statements is correct?
a. char c = ‘d’;
b. char c = 100;
c. char c = “d”;
d. char c = “100”;
#
20.
‘3’ – ‘2’ + ‘m’ / ‘n’ is .
a.
0
b.
1
c.
2
d.
3
#
Section 4.3.4 Comparing and Testing Characters
21. To check whether a char variable ch is an uppercase letter, you write .
a. (ch >= ‘A’ && ch >= ‘Z’)
b. (ch >= ‘A’ && ch <= ‘Z’)
c. (ch >= ‘A’ || ch <= ‘Z’)
d. (‘A’ <= ch <= ‘Z’)
#
22. Which of the following is not a correct method in the Character class?
a. isLetterOrDigit(char)
b. isLetter(char)
c. isDigit()
d. toLowerCase(char)
e. toUpperCase()
Section 4.4 The String Type
Section 4.4.2 Gettiing Characters from a String
24. Suppose s is a string with the value “java”. What will be assigned to x if you execute the following code?
char x = s.charAt(4);
a. ‘a’
b. ‘v’
c. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.
#
Section 4.4.3 Concatenating Strings
25. The expression “Java ” + 1 + 2 + 3 evaluates to .
a. Java123
b. Java6
c. Java 123
d. java 123
e. Illegal expression
#
26. Note that the Unicode for character A is 65. The expression “A” + 1 evaluates to .
a. 66
b. B
c. A1
d. Illegal expression
27. Note that the Unicode for character A is 65. The expression ‘A’ + 1 evaluates to .
a. 66
b. B
c. A1
d. Illegal expression
#
Section 4.4.4 Converting Strings
28. Which of the following is the correct statement to return JAVA?
a. toUpperCase(“Java”)
b. “Java”.toUpperCase(“Java”)
c. “Java”.toUpperCase()
d. String.toUpperCase(“Java”)
#
Section 4.4.7 Comparing Strings
29. Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect?
a. String s3 = s1 s2;
b. boolean b = s1.compareTo(s2);
c. char c = s1[0];
d. char c = s1.charAt(s1.length());
#
30. Suppose s1 and s2 are two strings. What is the result of the following code?
s1.equals(s2) == s2.equals(s1)
a. true
b. false
#
31.
“abc”.compareTo(“aba”) returns .
a.
1
b.
2
c.
1
d.
2
e.
0
#
32.
“AbA”.compareToIgnoreCase(“abC”) returns .
a.
1
b.
2
c.
1
d.
2
e.
0
#
33. returns true.
a. “peter”.compareToIgnoreCase(“Peter”)
b. “peter”.compareToIgnoreCase(“peter”)
c. “peter”.equalsIgnoreCase(“Peter”)
d. “peter”.equalsIgnoreCase(“peter”)
e. “peter”.equals(“peter”)
#
34. What is the return value of “SELECT”.substring(0, 5)?
a. “SELECT”
b. “SELEC”
c. “SELE”
d. “ELECT”
#
35. What is the return value of “SELECT”.substring(4, 4)?
a. an empty string
b. C
c. T
d. E
#
Section 4.4.9 Finding a Character or a Substring in a String
36. To check if a string s contains the prefix “Java”, you may write
a. if (s.startsWith(“Java”)) ...
b. if (s.indexOf(“Java”) == 0)
c. if (s.substring(0, 4).equals(“Java”))
d. if (s.charAt(0) == ‘J’ && s.charAt(1) == ‘a’ && s.charAt(2) == ‘v’ && s.charAt(3) == ‘a’)
#
37. To check if a string s contains the suffix “Java”, you may write
a. if (s.endsWith(“Java”)) ...
b. if (s.lastIndexOf(“Java”) >= 0)
c. if (s.substring(s.length() 4).equals(“Java”)) …
d. if (s.substring(s.length() 5).equals(“Java”)) …
e. if (s.charAt(s.length() 4) == ‘J’ && s.charAt(s.length() – 3) == ‘a’ && s.charAt(s.length() – 2) == ‘v’ &&
s.charAt(s.length() – 1) == ‘a’)
#
Section 4.4.10 Conversions between Strings and Numbers
38. The method parses a string s to an int value.
a. integer.parseInt(s);
b. Integer.parseInt(s);
c. integer.parseInteger(s);
d. Integer.parseInteger(s);
#
39. The method parses a string s to a double value.
a. double.parseDouble(s);
b. Double.parsedouble(s);
c. double.parse(s);
d. Double.parseDouble(s);
a.
123.4
b.
123.5
c.
1234.5
d.
1234.56
e.
1234.6
a.
12345
b.
23456
c.
123456
d.
12345.6
Key:d The parseDouble method is defined in the Double class. D is correct.
#
Section 4.6 Formatting Console Output
40. Which of the following are valid specifiers for the printf statement?
a. %4c
b. %10b
c. %6d
d. %8.2d
e. %10.2e
#
41. The statement System.out.printf(“%3.1f”, 1234.56) outputs .
#
42. The statement System.out.printf(“%3.1e”, 1234.56) outputs .
a. 0.1e+04
b. 0.123456e+04
c. 0.123e+04
d. 1.2e+03
e. 1.23+03
#
43. The statement System.out.printf(“%5d”, 123456) outputs .
#
44. The statement System.out.printf(“%10s”, 123456) outputs . (Note: * represents a space)
a. 123456****
b. 23456*****
c. 12345*****
d. ****123456
#
45. Analyze the following code:
int i = 3434; double d = 3434;
System.out.printf(“%5.1f %5.1f”, i, d);
a. The code compiles and runs fine to display 3434.0 3434.0.
b. The code compiles and runs fine to display 3434 3434.0.
c. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.