Chapter 7

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write a switch

switch (someNo) { case 0: System.out.println("Hi"); break; }

T/F: An array cannot hold object types.

Answer: False Explanation: An array can be declared to hold references to objects.

T/F: There is only one way to declare and initialized an array in Java.

Answer: False Explanation: There are two different ways to declare an array in Java - the standard syntax and the alternate syntax. Arrays can also be initialized in different ways. The first way is to use the new operator, which will create the array, and then initialize each index individually. You can also initialize an array using an initializer list.

T/F: ) It is possible to send in data to a Java program via the command-line.

Answer: True Explanation: Command-line arguments can be sent in to a Java program. They are sent into the program via the args[] array.

T/F: In Java, array indexes begin at 0 and end at one less than the length of the array.

Answer: True Explanation: For an array of length n, the indexes begin at 0 and end at n - 1 in Java.

T/F: If a program attempts to access an element outside of the range of the array indexes, a run-time error will occur.

Answer: True Explanation: If a program attempts to access an element outside of the range of the array indexes, an ArrayOutOfBoundsException will be thrown at run-time.

T/F: The foreach loop can be used to process each element of an array.

Answer: True Explanation: In Java, an array is an iterator and therefore it is possible to use the foreach loop to process individual elements.

T/F: It is possible for a method to have a variable length parameter list, meaning that the method can take in any number of parameters of a specified data type.

Answer: True Explanation: Java supports variable length parameter lists for methods.

Which of the following is a valid declaration for a two-dimensional array? a) int[][] matrix; b) int[2] matrix; c) int[]** matrix; d) int[] matrix; e) none of these are correct

Answer: a Explanation: Choice a is the only valid declaration for a two-dimensional array. Choices b and c contain invalid Java syntax, and choice d is a valid declaration for a single dimensional array.

Multi-dimensional arrays that contain arrays of different lengths in any one dimension are called _________________. a) ragged arrays b) static arrays c) two-dimensional arrays d) constant arrays e) overloaded arrays

Answer: a Explanation: Ragged arrays are multi-dimensional arrays that contain arrays at the same dimension with differing lengths.

int[] numbers = new int[50]; a) this is the declaration and initialization of an array that holds 50 integers b) this is a declaration and initialization of an array that holds 49 integers c) this is a declaration and initialization of an integer array that will hold integers smaller than 50 d) this is a declaration and initialization of an integer array that will hold integers smaller than 49 e) none of the above is correct

Answer: a Explanation: The code represents the declaration and initialization of an array that will hold 50 integers. The indexes of this array will begin at 0 and end at 49.

6) Which of the following is a true statement? a) Arrays are passed as parameters to methods like primitive types. b) Arrays are passed as parameters to methods like object types. c) Arrays cannot be passed as parameters to methods. d) All of the above are true. e) None of the above are true.

Answer: b Explanation: Arrays are passed to methods by reference. This means that if the content of the array is changed in a method, the change will be reflected in the calling method.

Which of the following lines of code accesses the second element of the first array in a two-dimensional array of integers, numbers, and stores the result in a variable called num? a) num = numbers[1][2]; b) num = numbers[0][1]; c) num = numbers.getElement(1, 2); d) num = numbers.getElement(0, 1); e) none of the above are correct

Answer: b Explanation: Choice b accesses the second element of the first array. Choice a accesses the third element of the second array. Choices c and d do not represent valid Java syntax.

Every Java array is a(n) _________________, so it is possible to use a foreach loop to process each element in the array. a) object b) iterator c) class d) operator e) none of the above

Answer: b Explanation: Every Java array is an iterator, therefore a foreach loop can be used to process each element in the array.

In Java, array indexes always begin at ________________ . a) -1 b) 0 c) 1 d) 2 e) you can declare an array to have any indexes you choose

Answer: b Explanation: In Java, the array indexes are from 0 to one less than the length of the array.

Which of the following method declarations correctly defines a method with a variable length parameter list? a) public int average(int[] list) b) public int average(int ... list) c) public int average(...) d) public int average(int a, int b, int c, ...) e) public int average(integers)

