CS II Chapter 9 Quiz

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Use the selectionSort method presented in this section to answer this question. What is list1 after executing the following statements? double[] list1 = {3.1, 3.1, 2.5, 6.4}; selectionSort(list1); Answers: A. list1 is 6.4, 3.1, 3.1, 2.5 B. Correct list1 is 2.5 3.1, 3.1, 6.4 C. list1 is 3.1, 2.5, 3.1, 6.4 D. list1 is 3.1, 3.1, 2.5, 6.4

Use the selectionSort method presented in this section to answer this question. What is list1 after executing the following statements? double[] list1 = {3.1, 3.1, 2.5, 6.4}; selectionSort(list1); Selected Answer: B. Correct list1 is 2.5 3.1, 3.1, 6.4 Answers: A. list1 is 6.4, 3.1, 3.1, 2.5 B. Correct list1 is 2.5 3.1, 3.1, 6.4 C. list1 is 3.1, 2.5, 3.1, 6.4 D. list1 is 3.1, 3.1, 2.5, 6.4

6.29 Do the following two programs produce the same result? Program I: public class Test { public static void main(String[] args) { int[] list = {1, 2, 3, 4, 5}; reverse(list); for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } Program II: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } A. Yes B. No

6.29 Do the following two programs produce the same result? Program I: public class Test { public static void main(String[] args) { int[] list = {1, 2, 3, 4, 5}; reverse(list); for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } Program II: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } Correct A. Yes B. No

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

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

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 } 10} A. in line 4 B. in line 8 C. in both line 4 and line 8 D. none

A

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

A

_______ 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

The java.util.Date class is introduced in this section. Analyze the following code and choose the best answer: Which of the following code in 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 and B

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 and C

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 and D

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, B and D

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, B, C and D

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, B, C and D

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, B, C and D

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. An object type property in an immutable object must also be immutable. E. An immutable object contains no mutator methods

A, B, D and E

Analyxe the following code: package Test; class HelloWorld { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } } Answers: Correct The program displays 0 0 The program displays 0 0 0 0 The program displays 1 2 3 4 The program displays 0 0 3 4

Analyxe the following code: package Test; class HelloWorld { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < y.length; i++) System.out.print(y[i] + " "); } } Correct The program displays 0 0 Answers: Correct The program displays 0 0 The program displays 0 0 0 0 The program displays 1 2 3 4 The program displays 0 0 3 4

Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } Answers: A.The program displays 1 2 3 4 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0

Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList; } } Selected Answer: Correct The program displays 1 2 3 4 Answers: Correct A.The program displays 1 2 3 4 B. The program displays 0 0 C. The program displays 0 0 3 4 D. The program displays 0 0 0 0

Analyze the following code: public class Test1 { public static void main(String[] args) { xMethod(new double[]{3, 3}); xMethod(new double[5]); xMethod(new double[3]{1, 2, 3}); } public static void xMethod(double[] a) { System.out.println(a.length); } } A. The program has a compile error because xMethod(new double[]{3, 3}) is incorrect. B. The program has a compile error because xMethod(new double[5]) is incorrect. C. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect. D. The program has a runtime error because a is null.

Analyze the following code: public class Test1 { public static void main(String[] args) { xMethod(new double[]{3, 3}); xMethod(new double[5]); xMethod(new double[3]{1, 2, 3}); } public static void xMethod(double[] a) { System.out.println(a.length); } } A. The program has a compile error because xMethod(new double[]{3, 3}) is incorrect. B. The program has a compile error because xMethod(new double[5]) is incorrect. Correct C. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect. D. The program has a runtime error because a is null.

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return? Answers: 0 -2 -1 2 1

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return? Selected Answer: Correct 2 Answers: 0 -2 -1 Correct 2 1

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

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

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

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 and C

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 and D

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, C, D and E

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

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

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

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

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

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

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

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

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

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

D

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

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

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

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

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

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? Answers: 1 2 1 2 3 3 1

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? Selected Answer: Correct 1 2 3 Answers: 1 2 Correct 1 2 3 3 1

How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2 Answers: args[1] args[3] args[0] args[2]

How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2 Selected Answer: Correct args[2] Answers: args[1] args[3] args[0] Correct args[2]

How can you initialize an array of two characters to 'a' and 'b'? Answers: A. Correct char[] charArray = new char[]{'a', 'b'}; B. Correct char[] charArray = {'a', 'b'}; C. char[2] charArray = {'a', 'b'}; D. char[] charArray = new char[2]; charArray = {'a', 'b'};

