Question

In: Computer Science

in java Suppose that an array is declared and constructed as below. Each part of this...

in java

Suppose that an array is declared and constructed as below. Each part of this problem is independent (i.e. the answer to b) should not depend on your answer to a).

int[] scores = {10, 8, 6, 4, 2, 0};

a) What is scores[1]?

b) What is scores.length?

c) What is scores[5]?

d) What array will the method call below return? Read the API carefully

Arrays.copyOfRange(scores, 2, 4)

e) What will the method call below return? Read the API very carefully.

Arrays.sort(scores);

Arrays.binarySearch(scores, 7);

Solutions

Expert Solution

a)sores[1]=8

Reason:In arrays index number starting from 0

______________

b)scores.length=6

______________

c)scores[5]=0

______________

d) Arrays.copyOfRange(scores, 2, 4) this will return an integer array which contains elements {6,4}

Reason:here starting position is 2 (inclusive)

Ending position is 4 (exclusive)

________________

e) Arrays.sort(scores); this will sort the elements in the array in the ascending order.

Int scores[] ={0,2,4,6,8,10};

_____________

f) Arrays.binarySearch(scores, 7);

here we are going to search the value 7 in the array scores.As we didn’t find the value 7 in the array score which returns an insertion point

Here the insertion point is point at which we can insert the value.

Here we have to insert the 7 value after the value 6 in the array(On sorted array in ascending order).so its(element 7) index position is 4

So it returns -insertionpoint -1=-4-1=-5

______________


Related Solutions

JAVA LANGUAGE 1. What is the value of creditScores.length, if you have declared an array as...
JAVA LANGUAGE 1. What is the value of creditScores.length, if you have declared an array as follows: int[] creditScores = {670, 720, 815};? a. 0 b. 1 c. 2 d. 3 2. Which of the following correctly assigns the value 100 to each of the array element? Assume the array is declared as int [] num = new int[4] a. for(x=0;x<3;++x) num[x]=100; c. for(x=1;x<4;++x) num[x]=100 ; 3. Consider the following code fragment int[][] rectangle = new int[4][2]; What is the...
Suppose you have declared a character array called board of size 9 for a game by...
Suppose you have declared a character array called board of size 9 for a game by the declaration char board[9] and you want to initialize it with a loop so that each array member has a space character each at the beginning of the game. Which one of the following loops is the correct initialization?
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array...
Part 1- Delete an element from an Array. See the instructions below: -Use the previous Array code provided to you (to demonstrate how to insert elements in an Array). It is an Array with the following details: Size 10 Length 5. Your task is to Delete element on index 4 please use the code below and write in C++ how to delete the 4th element. #include struct Array { int A[10]; int size; int length; }; void Display(struct Array arr)...
part 1 - Suppose that a stock index is constructed with three stocks priced at $7,...
part 1 - Suppose that a stock index is constructed with three stocks priced at $7, $43, and $56. The number of outstanding shares for each is 500,000 shares, 405,000 shares, and 553,000 shares, respectively. Today the prices for each stock are changed to $14, $44, and $52 and the number of outstanding shares for each are changed to 250,000 shares, 405,000 shares and 553,000 shares today, what is the price weighted index value today if the index yesterday was...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
Java Language The array s of ints contain integers each of which is between 1 and...
Java Language The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array { 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 } then your code should set ordinals to the array { "1st", "2nd", "3rd",...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the...
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the lab grades, quiz grades and exam grades of three students in the professor’s class. Write a C++ program that calculate the final grade for each student and save it to the last column. You program should display the following output:             Lab    Quiz      Exam       Final     Student 1          ## ## ## ## Student 2          ## ## ## ## Student 3          ## ## ## ## The...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT