Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

1.

Suppose the following array and integer are declared: int[] numbers = {4, 5, 6, 7}; int index = 1 + 6 % 2 * 2; What is the value of numbers[index]? (2 points)

An ArrayIndexOutOfBoundsException occurs.

4

5

6

7

2.

Assume that you have an array of integers named arr. Which of these code segments print the same results? (1 point)

  1. int i = 0; while (i < arr.length) { System.out.println(arr[i]); i++; }
  2. int i; for (i = 0; i <= arr.length; i++) { System.out.println(arr[i]); }
  3. for (int i : arr) { System.out.println(i); }

I and II only

II and III only

I and III only

All three print the same results

All three print different results

3.

Consider the following definitions: public boolean someMethod (int[] list, int value) { int counter; boolean flag = false; for (counter = 0; counter < list.length; counter++) { flag = (list[counter] != value); } return flag; } Under which of the following conditions must the method above return true? (1 point)

Under all conditions

Under the condition that value == list[list.length 1]

Under the condition that value != list[list.length 1]

Under the condition that value != list[i] for all i such that 0 <= i < list.length

Under no conditions

4.

Consider the expression !((x <= y) || (y > 0)). It is equivalent to which of the following expressions? (2 points)

  1. !(x <= y) || !(y > 0)
  2. !(x <= y) && !(y > 0)
  3. (x > y) && (y <= 0)

I only

II only

III only

I and III only

II and III only

5.

What is output by the following code? (2 points) ArrayList < Integer > a = new ArrayList < Integer >(); ArrayList b = a; a.add(4); b.add(5); a.add(5); b.add(7); System.out.println(b.get(0));

An IndexOutOfBoundsException occurs.

4

5

6

7

6.

What is output by the following code? (2 points) ArrayList < Integer > a = new ArrayList < Integer >(); ArrayList b = a; a.add(4); b.add(5); a.add(6); b.add(7); System.out.println(b.get(2));

An IndexOutOfBoundsException occurs.

4

5

6

7

7.

What is the value of vals[2][1]? (1 point) double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}};

1.3

3.3

3.5

5.3

8.

What is the value of vals[4][1]? (1 point) double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}};

1.1

7.1

7.3

An ArrayIndexOutOfBoundsException error

9.

What is the output of the following code? (1 point) ArrayList grades = new ArrayList(); grades.add(88); grades.add(92); grades.add(95); grades.add(83); System.out.println(grades.get(grades.size()));

An ArrayIndexOutOfBoundsException occurs

An IndexOutOfBoundsException occurs

92

95

83

10.

What is output by the following code fragment? (1 point) String[] fruits = { "apple", "banana", "peach", "strawberry" }; String str = "a"; for (String item : fruits) { str += item.substring(3, 4); } System.out.println(str);

apnar

aplnaacaw

alleanchaw

alaca

appbaeast

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is the purpose of a balance sheet?

Answered: 1 week ago

Question

1 What demand is and what affects it.

Answered: 1 week ago