Introduction to Java Programming, Includes Data Structures, Eleventh Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

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 = d;
C. s = new Double(d).stringOf();
D. s = String.stringOf(d);
E. s = d + "";

10.6  Which of the following statements are 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 = {123};
Line 2int i = intArray[0] + intArray[1];
Line 3int j = i + intArray[2];
Line 4double 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. BigDecimal(454.45);
B. new BigDecimal(454.45);
C. BigDecimal("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 builder = 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

Section 10.11 The StringBuilder/StringBuffer Class
10.39  Analyze the following code.

class Test {  
  public static void main(String[] args) {
    StringBuilder strBuilder = new StringBuilder(4);
    strBuilder.append("ABCDE");
    System.out.println("What's strBuilder.charAt(5)? " + strBuilder.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 builder's capacity is 4, but five characters "ABCDE" are appended into the builder.
C. The program has a runtime error because the length of the string in the builder is 5 after "ABCDE" is appended into the builder. Therefore, strBuilder.charAt(5) is out of range.
D. The program compiles and runs fine.

10.40  Which of the following is true?
A. You can add characters into a string builder.
B. You can delete characters from a string builder.
C. You can reverse the characters in a string buffer.
D. The capacity of a string buffer can be automatically adjusted.

10.41  _________ returns the last character in a StringBuilder variable named strBuilder?
A. strBuilder.charAt(strBuilder.length() - 1)
B. strBuilder.charAt(strBuilder.capacity() - 1)
C. StringBuilder.charAt(strBuilder.length() - 1)
D. StringBuilder.charAt(strBuilder.capacity() - 1)

10.42  Assume StringBuilder strBuilder is "ABCDEFG", after invoking _________, strBuilder contains "AEFG".
A. strBuilder.delete(0, 3)
B. strBuilder.delete(1, 3)
C. strBuilder.delete(1, 4)
D. strBuilder.delete(2, 4)

10.43  Assume StringBuilder strBuilder is "ABCDEFG", after invoking _________, strBuilder contains "ABCRRRRDEFG".
A. strBuilder.insert(1, "RRRR")
B. strBuilder.insert(2, "RRRR")
C. strBuilder.insert(3, "RRRR")
D. strBuilder.insert(4, "RRRR")

10.44  Assume StringBuilder strBuilder is "ABCCEFC", after invoking _________, strBuilder contains "ABTTEFT".
A. strBuilder.replace('C', 'T')
B. strBuilder.replace("C", "T")
C. strBuilder.replace("CC", "TT")
D. strBuilder.replace('C', "TT")
E. strBuilder.replace(2, 7, "TTEFT")

10.45  The StringBuilder methods _____________ not only change the contents of a string builder, but also returns a reference to the string builder.
A. delete
B. append
C. insert
D. reverse
E. replace

10.46  The following program displays __________.

public class Test {  
  public static void main(String[] args) {
    String s = "Java";
    StringBuilder builder = new StringBuilder(s);
    change(builder);
    System.out.println(builder);
  }
  
  private static void change(StringBuilder builder) {
    builder.append(" and HTML");
  }
}
A. Java
B. Java and HTML
C. and HTML
D. nothing is displayed