that is in Java Q 1,2,3,5 T or F Q 4 write the code: 1- The following code creat
ID: 3813933 • Letter: T
Question
that is in Java Q 1,2,3,5 T or F Q 4 write the code:
1- The following code creates a two-dimensional array:
int[] int[] num = new int[5] int[5] (True or False)
2- It's best to use an enhanced for loop to fill an array with numbers. (True or False)
3- A linear search inspects elements in sequence until a match is found. (True or False)
4- Given the following two-dimensional array, which for loop displays the output: 3 6 9 (True or False)
int[ ][ ] num = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} }; (write the code)
5- The following code creates a separate copy of an array, meaning there will be two separate arrays that contain the same elements:
int[] num = {1, 2, 3};
int[] numCopy = copyOf(num, num.length); (True or False)
This photo for Q 4:
for (int i 0; i K num length i++) system. rint (num [num [01.1 1] [i] i (int i 30; i num length; it for system. out.print (num [num.length 1] num length [0] 11 t for (int i 0; i K num length it System. out.print (num [2] [i] i for (int i 0; i K num length it System. out .print (num Cil [num [0] length 11Explanation / Answer
1- The following code creates a two-dimensional array:
int[] int[] num = new int[5] int[5] (True or False)
Answer: False
Inorder to create a two-dimensional array we should use the below”
int[][] num = new int[5][5];
2- It's best to use an enhanced for loop to fill an array with numbers. (True or False)
Answer: False
It is not possible to assign values to an array using Enhanced for loops, because enhanced is a for-each loop which doesn’t gives us the index value of the current array element. To set a value in array we must use array[i]=somevalue which is not possible with enhanced for loop.
In contrast enhanced for loops are used to retrieve the values from the array.
3- A linear search inspects elements in sequence until a match is found. (True or False)
Answer: True
The technique followed by Linear search algorithm is traversing the given elements sequentially until the desired element is found or the end of the list is reached.
4- Given the following two-dimensional array, which for loop displays the output: 3 6 9 (True or False)
int[ ][ ] num = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
Answer: Option D. The code that outputs 3 6 9 is
for(int i=0;i<num.length;i++)
{
System.out.print(num[i][num[0].length-1]+" ");
}
If you observe carefully 3 6 9 are the elements of the last column of each row.
Hence the elements are num[0][2], num[1][2] and num[2][2]
We get the last column using num[0].length-1
And finally a loop through all the rows and printing the last column element of each row the desired output is occurred.
5- The following code creates a separate copy of an array, meaning there will be two separate arrays that contain the same elements:
int[] num = {1, 2, 3};
int[] numCopy = copyOf(num, num.length); (True or False)
Answer: False
The copyOf method copies the array into another upto a specified length, but the problem with the above is the syntax of using copyOf method is incorrect.
In order to copy an array into another use the below
int[] num = {1, 2, 3};
int[] numCopy = Arrays.copyOf(num, num.length);
Note that in the give code copyOf() is directly used, but as copyOf is a method of Arrays class is should be used as Array.copyOf()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.