How can you initialize an array of two characters to 'a' and 'b'? Selected Answers: A. Correct char[] charArray = {'a', 'b'}; C. Incorrect char[2] charArray = {'a', 'b'}; Answers: A. Correct char[] charArray = new char[]{'a', 'b'}; B. Correct char[] charArray = {'a', 'b'}; C. char[2] charArray = {'a', 'b'}; D. char[] charArray = new char[2]; charArray = {'a', 'b'};

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. Answers: 2.0 5.5 3.5 3.4 undefined

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. Selected Answer: Correct 2.0 Answers: Correct 2.0 5.5 3.5 3.4 undefined

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ________. Answers: 0 1 2 3 4

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is ________. Selected Answer: Correct 3 Answers: 0 1 2 Correct 3 4

Suppose a method p has the following heading: public static int[] p() What return statement may be used in p()? Answers: return {1, 2, 3}; return int[]{1, 2, 3}; return 1; return new int[]{1, 2, 3}

Suppose a method p has the following heading: public static int[] p() What return statement may be used in p()? Selected Answer: Correct return new int[]{1, 2, 3}; Answers: return {1, 2, 3}; return int[]{1, 2, 3}; return 1; Correct return new int[]{1, 2, 3}

Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]? Answers: A. (int)(Math.random() * 100)) B. i C. i + 10 D. Math.random() * 100 E. i + 6.5

Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]? Selected Answers: Correct (int)(Math.random() * 100)) Correct i Correct i + 10 Answers: A. Correct (int)(Math.random() * 100)) B. Correct i C. Correct i + 10 D. Math.random() * 100 E. i + 6.5

The JVM stores the array in an area of memory, called ________, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order. Answers: memory block stack dynamic memory heap

The JVM stores the array in an area of memory, called ________, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order. Selected Answer: Correct heap Answers: memory block stack dynamic memory Correct heap

The ________ method copies the sourceArray to the targetArray. Answers: A. System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length); B. System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length); C. System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); D. System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);

The ________ method copies the sourceArray to the targetArray. A. System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length); B. System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length); C. Correct System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); D. System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);

The reverse method is defined in the textbook. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; list1 = reverse(list1); Answers: list1 is 0 0 0 0 0 0 list1 is 1 2 3 4 5 6 list1 is 6 5 4 3 2 1 list1 is 6 6 6 6 6 6

The reverse method is defined in the textbook. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; list1 = reverse(list1); Selected Answer: Correct list1 is 6 5 4 3 2 1 Answers: list1 is 0 0 0 0 0 0 list1 is 1 2 3 4 5 6 Correct list1 is 6 5 4 3 2 1 list1 is 6 6 6 6 6 6

The reverse method is defined in this section. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); Answers: list1 is 6 6 6 6 6 6 list1 is 0 0 0 0 0 0 list1 is 6 5 4 3 2 1 list1 is 1 2 3 4 5 6

The reverse method is defined in this section. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); Selected Answer: Incorrect list1 is 6 5 4 3 2 1 Answers: list1 is 6 6 6 6 6 6 list1 is 0 0 0 0 0 0 list1 is 6 5 4 3 2 1 Correct list1 is 1 2 3 4 5 6

What is output of the following code: public class Test { public static void main(String[] args) { int[] x = {120, 200, 016}; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } } A. 120 200 20 B. 120 200 14 C. 120 200 16 D. 016 is a compile error. It should be written as 16.

What is output of the following code: public class Test { public static void main(String[] args) { int[] x = {120, 200, 016}; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } }A. 120 200 20 Correct B. 120 200 14 C. 120 200 16 D. 016 is a compile error. It should be written as 16.

What is the output of the following code? public class Test { public static void main(String[] args) { int list[] = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < list.length; i++) list[i] = list[i - 1]; for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); Answers: A. 1 2 3 4 5 6 B. 2 3 4 5 6 6 C. 2 3 4 5 6 1 D. 1 1 1 1 1 1

What is the output of the following code? public class Test { public static void main(String[] args) { int list[] = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < list.length; i++) list[i] = list[i - 1]; for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); ected Answer: Correct 1 1 1 1 1 1 Answers: A. 1 2 3 4 5 6 B. 2 3 4 5 6 6 C. 2 3 4 5 6 1 D. Correct 1 1 1 1 1 1

What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax); Answers: A. 0 B. 1 C. 2 D. 3 E. 4

What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax); Selected Answer: Correct 1 Answers: A. 0 Correct B. 1 C. 2 D. 3 E. 4

When you pass an array to a method, the method receives ________. Answers: the reference of the array the length of the array a copy of the array a copy of the first element

