Simple Java question - Answer the following questions about a one-dimensional ar
ID: 3588931 • Letter: S
Question
Simple Java question -
Answer the following questions about a one-dimensional array
1. Declare a integer constant called MY_SIZE with a value of 10
2. Declare and create a one-dimensional called myArray with MY_SIZE elements where each element is a double
3. Print the 4th element
4. Write a counter-controlled for loop to print all of the elements (one per line)
5. Write an enhanced for loop to print all of the elements (one per line)
--------------------------------------------------------------------------------------------------------------------
Answer the following questions about a two by three integer array named scores
1. Write a statement that declares and creates the array
2. How many rows does it have?
3. How many Columns does it have?
4. Write a code snippet that will add 1 to each element in the array
Explanation / Answer
1. Declare a integer constant called MY_SIZE with a value of 10
Answer: int MY_SIZE = 10;
2. Declare and create a one-dimensional called myArray with MY_SIZE elements where each element is a double
Answer: double myArray[] = new double[MY_SIZE];
3. Print the 4th element
Answer: System.out.println(myArray[3]);
4. Write a counter-controlled for loop to print all of the elements (one per line)
Answer:
for(int i=0;i<myArray.length;i++) {
System.out.println(myArray[i]);
}
5. Write an enhanced for loop to print all of the elements (one per line)
Answer:
for(int i: myArray) {
System.out.println(myArray[i]);
}
1. Write a statement that declares and creates the array
Answer: int scores[][] = new int scores[2][3];
2. How many rows does it have?
Answer: System.out.println(scores.length);
3. How many Columns does it have?
Answer: System.out.println(scores[row].length);
4. Write a code snippet that will add 1 to each element in the array
Answer:
for(int i=0;i<scores.length; i++) {
for(int j=0;j<scores[i].length;i++) {
scores[i] = scores[i] + 1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.