Deck 7: Arrays

Full screen (f)
exit full mode
Question
An array declared as an int[] can contain elements of different primitive types.
Use Space or
up arrow
down arrow
to flip the card.
Question
In Java, array indexes begin at 0 and end at one less than the length of the array.
Question
The foreach loop can be used to process each element of an array.
Question
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.
Question
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
Question
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
Question
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.
Question
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
Question
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
Question
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];
Question
Which of the following statements best describes this line of code? 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
Question
It is possible to store 11 elements in an array that is declared in the following way.
int[] array = new int[10];
Question
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
Question
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
Question
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.
Question
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.
Question
There is only one way to declare and initialized an array in Java.
Question
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
Question
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)
Question
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.
Question
It is possible to send in data to a Java program via the command-line.
Question
Write the declaration for an array of doubles called averages that is initialized with an initializer list.
Question
Write a method that takes in an arbitrary number of String objects, and then prints out all of them that have over 10 characters.
Question
Write a method that accepts an array of integers and returns the smallest value in the list.
Question
In Java it is not possible to have arrays of more than two dimensions.
Question
Write a method that takes in at least one integer and returns the largest of all integer parameters sent in.
Question
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.
Question
Write a method called average that accepts an array of floating point numbers and returns their average.
Question
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.
Question
Write a line of code that initializes a two-dimensional array of integers using an initializer list.
Question
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.
Question
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.
Question
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.
Question
An array cannot hold object types.
Question
Write a method that accepts an array of integers as a parameter and returns the sum of all of the integers in the array.
Question
Write a short program that accepts an arbitrary number of command line arguments, and prints out those containing the character 'z'.
Question
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.
Question
Explain how arrays are passed to methods as parameters.
Question
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.
Question
If a program attempts to access an element outside of the range of the array indexes, a run-time error will occur.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 7: Arrays
1
An array declared as an int[] can contain elements of different primitive types.
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.
2
In Java, array indexes begin at 0 and end at one less than the length of the array.
True
Explanation: For an array of length n, the indexes begin at 0 and end at n - 1 in Java.
3
The foreach loop can be used to process each element of an array.
True
Explanation: In Java, an array is an iterator and therefore it is possible to use the foreach loop to process individual elements.
4
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
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];
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following statements best describes this line of code? 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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
It is possible to store 11 elements in an array that is declared in the following way.
int[] array = new int[10];
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
There is only one way to declare and initialized an array in Java.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
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)
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
It is possible to send in data to a Java program via the command-line.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Write the declaration for an array of doubles called averages that is initialized with an initializer list.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
Write a method that takes in an arbitrary number of String objects, and then prints out all of them that have over 10 characters.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Write a method that accepts an array of integers and returns the smallest value in the list.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
In Java it is not possible to have arrays of more than two dimensions.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
Write a method that takes in at least one integer and returns the largest of all integer parameters sent in.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
Write a method called average that accepts an array of floating point numbers and returns their average.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Write a line of code that initializes a two-dimensional array of integers using an initializer list.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
An array cannot hold object types.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Write a method that accepts an array of integers as a parameter and returns the sum of all of the integers in the array.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
Write a short program that accepts an arbitrary number of command line arguments, and prints out those containing the character 'z'.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
Explain how arrays are passed to methods as parameters.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
If a program attempts to access an element outside of the range of the array indexes, a run-time error will occur.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.