Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a method that compares the elements of two ArrayLists and returns the v

ID: 3842934 • Letter: C

Question

Consider a method that compares the elements of two ArrayLists and returns the value true if the elements in corresponding positions are equal, and returns false otherwise.

Which of the following code will accomplish this? (2 points)

Question 25 options:

boolean temp = false;
int i;
for (i = 0; i < a.size(); i++) {
if (a.get(i) == b.get(i)) {
temp = true;
}
}
return temp;

int i;
for (i = 0; i < a.size(); i++) {
if (a.get(i) != b.get(i)) {
return false;
} else {
return true;
}
}

int i = 0;
while (i < a.size() && a.get(i) == b.get(i)) {
i++;
}
return (i == a.size());

boolean temp = true;
int i;
for (i = 0; i < a.size(); i++) {
temp = (a.get(i) == b.get(i));
}
return temp;

int i = 0;
while (i < a.size() && a.get(i) == b.get(i)) {
i++;
}
if (i > a.size()) {
return true;
}
return false;

Suppose that you have an ArrayList and that it contains String objects. Which declaration of the ArrayList does not require that objects retrieved using the get method be cast to Strings before calling a Stringmethod? (2 points)

Question 26 options:

Which of the following lines of code could not be used to store a random number between 1 and 6, inclusive, into the variable dice? (2 points)

I. int dice = Math.random(6);
II. int dice = Math.random(6) + 1;
III. int dice = (int)(Math.random() * 6) + 1;

Question 27 options:

Consider the following code:

public class MyClass {
private int secret;

public int getSecret(){
return secret;
}
}

Is the instance variable secret really private, meaning that it cannot be cannot be changed by client code? (2 points)

Question 28 options:

Which of the following boolean expressions correctly determines that String s2 is greater than String s1 in logical order? (2 points)

Question 3 options:

Suppose the following array is declared: int[] numbers = {4, 5, 6, 7}; and the following integer is declared: int index = 1 + 6 % 2 * 2;

What is the value of numbers[index]? (2 points)

Question 4 options:

!((x > y) && (y <= 0)) is not equivalent to which of the following expressions?(2 points)

I. !(x > y) || !(y <= 0)
II. !(x > y) && !(y <= 0)
III. (x <= y) || (y > 0)

Question 5 options:

Polymorphism occurs at: (1 point)

Question 8 options:

1)

boolean temp = false;
int i;
for (i = 0; i < a.size(); i++) {
if (a.get(i) == b.get(i)) {
temp = true;
}
}
return temp;

2)

int i;
for (i = 0; i < a.size(); i++) {
if (a.get(i) != b.get(i)) {
return false;
} else {
return true;
}
}

3)

int i = 0;
while (i < a.size() && a.get(i) == b.get(i)) {
i++;
}
return (i == a.size());

4)

boolean temp = true;
int i;
for (i = 0; i < a.size(); i++) {
temp = (a.get(i) == b.get(i));
}
return temp;

5)

int i = 0;
while (i < a.size() && a.get(i) == b.get(i)) {
i++;
}
if (i > a.size()) {
return true;
}
return false;

Explanation / Answer

Question 25:
Answer:

3)  
int i = 0;
while (i < a.size() && a.get(i) == b.get(i)) {
i++;
}
return (i == a.size());

How?
       the while loop iterates untill end of size(a) and also each time it check each element of a is equal to b
       and then increments i value
       at the end it checks id size of a equal to i
       it means that i value is equal to size of a means all are equal, if nor return false
      
Question 26:
Answer:

1)   I only

How?
ArrayList a1 = new ArrayList(); // legal

ArrayList<Object> a2 = new ArrayList<Object>; // illegal declaration
ArrayList<String> a3 = new ArrayList<String>;       // illegal declaration


Question 27:
Answer:

3)   III only


Question 28:
Answer:

2)   Yes, because it is private and non static.

Question 3:
Answer:

4)   s2.compareTo(s1) > 0


Question 4:
Answer:

3)   5

how?
index is 1

Question 5:
Answer:

4)   I and III only

Question 8:
Answer:

2)   run time

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote