Java ch 14 - String Methods

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which of the following is not a word character? & w 0 -

&

Given the following declaration: StringBuilder buf = new StringBuilder(); What is the capacity of buf?

16

Consider the Java segment: String line1 = new String("c = 1 + 2 + 3") ;StringTokenizer tok = new StringTokenizer(line1); int count = tok.countTokens(); The value of count is ______________.

7

How many String objects are instantiated by the following code segment (not including literals)? String s1, output; s1 = "hello"; output = "\nThe string reversed is: "; for (int i = s1.length() - 1; i >= 0; i--) output += s1.chartAt(i)+ " "; a. 1 b. 4 c. 5 d. 7

7

StringBuilder objects can be used in place of String objects if ________. the string data is not constant the string data size may grow the programs frequently perform string concatenation All of the above

All of the above

The statement s1.startsWith("art"); has the same result as which of the following? s1.regionMatches(0, "art", 0, 3); s2 = s1.getChars(0, 3); s2.equals("art"); s1.regionMatches(true, 0, "art", 0, 3); All of the above

All of the above

Consider the examples below: A. a string. B. 'a string'. C. "a string". D. "1234". E. integer. Which could be the value of a Java variable of type String? a. A and B. b. B and E. c. B and C. d. C and D.

C and D

Which of the following statements is true? The capacity of a StringBuilder is equal to its length. The capacity of a StringBuilder cannot exceed its length. The length of a StringBuilder cannot exceed its capacity. Both a and b are true.

The length of a StringBuilder cannot exceed its capacity.

The String method substring returns ________.

a String

Consider the statements below: StringBuilder sb = new StringBuilder("a toyota"); sb.insert(2, "landrover"); sb.delete(11, 16); sb.insert(11, " "); The StringBuilder contents at the end of this segment will be ________.

a landover a

Consider the Java segment: String line1 = new String("c = 1 + 2 + 3"); StringTokenizer tok = new StringTokenizer(line1, "+="); String foo = tok.nextToken(); String bar = tok.nextToken(); The values of foo and bar are:

foo is "c ", bar is " 1 ".

For String c = "Now is the time for all"; The Java statements String i = c.substring(7); String j = c.substring(4, 15); will result in:

i = "the time for all" and j = "is the time "

For String c = "Hello. She sells sea shells"; The java statements int i = c.indexOf("ll"); int j = c.lastIndexOf("ll"); will result in:

i = 2 and j = 24

For String c = "hello world"; The Java statements int i = c.indexOf('o'); int j = c.lastIndexOf('I'); will result in:

i = 4 and j = 9

A String constructor cannot be passed ________ .

int arrays

Which of the following will create a String different from the other three? String r = "123456" int i = 123; int j = 456; String r = String.valueOf(j) + String.valueOf(i); int i = 123; int j = 456; String r = String.valueOf(i) + String.valueOf(j); int i = 123; int j = 456; String r = i + j;

int i = 123; int j = 456; String r = String.valueOf(j) + String.valueOf(i);

An anonymous String ________. has no value is a string literal can be changed none of the above

is a string literal

The statement s1.equalsIgnoreCase(s4) is equivalent to which of the following? s1.regionMatches(true, 0, s4, 0, s4.length()); s1.regionMatches(0, s4, 0, s4.length()); s1.regionMatches(0, s4, 0, s4.length); s1.regionMatches(true, s4, 0, s4.length);

s1.regionMatches(true, 0, s4, 0, s4.length());

The length of a string can be determined by ________.

the String method length()

Which of the following is not a method of class String?

toCharacterArray

Which of the following are static Character methods? Character.hashcode(char c); Character.isDigit(char c); Character.equals(char c); All of the above.

Character.isDigit(char c);

Which class is not a type-wrapper class? Character Int Double Byte

Int

Consider the Java segment: String line1 = new String("c = 1 + 2 + 3") ; StringTokenizer tok = new StringTokenizer(line1, delimArg); For the String line1 to have 4 tokens, delimArg should be:

String delimArg = "+=";

Consider the statements below: String a = "JAVA: "; String b = "How to "; String c = "Program"; Which of the statements below will create the String r1 = "JAVA: How to Program"? String r1 = c.concat(b.concat(a)); String r1 = a.concat(b.concat(c)); String r1 = b.concat(c.concat(a)); String r1 = c.concat(c.concat(b));

String r1 = a.concat(b.concat(c));

Consider the String below: String r = "a toyota"; Which of the following will create the String r1 = "a TOYOTa"? String r1 = r.replace("toyot", TOYOT"); String r1 = r.replace('t','T');r1 = r.replace('o','0');r1 = r.replace('y','Y'); String r1 = r.replace('t','T').replace('o', '0').replace('y', 'Y'); String r1 = r.substring(2, 4).toUpperCase();

String r1 = r.replace('t','T').replace('o', '0').replace('y', 'Y');

Which of the following creates the string of the numbers from 1 to 1000 most efficiently? String s; for (int i = 1; i <= 1000; i++) s += i; StringBuilder sb = new StringBuilder(10); for (int i = 1; i <= 1000; i++) sb.append(i); String s = new String(sb); StringBuilder sb = new StringBuilder(3000); for (int i = 1; i <= 1000; i++) sb.append(i); String s = new String(sb); All are equivalently efficient.

StringBuilder sb = new StringBuilder(3000); for (int i = 1; i <= 1000; i++) sb.append(i); String s = new String(sb);

Given the following declarations: StringBuilder buf;StringBuilder buf2 = new StringBuilder(); String c = new String("test"); Which of the following is not a valid StringBuilder constructor?

buf = new StringBuilder(buf2, 32);

Given the following declarations: StringBuilder buffer = new StringBuilder("Testing Testing"); buffer.setLength(7);buffer.ensureCapacity(5); Which of the following is true? buffer has capacity 5. buffer has capacity 31. buffer has content "Testin". buffer has length 15.

buffer has capacity 31.

String objects are immutable. This means they ________. a. must be initialized b. cannot be deleted c. cannot be changed d. None of the above

cannot be changed

Consider the statement below: StringBuilder sb1 = new StringBuilder("a toyota"); Which of the following creates a String object with the value "toy"? String res = sb1.subString(2, 5); char dest[] = new char[sb1.length()]; sb1.getChars(2, 5, dest, 0); String res = new String(dest); char dest[] = new char[sb1.length]; dest = sb1.getChars(2, 5); String res = new String(dest); char dest[] = new char[sb1.length()]; dest = sb1.getChars(0, 3); String res = new String(dest);

char dest[] = new char[sb1.length()]; sb1.getChars(2, 5, dest, 0); String res = new String(dest);


Kaugnay na mga set ng pag-aaral

HW3: Physical Development and Biological Aging

View Set

History of Graphic Design - ART3643: Chapter 11

View Set

SAFe Lesson 4: Building Solutions with Agile Product Delivery

View Set

Chapter 14: Composing Your Speech

View Set

Information systems final review

View Set