ch_8

Descripción

quiz 8
Joshua Villy
Test por Joshua Villy, actualizado hace más de 1 año
Joshua Villy
Creado por Joshua Villy hace más de 8 años
467
1

Resumen del Recurso

Pregunta 1

Pregunta
What is the representation of the third element in an array called a?
Respuesta
  • a[2]
  • a(2)
  • a[3]
  • a(3)

Pregunta 2

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

Pregunta 3

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

Pregunta 4

Pregunta
How many elements are in array double[] list = new double[5]?
Respuesta
  • 4
  • 5
  • 6
  • 0

Pregunta 5

Pregunta
What is the correct term for numbers[99]?
Respuesta
  • index
  • index variable
  • indexed variable
  • array variable
  • array

Pregunta 6

Pregunta
Analyze the following code. public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0] is " + x[0]); } }
Respuesta
  • The program has a compile error because the size of the array wasn't specified when declaring the array.
  • The program has a runtime error because the array elements are not initialized.
  • The program runs fine and displays x[0] is 0.
  • The program has a runtime error because the array element x[0] is not defined.

Pregunta 7

Pregunta
What would be the result of attempting to compile and run the following code? public class Test { public static void main(String[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]); } }
Respuesta
  • The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.
  • The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};
  • The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};
  • The program compiles and runs fine and the output "Value is 1.0" is printed.
  • The program compiles and runs fine and the output "Value is 2.0" is printed.

Pregunta 8

Pregunta
Assume int[] t = {1, 2, 3, 4}. What is t.length?
Respuesta
  • 0
  • 3
  • 4
  • 5

Pregunta 9

Pregunta
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);
Respuesta
  • 0
  • 1
  • 2
  • 3
  • 4

Pregunta 10

Pregunta
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; int i; for (i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]); } }
Respuesta
  • The program displays 0 1 2 3 4.
  • The program displays 4.
  • The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
  • The program has a compile error because i is not defined in the last statement in the main method.

Pregunta 11

Pregunta
(for-each loop) 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 + " "); } }
Respuesta
  • The program displays 2.5, 3, 4
  • The program displays 2.5 3 4
  • The program displays 2.5 3.0 4.0
  • The program displays 2.5, 3.0 4.0
  • The program has a syntax error because value is undefined.

Pregunta 12

Pregunta
hat is the output of the following code? int[] myList = {1, 2, 3, 4, 5, 6}; for (int i = myList.length - 2; i >= 0; i--) { myList[i + 1] = myList[i]; } for (int e: myList) System.out.print(e + " ");
Respuesta
  • 1 2 3 4 5 6
  • 6 1 2 3 4 5
  • 6 2 3 4 5 1
  • 1 1 2 3 4 5
  • 2 3 4 5 6 1

Pregunta 13

Pregunta
(Tricky) 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] + " "); } }
Respuesta
  • 120 200 16
  • 120 200 14
  • 120 200 20
  • 016 is a compile error. It should be written as 16.

Pregunta 14

Pregunta
What is 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] + " "); } }
Respuesta
  • 1 2 3 4 5 6
  • 2 3 4 5 6 6
  • 2 3 4 5 6 1
  • 1 1 1 1 1 1

Pregunta 15

Pregunta
What will be displayed by the following code? class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } }
Respuesta
  • 1 2 3
  • 1 1 1
  • 0 1 2
  • 0 1 3

Pregunta 16

Pregunta
What will be displayed by the following code? class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list1.length; i++) System.out.print(list1[i] + " "); } }
Respuesta
  • 123
  • 111
  • 012
  • 013

Pregunta 17

Pregunta
Analyze the following code: public class Test { 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] + " "); } }
Respuesta
  • The program displays 1 2 3 4
  • The program displays 0 0
  • The program displays 0 0 3 4
  • The program displays 0 0 0 0

Pregunta 18

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

Pregunta 19

