CSC Chapter 4
What is Math.round(3.6)?
4 Note that round returns an int value
What is Math.ceil(3.6)?
4.0 Note that ceil returns a double value
Which of the following is the correct statement to return JAVA?
"Java".toUpperCase()
To check whether a char variable ch is an uppercase letter, you write ___________.
(ch >= 'A' && ch <= 'Z') A is wrong because ch >= 'Z'. C is wrong because of using ||. D is wrong because of incorrect syntax. The correct answer is B.
The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space)
****123456 %10s specifies to display a string with width 10. By default, it is right justified. So, the correct answer is D.
Math.cos(Math.PI) returns _______.
-1.0 Note that Math.PI is 180 degrees.
"AbA".compareToIgnoreCase("abC") returns
-2 Ignoring case, you compare aba with abc. The first two characters in the two strings are the same. The different between the last two characters is -2. The correct answer is D.
Math.sin(Math.PI) returns _______.
0.0 Note that Math.PI is 180 degrees.
'3' - '2' + 'm' / 'n' is ______.
1 When an operand is a character in an arithmetic expression, the character is casted to an int value.
The statement System.out.printf("%3.1e", 1234.56) outputs ___________.
1.2e+03 %3.1e specifies a scientific notation with one digit after the decimal point. So, the correct answer is D.
The statement System.out.printf("%3.1f", 1234.56) outputs ___________.
1234.6 .1 specifies one digit after the decimal point. The rest is rounded up. So 1234.56 is displayed 1234.6.
The statement System.out.printf("%5d", 123456) outputs ___________.
123456 %5d specifies an integer with width 5. The width is automatically expanded if the number is larger than the specified width. So, the correct answer is C.
"abc".compareTo("aba") returns ___________.
2 The first two characters in the two strings are the same. The different between the last two characters is 2. The correct answer is B.
What is the output of System.out.println('z' - 'a')?
25 The Unicode offset between z and a is 25.
What is Math.floor(3.6)?
3.0 Note that floor returns a double value
What is Math.rint(3.6)?
4.0 Note that rint returns a double value
Which of the following are valid specifiers for the printf statement?
A. %4c B. %10b C. %6d E. %10.2e
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') ...
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()); A is wrong because the - operator cannot be used for strings. B is wrong because the compareTo method returns an int, not a boolean. C is wrong because the [] cannot be used for accessing string elements. D is wrong because of index out of bounds.
____________________ returns true.
C. "peter".equalsIgnoreCase("Peter") D. "peter".equalsIgnoreCase("peter") E. "peter".equals("peter")
The __________ method parses a string s to a double value.
Double.parseDouble(s); The parseDouble method is defined in the Double class. D is correct.
The __________ method parses a string s to an int value.
Integer.parseInt(s); The parseInt method is defined in the Integer class. B is correct.
The expression "Java " + 1 + 2 + 3 evaluates to ________.
Java 123
To obtain the arc sine of 0.5, use _______.
Math.asin(0.5) Note the trig methods use the radians for angles.
To obtain the sine of 35 degrees, use _______.
Math.sin(Math.toRadians(35) Note the trig methods use the radians for angles.
Which of the following statement prints smith\exam1\test.txt?
System.out.println("smith\\exam1\\test.txt"); To represent the character, use \, because it is an escape character.
Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
System.out.println((char)i); (char)i casts a number into a character.
What is the return value of "SELECT".substring(4, 4)?
an empty string If beginIndex is endIndex, substring(beginIndex, endIndex) returns an empty string with length 0. It would be a runtime error, if beginIndex > endIndex.
Which of the following assignment statements is correct?
char c = 'd'; char c = 100; Choice B is also correct, because an int value can be implicitly cast into a char variable. The Unicode of the character is the int value. In this case, the character is d (see Appendix B).
To check if a string s contains the suffix "Java", you may write
if (s.endsWith("Java")) ... if (s.substring(s.length() - 4).equals("Java")) 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') ...
Which of the following is not a correct method in the Character class?
isDigit() toUpperCase() isDigit() should be isDigit(char) and toUpperCase() should be toUpperCase(char)
Will System.out.println((char)4) display 4?
no. The character whose Unicode is is to be displayed, not number 4.
A Java character is stored in __________.
two bytes Java characters use Unicode encoding.
Suppose Character x = new Character('a'), __________________ returns true.
x.equals(new Character('a')) x.equals('a')