When you pass an array to a method, the method receives ________. Selected Answer: Correct the reference of the array Answers: Correct the reference of the array the length of the array a copy of the array a copy of the first element

When you return an array from a method, the method returns ________. Answers: A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array

When you return an array from a method, the method returns ________. Selected Answer: Correct the reference of the array Answers: A. a copy of the array B. a copy of the first element Correct C. the reference of the array D. the length of the array

Which of the following statements are correct to invoke the printMax method in Listing 7.5 in the textbook? Selected Answers: Answers: A. Correct printMax(1.0, 2.0, 2.0, 1.0, 4.0); B. printMax(new int[]{1, 2, 3}); C. Correct printMax(1, 2, 2, 1, 4); D. Correct printMax(new double[]{1, 2, 3});

Which of the following statements are correct to invoke the printMax method in Listing 7.5 in the textbook? Selected Answers: A. Correct printMax(1.0, 2.0, 2.0, 1.0, 4.0); C. Correct printMax(1, 2, 2, 1, 4); D. Correct printMax(new double[]{1, 2, 3}); Answers: A. Correct printMax(1.0, 2.0, 2.0, 1.0, 4.0); B. printMax(new int[]{1, 2, 3}); C. Correct printMax(1, 2, 2, 1, 4); D. Correct printMax(new double[]{1, 2, 3});

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 argv[] 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.

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 argv[] 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. Correct D. If you run this program without passing any arguments, the program would display argv.length is 0.

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

_________ 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

Which of the following statement is most accurate? A. A reference variable is an object. B. A reference variable refers to an object. C. An object may contain other objects. D. An object may contain the references of other objects.

B and D

How many elements are in array double[] list = new double[5]? Answers: 6 0 4 5

How many elements are in array double[] list = new double[5]? Selected Answer: Correct 5 Answers: 6 0 4 Correct 5

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

B

What is the representation of the third element in an array called a? Answers: a[3] a[2] a(2) a(3)

What is the representation of the third element in an array called a? Selected Answer: Correct a[2] Answers: a[3] Correct a[2] a(2) a(3)

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

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

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

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

Analyze the following code: public class Test { public static void main(String[] args) { double[] x = {2.5, 3, 4}; for (double value: x) System.out.print(value + " "); } } A. The program displays 2.5, 3, 4 B. The program displays 2.5 3 4 C. The program displays 2.5 3.0 4.0 D. The program displays 2.5, 3.0 4.0 E. The program has a syntax error because value is undefined.

Analyze the following code: public class Test { public static void main(String[] args) { double[] x = {2.5, 3, 4}; for (double value: x) System.out.print(value + " "); } } A. The program displays 2.5, 3, 4 B. The program displays 2.5 3 4 Correct C. The program displays 2.5 3.0 4.0 D. The program displays 2.5, 3.0 4.0 E. The program has a syntax error because value is undefined.

Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))? Answers: [1, 20, 30, 40, 50] {1 20 30 40 50} [1 20 30 40 50] {1, 20, 30, 40, 50}

Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))? Selected Answer: Correct [1, 20, 30, 40, 50] Answers: Correct [1, 20, 30, 40, 50] {1 20 30 40 50} [1 20 30 40 50] {1, 20, 30, 40, 50}

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? Answers: -1 -2 1 2 0

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? Selected Answer: Correct -2 Answers: -1 Correct -2 1 2 0

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

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

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

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

C

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

C

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

C

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

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 changes the implementation without changing a class's contract and causes no consequential changes to other code. D. It changes a class's contract without changing the implementation and causes no consequential changes to other code.

C

Correct If a key is not in the list, the binarySearch method returns ________. Answers: -insertion point -(insertion point + 1) insertion point - 1 insertion point

Correct If a key is not in the list, the binarySearch method returns ________. Selected Answer: Correct -(insertion point + 1) Answers: -insertion point Correct -(insertion point + 1) insertion point - 1 insertion point


संबंधित स्टडी सेट्स

BUSMHR 2500 Chapter 12 Informal Risk Capital, Venture Capital, and Going Public

View Set

Module 4: Nutrient Labeling/Content Claim

View Set

Chapter 37: Thyroid and Parathyroid Agents

View Set

Chapter 14: Assessing Skin, Hair, and Nails

View Set

Chapter 23: The French Revolution

View Set

Practice Questions Respiratory, Cardiac and Hematological exam 2

View Set

Ford and Carter Presidencies, Reagan Era and End of the Cold War, Foreign Policy Challenges of the 90s, Domestic and International Terrorism, Contemporary Domestic Issues

View Set