[Single Dimensional Arrays]

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

How fo you declare an array reference variable and how do you create an array?

You must declare a variable to reference the array and specify the arrays element type: Syntax: elementType [] arrayRefVar; ex: double [] myList;

7.21 In the following code, what is the output for list1? public 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] + " "); } } A. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3

The correct answer is C

7.2.9 What happens if your program attempts to access an array element with an invalid index?

Array indices run from 0 to array size 1. Accessing the array elements using subscripts beyond the boundaries causes array index out-of-bounds error. The compiler may report a memory access violation.

7.2.3: What is the output of the following code? int x = 30; int[] number = new int[x]; x = 60; System.out.println("x is " + x); System.out.println("the size of number is " + numbers.length);

Output: x is 60 The size of numbers is 30

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

The correct answer is A, B, C

Can elements in a reference variable have different data types? (double, int, Long etc.)

No, all the elements in the array will have the same data type.

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

The correct answer is A

7.13 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); A. 0 B. 1 C. 2 D. 3 E. 4

The correct answer is B

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

The correct answer is B

7.5 How many elements are in array double[] list = new double[5]? A. 4 B. 5 C. 6 D. 0

The correct answer is B

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A. 3.4 B. 2.0 C. 3.5 D. 5.5 E. undefined

The correct answer is B

7.17 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 16 B. 120 200 14 C. 120 200 20 D. 016 is a compile error. It should be written as 16.

The correct answer is B Explanation: 016 is an octal number. The prefix 0 indicates that a number is in octal.

7.19 Which of the following is correct? A. String[] list = new String{"red", "yellow", "green"}; B. String[] list = new String[]{"red", "yellow", "green"}; C. String[] list = {"red", "yellow", "green"}; D. String list = {"red", "yellow", "green"}; E. String list = new String{"red", "yellow", "green"};

The correct answer is B,C

7.12 Assume int[] t = {1, 2, 3, 4}. What is t.length? A. 0 B. 3 C. 4 D. 5

The correct answer is C

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

The correct answer is C

7.20 In the following code, what is the output for list2? public 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] + " "); } } A. 1 2 3 B. 1 1 1 C. 0 1 2 D. 0 1 3

The correct answer is C

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

The correct answer is C

7.6 What is the correct term for numbers[99]? A. index B. index variable C. indexed variable D. array variable E. array

The correct answer is C

7.8 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]); } } A. The program has a compile error because the size of the array wasn't specified when declaring the array. B. The program has a runtime error because the array elements are not initialized. C. The program runs fine and displays x[0] is 0. D. The program has a runtime error because the array element x[0] is not defined.

The correct answer is C

7.14 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]);}} A. The program displays 0 1 2 3 4. B. The program displays 4. C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException. D. The program has a compile error because i is not defined in the last statement in the main method.

The correct answer is C After the for loop i is 5. x[5] is out of bounds.

7.3 Which of the following are incorrect? A. int[] a = new int[2]; B. int a[] = new int[2]; C. int[] a = new int(2); D. int a = new int[2]; E. int a() = new int[2];

The correct answer is C, D, & E

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

The correct answer is CD

7.16 What 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 + " "); A. 1 2 3 4 5 6 B. 6 1 2 3 4 5 C. 6 2 3 4 5 1 D. 1 1 2 3 4 5 E. 2 3 4 5 6 1

The correct answer is D

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

The correct answer is D

7.4 If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________. A. 0 B. 1 C. 2 D. 3 E. 4

The correct answer is D

7.9 Which of the following statements are valid? A. int i = new int(30); B. double d[] = new double[30]; C. int[] i = {3, 4, 3, 2}; D. char[] c = new char(); E. char[] c = new char[4]{'a', 'b', 'c', 'd'};

The correct answer us B, C // e would be corrected if it is char[] c = new char[]{'a', 'b', 'c', 'd'};

When is memory allocated for the array?

The declaration of an array variable does not allocate any space in memory for the array. It only creates a storage location for the reference to an array.

7.2.6 How do you access elements in an array?

The elements in a array can be accessed using its index; each element in a array having its own index. Syntax: arrayRefrenceVariable[index] ex: X = var [1]; //Accessing the second element of the array "var" from variable "X"

7.24 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] + " "); } } A. The program displays 1 2 3 4. B. The program displays 0 0. C. The program has a compile error on the statement x = new int[2], because x is final and cannot be changed. D. The elements in the array x cannot be changed, because x is final.

The value stored in x is final, but the values in the array are not final. x is a constant reference variable that points to an array with four elements Because it is a constant, you cannot create a new reference variable x that points to a different array, but you can change the value of the elements in the array, e.g. x[1] could be changed to 10 instead of 2.

7.2.7 a. What is the array index type? b. What is the lowest index? c. What is the representation of the third element in an array named a?

a. The index type of an array is an Integer (Int). The array size must be specified by an Int values and not a long or short. b. The lowest index of an array is 0. c. a [0] is the representation of the first element a [1] is the representation of the second element a [2] is the representation of the third element

7.2.4 Indicate true or false for the following statements: a. Every element in an array is the same data type b. The array size is fixed after an array reference variable is declared. c. The array size is fixed after it's created. d. The elements in an array must be of primitive data type

a. True- an array contains similar data type elements in continuous memory collection b. False- The array declaration does not dix the size of the array until creating the array. ex. double [] myList; c. True- The size of the array is fixed once the array has been created by using the "new" operator and its variable reference. ex. typeOfElement[]arrayRefrencevariable = new typeOfElement[arraySize] d. False- The elements can be either primitive data type (int, double, long, floating-point) or reference data type (that is, storing references of values)

7.2.5 Which of the following statements are valid? a. int i = new int (30); b. double d[] = new double [30]; c. char [] r =new char (1...30); d. int i[] = (3,4,3,2); e. float f[] = {2.3, 4.5, 6.6}; f. char [] c = new char();

a. invalid- The proper syntax for declaring an array is not correct. It should be int i[] = new int [30]; b. valid- the syntax for declaring an array is correct. c. invalid- The statement is trying to create a character array named "r", but it contains an error since it does not follow proper syntax for array declaration. //it should be: char r[] = new char[30] d. invalid- The int array named i id assigned to values "3,4,3,2" but due to improper syntax it contains an error. //it should be: int i[] = {3,4,3,2}; e. valid- The dynamic float array named "f" is correctly assigned to values "2.3, 4.5, 6.6" f. invalid- The statement is trying to create a character array named "c", but it contains an error since it does not follow proper syntax for array declaration. //it should be: char c [] = new char [30] (i.e we created a variable with size 30)


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

kins 4100: chapter 12 linear forces part 2

View Set

A&P - Nutrition, Metabolism, and Energy Balance

View Set