29
Solutions Chapter 4: Fundamental Data Types Multiple Choice 1. In Java, every value is either a reference to an object, or it belongs to one of the eight _________. A) primitive types B) number types C) char types D) boolean types Ans: A Section Ref: Section 4.1 Number Types Title: TB 4.1 In Java, every value is either a reference to an object, or ... Difficulty: Easy 2. In Java, which of the following is not an integer primitive type? A) byte B) long C) double D) short Ans: C Section Ref: Section 4.1 Number Types Title: TB 4.2 In Java, which of the following is not an integer primitive type? Difficulty: Easy 3. In Java, there are four integer primitive types with different ranges of integer values. Which ordering of the integer types lists the types in increasing order by range? A) byte, int, short, long B) long, int, short, byte C) byte, short, long, int

Ch04 Solutions

Embed Size (px)

Citation preview

Page 1: Ch04 Solutions

SolutionsChapter 4: Fundamental Data Types

Multiple Choice

1. In Java, every value is either a reference to an object, or it belongs to one of the eight _________.A) primitive typesB) number typesC) char typesD) boolean types

Ans: ASection Ref: Section 4.1 Number TypesTitle: TB 4.1 In Java, every value is either a reference to an object, or ... Difficulty: Easy

2. In Java, which of the following is not an integer primitive type?A) byteB) longC) doubleD) short

Ans: CSection Ref: Section 4.1 Number TypesTitle: TB 4.2 In Java, which of the following is not an integer primitive type?Difficulty: Easy

3. In Java, there are four integer primitive types with different ranges of integer values. Which ordering of the integer types lists the types in increasing order by range?A) byte, int, short, longB) long, int, short, byteC) byte, short, long, intD) byte, short, int, long

Ans: DSection Ref: Section 4.1 Number TypesTitle: TB 4.3 Which ordering of the integer types lists the types in increasing order by range?Difficulty: Easy

4. A numeric computation ___________ if the result falls outside the range for the number type.A) overflows

Page 2: Ch04 Solutions

B) concatenatesC) truncatesD) rounds

Ans: ASection Ref: Section 4.1 Number TypesTitle: TB 4.4 A numeric computation ___________ if the result falls outside the range for the number type.Difficulty: Easy

5. Consider this code fragment.

int dollars = 10;double balance = dollars;

Which of the following statements is true?

A) The code compiles but it sets dollars to 10.0. B) The code compiles but it sets balance to a wrong value due to a rounding error.C) The code does not compile.D) The code compiles and runs correctly.

Ans: DSection Ref: Section 4.1 Number TypesTitle: TB 4.5 Which statement is true about this code involving assignment with double and int types assignment? Difficulty: Easy

6. What type carries out floating-point computation without roundoff errors?A) BigIntegerB) BigDecimalC) floatD) double

Ans: BSection Ref: Special Topic 4.1 Big NumbersTitle: TB 4.6 What type carries out floating-point computation without roundoff errors?Difficulty: Easy

7. Assuming that variables bigInt1 and bigInt2 are instances of the class BigInteger, which statement correctly adds these two variables?A) BigInteger result = bigInt1.add(bigInt2);B) BigInteger result = bigInt1 + bigInt2;C) Integer result = bigInt1 + bigInt2;

Page 3: Ch04 Solutions

D) BigInteger result = bigInt1.sum(bigInt2);

Ans: ASection Ref: Special Topic 4.1 Big NumbersTitle: TB 4.7 Which statement adds two BigInteger objects?Difficulty: Medium

8. The decimal equivalent of 110100 binary is ____.A) 26B) 52C) 52 D) 70

Ans: CSection Ref: Special Topic 4.2 Binary NumbersTitle: TB 4.8 The decimal equivalent of 110100 binary is ____. Difficulty: Medium

9. The decimal equivalent of 111010 binary is ____.A) 142 B) 72 C) 58 D) 112

Ans: CSection Ref: Special Topic 4.2 Binary NumbersTitle: TB 4.9 The decimal equivalent of 111010 binary is ____. Difficulty: Medium

10. The binary equivalent of 200 decimal is ____.A) 11100010 B) 10101000 C) 111001100 D) 11001000

