CPS180 Chapter 7 Quiz

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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

2 1

int[]list1 = {3,2,1}; int[]list2 = {1,2,3}; list2=list1; list1[0]=0; list1[1]=1; list2[2]=2; for(int i=list2.length-1; i>=0; i--) System.out.print(list2[i] + " ");

2 1 0

int[]myList = {1,2,3,4,5,6}; for(int i=1; i<myList.length; i++) myList[i-1]=myList[i]; for(int e: myList) System.out.print(e + " ");

2 3 4 5 6 6

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is

2.0

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?

2.1, 3.1, 2.5, 6.4, 3.1

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is

3

Assume int[] t = {1, 2, 3, 4}. What is t.length?

4

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

4

How many elements are in array double[] list = new double[5]?

5

If a key is not in the list, the binarySearch method returns

-(insertion point +1)

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

-2

Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]?

-2

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

0 1 2

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

0 1 2

public static void main(String[]args) { int[]x={0,1,2,3,4,5}; xMethod(x,5); } public static void xMethod(int[]x,int length) { for(int i=0; i<length; i++) System.out.print(" "+x[i]); }

0 1 2 3 4

public static void main(String[]args) { 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 + " "); }

1 1 2 3 4 5

for (int i =0; i<args.length; i++) System.out.print(args[i] + " "); What is the output, if you run the program using the following? java Test 1 2 3

1 2 3

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

2

Which correctly creates an array of five empty Strings?

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

The ________ method copies the sourceArray to the targetArray.

System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

int [] list = new int [10]

The array variable list contains a memory address that refers to an array of 10 int values.

int[]lsit = new int[5]; list = new int[6];

The code has compile errors because the variable list cannot be changed once it is assigned.

double[]x = new double[]{1,2,3}; System.out.println("Value is " + x[1]);

The program compiles and runs fine and the output "Value is 2.0" is printed.

double[]x=new double[]{1,2,3}; System.out.println("Value is " + x[1]);

The program compiles and runs fine and the output "Value is 2.0" is printed.

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

The program displays 0 0

int[]a = new int[4]; a[1]=1; a = new int [2]; System.out.println("a[1] is " +a[1]);

The program displays a[1] is 0.

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

The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.

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

The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.

int []x=new int[5]; int i; for(i=0; i<x.length; i++) x[i]=i; System.out.println(x[i]);

The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.

public static void main(String[]args) { int[]x=new int[5]; for(int i=0; i<x.length;i++) x[i]=i; System.out.println(x[i]); }

The program has a syntax error because i is not defined in the last statement in the main method.

public static void main(String[]args) { int[]x = new int[3]; System.out.println("x[0] is " + x[0]); }

The program runs fine and displays x[0] is 0.

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.

heap

What is the correct term for numbers[99]?

indexed variable

The ________ method sorts the array scores of the double[] type.

java.util.Arrays.sort(scores)

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

list1 is 1 2 3 4 5 6

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

list1 is 2.5 3.1, 3.1, 6.4

For the binarySearch method in Section 7.10.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)?

low is 0 and high is 6

Suppose a method p has the following heading: public static int[] p() What return statement may be used in p()?

return new int[]{1, 2, 3};

When you return an array from a method, the method return

the reference of the array

An array variable can be declared and redeclared in the same block.

true

The arguments passed to the main method is an array of strings. If no argument is passed, args.length is 0.

true

The array index of the first element in an array is 0.

true

The arraycopy method does not allocate memory space for the target array. The target array must already be created with memory space allocated.

true

x.length does not exist if x is null.

true

Assume the signature of the method xMethod is as follows. public static void xMethod(double[] a) Which of the following could be used to invoke xMethod?

xMethod(new double[2]);

Do the following programs produce the same result? Program 1: 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 2: 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; }

yes

_________ declares an array of char.

char[]char

Binary search can be applied on an unsorted list.

false

The element in the array must be of a primitive data type.

false

The name length is a method in an array.

false

You can use the operator == to check whether two variables refer to the same array.

false

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

args[2]

Which of the following are valid array declarations?

char[] charArray = new char[26];

What is the representation of the third element in an array called a?

a[2]

An array can be used in which of the following ways?

all of these


Kaugnay na mga set ng pag-aaral

CYSA Personal Test Prep Questions

View Set

EDTP 3304 Behavior Management Quizzes & Kahoot Questions

View Set

Marternity Exit practice questions

View Set

Fr. 3 - La Belgique, Object Pronouns, Stromae

View Set

Chapter 11-Corporate Reporting and Analysis Homework Assignment

View Set