Question
1) Which of the following would correctly declare and instantiate an array of 6 ints? Group of answer choices int[6] values = new int[6]; int[5]
1) Which of the following would correctly declare and instantiate an array of 6 ints?
Group of answer choices
int[6] values = new int[6];
int[5] values = new int[];
int[] values = new int[6];
int[] values = int[5];
none of these
int[] values = int[4];
int[] values = new int[4];
int[] values = int[6];
int[5] values = new int[5];
int[4] values = new int[];
int[] values = new int[5];
int[6] values = new int[];
int[4] values = new int[4];
2)
Given the following declaration:
int[] values = {31,69,75,77,36,64,49,86,21,99};
Evaluate the following expression:
values[9]
3)
Given the following declaration:
int[] values={9,7,8,3,5,5,2,5,1,9};
Evaluate the following expression:
values[values[5]]
4)
Which of the following loops would cause an ArrayIndexOutOfBoundsException? Choose all that apply
Group of answer choices
for (int j = values.length - 1; j >= 0; j--) values[j]++;
for (int j = 1; j < values.length - 1; j++) values[j]++;
for (int j = 1; j < values.length; j++) values[j]++;
for (int j = 0; j <= values.length; j++) values[j]++;
for (int j = values.length - 1; j > 0; j--) values[j]++;
for (int j = values.length; j >= 0; j--) values[j]++;
for (int j = 0; j < values.length - 1; j++) values[j]++;
for (int j = values.length; j > 0; j--) values[j]++;
none of these
5)
What is the output of this Java program?
class Driver { public static void main(String[] args) { int[] x = {6,8,9,6,7}; int n = x.length; for(int j = n-1; j > 0; j--) x[ j ] = x[ j-1 ]; for(int j = 2; j < n; j++) System.out.print(x[ j ]); } }
6)
What is the result of executing this code snippet?
int[] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); }
Group of answer choices
The code snippet does not give any output.
The code snippet displays all the marks stored in the array without any redundancy.
The code snippet causes a bounds error.
The code snippet executes an infinite loop.
7)
Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable.
String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); }
Group of answer choices
for (String str : arr)
for (str[] : arr)
for (arr[] : str)
for (str : String arr)
8)
Which code snippet calculates the sum of all the even elements in an array values?
Group of answer choices
int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum++; } }
int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum += values[i]; } }
int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } }
int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum++; } }
9)
Which one of the following statements is true about passing arrays to a method?
Group of answer choices
Arrays are passed only if size is specified as another argument
By default, arrays are passed by value to a method
By default, arrays are passed by reference to a method
Arrays, when updated in a called method, are not reflected to the calling method
10)
Which statements about array algorithms are true?
I. The array algorithms are building blocks for many programs that process arrays
II. Java contains ready-made array algorithms for every problem situation
III. It is inefficient to make multiple passes through an array if you can do everything in one pass
Group of answer choices
II, III
I, II, III
I, II
I, III
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