Ans: DSection Ref: Special Topic 4.2 Binary NumbersTitle: TB 4.10 The binary equivalent of 200 decimal is ____. Difficulty: Medium

11. Numerical ____________________ are values that do not change and that have a special significance for a computation. A) constants

Page 4: Ch04 Solutions

B) statics C) comments D) commands

Ans: ASection Ref: Section 4.2 Constants Title: TB 4.11 Numerical ____________________ are values that do not change and that have a special significance for a computation.Difficulty: Easy

12. In Java, what reserved word is used to identify constants?A) finalB) doubleC) constantD) int

Ans: ASection Ref: Section 4.2 ConstantsTitle: TB 4.12 In Java, what reserved word is used to identify constants?Difficulty: Easy

13. Which of the following correctly defines a constant in a method? A) public static final double LITERS_PER_GALLON == 3.785 B) final double NICKEL_VALUE == 0.05; C) final double NICKEL_VALUE = 0.05;D) public static final double LITERS_PER_GALLON = 3.785;

Ans: CSection Ref: Section 4.2 Constants Title: TB 4.13 Which of the following correctly defines a constant in a method? Difficulty: Medium

14. Which of the following correctly defines a constant in a method? A) final int HOURS_PER_DAY = 24;B) final int DAYS_PER_YEAR == 365; C) public static final int DAYS_PER_YEAR = 365;D) public static final int HOURS_PER_DAY == 24;

Ans: ASection Ref: Section 4.2 Constants Title: TB 4.14 Which of the following correctly defines a constant in a method? Difficulty: Medium

Page 5: Ch04 Solutions

15. Which of the following correctly defines a constant in a class? A) public static final int HOURS_PER_DAY == 24; B) final int HOURS_PER_DAY = 24; C) final int DAYS_PER_YEAR == 365; D) public static final int DAYS_PER_YEAR = 365;

Ans: DSection Ref: Section 4.2 Constants Title: TB 4.15 Which of the following correctly defines a constant in a class? Difficulty: Medium

16. Using the ________ reserved word when declaring a constant means that the constant belongs to the class.A) constantB) finalC) staticD) class

Ans: CSection Ref: Section 4.2 ConstantsTitle: TB 4.16 Using this reserved word when declaring a constant means that the constant belongs to the class.Difficulty: Easy

17. How do you compute the area of a circle referencing a constant in the Math class of the standard library?A) double area = Math.pi * radius * radius;B) double area = Math.PI * radius * radius;C) double area = Math.E * radius * radius;D) double area = Math.e * radius * radius;

Ans: BSection Ref: Section 4.2 ConstantsTitle: TB 4.17 How do you compute the area of a circle referencing a constant in the Math class of the standard library?Difficulty: Medium

18. What are the arithmetic operators (add, subtract, multiply, divide) in Java, respectively?A) +, -, x, / B) +, -, x, % C) +, -, *, /D) add, subtract, multiply, divide

Page 6: Ch04 Solutions

Ans: CSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.18 What are the arithmetic operators (add, subtract, multiply, divide) in Java, respectively?Difficulty: Easy

19. What is the name of the operation denoted by the ++ operator?A) decrementB) additionC) incrementD) plus

Ans: CSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.19 What is the name of the operation denoted by the ++ operator?Difficulty: Easy

20. What is the name of the operation denoted by the -- operator?A) subtractionB) minusC) incrementD) decrement

Ans: DSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.20 What is the name of the operation denoted by the -- operator?Difficulty: Easy

21. What is the value of x after the following sequence of statements?

x--;x++;

A) x - 1B) x + 1C) xD) 0

Ans: CSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.21 What is the value of x after the following sequence of statements?Difficulty: Easy

Page 7: Ch04 Solutions

22. What is the value of y after the following sequence of statements?

y++;y--;

A) yB) y + 1C) 0D) y - 1

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.22 What is the value of y after the following sequence of statements?Difficulty: Easy

23. What is the result of a / b when variables a and b are declared as follows: int a = 9; int b = 5; ?A) 1B) 1.80C) 4D) .80

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.23 What is the result of a / b when variables a and b are declared as follows: Difficulty: Easy

