Intro. to Java Programming, Ninth Edition - Ch.9, CS II Chapter 10 Quiz, Intro. to Java Programming, Ninth Edition - Ch.11, CS II Chapter 12 Quiz

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

A

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

A

BigInteger and BigDecimal are immutable A. true B. false

A

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

A

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

A

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

A

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

A

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

A

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

A

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

A

_________ 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)

A

___________ 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

A

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

A and B

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

A and E

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

A, B, C and D

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

A, B, C and D

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

A, B, C and D

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

A, B, C, D and E

Which of the following classes are immutable? A. Integer B. Double C. BigInteger D. BigDecimal E. String

A, B, C, D and E

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

A, B, C, D and E

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

A. Java //Explanation: Inside the method, the statement s = s + ' and HTML' creates a new String object s, which is different from the original String object passed to the change(s) method. The original String object has not been changed. Therefore, the output from the original string is Java.

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

A. String s = "Welcome to Java";

Suppose s1 and s2 are two strings. Which of the following statements or expressions is incorrect? (choose more than one) A. String s3 = s1 - s2; B. boolean b = s1.compareTo(s2); C. char c = s1[0]; D. char c = s1.charAt(s1.length());

A. String s3 = s1 - s2; // you can only combine (concatenate) strings, you cannot subtract them. B. boolean b = s1.compareTo(s2); //not sure about this but I'm guessing its because its redundant, s1.compareTo(s2) is already boolean with true or false. C. char c = s1[0]; //not sure about this either but I'm guessing this is wrong because a string isn't an array and shouldn't use brackets D. char c = s1.charAt(s1.length()); //Attempting to access characters in a string s out of bounds is a common programming error. To avoid it, make sure that you do not use an index beyond s.length() - 1. For example, s.charAt(s.length()) would cause a StringIndexOutOfBoundsException.

__________ returns a string. (choose more than one) A. String.valueOf(123) B. String.valueOf(12.53) C. String.valueOf(false) D. String.valueOf(new char[]{'a', 'b', 'c'})

A. String.valueOf(123) B. String.valueOf(12.53) C. String.valueOf(false) D. String.valueOf(new char[]{'a', 'b', 'c'}) //The static valueOf method can be used to convert an array of characters into a string. For example, to convert a double value 5.44 to a string, use String.valueOf(5.44). The return value is a string consisting of the characters '5', '.', '4', and '4'.

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 compilation 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.

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

Which of the following is true? (choose more than one) 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.

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.

What is the return value of "SELECT".substring(4, 4)? A. an empty string B. C C. T D. E

A. an empty string

The StringBuilder methods _____________ not only change the contents of a string buffer, but also returns a reference to the string buffer. (choose more than one) A. delete B. append C. insert D. reverse E. replace

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

To check if a string s contains the suffix "Java", you may write (choose more than one) A. if (s.endsWith("Java")) ... B. if (s.lastIndexOf("Java") >= 0) ... C. if (s.substring(s.length() - 4).equals("Java")) ... D. if (s.substring(s.length() - 5).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') ...

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 prefix "Java", you may write (choose more than one) 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') ...

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') ...

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? A. int count = args.length; B. int count = args.length - 1; C. int count = 0; while (args[count] != null) count ++; D. int count=0; while (!(args[count].equals(""))) count ++;

A. int count = args.length;

Which of the following is the correct header of the main method? (choose more than one) 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[]) E. static void main(String[] args)

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[]) //Explanation: e is incorrect because the main method must be public.

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

A. s1 and s2 have the same contents //key line - ____if (s1.equals(s2))

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

A. s1 and s2 reference to the same String object

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

A. s1 and s2 reference to the same String object //Explanation: Since strings are immutable and are ubiquitous in programming, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. // also this is missing the new keyword which would be two different strings

_________ 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)

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

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

A. true

Suppose Character x = new Character('a'), __________________ returns true. (choose more than one) A. x.equals(new Character('a')) B. x.compareToIgnoreCase('A') C. x.equalsIgnoreCase('A') D. x.equals('a') E. x.equals("a")

A. x.equals(new Character('a')) D. x.equals('a') //Explanation: (B) and (C) are wrong because no methods compareToIgnoreCase and equalsIgnoreCase are in the Character class. (E) is wrong because a character is not a string.

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()

B

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

B

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

B

What is the output of Integer.parseInt("10", 2)? A. 1; B. 2; C. 10; D. Invalid statement;

B

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

B

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

B

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)

B

What is the return value of "SELECT".substring(0, 5)? A. "SELECT" B. "SELEC" C. "SELE" D. "ELECT"

B. "SELEC"

What is the output of the following code? public class Test1 { __public static void main(String[] args) { ____ChildClass c = new ChildClass(); ____c.print(); } } class ParentClass { __int id = 1; __void print() { ____System.out.println(id); } } class ChildClass extends ParentClass { __int id = 2; } A. 0 B. 1 C. 2 D. Nothing

B. 1

"abc".compareTo("aba") returns ___________. A. 1 B. 2 C. -1 D. -2 E. 0

B. 2

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

B. A B C D

Which of the following statements are true? (choose more than one) A. A subclass is a subset of a superclass. B. A subclass is usually extended to contain more functions and more detailed information than its superclass. C. "class A extends B" means A is a subclass of B. D. "class A extends B" means B is a subclass of A.

B. A subclass is usually extended to contain more functions and more detailed information than its superclass. C. "class A extends B" means A is a subclass of B.

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

B. Java and HTML //Explanation: Inside the method, the content of the StringBuilder object is changed to Java and HTML. Therefore, the output from buffer is Java and HTML.

Which correctly creates an array of five empty Strings? A. String[] a = new String [5]; B. String[] a = {"", "", "", "", ""}; C. String[5] a; D. String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

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

Analyze the following code: public class A extends B { } class B { __public B(String s) { } } (choose more than one) A. The program has a compilation error because A does not have a default constructor. B. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. C. The program would compile fine if you add the following constructor into A: A(String s) { } D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

B. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. D. The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code: class Square extends GeometricObject { double length; Square(double length) { GeometricObject(length); } } A. The program compiles fine, but you cannot create an instance of Square because the constructor does not specify the length of the Square. B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally. C. The program compiles fine, but it has a runtime error because of invoking the Square class's constructor illegally.

B. The program has a compile error because you attempted to invoke the GeometricObject class's constructor illegally.

Analyze the following code: public class Test extends A { __public static void main(String[] args) { ____Test t = new Test(); ____t.print(); } } class A { __String s; __A(String s) { _____this.s = s; } __public void print() { _____System.out.println(s); } } (choose more than one) A. The program does not compile because Test does not have a default constructor Test(). B. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. C. The program would compile if a default constructor A(){ } is added to class A explicitly. D. The program compiles, but it has a runtime error due to the conflict on the method name print.

B. The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. C. The program would compile if a default constructor A(){ } is added to class A explicitly.

Object-oriented programming allows you to derive new classes from existing classes. This is called ____________. A. encapsulation B. inheritance C. abstraction D. generalization

B. inheritance

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)

B. new String(a)

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()

B. s.toCharArray()

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

B. s1 and s2 reference to different String objects

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

B. 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 = *new* String("Welcome to Java!");

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.

C

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

C

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)

C

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

C

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

C

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

C

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

C

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

C

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

C and D

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

C and E

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

C and E

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'

C, D and E

Which of the following is the correct statement to return JAVA? A. toUpperCase("Java") B. "Java".toUpperCase("Java") C. "Java".toUpperCase() D. String.toUpperCase("Java")

C. "Java".toUpperCase()

____________________ returns true. (choose more than one) A. "peter".compareToIgnoreCase("Peter") B. "peter".compareToIgnoreCase("peter") C. "peter".equalsIgnoreCase("Peter") D. "peter".equalsIgnoreCase("peter") E. "peter".equals("peter")

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

