Java Chapters 8, 9, and 10

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

8.10 What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int[] list : values) for (int element : list) if (v > element) v = element; System.out.print(v); } } A. 1 B. 3 C. 5 D. 6 E. 33

A. 1

9.39 What is the value of myCount.count displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = "+ times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } A. 101 B. 100 C. 99 D. 98

A. 101

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

A. 3

8.16 Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length? A. 4, 5, and 6 B. 6, 5, and 4 C. 5, 5, and 5 D. 4, 5, and 4

A. 4, 5, and 6

9.2 _______ is a construct that defines objects of the same type. A. A class B. An object C. A method D. A data field

A. A class

9.5 ________ is invoked to create an object. A. A constructor B. The main method C. A method with a return type D. A method with the void return type

A. A constructor

9.6 Which of the following statements are true? A. A default constructor is provided automatically if no constructors are explicitly declared in the class. B. At least one constructor must always be defined explicitly. C. Every class has a default constructor. D. The default constructor is a no-arg constructor.

A. A default constructor is provided automatically if no constructors are explicitly declared in the class. D. The default constructor is a no-arg constructor.

9.18 Which of the following code A or B, or both creates an object of the Date class: A: public class Test { public Test() { new java.util.Date(); } } B: public class Test { public Test() { java.util.Date date = new java.util.Date(); } } A. A. B. B. C. Neither

A. A. B. B.

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

A. An empty diamond

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

A. Integer B. Double C. BigInteger D. BigDecimal E. String

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

9.13 Which of the following statements are true? A. Local variables do not have default values. B. Data fields have default values. C. A variable of a primitive type holds a value of the primitive type. D. A variable of a reference type holds a reference to where an object is stored in the memory. E. You may assign an int value to a reference variable.

A. Local variables do not have default values. B. Data fields have default values. C. A variable of a primitive type holds a value of the primitive type. D. A variable of a reference type holds a reference to where an object is stored in the memory.

9.7 Which of the following statements are true? A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created.

A. Multiple constructors can be defined in a class. B. Constructors do not have a return type, not even void. C. Constructors must have the same name as the class itself. D. Constructors are invoked using the new operator when an object is created.

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

A. String s = "Welcome to Java";

9.44 Which of the following statements are true about an immutable object? A. The contents of an immutable object cannot be modified. B. All properties of an immutable object must be private. C. All properties of an immutable object must be of primitive types. D. A readable object type property in an immutable object must also be immutable. E. An immutable object contains no mutator methods.

A. The contents of an immutable object cannot be modified. B. All properties of an immutable object must be private. D. A readable object type property in an immutable object must also be immutable. E. An immutable object contains no mutator methods.

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.

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

9.32 Analyze the following code: public class Test { public static void main(String args[]) { NClass nc = new NClass(); nc.t = nc.t++; } } class NClass { int t; private NClass() { } } A. The program has a compile error because the NClass class has a private constructor. B. The program does not compile because the parameter list of the main method is wrong. C. The program compiles, but has a runtime error because t has no initial value. D. The program compiles and runs fine.

A. The program has a compile error because the NClass class has a private constructor.

9.14 Analyze the following code: public class Test { public static void main(String[] args) { double radius; final double PI= 3.15169; double area = radius * radius * PI; System.out.println("Area is " + area); } } A. The program has compile errors because the variable radius is not initialized. B. The program has a compile error because a constant PI is defined inside a method. C. The program has no compile errors but will get a runtime error because radius is not initialized. D. The program compiles and runs fine.

A. The program has compile errors because the variable radius is not initialized.

8.4 When you create an array using the following statement, the element values are automatically initialized to 0. int[][] matrix = new int[5][5]; A. True B. False

A. True

9.35 Which of the following statements are true? A. Use the private modifier to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. C. Encapsulating data fields makes the program short. D. Encapsulating data fields helps prevent programming errors.

A. Use the private modifier to encapsulate data fields. B. Encapsulating data fields makes the program easy to maintain. D. Encapsulating data fields helps prevent programming errors.

