Question: Question 21 (12 points) Saved Consider the following code: String[] fruits = {apple, pear, mango, peach}; int i = 3; String str = p; for(String

Question 21 (12 points)

Saved

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 21 options:

1)

learangoach

2)

peearmangoeach

3)

plearangoach

4)

ppplepearpeach

5)

A StringIndexOutOfBoundsException occurs.

Question 22 (12 points)

Saved

Consider the following code:

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

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

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

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

Question 22 options:

1)

When index is less than 0.

2)

When index is equal to 0.

3)

When index is greater than the arr.length.

4)

When index is equal to the arr.length.

5)

This code cannot throw an exception because the if statement prevents it.

Question 23 (12 points)

Saved

Consider the following class descriptions and code. 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 acct; double rate1, rate2; acct = new BasicCheckingAccount(< parameters not shown >); rate1 = acct.getInterestRate(); // assignment 1 acct = new EnhancedCheckingAccount(< parameters not shown >); rate2 = acct.getInterestRate(); // assignment 2

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

Question 23 options:

1)

Account

2)

BasicCheckingAccount

3)

EnhancedCheckingAccount

4)

SavingsAccount

5)

The code cannot run, because the variable acct cannot be assigned to an EnhancedCheckingAccount object in the line before assignment 2.

Question 24 (12 points)

Saved

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 falseotherwise.

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

Question 24 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;

Question 25 (12 points)

Assume table has been declared and initialized as a two-dimensional integer array with 9 rows and 5 columns. Which segment of code will correctly find the sum of the elements in the fifth column? (2 points)

Question 25 options:

1)

int sum = 0; for(int i = 0; i < table.length; i++) sum += table[4][i];

2)

int sum = 0; for(int i = 0; i < table.length; i++) sum += table[i][4];

3)

int sum = 0; for(int i = 0; i < table[0].length; i++) sum += table[i][4];

4)

int sum = 0; for(int outer = 0; outer < table[0].length; outer++) for(int inner = 0; inner < table.length; inner++) sum += table[outer][4];

5)

int sum = 0; for(int outer = 0; outer < table.length; outer++) for(int inner = 0; inner < table[0].length; inner++) sum += table[outer][4];

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!