24. What is the result of a / b when variables a and b are declared as follows: int a = 9; double b = 5; ?A) .80B) 4C) 1D) 1.80

Ans: DSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.24 What is the result of a / b when variables a and b are declared as follows: Difficulty: Easy

25. What is the Java operator that computes the remainder of an integer division?A) %B) modC) /

Page 8: Ch04 Solutions

D) div

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.25 What is the Java operator that computes the remainder of an integer division?Difficulty: Easy

26. Which statement correctly computes the square root of the given number?A) int s = Math.sqrt(16);B) int s = Math.squareroot(16);C) double s = Math.sqrt(16);D) double s = Math.squareroot(16);

Ans: CSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.26 Which statement correctly computes the square root of the given number?Difficulty: Easy

27. Which statement correctly computes a specified number raised to the specified power?A) double p = 2.pow(3);B) double p = Math.pow(2,3);C) double p = 2.power(3);D) double p = Math.power(2,3);

Ans: BSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.27 Which statement correctly computes a specified number raised to the specified power?Difficulty: Easy

28. Which statement correctly computes the absolute value of the difference between x and y?A) Math.abs(x-y)B) Math.absoluteValue(x-y)C) Math.absolute(x-y)D) |x-y|

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.28 Which statement correctly computes the absolute value of the difference between x and y?Difficulty: Easy

29. What is the term that means to explicitly convert a value to a different type?A) case

Page 9: Ch04 Solutions

B) overflowC) roundingD) cast

Ans: DSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.29 What is the term that means to explicitly convert a value to a different type?Difficulty: Easy

30. Which of the following code fragments will compile without error?A) int balance = (double) 100;B) double balance = 13.75; int dollars = balance;C) int dollars = 100; double balance = dollars;D) (int) balance = 13.75

Ans: CSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.30 Which code fragment involving conversions between double and int types will compile? Difficulty: Medium

31. Which of the following code fragments will compile without error?A) double balance = 13.75;; int dollars = (int) balance;B) double balance = 13.75; int dollars = balance;C) (int) balance = 13.75D) int balance = (double) 100;

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.31 Which code fragment involving conversions between double and int types will compile? Difficulty: Medium

32. Which of the following code fragments converts a floating-point number to the nearest integer?

double f = 4.65;

A) int n = (int) f;B) int n = Math.round(f);C) int n = (int) Math.round(f);D) int n = (int) Math.round(100 * f);

Page 10: Ch04 Solutions

Ans: CSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.32 Which of the following code fragments converts a floating-point number to the nearest integer?Difficulty: Medium

33. Which of the following code fragments converts a floating-point number to the nearest integer?

double d = 6.54;

A) int i = (int) Math.round(100 * d);B) int i = (int) Math.round(d);C) int i = (int) d;D) int i = Math.round(d);

Ans: BSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.33 Which of the following code fragments converts a floating-point number to the nearest integer?Difficulty: Medium

34. Which statement displays the correct average of the value of these variables of type int?A) System.out.println( (int1 + int2) / 2.0 );B) System.out.println( int1 + int2 / 2.0 );C) System.out.println( int1 + int2 / 2 );D) System.out.println( (int1 + int2) / 2 );

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.34 Which statement displays the correct average of the value of these variables of type int?Difficulty: Easy

35. Which of the following statements is equivalent to balance = balance + amount; ?A) balance += amount;B) balance == amount;C) balance +== amount;D) balance =+ amount;

Ans: ASection Ref: Special Topic 4.3: Combining Assignment and Arithmetic Title: TB 4.35 Which of the following statements is equivalent to balance = balance + amount; ?Difficulty: Easy

Page 11: Ch04 Solutions

36. Which of the following statements is equivalent to balance = balance - amount; ?A) balance -= amount;B) balance =- amount;C) balance -== amount;D) balance == amount;

Ans: ASection Ref: Special Topic 4.3: Combining Assignment and Arithmetic Title: TB 4.36 Which of the following statements is equivalent to balance = balance - amount; ?Difficulty: Easy

