CS II Chapter 10 Quiz

24 July 2022
4.7 (114 reviews)
46 test answers

Unlock all answers in this set

Unlock answers (42)
question
___________ 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
answer
A
question
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
answer
A
question
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));
answer
A, B, C, D and E
question
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 aboveD
answer
D
question
Which of the following statements convert a double value d into a string s? A. s = (new Double(d)).toString(); B. s = d; C. s = new Double(d).stringOf(); D. s = String.stringOf(d); E. s = d + "";
answer
A and E
question
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);
answer
C and E
question
What is the output of Integer.parseInt("10", 2)? A. 1; B. 2; C. 10; D. Invalid statement;
answer
B
question
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
answer
A
question
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
answer
A, B, C and D
question
To create an instance of BigInteger for 454, use A. BigInteger(454); B. new BigInteger(454); C. BigInteger("454"); D. new BigInteger("454");
answer
D
question
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");
answer
D
question
BigInteger and BigDecimal are immutable A. true B. false
answer
A
question
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);
answer
C and D
question
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
answer
A
question
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);
answer
C
question
Which of the following classes are immutable? A. Integer B. Double C. BigInteger D. BigDecimal E. String
answer
A, B, C, D and E
question
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);
answer
A and B
question
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");
answer
A
question
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
answer
A
question
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
answer
A
question
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
answer
B
question
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
answer
A
question
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
answer
C
question
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
answer
B
question
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'
answer
C, D and E
question
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
answer
D
question
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
answer
A
question
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)
answer
B
question
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()
answer
D
question
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")
answer
C and E
question
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()
answer
B
question
__________ returns a string. A. String.valueOf(123) B. String.valueOf(12.53) C. String.valueOf(false) D. String.valueOf(new char[]{'a', 'b', 'c'})
answer
A, B, C and D
question
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
answer
A
question
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
answer
C
question
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
answer
C
question
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
answer
D
question
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
answer
C
question
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
answer
B
question
Analyze the following code. class Test { public static void main(String[] args) { StringBuilder strBuf = new StringBuilder(4); strBuf.append("ABCDE"); System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(5)); } } A. The program has a compile error because you cannot specify initial capacity in the StringBuilder constructor. B. The program has a runtime error because because the buffer's capacity is 4, but five characters "ABCDE" are appended into the buffer. C. The program has a runtime error because the length of the string in the buffer is 5 after "ABCDE" is appended into the buffer. Therefore, strBuf.charAt(5) is out of range. D. The program compiles and runs fine.
answer
C
question
Which of the following is true? A. You can add characters into a string buffer. B. You can delete characters into a string buffer. C. You can reverse the characters in a string buffer. D. The capacity of a string buffer can be automatically adjusted
answer
A, B, C and D
question
_________ returns the last character in a StringBuilder variable named strBuf? A. strBuf.charAt(strBuf.length() - 1) B. strBuf.charAt(strBuf.capacity() - 1) C. StringBuilder.charAt(strBuf.length() - 1) D. StringBuilder.charAt(strBuf.capacity() - 1)
answer
A
question
Assume StringBuilder strBuf is "ABCDEFG", after invoking _________, strBuf contains "AEFG". A. strBuf.delete(0, 3) B. strBuf.delete(1, 3) C. strBuf.delete(1, 4) D. strBuf.delete(2, 4)
answer
C
question
Assume StringBuilder strBuf is "ABCDEFG", after invoking _________, strBuf contains "ABCRRRRDEFG". A. strBuf.insert(1, "RRRR") B. strBuf.insert(2, "RRRR") C. strBuf.insert(3, "RRRR") D. strBuf.insert(4, "RRRR")
answer
C
question
Assume StringBuilder strBuf is "ABCCEFC", after invoking _________, strBuf contains "ABTTEFT". A. strBuf.replace('C', 'T') B. strBuf.replace("C", "T") C. strBuf.replace("CC", "TT") D. strBuf.replace('C', "TT") E. strBuf.replace(2, 7, "TTEFT")
answer
E
question
The StringBuilder methods _____________ not only change the contents of a string buffer, but also returns a reference to the string buffer. A. delete B. append C. insert D. reverse E. replace
answer
A, B, C, D and E
question
The following program displays __________. public class Test { public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(buffer); System.out.println(buffer); } private static void change(StringBuilder buffer) { buffer.append(" and HTML"); } } A. Java B. Java and HTML C. and HTML D. nothing is displayed
answer
B