9.19 Which of the following statements are correct? A. When creating a Random object, you have to specify the seed or use the default seed. B. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. C. The nextInt() method in the Random class returns the next random int value. D. The nextDouble() method in the Random class returns the next random double value.

A. When creating a Random object, you have to specify the seed or use the default seed. B. If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical. C. The nextInt() method in the Random class returns the next random int value. D. The nextDouble() method in the Random class returns the next random double value.

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

A. a data field/the aggregating class

9.30 Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class. public static void main(String[] args) { xMethod(); } A. a static method B. an instance method C. a static method or an instance method

A. a static method

8.3 What is the index variable for the element at the first row and first column in array a? A. a[0][0] B. a[1][1] C. a[0][1] D. a[1][0]

A. a[0][0]

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

A. auto boxing

8.17 Which of the following statements are correct? A. char[][][] charArray = new char[2][2][]; B. char[2][2][] charArray = {'a', 'b'}; C. char[][][] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}; D. char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};

A. char[][][] charArray = new char[2][2][]; D. char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};

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

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

9.22 You should add the static keyword in the place of ? in Line ________ in the following code: 1 public class Test { 2 private int age; 3 4 public ? int square(int n) { 5 return n * n; 6 } 7 8 public ? int getAge() { 9 return age; 10 } 11 } A. in line 4 B. in line 8 C. in both line 4 and line 8 D. none

A. in line 4

9.47 What is the output for the third statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } A. j is 0 B. j is 1 C. j is 2 D. j is 3

A. j is 0

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

A. new java.math.BigInteger("343"); B. new java.math.BigDecimal("343.445"); D. new java.math.BigDecimal(343.445);

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

A. s = (new Double(d)).toString(); E. s = d + "";

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

A. s1 and s2 have the same contents

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

A. s1 and s2 reference to the same String object

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

A. s1 and s2 reference to the same String object

9.28 What code may be filled in the blank without causing syntax or runtime errors: public class Test { java.util.Date date; public static void main(String[] args) { Test test = new Test(); System.out.println(_________________); } } A. test.date B. date C. test.date.toString() D. date.toString()

A. test.date

9.51 Which of the following can be placed in the blank line in the following code? public class Test { private int id; public void m1() { _____.id = 45; } } A. this B. Test

A. this

9.50 Analyze the following code: class Test { private double i; public Test(double i) { this.t(); this.i = i; } public Test() { System.out.println("Default constructor"); this(1); } public void t() { System.out.println("Invoking t"); } } A. this.t() may be replaced by t(). B. this.i may be replaced by i. C. this(1) must be called before System.out.println("Default constructor"). D. this(1) must be replaced by this(1.0).

A. this.t() may be replaced by t(). C. this(1) must be called before System.out.println("Default constructor").

10.12 BigInteger and BigDecimal are immutable A. true B. false

A. true

9.41 What is the output of the following program? import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(1234567); m1(date); System.out.print(date.getTime() + " "); m2(date); System.out.println(date.getTime()); } public static void m1(Date date) { date = new Date(7654321); } public static void m2(Date date) { date.setTime(7654321); } } A. 1234567 1234567 B. 1234567 7654321 C. 7654321 1234567 D. 7654321 7654321

B. 1234567 7654321

8.8 Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length? A. 2, 3, and 3 B. 2, 3, and 4 C. 3, 3, and 3 D. 3, 3, and 4 E. 2, 2, and 2

B. 2, 3, and 4

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

B. 2;

8.13 What is the output of the following code? public class Test5 { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " "); } } A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14

B. 4 5 6 7

8.2 Assume double[][] x = new double[4][5], what are x.length and x[2].length? A. 4 and 4 B. 4 and 5 C. 5 and 4 D. 5 and 5

B. 4 and 5

9.17 Which of the following statements are correct? A. A reference variable is an object. B. A reference variable references to an object. C. A data field in a class must be of a primitive type. D. A data field in a class can be of an object type.

B. A reference variable references to an object. D. A data field in a class can be of an object type.

9.1 __________ represents an entity in the real world that can be distinctly identified. A. A class B. An object C. A method D. A data field