37. Which of the following statements is equivalent to items = items * 2; ?A) items *= 2;B) items *== 2;C) items =* 2;D) items ==* 2 ;

Ans: ASection Ref: Special Topic 4.3: Combining Assignment and ArithmeticTitle: TB 4.37 Which of the following statements is equivalent to items = items * 2; ?Difficulty: Easy

38. How many cards are left in a 52-card deck after dealing NUMBER_OF_CARDS to NUMBER_OF_PLAYERS?A) (52 - NUMBER_OF_PLAYERS) * NUMBER_OF_CARDSB) NUMBER_OF_CARDS * NUMBER_OF_PLAYERS - 52C) (52 - NUMBER_OF_CARDS) * NUMBER_OF_PLAYERSD) 52 - NUMBER_OF_CARDS * NUMBER_OF_PLAYERS

Ans: DSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.38 How many cards are left in a 52-card deck after dealing?Difficulty: Easy

39. Which of the following statements corresponds to computing the line item total on an invoice given the quantity and price of the item?A) int lineItemTotal = (int) quantity * price;B) double lineItemTotal = qty * price;C) double lineItemTotal = quantity * price;D) int lineItemTotal = quantity * price;

Ans: C

Page 12: Ch04 Solutions

Section Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.39 Which of the following statements corresponds to computing the line item total on an invoice?Difficulty: Easy

40. Which of the following statements corresponds to revising an invoice subtotal with a line item given the quantity and price of the item?A) subtotal = subtotal + (quantity * price);B) subtotal =+ quantity * price;C) subtotal = (subtotal + quantity) * price;D) subtotal =+ (quantity * price);

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.40 Which of the following statements corresponds to revising an invoice subtotal?Difficulty: Medium

41. Which of the following statements corresponds to computing an invoice total given the subtotal and SALES_TAX rate?A) double total = (subtotal * SALES_TAX) + total;B) double total = (subtotal * SALES_TAX);C) double total += (subtotal * SALES_TAX);D) double total = subtotal + (subtotal * SALES_TAX);

Ans: DSection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.41 Which of the following statements corresponds to revising an invoice subtotal?Difficulty: Medium

42. Which of the following statements corresponds to computing a student's gpa after receiving an A (4.0) in a 3-credit course, given the currentGPA and currentCredits?A) double gpa = ((currentGPA * currentCredits) + (4.0 * 3))/(currentCredits + 3);B) double gpa = ((currentGPA * currentCredits) + 4.0)/(currentCredits + 3);C) double gpa = (currentCredits + (4.0 * 3))/(currentCredits + 3);D) double gpa = (currentGPA + (4.0 * 3))/(currentCredits + 3);

Ans: ASection Ref: Section 4.3 Arithmetic Operations and Mathematical FunctionsTitle: TB 4.42 Which of the following statements corresponds to computing a student's gpa?Difficulty: Medium

Page 13: Ch04 Solutions

43. What type of method does NOT operate on an object?A) privateB) staticC) finalD) instance

Ans: BSection Ref: Section 4.4 Calling Static MethodsTitle: TB 4.43 What type of method does NOT operate on an object?Difficulty: Easy

44. Which statement illustrates the invocation of a static method?A) mySavings.deposit(100);B) double s = Math.sqrt(100);C) deposit(100).mySavings;D) double s = 100.sqrt();

Ans: BSection Ref: Section 4.4 Calling Static MethodsTitle: TB 4.44 Which statement illustrates the invocation of a static method?Difficulty: Medium

45. Which statement illustrates the invocation of an instance method?A) double s = Math.sqrt(100);B) mySavings.deposit(100);C) deposit(100).mySavings;D) double s = 100.sqrt();

Ans: BSection Ref: Section 4.4 Calling Static MethodsTitle: TB 4.45 Which statement illustrates the invocation of an instance method?Difficulty: Medium

46. Which statement illustrates the invocation of an instance method?A) withdraw(200).mySavings;B) mySavings.withdraw(200);C) double s = 100.sqrt(); D) double s = Math.sqrt(100);

Ans: BSection Ref: Section 4.4 Calling Static MethodsTitle: TB 4.46 Which statement illustrates the invocation of an instance method?

