Declare AND initialize an single-dimensional array of 5 doubles with the name \"
ID: 3725617 • Letter: D
Question
Declare AND initialize an single-dimensional array of 5 doubles with the name "list". You do not need to set the values of the doubles.
If we had declared but NOT initialized "list", what value would it have?
Write code using a for loop to set each value of "list" to 1. Do NOT use the number 5 directly as the length!
Write code using a for-each loop to print each value of "list" on each line.
Given the code below, what will the values in the array "powers" be?
If I want to copy the contents of "list2" into "list1", should I use the code below? If not, why not?
If I call my Java program "TestArgs" on the command line as shown below, what would be the contents of args[2]?
Declare and initialize a 2D doubles array of 3 rows and 4 columns named "M". You do not need to set the values in the array.
Given the 2D array "M", write code to store the length of the first row into an int variable "firstLen". Do NOT use the number 4 directly as the length!
Explanation / Answer
1)
public class Prog {
public static void main(String[] args) {
int N = 5;
double[] list = new double[N];
for(int i=0; i<N; i++)
list[i] = 1;
}
}
2)
[1, 2, 4, 8]
3)
If I want to copy the contents of "list2" into "list1", should I use the code below? If not, why not?
Ans: No, Code: list1 = list2; means list1 also points to object to which list2 pointing. so, both will point to same object
4)
If I call my Java program "TestArgs" on the command line as shown below, what would be the contents of args[2]?
Ans:
5)
Declare and initialize a 2D doubles array of 3 rows and 4 columns named "M". You do not need to set the values in the array.
Ans:
double [][] M = new double[3][4];
6)
Given the 2D array "M", write code to store the length of the first row into an int variable "firstLen".
Ans: firstLen = M[0].length;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.