B. An object

9.25 Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } } A. The code has a compile error because xMethod does not return a value. B. The code has a compile error because xMethod is not declared static. C. The code prints n is 1. D. The code prints n is 2. E. The code prints n is 3.

B. The code has a compile error because xMethod is not declared static.

9.9 Analyze the following code. class TempClass { int i; public void TempClass(int j) { int i = j; } } public class C { public static void main(String[] args) { TempClass temp = new TempClass(2); } } A. The program has a compile error because TempClass does not have a default constructor. B. The program has a compile error because TempClass does not have a constructor with an int argument. C. The program compiles fine, but it does not run because class C is not public. D. The program compiles and runs fine.

B. The program has a compile error because TempClass does not have a constructor with an int argument.

9.8 Analyze the following code: public class Test { public static void main(String[] args) { A a = new A(); a.print(); } } class A { String s; A(String s) { this.s = s; } void print() { System.out.println(s); } } A. The program has a compile error because class A is not a public class. B. The program has a compile error because class A does not have a default constructor. C. The program compiles and runs fine and prints nothing. D. The program would compile and run if you change A a = new A() to A a = new A("5").

B. The program has a compile error because class A does not have a default constructor. D. The program would compile and run if you change A a = new A() to A a = new A("5").

9.49 Analyze the following code: class Circle { private double radius; public Circle(double radius) { radius = radius; } } A. The program has a compile error because it does not have a main method. B. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0. C. The program has a compile error because you cannot assign radius to radius. D. The program does not compile because Circle does not have a default constructor.

B. The program will compile, but you cannot create an object of Circle with a specified radius. The object will always have radius 0.

9.3 An object is an instance of a __________. A. program B. class C. method D. data

B. class

9.43 Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true? A. dates is null. B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates. D. dates = new Date() is fine, which creates a new Date object and assigns to dates.

B. dates[0] is null. C. dates = new java.util.Date[5] is fine, which assigns a new array to dates.

9.26 What is the output of the second println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } A. f2.i is 1 f2.s is 1 B. f2.i is 1 f2.s is 2 C. f2.i is 2 f2.s is 2 D. f2.i is 2 f2.s is 1

B. f2.i is 1 f2.s is 2

9.12 The default value for data field of a boolean type, numeric type, object type is ___________, respectively. A. true, 1, Null B. false, 0, null C. true, 0, null D. true, 1, null E. false, 1, null

B. false, 0, null

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

B. new BigDecimal(454.45); D. new BigDecimal("454.45");

9.20 To obtain the distance between the points (40, 50) and (5.5, 4.4), use _________. A. distance(40, 50, 5.5, 4.4) B. new Point2D(40, 50).distance(5.5, 4.4) C. new Point2D(40, 50).distance(new Point2D(5.5, 4.4)) D. new Point2D(5.5, 4.4).distance(40, 50) E. new Point2D(5.5, 4.4).distance(new Point2D(40, 50))

B. new Point2D(40, 50).distance(5.5, 4.4) C. new Point2D(40, 50).distance(new Point2D(5.5, 4.4)) D. new Point2D(5.5, 4.4).distance(40, 50) E. new Point2D(5.5, 4.4).distance(new Point2D(40, 50))

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)

B. new String(a)

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

B. s1 and s2 reference to different String objects

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

B. s1 and s2 reference to different String objects

8.5 How many elements are in array matrix (int[][] matrix = new int[5][5])? A. 14 B. 20 C. 25 D. 30

C. 25

8.7 Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length? A. 2 and 1 B. 2 and 2 C. 3 and 2 D. 2 and 3 E. 3 and 3

C. 3 and 2

8.19 What is the output of the following code? public class Test { public static void main(String[] args) { int[][][] data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; System.out.print(ttt(data[0])); } public static int ttt(int[][] m) { int v = m[0][0]; for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) if (v < m[i][j]) v = m[i][j]; return v; } } A. 1 B. 2 C. 4 D. 5 E. 6

C. 4