Page 14: Ch04 Solutions

Difficulty: Medium

47. What is a sequence of characters called?A) charactersB) charC) charsD) string

Ans: DSection Ref: Section 4.5 StringsTitle: TB 4.47 What is a sequence of characters called?Difficulty: Easy

48. Which of the following statements about strings is true?A) String is a primitive type in Java.B) Strings are objects of the CharacterSequence class.C) Strings are objects of the String class.D) Strings are not important in Java.

Ans: CSection Ref: Section 4.5 StringsTitle: TB 4.48 Which of the following statements about strings is true?Difficulty: Easy

49. Which statement correctly determines the number of characters in the string referenced by variable myString?A) int n = myString.length();B) int n = myString.length;C) int n = myString.size;D) int n = myString.size();

Ans: ASection Ref: Section 4.5 StringsTitle: TB 4.49 Which statement correctly determines the number of characters in the string referenced by variable myString?Difficulty: Medium

50. What is the name of the + operator when applied to strings?A) plusB) pastingC) additionD) concatenation

Page 15: Ch04 Solutions

Ans: DSection Ref: Section 4.5 StringsTitle: TB 4.50 What is the name of the + operator when applied to strings?Difficulty: Easy

51. String concatenation is denoted by what operator? A) ++B) &C) &&D) +

Ans: DSection Ref: Section 4.5 StringsTitle: TB 4.51 String concatenation is denoted by what operator? Difficulty: Easy

52. Consider this code fragment.

System.out.println("The account balance is: " + acct.getBalance());

Which of the following statements is true?

A) The code compiles and runs correctly, displaying the label and balance of the account. B) The code compiles and runs but it does not display the balance of the account.C) The code compiles but it generates an exception at run-time. D) The code does not compile because you cannot add a string and a number.

Ans: ASection Ref: Section 4.5 StringsTitle: TB 4.52 Which statement is true about this code involving the display of a string and numeric value? Difficulty: Medium

53. Assuming the following code fragment, what is the value of tt?

String t = "2"; String tt = t + t;

What is the value of t? A) "4"B) "22"C) 22

Page 16: Ch04 Solutions

D) The code fragment has a syntax error and does not compile.

Ans: BSection Ref: Section 4.5 StringsTitle: TB 4.53 What is the value of tt?Difficulty: Medium

54. Convert the following string to its integer value as quantity:

String input = "6";

A) int quantity = Integer.parseInteger(input);B) double quantity = Double.parseDouble(input); C) int quantity = String.parseString(input);D) int quantity = Integer.parseString(input);

Ans: ASection Ref: Section 4.5 Strings Title: TB 4.54 Convert the following string to its integer value as quantity:Difficulty: Easy

55. Convert the following string to its floating-point value as price:

String input = "75.23";

A) double price = Double.parseString(input);B) double price = Double.parseDouble(input);C) int price = Integer.parseInteger(input);D) double price = String.parseString(input);

Ans: BSection Ref: Section 4.5 Strings Title: TB 4.55 Convert the following string to its floating-point value as price:Difficulty: Easy

56. What is the name of the method that extracts part of a string?A) extractstringB) partstringC) substringD) stringpart

Ans: CSection Ref: Section 4.5 StringsTitle: TB 4.56 What is the name of the method that extracts part of a string?

Page 17: Ch04 Solutions

Difficulty: Easy

57. What is the starting position of a string?A) 1B) Strings don't have positions.C) startPosition()D) 0

Ans: DSection Ref: Section 4.5 StringsTitle: TB 4.57 What is the starting position of a string?Difficulty: Easy

58. What is the ending position of a string?A) length()B) length() - 1C) Strings do not have positions.D) endPosition()

Ans: BSection Ref: Section 4.5 StringsTitle: TB 4.58 What is the ending position of a string?Difficulty: Easy

59. Based on the code below, which statement extracts the string "Hello"?

String greeting = "Hello, World!";

A) String hello = greeting.substring(",");B) String hello = greeting.substring(5);C) String hello = greeting.substring(1,6);D) String hello = greeting.substring(0,5);