Answer: b Explanation: The only choices with valid syntax are choice a and choice b. Choice a represents a method declaration with a single parameter, which is a reference to an array. Choice b correctly represents a valid declaration for a method with a variable length parameter list.

Suppose we have an array of String objects identified by the variable names. Which of the following for loops will not correctly process each element in the array. a) for(int i = 0; i < names.length; i++) b) for(String name : names) c) for(int i = 0; i < names.length(); i++) d) none of these will correctly process each element e) all of these will correctly process each element

Answer: c Explanation: Choice c will not process each element correctly due to a syntax error. The length variable is not a method and, therefore, does not have parenthesis after it. Choice b is an example of using a foreach loop to process an array, and choice a is a correct for loop.

Which of the following array declarations are invalid? a) int[] grades = new int[5]; b) int grades[] = new int[5]; c) int[] grades = { 91, 83, 42, 100, 77 }; d) all of the above are valid e) none of the above are valid

Answer: d Explanation: All three of these are valid array declarations. Choice b uses an alternate syntax. Choice c uses an initializer list to initialize the array.

Which of the following are true about two-dimensional arrays? a) Two-dimensional integer arrays cannot be initialized via an initializer list. b) Two-dimensional arrays can only store up to 10 elements per array (or per row). c) Two-dimensional arrays are not accessible using for loops. d) Two-dimensional arrays cannot hold objects. e) None of the above are true.

Answer: e Explanation: None of the statements about two-dimensional arrays are true.

Which of the statements is true about the following code snippet? int[] array = new int[25]; array[25] = 2; a) The integer value 2 will be assigned to the last index in the array. b) The integer value 25 will be assigned to the second index in the array. c) The integer value 25 will be assigned to the third value in the array. d) This code will result in a compile-time error. e) This code will result in a run-time error.

Answer: e Explanation: This code will throw an ArrayIndexOutOfBoundsException, since the last index in this array will be 24. This causes a run-time error.

Write a method that accepts an array of integers and returns the smallest value in the list.

public int smallest(int[] list) { int currentSmallest = list[0]; for(int i = 0; i < list.length; i++) if(list[i] < currentSmallest) currentSmallest = list[i]; return currentSmallest; }

Write a method called doubleSize that accepts an integer array as a parameter and returns a reference to a new integer array that is twice as long and contains all of the elements of the first array in the same positions.

public int[] doubleSize(int[] originalArray) { int[] newArray = int[originalArray.length*2]; for(int i = 0; i < originalArray.length; i++) newArray[i] = originalArray[i]; return newArray; }

Write a method that accepts an array of integers as a parameter and returns a reference to an array that contains the even numbers in the array original array. The returned array should have a size equal to the number of even numbers in the original array.

public int[] getEvenArray(int[] numbers) { int size = 0; for(int i = 0; i < numbers.length; i++) if(numbers[i]%2 == 0) size++; int[] evenArray = new int[size]; int evenArrayIndex = 0; for(int i = 0; i < numbers.length; i++) { if(numbers[i]%2 == 0) { evenArray[evenArrayIndex] = numbers[i]; evenArrayIndex++; }//end if }//end for }

Write a method that takes in at least one integer and returns the largest of all integer parameters sent in.

public void largest(int first, int ... numbers) { int currentLargest = first; for(int num : numbers) if(num > currentLargest) currentLargest = num; return currentLargest; }

Write a method that takes in an arbitrary number of String objects, and then prints out all of them that have over 10 characters.

public void printLongStrings(String ... words) { for(String w : words) if(w.length() > 10) System.out.println(w); }

T/F: It is possible to store 11 elements in an array that is declared in the following way. int[] array = new int[10];

Answer: False Explanation: An array declared as above can only store 10 elements.

Which of the following statements will assign the first command-line argument sent into a Java program to a variable called argument? a) argument = System.getFirstArgument(); b) argument = System.getArgument[1]; c) argument = System.getArgument[0]; d) argument = args[0]; e) argument = args[1];

