Consider a three-dimensional array
int[][][] numbers = new int[10][10][10].
If this array were to be looped through using for-each loops, what type of variable must be declared in the outermost loop?
Advanced Java Study Guide
Quiz
•
Computers
•
11th - 12th Grade
•
Hard
Joshua Donato
Used 8K+ times
FREE Resource
10 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider a three-dimensional array
int[][][] numbers = new int[10][10][10].
If this array were to be looped through using for-each loops, what type of variable must be declared in the outermost loop?
int
int[]
int[][]
int[][][]
Answer explanation
A three-dimensional array in Java is actually an array of two-dimensional arrays. Therefore, the outermost for-each loop must iterate through these two-dimensional arrays which are of type int[][].
2.
FILL IN THE BLANK QUESTION
1 min • 1 pt
What keyword in Java is used to determine if an Object is a certain class?
Answer explanation
In Java, the instanceof keyword is used to test if an object is of a given type. It is also known as type comparison operator because it compares the instance of an Object with a type.
3.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider two classes class ChessPiece and class King extends ChessPiece. Suppose King has a method getChecked() that is not found in the super class.
Which of the following can be used with ChessPiece cp to access the getChecked() method?
((King)cp).getChecked()
(King)(cp).getChecked()
(King)cp.getChecked()
((King)cp.getChecked())
Answer explanation
The ChessPiece cp must be typecast to King in order to access methods unique to King. In Java, the syntax for this is ((King)cp).getChecked().
4.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider a two-dimensional array
int[][] grid = new int[8][8]. Which of these for loops will iterate only through the elements on the diagonal from grid[5][3] to grid[7][1]?
for(int i = 5, j = 3; i <= 7 && j >= 1; i++, j--)
for(int i = 5; i<= 7; i++)
for(int j = 3; j >= 1; j--)
for(int i = 5, j = 3; i <= 7 || j >= 1; i++, j--)
for(int i = 5 && j = 3; i <= 7 && j >= 1; i++ && j--)
Answer explanation
To do a for loop in two variables, use comma-separated variable declarations, combine terminating conditions with the && operator, and comma-separate the incrementing commands. In this case, the syntax is
for(int i = 5, j = 3; i <= 7 && j >= 1; i++, j--).
5.
FILL IN THE BLANK QUESTION
1 min • 1 pt
What function is used to add the contents of an ArrayList to another ArrayList?
Answer explanation
An arbitrary number of elements of a Collection such as an ArrayList can be added to another ArrayList using the addAll() function.
6.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider an array
int[][] grid = new int[8][8]. Given two arbitary indices x and y, which of the following for loops will iterate through the 3x3 region centered on (x,y) without ever throwing an ArrayIndexOutOfBoundsException?
for(int i = Math.max(0,x-1); i <= Math.min(7,x+1); i++)
for(int j = Math.max(0,y-1); j <= Math.min(7,y+1); j++)
for(int i = Math.max(0,x-1), j = Math.max(0,y-1); i <= Math.min(7,x+1) && j <= Math.min(7,y+1); i++, j++)
for(int i = Math.min(0,x-1); i <= Math.max(7,x+1); i++)
for(int j = Math.min(0,y-1); j <= Math.max(7,y+1); j++)
for(int i = Math.min(0,x-1), j = Math.min(0,y-1); i <= Math.max(7,x+1) && j <= Math.max(7,y+1); i++, j++)
Answer explanation
To prevent the throwing of an ArrayIndexOutOfBoundsException, the outer loop should iterate starting at either 0 or x-1, whichever is greater. Likewise, it should stop iterating at either 7 or x+1, whichever is smaller. The inner loop should be bounded similarly but with y. Because we want the entire 3x3 region and not just the diagonal, we should use two nested for loops. With all of that in mind, the correct code is:
for(int i = Math.max(0,x-1); i <= Math.min(7,x+1); i++)
for(int j = Math.max(0,y-1); j <= Math.min(7,y+1); j++)
7.
MULTIPLE SELECT QUESTION
45 sec • 1 pt
Which of the following is best refactored by moving it to a separate function?
Select all that apply.
Identical code in different for loops.
Identical code in different classes.
An important part of an existing function.
Code within nested if statements that have no else statements.
Answer explanation
Repeated code such as identical code in different for loops or identical code in different classes with only small differences should be moved to its own function. Code which is an important part of an existing function is already in its own function and doesn't need to be moved. Nested if statements with no else staements can be simplified to a single if statement using the && operator, but doesn't need to go in its own function.
12 questions
Arrays Intro
Quiz
•
9th - 12th Grade
10 questions
AP CSA Unit 1 & 2 Review
Quiz
•
9th - 12th Grade
15 questions
Latihan UPS2
Quiz
•
5th - 12th Grade
10 questions
Algoritmo y estrucutura de datos S4
Quiz
•
12th Grade
10 questions
Google Drive Knowledge Check
Quiz
•
10th - 12th Grade
15 questions
Java Increment Quiz
Quiz
•
11th Grade
12 questions
Compound Operators in Java
Quiz
•
9th - 12th Grade
10 questions
C++ Programming Quiz
Quiz
•
12th Grade
15 questions
Character Analysis
Quiz
•
4th Grade
17 questions
Chapter 12 - Doing the Right Thing
Quiz
•
9th - 12th Grade
10 questions
American Flag
Quiz
•
1st - 2nd Grade
20 questions
Reading Comprehension
Quiz
•
5th Grade
30 questions
Linear Inequalities
Quiz
•
9th - 12th Grade
20 questions
Types of Credit
Quiz
•
9th - 12th Grade
18 questions
Full S.T.E.A.M. Ahead Summer Academy Pre-Test 24-25
Quiz
•
5th Grade
14 questions
Misplaced and Dangling Modifiers
Quiz
•
6th - 8th Grade
17 questions
Chapter 12 - Doing the Right Thing
Quiz
•
9th - 12th Grade
30 questions
Linear Inequalities
Quiz
•
9th - 12th Grade
20 questions
Types of Credit
Quiz
•
9th - 12th Grade
20 questions
Taxes
Quiz
•
9th - 12th Grade
17 questions
Parts of Speech
Quiz
•
7th - 12th Grade
20 questions
Chapter 3 - Making a Good Impression
Quiz
•
9th - 12th Grade
20 questions
Inequalities Graphing
Quiz
•
9th - 12th Grade
10 questions
Identifying equations
Quiz
•
KG - University