Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
1.
(b) Math.sin(2*Math.PI) = 0
(c) Math.cos(2*Math.PI) = 1
(r) Math.round(Math.abs(-2.5)) = 3
2. True
5.
6.
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.
7.
5.5
5.5
0.5235987755982988
0.5235987755982988
8.
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);
9 '\u345dE' is wrong. It must have exactly four hex numbers in the Unicode.
10 '\\' and '\”'
11
12.
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'
13.
-2
14.
(int)(Math.random() * 26 + 'a')
15.
true
false
false
true
true
true
16.
s1 == s2 => false
s2 == s3 => false
s1.equals(s2) => false
s1.equals(s3) => true
s1.compareTo(s2) => a positive number
s2.compareTo(s3) => a negative number
s2.compareTo(s0) => 0
s1.charAt(0) => W
s1.indexOf('j') => -1
s1.indexOf("to") => 8
s1.lastIndexOf('a') => 14
s1.lastIndexOf("o", 15) => 9
s1.length() => 16
s1.substring(5) => me to Java
s1.substring(5, 11) => me to
s1.startsWith("Wel") => true
s1.endsWith("Java") => true
s1.toLowerCase() => welcome to java
s1.toUpperCase()=> WELCOME TO JAVA
s1.concat(s2) => “Welcome to JavaProgramming is fun"
17.
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.
18.
19.
20.
Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual.
Check whether s1 is equal to s2 ignoring case and assign the result to a Boolean variable
isEqual.
int x = s1.compareTo(s2);
Compare s1 with s2 ignoring case and assign the result to an int variable x.
int x = s1.compareToIgnoreCase(s2);
Check whether s1 has prefix "AAA" and assign the result to a Boolean variable b.
boolean b = s1.startsWith("AAA");
Check whether s1 has suffix "AAA" and assign the result to a Boolean variable b.
boolean b = s1.endsWith("AAA");
int x = s1.length();
char x = s1.charAt(0);
String s3 = s1 + s2;
String s3 = s1.substring(1);
String s3 = s1.toLowerCase();
String s3 = s1.toUpperCase();
Create a new string s3 that trims blank spaces on both ends of s1.
String s3 = s1.trim();
int x = s1.indexOf(‘e‘);
Assign the index of the last occurrence of string abc in s1 to an int variable x.
int x = s1.lastIndexOf(“abc”);
21. 0
22. 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.
23.
24.
(e) false*Java
(f) *falseJava
(g) 312,342 315,562.9