Question

In: Computer Science

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 size of this array?
a. 4
b. for(x=0;x<4;++x) num[x]=100; d. for(x=1;x<5;++x) num[x]=100 ;
b. 8
d.10
c.6
4. Which of the following data type can be used as the data type of an array subscript? a. character b. double
c. int d. String
5. What is the value of num[2], If you declare an integer array as follows,?
int[] num = {101, 202, 303, 404, 505, 606};
a. 101 b. 202
c. 303 d. impossible to tell
6. How do you indicate the final element of the array, if you declare an array as follows?
int [] num = new int[6];
a. num[0] b. num[6] c.num[5] d. impossible
1. Write the statements for the following operations based on an array Num and method sum. Assume the
array is declared as double [] Num = new int[5];
a. Call the method sum by passing the data Num to the method
b. Declare the method sum appropriately; assume that the method returns the result back to main.
2. Declare an array named NUM of ten elements of type int and initialize the elements to the values 10 , 20 , ..., 100 respectively.
3. Differentiate length field and length() method by illustrating appropriate example. (4 Marks : 2 marks for correct definition of the field and method + 2 marks for correct illustration of example)
4. Write a statement that declares an array named Marks that contains exactly 25 elements of type
double.
5. Given an array A = {3, 7, 8, 5}. What will be in array A after running the given function
Evaluate (4, A)   
public void Evaluate(int n, int B[])
{
int k;
for(k = 0; k < n; k++)

Solutions

Expert Solution

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

Ans: Option (d) 3

when we use arr.length it gives the total number of elements in the array.

So, creditScores.length gives 3 since there are 3 elements in the creditScores[]

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;

b. for(x=0;x<4;++x) num[x]=100;

c. for(x=1;x<4;++x) num[x]=100 ;

d. for(x=1;x<5;++x) num[x]=100 ;

Ans: Option (b) for(x=0; x<4; ++x) num[x]=100 ;

The index of array starts from 0 . Here the size of array is 4. So the indexes are 0,1,2,3.

So the for loop should run from x=0 to x<4.and assign num[x] = 100;

So, option b is correct

3.

Consider the following code fragment

int[][] rectangle = new int[4][2]; What is the size of this array?

a. 4

b. 8

c.6

d.10

Ans: Option (b) 8

There are 4 rows and 2 columns in this array. So, the size of this array is 4*2 = 8

4.

Which of the following data type can be used as the data type of an array subscript?

a. character b. double

c. int d. String

Ans:Option (c) int

Suppose if we need to access an element in 1st row(index starts from 0) and 2nd column(index starts from 0) in a 2d array M. Then we use the indexes.

M[0][1].

So, here the indexes are of type int.

5.

What is the value of num[2], If you declare an integer array as follows,?

int[] num = {101, 202, 303, 404, 505, 606};

a. 101 b. 202

c. 303 d. impossible to tell

Ans: Option (c) 303

Since the index starts from 0

num[0] = 101

num[1] = 202

num[2] = 303

num[3] = 404

num[4] = 505

num[5] = 606

6.

How do you indicate the final element of the array, if you declare an array as follows?

int [] num = new int[6];

a. num[0] b. num[6] c.num[5] d. impossible

Ans: Option (c) num[5]

If n is the size of an array then the final element in the array is at position n-1

Given the size of num array is 6

Therefore the final element in the array is num[6-1] = num[5]

1.

Write the statements for the following operations based on an array Num and method sum. Assume the

array is declared as double [] Num = new int[5];

a. Call the method sum by passing the data Num to the method

Ans:

In the above array it is declared in a wrong way. The datatype in the left side and right side are not equal.

So, we should convert eithere double to int or int to double and call the sum() method

I am converting it as an int array

so double [] Num = new int[5];

To call the sum method we should call like the following

sum(Num);

b. Declare the method sum appropriately; assume that the method returns the result back to main.

Ans:

public static int sum(int[] Num)
{
    int n = Num.length;
    int x = 0;
    for(int i = 0; i<n; i++)
    {
        x = x+Num[i];
    }
    return(x);
}

Total code with call method and method declaration:

public class Main
{
    public static void main(String[] args) {
        int[] Num = new int[5];
        int res = sum(Num);
        System.out.println(res);
    }


    public static int sum(int[] Num)
    {
        int n = Num.length;
        int x = 0;
        for(int i = 0; i<n; i++)
        {
            x = x+Num[i];
        }
        return(x);
    }
}

Here since the elements of array is not initialized they will be initialized automatically to 0.

SO, the sum method will return 0.

2.

Declare an array named NUM of ten elements of type int and initialize the elements to the values 10 , 20 , ..., 100 respectively.

Ans:

Array can be initialized like this

int[] Num = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

or

int[] Num = new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

3.

Differentiate length field and length() method by illustrating appropriate example. (4 Marks : 2 marks for correct definition of the field and method + 2 marks for correct illustration of example)

length:

length is a final variable which is used to get the length of an array using syntax arrayname,length

length() is a final variable which is used for string variables. It gives the length of string by using the syntax

stringname.length();

For eg:

int[] Num = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

Num.length gives result 10 since there are 10 elements in the Num array

String s = "Hello";

s.length(); gives result as 5 since there are 5 characters in the above string s

Code:

public class Main {
    public static void main(String[] args) {
        int[] Num = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
        int n1 = Num.length;
        String s = "Hello";
        int n2 = s.length();
        System.out.println("length of Num array: " + n1);
        System.out.println("length of string s: " + n2);
    }
}

O/P:

length of Num array: 10
length of string s: 5

4.

Write a statement that declares an array named Marks that contains exactly 25 elements of type

double.

Ans:

Here the size is 25 and the type is double. So the array will be declared as

double[] Marks = new double[25]

5.

Given an array A = {3, 7, 8, 5}. What will be in array A after running the given function

Evaluate (4, A)   

public void Evaluate(int n, int B[])

{

int k;

for(k =0;k<n;k++)

System.out.println(B[i]);

}

Ans:

It prints all the elements in the array B

So the result will be

3

7

8

5

(Reupload the last question q5. The last lines are missing in the question))

(I thought it is print statement. If its not print reupload it or write it in comments I will solve.))

(If you still have any doubts regarding the answer please comment I will definitely help).


Related Solutions

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...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Define what an array index value is in JAVA. Then, write a JAVA program that passes...
Define what an array index value is in JAVA. Then, write a JAVA program that passes an array to a method and finds the average value or mean value (add up the numbers in the array and divide by the number of values) of the array and prints it out.
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
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",...
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?
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
language c++(Data structure) You have to read a file and store a data in character array...
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u you can use link list to store data
In java language how would I write the following? Create an array called “boyNames” and store...
In java language how would I write the following? Create an array called “boyNames” and store all names from the BoyNames.txt file Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file Create a text menu that allowing users to:          1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array) If the name is found, tell the user if it is a boy’s...
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