Pregunta
Analyze the following code: public class Test { public static void main(String[] args) { final 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] + " "); } }
Respuesta
  • The program displays 1 2 3 4
  • The program displays 0 0
  • The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.
  • The elements in the array x cannot be changed, because x is final.

Pregunta 20

Pregunta
Analyze the following code. int[] list = new int[5]; list = new int[6];
Respuesta
  • The code has compile errors because the variable list cannot be changed once it is assigned.
  • The code has runtime errors because the variable list cannot be changed once it is assigned.
  • The code can compile and run fine. The second line assigns a new array to list.
  • The code has compile errors because you cannot assign a different size array to list.

Pregunta 21

Pregunta
Analyze the following code: public class Test { public static void main(String[] args) { int[] a = new int[4]; a[1] = 1; a = new int[2]; System.out.println("a[1] is " + a[1]); } }
Respuesta
  • The program has a compile error because new int[2] is assigned to a.
  • The program has a runtime error because a[1] is not initialized.
  • The program displays a[1] is 0.
  • The program displays a[1] is 1.

Pregunta 22

Pregunta
The __________ method copies the sourceArray to the targetArray.
Respuesta
  • System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);
  • System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length);
  • System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length);
  • System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Pregunta 23

Pregunta
When you pass an array to a method, the method receives __________.
Respuesta
  • a copy of the array
  • a copy of the first element
  • the reference of the array
  • the length of the array

Pregunta 24

Pregunta
Show the output of the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; increase(x); int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]); } public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++; } public static void increase(int y) { y++; } }
Respuesta
  • 0 0
  • 1 1
  • 2 2
  • 2 1
  • 1 2

Pregunta 25

Pregunta
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; } }
Respuesta
  • yes
  • no

Pregunta 26

Pregunta
Analyze the following code: 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; } }
Respuesta
  • The program displays 1 2 3 4 5.
  • The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.
  • The program displays 5 4 3 2 1.
  • The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

Pregunta 27

Pregunta
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); } }
Respuesta
  • The program has a compile error because xMethod(new double[]{3, 3}) is incorrect.
  • The program has a compile error because xMethod(new double[5]) is incorrect.
  • The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.
  • The program has a runtime error because a is null.

Pregunta 28

Pregunta
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.
Respuesta
  • stack
  • heap
  • memory block
  • dynamic memory

Pregunta 29

Pregunta
When you return an array from a method, the method returns __________.
Respuesta
  • a copy of the array
  • a copy of the first element
  • the reference of the array
  • the length of the array

Pregunta 30

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

Pregunta 31

Pregunta
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);
Respuesta
  • list1 is 1 2 3 4 5 6
  • list1 is 6 5 4 3 2 1
  • list1 is 0 0 0 0 0 0
  • list1 is 6 6 6 6 6 6

Pregunta 32

Pregunta
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);
Respuesta
  • list1 is 1 2 3 4 5 6
  • list1 is 6 5 4 3 2 1
  • list1 is 0 0 0 0 0 0
  • list1 is 6 6 6 6 6 6

Pregunta 33

Pregunta
For the binarySearch method in Section 6.9.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)?
Respuesta
  • low is 0 and high is 6
  • low is 0 and high is 3
  • low is 3 and high is 6
  • low is 4 and high is 6
  • low is 0 and high is 5

Pregunta 34

Pregunta
If a key is not in the list, the binarySearch method returns _________.
Respuesta
  • insertion point
  • insertion point - 1
  • -(insertion point + 1)
  • -insertion point

Pregunta 35

Pregunta
Use the selectionSort method presented in this section to answer this question. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method?
Respuesta
  • 3.1, 3.1, 2.5, 6.4, 2.1
  • 2.5, 3.1, 3.1, 6.4, 2.1
  • 2.1, 2.5, 3.1, 3.1, 6.4
  • 3.1, 3.1, 2.5, 2.1, 6.4
  • 2.1, 3.1, 2.5, 6.4, 3.1