Ans: DSection Ref: Section 4.5 StringsTitle: TB 4.59 Which statement extracts the string "Hello"?Difficulty: Hard

60. Based on the code below, which statement creates a string in the format "Hello, NAME!"?

String hello = "Hello";String name; // Assume value is initialized by the program

Page 18: Ch04 Solutions

A) String helloName = hello + ", " + name + "!";B) String helloName = hello + name + "!";C) String helloName = hello + ", " + name;D) String helloName = hello + name;

Ans: ASection Ref: Section 4.5 StringsTitle: TB 4.60 Which statement creates a string in the format "Hello, NAME!"?Difficulty: Medium

61. Based on the code below, write a statement that copies all characters after the comma to the string sub.

String greeting = "Hello, World!";

A) String sub = greeting.substring(" ");B) String sub = greeting.substring(7);C) String sub = greeting.substring(6);D) String sub = greeting.substring(",");

Ans: CSection Ref: Section 4.5 StringsTitle: TB 4.61 Write a statement that copies all characters after the comma to the string sub.Difficulty: Hard

62. Assume the string phoneNumber consists of a valid phone number with dashes, e.g., ###-###-####. Which statement returns a string for the area code associated with a phone number, which is the first three digits before the initial dash? A) String areaCode = phoneNumber.substring(1,3);B) String areaCode = phoneNumber.substring(0,2);C) String areaCode = phoneNumber.substring(0,3);D) String areaCode = phoneNumber.substring(3);

Ans: CSection Ref: Section 4.5 StringsTitle: TB 4.62Which statement returns the area code associated with a phone number?Difficulty: Hard

63. Assume the string url consists of a valid url. Consider the code fragment:

int i = url.indexOf("://");

which returns the index within the url of the first occurrence of the specified substring.

Page 19: Ch04 Solutions

Which statement correctly returns the protocol of the url, which appears at the beginning of the url before the ://? A) String protocol = url.substring(0,i-1);B) String protocol = url.substring(1,i);C) String protocol = url.substring(i);D) String protocol = url.substring(0,i);

Ans: DSection Ref: Section 4.5 StringsTitle: TB 4.63 Which statement returns the protocol associated with a url?Difficulty: Hard

64. Which statement returns the punctuation mark ending a sentence as a string?A) String punctuationMark = sentence.substring(sentence.length(), sentence.length() + 1);B) String punctuationMark = sentence.substring(sentence.length() - 1, sentence.length());C) String punctuationMark = sentence.substring(sentence.length, sentence.length + 1);D) String punctuationMark = sentence.substring(sentence.length - 1, sentence.length);

Ans: BSection Ref: Section 4.5 StringsTitle: TB 4.64 Which statement returns the punctuation mark ending a sentence as a string?Difficulty: Medium

65. Assume the string server consists of a valid server for a url. Consider the code fragment:

int i = server.lastIndexOf(".");

which returns the index within the string of the rightmost occurrence of the specified substring.Which statement correctly returns the top-level domain of the url (e.g., edu, com, gov), which appears at the end of the url after the last period? A) String domain = server.substring(i + 1, server.length());B) String domain = server.substring(i, server.length() - 1)C) String domain = server.substring(i, server.length());D) String domain = server.substring(i + 1, server.length() - 1);

Ans: ASection Ref: Section 4.5 StringsTitle: TB 4.65 Which statement returns the top-level domain associated with a url?Difficulty: Hard

Page 20: Ch04 Solutions

66. Consider the following valid code fragment:

String partOfString = input.substring(first,last);

What is the length of partOfString? A) last - first + 1B) last - firstC) first - last + 1D) first - last

Ans: BSection Ref: Section 4.5 StringsTitle: TB 4.66 How many characters are in the substring?Difficulty: Medium

67. Which of the following is the constant of type char that represents the lowercase letter n?A) "N"B) "n"C) 'N'D) 'n'

Ans: DSection Ref: Special Topic 4.5: Strings and the char TypeTitle: TB 4.67 Which choice is the constant of type char representing the lowercase letter n?Difficulty: Easy

