Uploaded by miyudinh

chapter4 Sample Questions

advertisement
Textbook:
Introduction to Java Programming, Comprehensive Version, 10th Edition, Y.
Daniel Liang, Pearson, 2015.
ISBN-13: 978-0133761313 ISBN-10: 0133761312
Chapter 4 Mathematical Functions, Characters, and Strings
Section 4.2 Common Mathematical Functions
20.
What is Math.round(3.6)?
a.
3.0
b.
3
c.
4
d.
4.0
#
21.
a.
b.
c.
d.
#
22.
a.
b.
c.
d.
e.
4.0.
What is Math.rint(3.6)?
3.0
3
4.0
5.0
What is Math.rint(3.5)?
3.0
3
4
4.0
5.0
#
23.
a.
b.
c.
d.
What is Math.ceil(3.6)?
3.0
3
4.0
5.0
#
24.
a.
b.
c.
d.
What is Math.floor(3.6)?
3.0
3
4
5.0
#
24.
a.
b.
c.
To obtain the sine of 35 degrees, use _______.
Math.sin(35)
Math.sin(Math.toRadians(35))
Math.sin(Math.toDegrees(35))
d.
e.
Math.sin(Math.toRadian(35))
Math.sin(Math.toDegree(35))
#
24.
a.
b.
c.
e.
To obtain the arc sine of 0.5, use _______.
Math.asin(0.5)
Math.asin(Math.toDegrees(0.5))
Math.sin(Math.toRadian(0.5))
Math.sin(0.5)
#
24.
a.
b.
c.
e.
Math.asin(0.5) returns _______.
30
Math.toRadians(30)
Math.PI / 4
Math.PI / 2
#
24.
a.
b.
c.
e.
Math.sin(Math.PI) returns _______.
0.0
1.0
0.5
0.4
#
24.
a.
b.
c.
e.
Math.cos(Math.PI) returns _______.
0.0
1.0
-1.0
0.5
Section 4.3 Character Data Type and Operations
45.
Which of the following is the correct expression of character 4?
a.
4
b.
"4"
c.
'\0004'
d.
'4'
#
46.
a.
b.
c.
d.
#
A Java character is stored in __________.
one byte
two bytes
three bytes
four bytes
47.
Suppose x is a char variable with a value 'b'. What is the printout
of the statement System.out.println(++x)?
a.
a
b.
b
c.
c
d.
d
#
48.
a.
b.
c.
d.
Which of the following statement prints smith\exam1\test.txt?
System.out.println("smith\exam1\test.txt");
System.out.println("smith\\exam1\\test.txt");
System.out.println("smith\"exam1\"test.txt");
System.out.println("smith"\exam1"\test.txt");
#
49.
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 + " ");
#
50.
a.
b.
c.
d.
The Unicode of 'a' is 97. What is the Unicode for 'c'?
96
97
98
99
#
51.
a.
b.
Will System.out.println((char)4) display 4?
Yes
No
#
52.
a.
b.
c.
d.
What is the printout of System.out.println('z' - 'a')?
25
26
a
z
#
53.
a.
b.
c.
d.
e.
An int variable can hold __________.
'x'
120
120.0
"x"
"120"
#
54.
a.
b.
c.
d.
Which of
char c =
char c =
char c =
char c =
#
54.
a.
b.
c.
d.
'3' - '2' + 'm' / 'n' is ______.
0
1
2
3
the following assignment statements is correct?
'd';
100;
"d";
"100";
#
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')
#
30.
Which of following is not a correct method in Character?
a. isLetterOrDigit(char)
b. isLetter(char)
c. isDigit()
d. toLowerCase(char)
e. toUpperCase()
#
31. Suppose Character x = new Character('a'), __________________ returns
true.
a.
x.equals(new Character('a'))
b.
x.compareToIgnoreCase('A')
c.
x.equalsIgnoreCase('A')
d.
x.equals('a')
e.
x.equals("a")
#
Section 4.4 The String Type
1. 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.
#
11.
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());
#
6.
Suppose s1 and s2 are two strings. What is the result of the
following code?
a.
b.
s1.equals(s2) == s2.equals(s1)
true
false
#
12.
a.
b.
c.
d.
e.
"abc".compareTo("aba") returns ___________.
1
2
-1
-2
0
#
13.
a.
b.
c.
d.
e.
"AbA".compareToIgnoreCase("abC") returns ___________.
1
2
-1
-2
0
#
14.
a.
b.
c.
d.
e.
____________________ returns true.
"peter".compareToIgnoreCase("Peter")
"peter".compareToIgnoreCase("peter")
"peter".equalsIgnoreCase("Peter")
"peter".equalsIgnoreCase("peter")
"peter".equals("peter")
#
16.
a.
b.
c.
What is the return value of "SELECT".substring(0, 5)?
"SELECT"
"SELEC"
"SELE"
d.
"ELECT"
#
17.
a.
b.
c.
d.
What is the return value of "SELECT".substring(4, 4)?
an empty string
C
T
E
#
19.
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') ...
#
20.
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')
...
#
21.
a.
b.
c.
d.
Which of the following is the correct statement to return JAVA?
toUpperCase("Java")
"Java".toUpperCase("Java")
"Java".toUpperCase()
String.toUpperCase("Java")
#
55.
The expression "Java " + 1 + 2 + 3 evaluates to ________.
a. Java123
b. Java6
c. Java 123
d. java 123
e. Illegal expression
#
56.
Note that the Unicode for character A is 65. The expression "A" + 1
evaluates to ________.
a.
b.
c.
d.
66
B
A1
Illegal expression
#
57.
Note that the Unicode for character A is 65. The expression 'A' + 1
evaluates to ________.
a. 66
b. B
c. A1
d. Illegal expression
#
63. 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);
#
64. 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);
#
Section 4.6 Formatting Console Output
31. Which of the following are valid specifiers for the printf statement?
a.
%4c
b.
%10b
c.
%6d
d.
%8.2d
e.
%10.2e
#
32. The statement System.out.printf("%3.1f", 1234.56) outputs
___________.
a.
123.4
b.
123.5
c.
1234.5
d.
1234.56
e.
1234.6
#
33. The statement System.out.printf("%3.1e", 1234.56) outputs
___________.
a.
b.
c.
d.
e.
0.1e+04
0.123456e+04
0.123e+04
1.2e+03
1.23+03
#
34. The statement System.out.printf("%5d", 123456) outputs ___________.
a.
12345
b.
23456
c.
123456
d.
12345.6
#
35. The statement System.out.printf("%10s", 123456) outputs ___________.
(Note: * represents a space)
a.
123456****
b.
23456*****
c.
12345*****
d.
****123456
#
36. 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.
#
9.
You can cast a character value to an int, or an int to char.
a. true
b. false
#
15.
Is 'a' larger than 'A'?
a. Yes
b. No
#
14.
a.
b.
c.
d.
You can assign the value in ________ to an int variable.
'x'
93
98.3
true
#
7.
a.
b.
c.
d.
Which of the following is the correct expression of character a?
'a'
"a"
'\000a'
'\a'
#
15.
Which of
character 5 to
a.
char c =
b.
char c =
c.
char c =
d.
char c =
the following assignment statements is correct to assign
c?
'5';
5;
"5";
"344";
#
15.
a.
b.
c.
d.
The expression 'c' - 'e' is ________________.
2
-2
a random number
invalid
#
25.
a.
b.
c.
d.
What is "Welcome" + 1 + 1*2?
Welcome11*2
Welcome4
Welcome12
Welcome3
#
35. parseInt is a method in the _______ class.
a. Integer
b. Double
c. Math
d. System
#
36. parseDouble is a method in the _______ class.
a. Integer
b. Double
c. Math
d. System
#
0. To concatenate s1, s2, s3, and s4, you write ______________.
a. s1.concate(s2).concate(s3).concate(s4);
b. s1.concate(s2) + s3.concate(s4);
c. ((s1.concate(s2)).concate(s3)).concate(s4);
d. s1.concate(s2).(concate(s3).concate(s4));
#
3. "unhappy".substring(2) returns "happy".
a. true
b. false
#
4. "smiles".substring(1, 5) returns "mile".
a. true
b. false
#
12. You compare two strings s1 and s2 using ________.
a. compare(s1, s2)
b. s1.compare(s2)
c. compareTo(s1, s2)
d. s1.compareTo(s2)
#
13. "ab".compareTo("aB") is _________.
a. greater than 0
b. less than 0
c. equal to 0
d. less than or equal to 0
#
15. Which of the following statements are correct to concatenate string
s1 into s2.
a. s1 += s2;
b. s2 += s1;
c. s2.concate(s1);
d. s1.concate(s2);
#
17. Which of the following statements are incorrect?
a. float f = 4.5;
b. char c = "r";
c. String s = 'r';
d. int k = null;
#
18. "abcdefgh".subString(1, 3) returns ________.
a. abc
b. bcd
c. bc
d. cde
#
19. Assume that the ASCII code for character c is 99. What is the
printout of the following code?
System.out.println("a" + 'c');
a. a99
b. ac
c. 9799
d. 196
#
20. Assume that the ASCII code for character c is 99 and for a is 97.
What is the printout of the following code?
System.out.println('a' + 'c');
a. a99
b. ac
c. 9799
d. 196
#
21. Assume that the ASCII code for character c is 99 and for a is 97.
What is the printout of the following code?
System.out.println("AB" + 'a' + 'c');
a. ABac
b. AB9799
c. ABa99
d. AB196
#
11.
Which of the following is a possible output for (int)(51 *
Math.random())?
a.
0
b.
50
c.
100
d.
500
#
12.
a.
b.
c.
d.
Which of the following method results in 8.0?
Math.round(8.5)
Math.rint(8.5)
Math.ceil(8.5)
Math.floor(8.5)
#
13.
a.
b.
c.
d.
e.
Which of the following method returns the sine of 90 degree?
Math.sine(90)
Math.sin(90)
Math.sin(PI)
Math.sin(Math.toRadian(90))
Math.sin(Math.PI)
#
16. Math.pow(3, 3) returns _______.
a.
27
b.
9
c.
27.0
d.
9.0
#
17. Math.sqrt(4) returns _______.
a.
2
b.
2.0
c.
1
d.
1.0
#
25. Math.ceil(5.5) evaluates to _____.
a. 5.0
b. 6.0
c. 5
d. 6
#
26. Math.floor(5.5) evaluates to _____.
a. 5.0
b. 6.0
c. 5
d. 6
#
26. Given two Strings s1 = "Welcome to Java" and s2 = "Programming is
fun", which of the following is true?
a. s1.equals(s2)
b. s2.equals(s1)
c. s1.contains(s2)
d. s2.contains(s1)
e. !s1.contains(s2)
Download