Pregunta 36

Pregunta
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);
Respuesta
  • list1 is 3.1, 3.1, 2.5, 6.4
  • list1 is 2.5 3.1, 3.1, 6.4
  • list1 is 6.4, 3.1, 3.1, 2.5
  • list1 is 3.1, 2.5, 3.1, 6.4

Pregunta 37

Pregunta
The __________ method sorts the array scores of the double[] type.
Respuesta
  • java.util.Arrays(scores)
  • java.util.Arrays.sorts(scores)
  • java.util.Arrays.sort(scores)
  • Njava.util.Arrays.sortArray(scores)

Pregunta 38

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

Pregunta 39

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

Pregunta 40

Pregunta
Assume int[] list1 = {3, 4, 1, 9, 13}, int[] list2 = {3, 4, 1, 9, 13}, and int[] list3 = {4, 3, 1, 9, 13}, what is System.out.println(java.util.Arrays.equals(list1, list2) + " " + java.util.Arrays.equals(list1, list3));
Respuesta
  • true true
  • true false
  • false true
  • false fasle

Pregunta 41

Pregunta
Assume int[] scores = {3, 4, 1, 9, 13}, which of the following statement displays all the element values in the array?
Respuesta
  • System.out.println(scores);
  • System.out.println(scores.toString());
  • System.out.println(java.util.Arrays.toString(scores));
  • System.out.println(scores[0]);

Pregunta 42

Pregunta
Which of the following is incorrect?
Respuesta
  • int a[] = new int[2];
  • int[] a = new int(2);
  • int a = new int[2];
  • int a() = new int[2];
  • int[] a = new int[2];

Pregunta 43

Pregunta
Suppose int i = 5, which of the following can be used as an index for array double[] t = new double[100]?
Respuesta
  • i
  • (int)(Math.random() * 100))
  • i + 10
  • i + 6.5
  • Math.random() * 100

Pregunta 44

Pregunta
Which of the following statements is valid?
Respuesta
  • int i = new int(30);
  • double d[] = new double[30];
  • int[] i = {3, 4, 3, 2};
  • char[] c = new char();
  • char[] c = new char[4]{'a', 'b', 'c', 'd'};

Pregunta 45

Pregunta
How can you initialize an array of two characters to 'a' and 'b'?
Respuesta
  • char[] charArray = new char[2]; charArray = {'a', 'b'};
  • char[2] charArray = {'a', 'b'};
  • char[] charArray = {'a', 'b'};
  • char[] charArray = new char[]{'a', 'b'};

Pregunta 46

Pregunta
Which of the following declarations are correct?
Respuesta
  • public static void print(String... strings, double... numbers)
  • public static void print(double... numbers, String name)
  • public static double... print(double d1, double d2)
  • public static void print(double... numbers)
  • public static void print(int n, double... numbers)

Pregunta 47

Pregunta
Which of the following statements are correct to invoke the printMax method in Listing 6.5 in the textbook?
Respuesta
  • printMax(1, 2, 2, 1, 4);
  • printMax(new double[]{1, 2, 3});
  • printMax(1.0, 2.0, 2.0, 1.0, 4.0);
  • printMax(new int[]{1, 2, 3});
Mostrar resumen completo Ocultar resumen completo

Similar

Translations and transformations of functions
Christine Laurich
Java Week 5 Object Oriented Programming
Troy Bowlin
Java Practice 2
Ummm No
Waves
kate.siena
Computer science quiz
Ryan Barton
CCNA Security 210-260 IINS - Exam 1
Mike M
CCNA Security 210-260 IINS - Exam 2
Mike M
AQA Biology B1 Questions
Bella Statham
AQA Biology B2 Questions
Bella Statham
AQA Physics P1 Quiz
Bella Statham
GCSE AQA Biology 1 Quiz
Lilac Potato