Problems 1 – 3 refer to the following class meant to represent the spinner in a
ID: 3738692 • Letter: P
Question
Problems 1 – 3 refer to the following class meant to represent the spinner in a board game:
class Spinner {
private int numOptions;
private int chosen;
public Spinner (int n) {
if (n >= 2 && n <= 20) {
numOptions = n;
}
spin ();
}
public void spin () {
chosen = (int)(Math.random () * numOptions) + 1;
}
}
Consider the following code:
Spinner s = new Spinner(17);
System.out.println(s);
The intention is for this code to print the value stored in chosen. For this to work correctly which of the following must be true?
The variable chosen must be declared as private.
A toString method must be added.
An equals method must be added.
A mutator for the variable method for chosen must be added.
The variable chosen must be declared as public.
Question 2
Which of the following would be correct declarations for a default constructor?
I.
public Spinner() {
this(2);
}
II.
public Spinner() {
numOptions = n;
}
III.
public Spinner() {
numOptions = 2;
spin ();
}
III only
I only
II only
I, II and III
I and III only
Question 3
Which of the following is a correct declaration for a Spinner object as written?
Spinner s = Spinner (15);
Spinner s = new Spinner(15);
new Spinner s = Spinner(15);
Spinner s = 20;
Spinner = s;
Question 4
Which of the following is true about classes and objects?
A class is an instance of an object.
Objects can be declared abstract.
Objects have a reference pointing to their location in memory.
It is possible to have many classes of the same object.
Classes have a reference pointing to their location in memory.
Question 5
Consider the following class declarations:
public abstract class Thing {
private int x;
public abstract void one();
public void two () {
/* Code not shown */
}
/* Code not shown */
}
public class Item extends Thing {
/* Code not shown */
}
To instantiate an object of type Item which of the following must be true?
Item must contain a method named two.
The method two in Thing should be declared abstract.
Item must contain a variable named x.
The variable x in Thing should be declared abstract.
Item must contain a method named one.
Question 6
Consider the following code:
String vocabulary[] = new String [50];
The index of the first value is ______ and the last index is ______.
0 50
1 50
0 49
1 49
1 51
Question 7
Consider the following code:
int[] a = {64 , 66 , 67 , 37 , 73 , 70 , 95 , 52 , 81 , 82};
for(int i =0; i < a.length; i++) {
a[i] = a[i] /10;
}
for(int i =0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
What is output?
4 6 7 7 3 0 5 2 1 2
2 4 5 2 2 8 6 8 1 2
5 7 8 8 4 1 6 3 2 3
7 7 7 4 8 8 10 6 9 9
6 6 6 3 7 7 9 5 8 8
Question 8
When using ______ search, the array does not need to be ordered.
insertion
binary
merge
linear
selection
Question 9
Which of the following is NOT true about ArrayList objects?
ArrayList can only hold one data type.
They can be resized.
ArrayList is a class data type.
Methods for insertion and deletion are built into the class.
ArrayList can hold many data types at once.
Question 10
Which of the following implements the Comparable interface?
List
Math
Object
String
ArrayList
Question 11
Consider the following code segment:
ArrayList <String> list = new ArrayList <String> ();
list.add ("Cookies");
list.add ("nachos");
list.add ("chips");
list.add ("Trail mix");
list.add ("Celery");
for (String s: list) {
if (s.toUpperCase().charAt(0) == s.charAt(0)) {
System.out.print(s +" ");
}
}
What is printed as a result of executing the code segment?
nachos chips
Cookies chips Celery
Cookies nachos chips Trail mix Celery
Cookies Trail mix Celery
Nothing is printed
Question 12
What is output by the following code segment? Assume a Time class exists.
Time appointment1 = new Time(5, 45);
Time appointment2 = new Time(5, 45);
if (appointment1 == appointment2) {
System.out.print("equal");
} else {
System.out.print("not equal");
}
not equalequal
equalnot equal
not equal
Nothing, there is an error
equal
Question 13
Consider the following method intended to swap the first and last rows in a two-dimensional array:
public static void swapRow (int[][] a) {
/* missing code */
}
Which of the following correctly replaces /* missing code */?
None of the answers listed.
for (int k = 0; k < a[0].length; k++) {
int last = a.length;
int temp = a [0][k];
a[0][k] = a[last][k];
a[last][k] = temp;
}
for (int k = 0; k < a[0].length; k++) {
int last = a.length - 1;
int temp = a [0][k];
a[0][k] = a[last][k];
a[last][k] = temp;
}
for (int k = 0; k < a[0].length; k++) {
int last = a.length;
a[0][k] = a[last][k];
a[last][k] = a [0][k];
}
for (int k = 0; k < a[0].length; k++) {
int last = a.length - 1;
a[0][k] = a[last][k];
a[last][k] = a [0][k];
}
Question 14
Consider the following method declaration.
public static void increment(int[][] a) {
for (int r = 0; r < a.length; r++) {
for (int c = 0; c < a[0].length; c++) {
if (a[r][c] >= 0) {
a[r][c]++;
} else {
a[r][c]--;
}
}
}
}
Assume the 2D array, matrix, has been initialized to the following values:
4 6 -15
5 11 21
-11 -10 3
4 -10 -18
-21 14 -23
What is the value of matrix after the method call, increment(matrix);?
5 7 -14
6 12 22
-10 -9 4
5 -9 -17
-20 15 -22
5 7 -15
6 12 22
-11 -10 4
5 -10 -18
-21 15 -23
5 7 -16
6 12 22
-12 -11 4
5 -11 -19
-22 15 -24
4 6 -16
5 11 21
-12 -11 3
4 -11 -19
-22 14 -24
4 6 -15
5 11 21
-11 -10 3
4 -10 -18
-21 14 -23
Question 15
Consider the following code.
int[][] matrix = new int[4][5];
Suppose we want to initialize matrix to the following rows and columns.
0 1 2 3 4
4 3 2 1 0
0 1 2 3 4
4 3 2 1 0
Which of the options below correctly initializes matrix?
I.
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][j] = j;
}
}
II.
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][matrix[i].length - j - 1] = j;
}
}
III.
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][matrix[i].length - j - 1] = i;
}
}
I only
II and III only
I, II and III
II only
III only
Question 16
Consider the following code:
int[][] grid = /* code not shown */;
Which of the following could be used to calculate how many cells are in the array?
grid.length[0] * grid[0].length
grid.length * grid[0].length
grid.length * grid.length
grid[0].length
grid.length
Question 17
Consider the following declaration for a two-dimensional array.
int[][] grid = new int[3][5];
int c = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = c;
c++;
}
}
What element is displayed when the following line of code is executed?
System.out.println(grid[2][1]);
6
7
11
10
12
Question 18
What does the following segment of code do?
int[][] a = /* initialization not shown */;
int sum = 0;
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[0].length; j++) {
if(i % 2 == 0) {
sum += a[i][j];
}
}
}
It finds the sum of every other element in the array.
It finds the sum of the elements in the even rows in the array.
It finds the sum of the elements in the even columns in the array.
It finds the sum of all elements in the array.
It finds the sum of the even elements in the array.
Question 19
Which option best describes what the following algorithm does?
int a [][] = /* initialization not shown */;
int j = 2;
for (int i = 0; i < a[0].length; i++) {
int temp = a[j + 1][i];
a[j + 1][i] = a[j][i];
a[j][i] = temp;
}
Swaps columns 1 and 2.
Swaps columns 2 and 3.
Swaps rows 1 and 2.
Initializes the values in the array.
Swaps rows 2 and 3.
Question 20
You need a method to find the maximum value in every row of an array. Which of the following methods works as intended?
I.
public static int[] findMaxList (int a[][]) {
int temp [] = new int [a.length];
for(int i = 0; i < a.length; i++)
{
int max = a[i][0];
for(int j = 0; j < a[0].length; j++) {
if (a[i][j] > max) {
max = a[0][j];
}
}
temp [i] = max;
}
return temp;
}
II.
public static int[] findMaxList (int a[][]) {
int temp [] = new int [a.length];
for(int i = 0; i < a.length; i++) {
int max = a[i][0];
for(int j =0; j < a[0].length; j++) {
if (a[i][j] > max) {
max = a[i][j];
}
}
temp [i] = max;
}
return temp;
}
III.
public static int[] findMaxList (int a[][]) {
int temp [] = new int [a.length];
for(int i = 0; i < a.length; i++) {
int max = a[i][0];
for(int j = 0; j < a[0].length; j++) {
if(a[i][j] > a[0][j]) {
max = a[i][j];
}
}
temp [i] = max;
}
return temp;
}
II only
I, II and III
III only
I only
II and III only
Explanation / Answer
Ans 1 :-
class Spinner {
private int numOptions;
private int chosen;
public Spinner (int n) {
if (n >= 2 && n <= 20) {
numOptions = n;
}
spin ();
}
public void spin () {
chosen = (int)(Math.random () * numOptions) + 1;
}
public static void main(String[] ar){
Spinner s = new Spinner(17);
System.out.println(s.chosen); //access the variable as this way
}
}
output :- 15
The variable chosen must be declared as public.
Ans 2 :-
I.
public Spinner() {
this(2);
}
I only
Ans 3 :-
Spinner s = new Spinner(15);
Ans 4 :-
Objects have a reference pointing to their location in memory.
Ans 5 :-
public abstract class Thing {
private int x;
public abstract void one();
public void two () {
/* Code not shown */
}
/* Code not shown */
}
public class Item extends Thing {
/* Code not shown */
public void one(){
}
public static void main(String ar[]){
Item t = new Item();
}
}
Item must contain a method named one.
Ans 6 :-
String vocabulary[] = new String [50];
The index of the first value is ______ and the last index is ______.
0 49
Ans 7 :-
6 6 6 3 7 7 9 5 8 8
Ans 8 :-
When using ______ search, the array does not need to be ordered.
Linear
Ans 9 :-
Which of the following is NOT true about ArrayList objects?
ArrayList can only hold one data type.
Ans 10 :-
Which of the following implements the Comparable interface?
List
Ans 11 :-
Cookies Trail mix Celery
Ans 12 :-
class Time{
public Time(int x,int y){
}
public static void main(String[] ar){
Time appointment1 = new Time(5, 45);
Time appointment2 = new Time(5, 45);
if (appointment1 == appointment2) {
System.out.print("equal");
} else {
System.out.print("not equal");
}
}
}
output :-
not equal
Ans 13 :-
for (int k = 0; k < a[0].length; k++) {
int last = a.length - 1;
int temp = a [0][k];
a[0][k] = a[last][k];
a[last][k] = temp;
}
and
for (int k = 0; k < a[0].length; k++) {
int last = a.length - 1;
a[0][k] = a[last][k];
a[last][k] = a [0][k];
}
replace the code
Ans 14 :-
5 7 -16
6 12 22
-12 -11 4
5 -11 -19
-22 15 -24
Ans 15 :-
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][matrix[i].length - j - 1] = j;
}
}
II only
Ans 16 :-
grid.length * grid[0].length
output :- 15
Ans 17 :-
output :- 11
Ans 18 :-
It finds the sum of the elements in the even rows in the array.
Ans 19 :-
Swaps rows 2 and 3.
Ans 20 :-
I, II and III
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.