Answer: d Explanation: Choice d is the correct answer. The System object does not have any methods called getFirstArgument or getArgument, and the args array's index starts at 0. Therefore the other choices are incorrect.

What is the precedence of the index operator ( [ ] ) relative to other operators in Java? a) It has the lowest precedence of all Java operators. b) It has a higher precedence than addition, but a lower precedence than multiplication. c) It has a higher precedence than multiplication, but a lower precedence than addition. d) It has the highest precedence of all Java operators. e) [ ] is not an operator.

Answer: d Explanation: The index operator has the highest precedence of all Java operators.

Explain how arrays are passed to methods as parameters.

Answer: Arrays are passed to methods by reference. This means that a reference to the original array is sent into the method. Therefore any changes that are made to the array in the method will be reflected in the original array in the calling method.

T/F: An array declared as an int[] can contain elements of different primitive types.

Answer: False Explanation: An array that has been declared with a specific type may only contain elements of that type. In this case the array can only contain integers.

T/F: In Java it is not possible to have arrays of more than two dimensions.

Answer: False Explanation: It is possible to have arrays of any dimension in Java. It is not common, however, for these types of arrays to be used in object-oriented programs.

What will be the output of the following code snippet? int[] array = new int[25]; System.out.println(array.length); a) 26 b) 24 c) 25 d) This code will result in a compile time error. e) This code will result in a run time error.

Answer: c Explanation: Note that the length instance variable can be accessed directly. Therefore this will not result in any errors. The array has length 25, and therefore the code will output 25.

Write the declaration for an array of doubles called averages that is initialized with an initializer list.

double[] averages = { 25.2, 36.18, 42.1, 30.5 };

Write a loop that cycles through an array of String objects called names and prints out the ones that start with a vowel, one per line. Your loop should use a foreach loop.

for(String n : names) if(n.charAt(0) == 'A' || n.charAt(0) == 'a' || n.charAt(0) == 'E' || n.charAt(0) == 'e' || n.charAt(0) == 'I' || n.charAt(0) == 'i' || n.charAt(0) == 'O' || n.charAt(0) == 'o' || n.charAt(0) == 'U' || n.charAt(0) == 'u') System.out.println(n);

Write a loop that cycles through an array of String objects called names and prints them out, one per line. Your loop should not use a foreach loop.

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

Write the declaration for a two-dimensional array of integers that can be thought of as a table with three rows and three columns. Assign the value 3 to the cell that is in the second row and the third column.

int[][] array = new int[3][3]; array[1][2] = 3;

Write a line of code that initializes a two-dimensional array of integers using an initializer list.

int[][] numbers = { {2,3},{4,5} };

Write a method called containsPair that accepts an array of integers as a parameter and returns true if it contains two integers that are equal.

public boolean containsPair(int[] values) { for(int i = 0; i < values.length; i++) { for(int j = i+1; j < values.length; j++) if(values[i] == values[j]) return true; } return false; }

Write a short program that accepts an arbitrary number of command line arguments, and prints out those containing the character 'z'.

public class PrintZArgs { public static void main(String[] args) { for(String s : args) { for(int i = 0; i < s.length; i++) if(s.charAt(i) == 'z') System.out.println(s); }//end foreach }//end main }//end class

Write a method called average that accepts an array of floating point numbers and returns their average.

public double average(float[] array) { double sum = 0; for(int i = 0; i < array.length; i++) sum += array[i]; return (double) sum/array.length; }

Write a method that accepts an array of integers as a parameter and returns the sum of all of the integers in the array.

public int arraySum(int[] array) { int sum = 0; for(int i = 0; i < array.length; i++) sum += array[i]; return sum; }


Conjuntos de estudio relacionados

Chapter 11 Nutrition and Body Composition Coaching and Assessment

View Set

CSE 12 Quiz 3 Questions Gary Gillespie

View Set

Lecture 1B: Fluids and electrolytes continued: electrolyte disorders

View Set

Macroeconomics : Policy and Effects

View Set

The CE Shop Principles of Real Estate I Exam Review

View Set

Which of these statements best describes absolute rulers? Vocab Unit 4

View Set