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.
Create a free account and access millions of resources
Similar Resources on Wayground
15 questions
2D Arrays

Quiz
•
9th - 12th Grade
10 questions
Introduction to Arrays

Quiz
•
KG - University
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
10 questions
AP CS A Unit 6 Quiz PRACTICE

Quiz
•
9th - 12th Grade
14 questions
Taylor Chess Quiz

Quiz
•
4th - 12th Grade
Popular Resources on Wayground
50 questions
Trivia 7/25

Quiz
•
12th Grade
11 questions
Standard Response Protocol

Quiz
•
6th - 8th Grade
11 questions
Negative Exponents

Quiz
•
7th - 8th Grade
12 questions
Exponent Expressions

Quiz
•
6th Grade
4 questions
Exit Ticket 7/29

Quiz
•
8th Grade
20 questions
Subject-Verb Agreement

Quiz
•
9th Grade
20 questions
One Step Equations All Operations

Quiz
•
6th - 7th Grade
18 questions
"A Quilt of a Country"

Quiz
•
9th Grade