Introduction to Java Programming, AP Version,
Y. Daniel Liang
This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.
Chapter 10 Object-Oriented Thinking
Section 10.4 Class Relationships
10.1
___________ is attached to the class of the composing class to denote the aggregation relationship with the composed object.
A.
An empty diamond
B.
A solid diamond
C.
An empty oval
D.
A solid oval
10.2
An aggregation relationship is usually represented as __________ in ___________.
A.
a data field/the aggregating class
B.
a data field/the aggregated class
C.
a method/the aggregating class
D.
a method/the aggregated class
Section 10.7 Processing Primitive Data Type Values as Objects
10.3
Which of the following statements will convert a string s into i of int type?
A.
i = Integer.parseInt(s);
B.
i = (new Integer(s)).intValue();
C.
i = Integer.valueOf(s).intValue();
D.
i = Integer.valueOf(s);
E.
i = (int)(Double.parseDouble(s));
10.4
Which of the following statements will convert a string s into a double value d?
A.
d = Double.parseDouble(s);
B.
d = (new Double(s)).doubleValue();
C.
d = Double.valueOf(s).doubleValue();
D.
All of the above.
10.5
Which of the following statements convert a double value d into a string s?
A.
s = (new Double(d)).toString();
B.
s = (Double.valueOf(s)).toString();
C.
s = new Double(d).stringOf();
D.
s = String.stringOf(d);
10.6
Which of the following statements is correct?
A.
Integer.parseInt("12", 2);
B.
Integer.parseInt(100);
C.
Integer.parseInt("100");
D.
Integer.parseInt(100, 16);
E.
Integer.parseInt("345", 8);
10.7
What is the output of Integer.parseInt("10", 2)?
A.
1;
B.
2;
C.
10;
D.
Invalid statement;
Section 10.8 Automatic Conversion Between Primitive Types and Wrapper Class Types
10.8
In JDK 1.5, you may directly assign a primitive data type value to a wrapper object. This is called ______________.
A.
auto boxing
B.
auto unboxing
C.
auto conversion
D.
auto casting
10.9
In JDK 1.5, analyze the following code.
Line 1: Integer[] intArray = {
1
,
2
,
3
};
Line 2:
int
i = intArray[
0
] + intArray[
1
];
Line 3:
int
j = i + intArray[
2
];
Line 4:
double
d = intArray[
0
];
A.
It is OK to assign 1, 2, 3 to an array of Integer objects in JDK 1.5.
B.
It is OK to automatically convert an Integer object to an int value in Line 2.
C.
It is OK to mix an int value with an Integer object in an expression in Line 3.
D.
Line 4 is OK. An int value from intArray[0] object is assigned to a double variable d.
Section 10.9 The BigInteger and BigDecimal Classes
10.10
To create an instance of BigInteger for 454, use
A.
BigInteger(454);
B.
new BigInteger(454);
C.
BigInteger("454");
D.
new BigInteger("454");
10.11
To create an instance of BigDecimal for 454.45, use
A.
BigInteger(454.45);
B.
new BigInteger(454.45);
C.
BigInteger("454.45");
D.
new BigDecimal("454.45");
10.12
BigInteger and BigDecimal are immutable
A.
true
B.
false
10.13
To add BigInteger b1 to b2, you write _________.
A.
b1.add(b2);
B.
b2.add(b1);
C.
b2 = b1.add(b2);
D.
b2 = b2.add(b1);
E.
b1 = b2.add(b1);
10.14
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
java.math.BigInteger x =
new
java.math.BigInteger(
"3"
);
java.math.BigInteger y =
new
java.math.BigInteger(
"7"
);
x.add(y);
System.out.println(x);
}
}
A.
3
B.
4
C.
10
D.
11
10.15
To divide BigDecimal b1 by b2 and assign the result to b1, you write _________.
A.
b1.divide(b2);
B.
b2.divide(b1);
C.
b1 = b1.divide(b2);
D.
b1 = b2.divide(b1);
E.
b2 = b2.divide(b1);
10.16
Which of the following classes are immutable?
A.
Integer
B.
Double
C.
BigInteger
D.
BigDecimal
E.
String
10.17
Which of the following statements are correct?
A.
new java.math.BigInteger("343");
B.
new java.math.BigDecimal("343.445");
C.
new java.math.BigInteger(343);
D.
new java.math.BigDecimal(343.445);
Section 10.10 The String Class
10.18
Which of the following statements is preferred to create a string "Welcome to Java"?
A.
String s = "Welcome to Java";
B.
String s = new String("Welcome to Java");
C.
String s; s = "Welcome to Java";
D.
String s; s = new String("Welcome to Java");
10.19
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
String s1 =
"Welcome to Java!"
;
String s2 = s1;
if
(s1 == s2)
System.out.println(
"s1 and s2 reference to the same String object"
);
else
System.out.println(
"s1 and s2 reference to different String objects"
);
}
}
A.
s1 and s2 reference to the same String object
B.
s1 and s2 reference to different String objects
10.20
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
String s1 =
"Welcome to Java!"
;
String s2 =
"Welcome to Java!"
;
if
(s1 == s2)
System.out.println(
"s1 and s2 reference to the same String object"
);
else
System.out.println(
"s1 and s2 reference to different String objects"
);
}
}
A.
s1 and s2 reference to the same String object
B.
s1 and s2 reference to different String objects
10.21
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
String s1 =
new
String(
"Welcome to Java!"
);
String s2 =
new
String(
"Welcome to Java!"
);
if
(s1 == s2)
System.out.println(
"s1 and s2 reference to the same String object"
);
else
System.out.println(
"s1 and s2 reference to different String objects"
);
}
}
A.
s1 and s2 reference to the same String object
B.
s1 and s2 reference to different String objects
10.22
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
String s1 =
new
String(
"Welcome to Java!"
);
String s2 =
new
String(
"Welcome to Java!"
);
if
(s1.equals(s2))
System.out.println(
"s1 and s2 have the same contents"
);
else
System.out.println(
"s1 and s2 have different contents"
);
}
}
A.
s1 and s2 have the same contents
B.
s1 and s2 have different contents
10.23
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
String s1 =
new
String(
"Welcome to Java!"
);
String s2 = s1.toUpperCase();
if
(s1 == s2)
System.out.println(
"s1 and s2 reference to the same String object"
);
else
if
(s1.equals(s2))
System.out.println(
"s1 and s2 have the same contents"
);
else
System.out.println(
"s1 and s2 have different contents"
);
}
}
A.
s1 and s2 reference to the same String object
B.
s1 and s2 have the same contents
C.
s1 and s2 have different contents
10.24
What is the output of the following code?
public
class
Test {
public
static
void
main(String[] args) {
String s1 =
new
String(
"Welcome to Java"
);
String s2 = s1;
s1 +=
"and Welcome to HTML"
;
if
(s1 == s2)
System.out.println(
"s1 and s2 reference to the same String object"
);
else
System.out.println(
"s1 and s2 reference to different String objects"
);
}
}
A.
s1 and s2 reference to the same String object
B.
s1 and s2 reference to different String objects
10.25
Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
A.
String s = new String("new string");
B.
String s3 = s1 + s2
C.
s1 >= s2
D.
int i = s1.length
E.
s1.charAt(0) = '5'
10.26
What is the output of the following code?
String s =
"University"
;
s.replace(
"i"
,
"ABC"
);
System.out.println(s);
A.
UnABCversity
B.
UnABCversABCty
C.
UniversABCty
D.
University
10.27
Analyze the following code.
class
Test {
public
static
void
main(String[] args) {
String s;
System.out.println(
"s is "
+ s);
}
}
A.
The program has a compile error because s is not initialized, but it is referenced in the println statement.
B.
The program has a runtime error because s is not initialized, but it is referenced in the println statement.
C.
The program has a runtime error because s is null in the println statement.
D.
The program compiles and runs fine.
10.28
Which of the following is the correct statement to return a string from an array a of characters?
A.
toString(a)
B.
new String(a)
C.
convertToString(a)
D.
String.toString(a)
10.29
Assume s is " abc ", the method __________ returns a new string "abc".
A.
s.trim(s)
B.
trim(s)
C.
String.trim(s)
D.
s.trim()
10.30
Assume s is "ABCABC", the method __________ returns a new string "aBCaBC".
A.
s.toLowerCase(s)
B.
s.toLowerCase()
C.
s.replace('A', 'a')
D.
s.replace('a', 'A')
E.
s.replace("ABCABC", "aBCaBC")
10.31
Assume s is "ABCABC", the method __________ returns an array of characters.
A.
toChars(s)
B.
s.toCharArray()
C.
String.toChars()
D.
String.toCharArray()
E.
s.toChars()
10.32
__________ returns a string.
A.
String.valueOf(123)
B.
String.valueOf(12.53)
C.
String.valueOf(false)
D.
String.valueOf(new char[]{'a', 'b', 'c'})
10.33
The following program displays __________.
public
class
Test {
public
static
void
main(String[] args) {
String s =
"Java"
;
StringBuilder buffer =
new
StringBuilder(s);
change(s);
System.out.println(s);
}
private
static
void
change(String s) {
s = s +
" and HTML"
;
}
}
A.
Java
B.
Java and HTML
C.
and HTML
D.
nothing is displayed
10.34
What is displayed by the following statement?
System.out.println(
"Java is neat"
.replaceAll(
"is"
,
"AAA"
));
A.
JavaAAAneat
B.
JavaAAA neat
C.
Java AAA neat
D.
Java AAAneat
10.35
What is displayed by the following code?
public
static
void
main(String[] args) {
String[] tokens =
"Welcome to Java"
.split(
"o"
);
for
(
int
i =
0
; i < tokens.length; i++) {
System.out.print(tokens[i] +
" "
);
}
}
A.
Welcome to Java
B.
Welc me to Java
C.
Welc me t Java
D.
Welcome t Java
10.36
What is displayed by the following code?
System.out.print(
"Hi, ABC, good"
.matches(
"ABC "
) +
" "
);
System.out.println(
"Hi, ABC, good"
.matches(
".*ABC.*"
));
A.
false false
B.
true false
C.
true true
D.
false true
10.37
What is displayed by the following code?
System.out.print(
"A,B;C"
.replaceAll(
",;"
,
"#"
) +
" "
);
System.out.println(
"A,B;C"
.replaceAll(
"[,;]"
,
"#"
));
A.
A B C A#B#C
B.
A#B#C A#B#C
C.
A,B;C A#B#C
D.
A B C A B C
10.38
What is displayed by the following code?
String[] tokens =
"A,B;C;D"
.split(
"[,;]"
);
for
(
int
i =
0
; i < tokens.length; i++)
System.out.print(tokens[i] +
" "
);
A.
A,B;C;D
B.
A B C D
C.
A B C;D
D.
A B;C;D