9.16 Suppose TestCircle and Circle in Listing 9.1 are in two separate files named TestCircle.java and Circle.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java? A. Only TestCircle.java compiles. B. Only Circle.java compiles. C. Both compile fine. D. Neither compiles successfully.

C. Both compile fine.

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

C. Integer.parseInt("100"); E. Integer.parseInt("345", 8);

9.37 Which is the advantage of encapsulation? A. Only public methods are needed. B. Making the class final causes no consequential changes to other code. C. It enables changes to the implementation without changing a class's contract and causes no consequential changes to other code. D. It enables changes to a class's contract without changing the implementation and causes no consequential changes to other code.

C. It enables changes to the implementation without changing a class's contract and causes no consequential changes to other code.

9.34 Analyze the following code and choose the best answer: public class Foo { private int x; public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } } A. Since x is private, it cannot be accessed from an object foo. B. Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. D. You cannot create a self-referenced object; that is, foo is created inside the class Foo.

C. Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.

9.29 Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class. public MyClass() { xMethod(); } A. a static method B. an instance method C. a static method or an instance method

C. a static method or an instance method

9.23 A method that is associated with an individual object is called __________. A. a static method B. a class method C. an instance method D. an object method

C. an instance method

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

C. b1 = b1.divide(b2);

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

C. b2 = b1.add(b2); D. b2 = b2.add(b1);

9.4 The keyword __________ is required to declare a class. A. public B. private C. class D. All of the above.

C. class

9.27 What is the output of the third println statement in the main method? public class Foo { int i; static int s; public static void main(String[] args) { Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); } public Foo() { i++; s++; } } A. f3.i is 1 f3.s is 1 B. f3.i is 1 f3.s is 2 C. f3.i is 1 f3.s is 3 D. f3.i is 3 f3.s is 1 E. f3.i is 3 f3.s is 3

C. f3.i is 1 f3.s is 3

9.46 What is the output for the second statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } A. k is 0 B. k is 1 C. k is 2 D. k is 3

C. k is 2

9.36 Suppose you wish to provide an accessor method for a boolean property finished, what should the signature of this method? A. public void getFinished() B. public boolean getFinished() C. public boolean isFinished() D. public void isFinished()

C. public boolean isFinished()

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

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

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'

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

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

C. s1 and s2 have different contents

9.33 Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } A. The variable t is not initialized and therefore causes errors. B. The variable t is private and therefore cannot be accessed in the main method. C. t is non-static and it cannot be referenced in a static context in the main method. D. The variable x is not initialized and therefore causes errors. E. The program compiles and runs fine.

C. t is non-static and it cannot be referenced in a static context in the main method.

9.38 When invoking a method with an object argument, ___________ is passed. A. the contents of the object B. a copy of the object C. the reference of the object D. the object is copied, then the reference of the copied object

C. the reference of the object

9.31 To prevent a class from being instantiated, _____________________ A. don't use any modifiers on the constructor. B. use the public modifier on the constructor. C. use the private modifier on the constructor. D. use the static modifier on the constructor.

C. use the private modifier on the constructor.

9.10 Given the declaration Circle x = new Circle(), which of the following statement is most accurate. A. x contains an int value. B. x contains an object of the Circle type. C. x contains a reference to a Circle object. D. You can assign an int value to x.

C. x contains a reference to a Circle object.

9.42 Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? A. x contains an array of ten int values. B. x contains an array of ten objects of the Circle type. C. x contains a reference to an array and each element in the array can hold a reference to a Circle object. D. x contains a reference to an array and each element in the array can hold a Circle object.

C. x contains a reference to an array and each element in the array can hold a reference to a Circle object.

8.12 What is the output of the following code? public class Test { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[i][1] + " "); } } A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14

D. 2 5 9 13

8.18 What is the output of the following code? public class Test { public static void main(String[] args) { int[][][] data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; System.out.print(data[1][0][0]); } } A. 1 B. 2 C. 4 D. 5 E. 6

D. 5

8.15 What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { System.out.print(m(values[row]) + " "); } } public static int m(int[] list) { int v = list[0]; for (int i = 1; i < list.length; i++) if (v < list[i]) v = list[i]; return v; } } A. 3 33 B. 1 1 C. 5 6 D. 5 33 E. 33 5

