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

Suppose that you write a subclass of Insect named Ant. You add a new method name

ID: 3842723 • Letter: S

Question

Suppose that you write a subclass of Insect named Ant. You add a new method named doSomething to the Ant class. You write a client class that instantiates an Ant object and invokes the doSomething method. Which of the following Ant declarations willnot permit this? (2 points)

I. Insect a = new Ant ();
II. Ant a = new Ant ();
III. Ant a = new Insect ();

Question 21 options:

Consider the following code:

String[] fruits = { "apple", "pear", "mango", "peach" };
int i = 3;
String str = "p";

for (String item : fruits) {
i = item.indexOf("p") + 2;
str += item.substring(i);
}

System.out.println(str);

Assume that the loop accesses array elements in order. What is output by this code? (2 points)

Question 22 options:

Consider the following code:

int[] array = new int[ < some positive integer > ];
int index;

// assume that array is filled with values here
// assume that the user enters a value for index here

if ((index >= 0) && (index <= array.length)){
System.out.println(array[index]);
}

When could this code throw an exception? (2 points)

Question 23 options:

Account is an abstract class with one abstract method named getInterestRate.
BasicCheckingAccount is a concrete class that extends Account.
EnhancedCheckingAccount extends BasicCheckingAccount, and overrides getInterestRate.

Account a;
double rate1, rate2;

a = new BasicCheckingAccount();
rate1 = a.getInterestRate(); // assignment 1
a = new EnhancedCheckingAccount();
rate2 = a.getInterestRate(); // assignment 2

The reference to getInterestRate() in assignment 2 is to the class: (2 points)

Question 24 options:

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 String method? (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) I only 2) II only 3) III only 4) I and II only 5) I and III only ArrayList a new Array List II ArrayList object a new Array List Object Array List String> a new Array List string>

Explanation / Answer

Q21. Now for this question Ant is a subclass of Insect. So as per subclass rule Insect can access Ant and some features of insect class will common with Ant calss but not all. But reversly all the features of Ant class must be in Insect class.

Insect a = new Ant (); This statement indicates that we are creating a object in Insect class and as Ant is a subclass of Insect so this created variable can access Ant class. So it is permitted.

Ant a = new Ant (); As doSomething method is in Ant calss so any object can invoke the method. Hence this is also allowded.

Ant a = new Insect (); Now in this instruction one Ant class object creating memory for its super class Insect. So this object can't invoke the doSomething method. So final option is (3) III only.

Q22. String[] fruits = { "apple", "pear", "mango", "peach" }; in this line 'String' was not declared in this scope.So the final result of this question is (5) A StringIndexOutOfBoundsException occurs.

Q23. The answer of this code segment is This code cannot throw an exception because the if statement prevents it. Look at the if condition once-

if ((index >= 0) && (index <= array.length))

So it will obiviously start from 0 and it will run till index is equal to array length. So because of this specific loop condition it can't throw exception error.

Q24. As we can see 2 improtant statements are-

a = new EnhancedCheckingAccount();
rate2 = a.getInterestRate(); // assignment 2

And according to the question BasicCheckingAccount is a concrete class that extends Account. EnhancedCheckingAccount extends BasicCheckingAccount, and overrides getInterestRate.

So the final answer is (2) BasicCheckingAccount.

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