Given the following program: 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 A. 3 B. 1 C. 1 2 3 D. 1 2

C. 1 2 3

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

C. A,B;C A#B#C

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

C. Java AAA neat

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); A. 'a' B. 'v' C. Nothing will be assigned to x, because the execution causes the runtime error StringIndexOutofBoundsException.

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

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 compilation 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.

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. //Explanation: The charAt method returns the character at a specific index in the string buffer. The first character of a string buffer is at index 0, the next at index 1, and so on. The index argument must be greater than or equal to 0, and *less than the length of the string buffer*. So length of the string buffer is 5 but the last character is at place 4 meaning this should have been strBuf.charAt(5) - 1 or strBufCharAt(4). A(0) B(1) C(2) D(3) E(4)

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

C. Welc me t Java

How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2 A. args[0] B. args[1] C. args[2] D. args[3]

C. args[2]

Which of following is not a correct method in Character? (choose more than one) A. isLetterOrDigit(char) B. isLetter(char) C. isDigit() D. toLowerCase(char) E. toUpperCase()

C. isDigit() //Should be isDigit(char) E. toUpperCase() //Should be toUpperCase(char)

Assume s is "ABCABC", the method __________ returns a new string "aBCaBC". (choose more than one) A. s.toLowerCase(s) B. s.toLowerCase() C. s.replace('A', 'a') D. s.replace('a', 'A') E. s.replace("ABCABC", "aBCaBC")

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

Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect? (choose more than one) 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'

C. s1 >= s2 //Syntax error when using comparison operators D. int i = s1.length //also tricky, s1.length should only be used if this was an array. Since it is a string you use s1.length() E. s1.charAt(0) = '5' //s1.charAt(0) retrieves a string, it does not set a string. Strings are immutable so you can't add in '5' at place 0.

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

C. s1 and s2 have different contents

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)

C. strBuf.delete(1, 4)

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

C. strBuf.insert(3, "RRRR")

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()

D

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

D

To create an instance of BigInteger for 454, use A. BigInteger(454); B. new BigInteger(454); C. BigInteger("454"); D. new BigInteger("454");

D

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

D

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

D

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

D

"AbA".compareToIgnoreCase("abC") returns ___________. A. 1 B. 2 C. -1 D. -2 E. 0

D. -2 //ignoring case here so AbA is two off of abC because a - c is two places. //more detail it compares abC first then C to A is negative. So from Z up to A is negative where A down to Z is positive.

Identify the problems in the following code. public class Test { __public static void main(String argv[]) { _____System.out.println("argv.length is " + argv.length); } } A. The program has a compile error because String argv[] is wrong and it should be replaced by String[] args. B. The program has a compile error because String args[] is wrong and it should be replaced by String args[]. C. If you run this program without passing any arguments, the program would have a runtime error because argv is null. D. If you run this program without passing any arguments, the program would display argv.length is 0.

D. If you run this program without passing any arguments, the program would display argv.length is 0. //Explanation: The parameter for the main method is an array of String. The declaration String argv[] is correct. When you run the program without passing arguments, argv is new String[0]. Thus, argv.length is 0. See the NOTE box in the section, 'Passing Arguments to Java Programs.'

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

D. University //String is immutable while StringBuilder is not. You could replace by using the following command StringBuilder.replace.

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 fasle B. true fasle C. true true D. false true //false is misspelled but was copied from the test this way and I'm leaving it in so people can search for it if necessary. Also ignore quizlet bold.

D. false true

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()

D. s.trim()

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

E

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

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


Set pelajaran terkait

The Tragedy of Julius Caesar: Quotation and Character Matching

View Set

CNS MNT Psychiatric and Cognitive Disorders

View Set

Neuroscience UTD Exam 2: Hunger/Thirst (10)

View Set

Evolution Chapter Six : Exam Two

View Set

sensation & perception: unit 6, lesson 1

View Set

chapter 3 - race, ethnicity and immigration

View Set