D. 5 33

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.

D. All of the above.

9.15 Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.x); } } A. The program has a compile error because System.out.println method cannot be invoked from the constructor. B. The program has a compile error because x has not been initialized. C. The program has a compile error because you cannot create an object from the class that defines the object. D. The program has a compile error because Test does not have a default constructor.

D. The program has a compile error because Test does not have a default constructor.

8.11 What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1 }, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { java.util.Arrays.sort(values[row]); for (int column = 0; column < values[row].length; column++) System.out.print(values[row][column] + " "); System.out.println(); } } } A. The program prints two rows 3 4 5 1 followed by 33 6 1 2 B. The program prints on row 3 4 5 1 33 6 1 2 C. The program prints two rows 3 4 5 1 followed by 2 1 6 33 D. The program prints two rows 1 3 4 5 followed by 1 2 6 33 E. The program prints one row 1 3 4 5 1 2 6 33

D. The program prints two rows 1 3 4 5 followed by 1 2 6 33

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

D. University

8.1 Which of the following statements are correct? A. char[][] charArray = {'a', 'b'}; B. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}}; C. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}}; D. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

D. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

9.21 Variables that are shared by every instance of a class are __________. A. public variables B. private variables C. instance variables D. class variables

D. class variables

9.48 You can declare two variables with the same name in __________. A. a method, one as a formal parameter and the other as a local variable B. a block C. two nested blocks in a method (two nested blocks means one being inside the other) D. different methods in a class

D. different methods in a class

9.45 What is the output for the first statement in the main method? public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); } } A. i + j is 5 B. i + j is 6 C. i + j is 22 D. i + j is 23

D. i + j is 23

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

D. new BigInteger("454");

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

D. s.trim()

9.40 What is the value of times displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println( "myCount.count = " + myCount.count); System.out.println("times = "+ times); } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } A. 101 B. 100 C. 99 D. 98 E. 0

E. 0

8.9 What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); } } A. 1 B. 3 C. 5 D. 6 E. 33

E. 33

9.11 Analyze the following code. public class Test { int x; public Test(String t) { System.out.println("Test"); } public static void main(String[] args) { Test test = null; System.out.println(test.x); } } A. The program has a compile error because test is not initialized. B. The program has a compile error because x has not been initialized. C. The program has a compile error because you cannot create an object from the class that defines the object. D. The program has a compile error because Test does not have a default constructor. E. The program has a runtime NullPointerException because test is null while executing test.x.

E. The program has a runtime NullPointerException because test is null while executing test.x.

8.6 Analyze the following code: public class Test { public static void main(String[] args) { boolean[][] x = new boolean[3][]; x[0] = new boolean[1]; x[1] = new boolean[2]; x[2] = new boolean[3]; System.out.println("x[2][2] is " + x[2][2]); } } A. The program has a compile error because new boolean[3][] is wrong. B. The program has a runtime error because x[2][2] is null. C. The program runs and displays x[2][2] is null. D. The program runs and displays x[2][2] is true. E. The program runs and displays x[2][2] is false.

E. The program runs and displays x[2][2] is false.

9.24 To declare a constant MAX_LENGTH as a member of the class, you write A. final static MAX_LENGTH = 99.98; B. final static float MAX_LENGTH = 99.98; C. static double MAX_LENGTH = 99.98; D. final double MAX_LENGTH = 99.98; E. final static double MAX_LENGTH = 99.98;

E. final static double MAX_LENGTH = 99.98;

8.14 Suppose a method p has the following heading: public static int[][] p() What return statement may be used in p()? A. return 1; B. return {1, 2, 3}; C. return int[]{1, 2, 3}; D. return new int[]{1, 2, 3}; E. return new int[][]{{1, 2, 3}, {2, 4, 5}};

E. return new int[][]{{1, 2, 3}, {2, 4, 5}};


Set pelajaran terkait

Platelet and Coagulation Disorders (Exam 5)

View Set

Great Britain raises taxes S.S. lesson

View Set