In: Computer Science
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).