68. Which of the following finds the first character of the string referenced by the variable input?A) Char ch = input.charAt(1);B) char ch = input.charAt(0);C) Char ch = input.charAt(0);D) char ch = input.charAt(1);

Ans: BSection Ref: Special Topic 4.5: Strings and the char TypeTitle: TB 4.68 Find the first character of the string referenced by the variable input.Difficulty: Easy

69. The first digit of a credit card number typically indicates the type of credit card. Which of the following finds the first digit of the string referenced by creditCardNumber?A) char digit = creditCardNumber.charAt(0);B) Char digit = creditCardNumber.charAt(0);C) char digit = creditCardNumber.charAt(1);

Page 21: Ch04 Solutions

D) Char digit = creditCardNumber.charAt(1);

Ans: ASection Ref: Special Topic 4.5: Strings and the char TypeTitle: TB 4.69 Find the first digit of a creditCardNumber.Difficulty: Easy

70. Which of the following gets the last character of the string referenced by sentence?A) char ch = sentence.charAt(sentence.length()-1);B) Char ch = sentence.charAt(length());C) char ch = sentence.charAt(sentence.length());D) Char ch = sentence.charAt(length()-1);

Ans: ASection Ref: Special Topic 4.5: Strings and the char TypeTitle: TB 4.70 Find the last character of a sentence.Difficulty: Easy

71. Given a firstName and a lastName, what are the person's initials?A) String initials = firstName.charAt(1) + lastName.charAt(1);B) String initials = firstName.charAt(0) + lastName.charAt(0);C) char initials = firstName.charAt(1) + lastName.charAt(1);D) char initials = firstName.charAt(0) + lastName.charAt(0);

Ans: BSection Ref: Special Topic 4.5: Strings and the char TypeTitle: TB 4.71 What are the person's initials?Difficulty: Easy

72. What is the purpose of the Scanner class?A) The Scanner class provides methods to read numbers and strings from the console or a file.B) The Scanner class provides methods to scan a file and create a pdf.C) The Scanner class provides methods to scan a file and create a tif.D) The Scanner class provides methods to scan a picture and create a jpg.

Ans: ASection Ref: Section 4.6 Reading InputTitle: TB 4.72 What is the purpose of the Scanner class?Difficulty: Medium

73. Consider the following pseudocode:

Prompt user to enter item description (may contain spaces)

Page 22: Ch04 Solutions

Get item description and save in a variable

Using the Scanner class, which code correctly implements the pseudocode, assuming the variable in references the console? A) System.out.println("Enter item description: "); String itemDescription = in.next();B) System.out.println("Enter item description: "); String itemDescription = in.nextDouble();C) System.out.println("Enter item description: "); String itemDescription = in.nextInt();D) System.out.println("Enter item description: "); String itemDescription = in.nextLine();

Ans: DSection Ref: Section 4.6 Reading InputTitle: PQ 4.73 Using the Scanner class, which code correctly inputs an item description?Difficulty: Medium

74. Which statement creates an instance of the Scanner class to read keyboard input in a console window?A) Scanner in = new Scanner(console);B) console = new Scanner(System.in);C) Scanner in = new Scanner(System.in);D) System.in = new Scanner();

Ans: CSection Ref: Section 4.6 Reading InputTitle: TB 4.74 Which statement creates an instance of the Scanner class to read keyboard input in a console window?Difficulty: Medium

75. Consider the following pseudocode:

Prompt user to enter item identifier (one word with letters and digits without spaces)Get item identifier and save in a variable

Using the Scanner class, which code correctly implements the pseudocode, assuming the variable in references the console? A) System.out.println("Enter item identifier: "); String itemIdentifier = in.nextInt();B) System.out.println("Enter item identifier: "); String itemIdentifier = in.nextDouble();C) System.out.println("Enter item identifier: ");

Page 23: Ch04 Solutions

String itemIdentifier = in.nextLn();D) System.out.println("Enter item identifier: "); String itemIdentifier = in.next();

Ans: DSection Ref: Section 4.6 Reading InputTitle: TB 4.75 Using the Scanner class, which code correctly inputs an item identifier?Difficulty: Medium