CS 2336 Ch. 9

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

-2

"AbA".compareToIgnoreCase("abC") returns ___________.

2

"abc".compareTo("aba") returns ___________.

strBuf.replace(2, 7, "TTEFT")

Assume StringBuilder strBuf is "ABCCEFC", after invoking _________, strBuf contains "ABTTEFT".

strBuf.insert(3, "RRRR")

Assume StringBuilder strBuf is "ABCDEFG", after invoking _________, strBuf contains "ABCRRRRDEFG".

strBuf.delete(1, 4)

Assume StringBuilder strBuf is "ABCDEFG", after invoking _________, strBuf contains "AEFG".

s.trim()

Assume s is " abc ", the method __________ returns a new string "abc".

s.replace('A', 'a') s.replace("ABCABC", "aBCaBC")

Assume s is "ABCABC", the method __________ returns a new string "aBCaBC".

s.toCharArray()

Assume s is "ABCABC", the method __________ returns an array of characters.

args[2]

How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2

University

String s = "University"; s.replace("i", "ABC"); System.out.println(s);

A B C D

String[] tokens = "A,B;C;D".split("[,;]"); for (int i = 0; i < tokens.length; i++) System.out.print(tokens[i] + " ");

x.equals(new Character('a')) x.equals('a')

Suppose Character x = new Character('a'), __________________ returns true.

Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

Suppose s is a string with the value "java". What will be assigned to x if you execute the following code? char x = s.charAt(4);

true

Suppose s1 and s2 are two strings. What is the result of the following code? s1.equals(s2) == s2.equals(s1)

C. s1 >= s2 D. int i = s1.length E. s1.charAt(0) = '5'

Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?

A. String s3 = s1 - s2; B. boolean b = s1.compareTo(s2); C. char c = s1[0]; D. char c = s1.charAt(s1.length());

Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect?

A,B;C A#B#C

System.out.print("A,B;C".replaceAll(",;", "#") + " "); System.out.println("A,B;C".replaceAll("[,;]", "#"));

A. delete B. append C. insert D. reverse E. replace

The StringBuilder methods _____________ not only change the contents of a string buffer, but also returns a reference to the string buffer.

A. if (s.startsWith("Java")) ... B. if (s.indexOf("Java") == 0) ... C. if (s.substring(0, 4).equals("Java")) ... D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...

To check if a string s contains the prefix "Java", you may write

A. if (s.endsWith("Java")) ... C. if (s.substring(s.length() - 4).equals("Java")) ... E. if (s.charAt(s.length() - 4) == 'J' && s.charAt(s.length() - 3) == 'a' && s.charAt(s.length() - 2) == 'v' && s.charAt(s.length() - 1) == 'a') ...

To check if a string s contains the suffix "Java", you may write

false true

What is displayed by the following code? System.out.print("Hi, ABC, good".matches("ABC ") + " "); System.out.println("Hi, ABC, good".matches(".*ABC.*"));

Java AAA neat

What is displayed by the following statement? System.out.println("Java is neat".replaceAll("is", "AAA"));

s1 and s2 have the same contents

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"); } }

"SELEC"

What is the return value of "SELECT".substring(0, 5)?

an empty string

What is the return value of "SELECT".substring(4, 4)?

int count = args.length;

Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding the name of the class that is being invoked?

String[] a = {"", "", "", "", ""};

Which correctly creates an array of five empty Strings?

isDigit() toUpperCase()

Which of following is not a correct method in Character?

A. public static void main(String[] args) B. public static void main(String args[]) C. public static void main(String[] x) D. public static void main(String x[])

Which of the following is the correct header of the main method?

"Java".toUpperCase()

Which of the following is the correct statement to return JAVA?

new String(a)

Which of the following is the correct statement to return a string from an array a of characters?

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.Which of the following is true?

Which of the following is true?

String s = "Welcome to Java";

Which of the following statements is preferred to create a string "Welcome to Java"?

strBuf.charAt(strBuf.length() - 1)

_________ returns the last character in a StringBuilder variable named strBuf?

A. String.valueOf(123) B. String.valueOf(12.53) C. String.valueOf(false) D. String.valueOf(new char[]{'a', 'b', 'c'})

__________ returns a string.

C. "peter".equalsIgnoreCase("Peter") D. "peter".equalsIgnoreCase("peter") E. "peter".equals("peter")

____________________ returns true.

The program has a compilation error because s is not initialized, but it is referenced in the println statement.

class Test { public static void main(String[] args) { String s; System.out.println("s is " + s); } }

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.

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)); } }

If you run this program without passing any arguments, the program would display argv.length is 0.

public class Test { public static void main(String argv[]) { System.out.println("argv.length is " + argv.length); } }

1 2 3

public class Test { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.print(args[i] + " "); } } } What is the output, if you run the program using java Test 1 2 3

Java and HTML

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"); } }

Java

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"; } }

s1 and s2 reference to the same String object

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"); } }

s1 and s2 reference to the same String object

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"); } }

s1 and s2 have different contents

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"); } }

s1 and s2 reference to different String objects

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"); } }

Welc me t Java

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] + " "); } }

s1 and s2 reference to the same String object

s1 and s2 reference to the same String object


Ensembles d'études connexes

Social Psych Ch. 11 Prosocial Behavior

View Set

Chapter 15 - Integumentary Problems

View Set

chapter 15 assessing head and neck

View Set

Supply Chain Test #1 (Ch. 1,2,4,5,14)

View Set