Question
1. Suppose the following array and integer are declared: int[] numbers = {4, 5, 6, 7}; int index = 1 + 6 % 2 *
1.
Suppose the following array and integer are declared: int[] numbers = {4, 5, 6, 7}; int index = 1 + 6 % 2 * 2; What is the value of numbers[index]? (2 points)
An ArrayIndexOutOfBoundsException occurs. | |
4 | |
5 | |
6 | |
7 |
2.
Assume that you have an array of integers named arr. Which of these code segments print the same results? (1 point)
- int i = 0; while (i < arr.length) { System.out.println(arr[i]); i++; }
- int i; for (i = 0; i <= arr.length; i++) { System.out.println(arr[i]); }
- for (int i : arr) { System.out.println(i); }
I and II only | |
II and III only | |
I and III only | |
All three print the same results | |
All three print different results |
3.
Consider the following definitions: public boolean someMethod (int[] list, int value) { int counter; boolean flag = false; for (counter = 0; counter < list.length; counter++) { flag = (list[counter] != value); } return flag; } Under which of the following conditions must the method above return true? (1 point)
Under all conditions | |
Under the condition that value == list[list.length 1] | |
Under the condition that value != list[list.length 1] | |
Under the condition that value != list[i] for all i such that 0 <= i < list.length | |
Under no conditions |
4.
Consider the expression !((x <= y) || (y > 0)). It is equivalent to which of the following expressions? (2 points)
- !(x <= y) || !(y > 0)
- !(x <= y) && !(y > 0)
- (x > y) && (y <= 0)
I only | |
II only | |
III only | |
I and III only | |
II and III only |
5.
What is output by the following code? (2 points) ArrayList < Integer > a = new ArrayList < Integer >(); ArrayList b = a; a.add(4); b.add(5); a.add(5); b.add(7); System.out.println(b.get(0));
An IndexOutOfBoundsException occurs. | |
4 | |
5 | |
6 | |
7 |
6.
What is output by the following code? (2 points) ArrayList < Integer > a = new ArrayList < Integer >(); ArrayList b = a; a.add(4); b.add(5); a.add(6); b.add(7); System.out.println(b.get(2));
An IndexOutOfBoundsException occurs. | |
4 | |
5 | |
6 | |
7 |
7.
What is the value of vals[2][1]? (1 point) double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}};
1.3 | |
3.3 | |
3.5 | |
5.3 |
8.
What is the value of vals[4][1]? (1 point) double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}};
1.1 | |
7.1 | |
7.3 | |
An ArrayIndexOutOfBoundsException error |
9.
What is the output of the following code? (1 point) ArrayList
An ArrayIndexOutOfBoundsException occurs | |
An IndexOutOfBoundsException occurs | |
92 | |
95 | |
83 |
10.
What is output by the following code fragment? (1 point) String[] fruits = { "apple", "banana", "peach", "strawberry" }; String str = "a"; for (String item : fruits) { str += item.substring(3, 4); } System.out.println(str);
apnar | |
aplnaacaw | |
alleanchaw | |
alaca | |
appbaeast |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started