Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 6

Chapter 6

Arrays

This activity contains 15 questions.

Question 1.

Consider this code fragment. What geometric figure best describes the array that it constructs? Can you make a picture showing its contents?
char ch = 'A';
char[][] ary = new char[5][];

for (int k = 0; k < ary.length; k++) {
    ary[k] = new char[k+1];
    for (int j = 0; j < ary[k].length; j++)
        ary[k][j] = ch++;
}

Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.

What is printed?
public class Array {

	Array() {
		char[] a = { 'a', 'b', 'c', 
					 'd', 'e' };
		change(a);
		System.out.println(new String(a));
		}

	private void change(char[] a) {
		for (int i=0; i

Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.
What is printed?
public class Array {

	Array() {
		char[] chs = { 'a', 'b', 'c',
					   'd', 'e', 'f'
					 };
		String s = new String(chs, 1, 3);
		System.out.println(s);
	}

	public static void main(String[] args) {
		new Array();
	}
}


Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.

What is printed?
public class Array {

	Array() {
		int[] a = create(2047461578);
		System.out.println(evs(a));
		}

	private int evs(int[] a) {
		int n = 0;
		for (int i=0; i 0) {
			nn /= 10;
			++cnt;
		}
		int[] a = new int[cnt];
		for (int i=0; n>0; i++) {
			a[i] = (int) (n % 10);
			n /= 10;
		}
		return a;
	}

	public static void main(String[] args) {
		new Array();
	}
}

Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.

What is printed?
public class Array {

	Array() {
		int[] a = create(24746, 5);
		int[] b = create(35879, 5);
		System.out.print(less(a, b) + " ");
		a = create(247461, 3);
		b = create(358790, 3);
		System.out.println(less(b, a));		
		}

	private int[] create(int n, int lth) {
		int[] a = new int[lth];
		for (int i=0; i= b[i]) {
				isLess = false;
				break;
			}
		return isLess;
	}

	public static void main(String[] args) {
		new Array();
	}
}

Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.

What is printed?
public class Array {

	Array() {
		int[] a = create(5);
		System.out.println(calc(a));
	}

	int[] create(int n) {
		int[] a = new int[n];
		for (int i=0; i

Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.

What is printed?
public class Array {

	Array() {
		int[] a = create(6);
		arrange(a);
		arrange(a);
		System.out.println(a[0] + " " + a[1]
						   + " " + a[2]);
	}

	int[] create(int n) {
		int[] a = new int[n];
		for (int i=0; i
 
End of Question 7


Question 8.

What is printed?
public class Array {

	Array() {
		int[][] a = create(6);
		int i = a.length - 1;
		for (int j=1; j<=i; j++)
			System.out.print(a[i][j] + " ");
		System.out.println();
	}

	int[][] create(int n) {
		int[][] a = new int[n][];
		a[1] = new int[3];
		a[1][1] = 1;
		for (int i=2; i

Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.

What is printed?
public class Array {

	Array() {
		double[] ary = make(7);
		System.out.println(ary[3]);
	}

	double[] make(final int length) {
		double[] ary = new double[length];
		for (int j = 0; j < length; j++)
			ary[j] = j + 3;
		return ary;
	}

	public static void main(String[] args) {
		new Array();
	}
}

Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.
What is printed?
public class Array {

	Array() {
		int[] array = new int[12];
		for (int n=0; n


Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.

What is printed?
public class Array {

	Array() {
		int[] ary = { 3, 1, 4, 5, 9 };
		bump(ary);
		dump(ary);
	}

	void bump(int[] ary) {
		for (int n=0; n

Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.

What is printed?
public class Array {

	Array() {
		int[] ary12 = new int[12];
		int[] ary6 = 
					new int[ary12.length/2];
		for (int n=ary6.length; n>0; n--)
			ary6[n-1] = n + n;
		for (int n=0; n

Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.

Which if statement should be inserted into the code below to make the fragment create the transpose of the 2-D array? A transpose has the property that

Transpose[j][k] = Original[k][j].
int[][] twod = { { 1, 2, 3 },
				 { 4, 5, 6 },
				 { 7, 8, 9 }
			   };

final int LIM = twod.length;
for (int j = 0; j < LIM; j++)
	for (int k = 0; k < LIM; k++)
		if ( ???? ) {
			int x = twod[j][k];
			twod[j][k] = twod[k][j];
			twod[k][j] = x;
		}

Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.
Which of these methods will sort an array of floats into ascending order?


Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.

Which statement is true after this program executes?
public class Array {

	Array() {
		String[] ary = new String[6];
		load(ary);
		dump(ary);
	}

	void load(String[] ary) {
		for (int n=1; n

Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page