OutputEasy2 marks
Consider the following array and answer the questions given below: int a[] = {12, 10, 8, 4, 6, 2, 3, 5, 7}; (a) What is the output of System.out.print(a[0] + a[5]);? (b) What is the index (subscript) of the largest element of the array a[]?
Topic: Array elements and indices
Previous-year question practice database
Answer
Exam answer
(a) 14 (b) 0
Explanation
Write the indices under the values before answering — it prevents every off-by-one error: index: 0 1 2 3 4 5 6 7 8 value: 12 10 8 4 6 2 3 5 7 (a) a[0] is 12 and a[5] is 2, and since both are ints the + adds them: 12 + 2 = 14. (Had they been Strings, + would have joined them as "122".) (b) The largest value in the array is 12, and it stands at index 0. Note the question asks for the index, not the value — a common place to lose the mark.
More from Arrays
An array with 3 elements is arranged in ascending order as follows:
4 1 3 -> 1 4 3 -> 1 3 4
Name the technique used:2026The sales made by 5 salesmen selling 5 products is stored in a two-dimensional array of integer data type. How many bytes does the array occupy?2026int X[][] = {{4, 5}, {7, 2}, {19, 4}, {7, 4}};
Write the index of the maximum element and the index of the minimum element of the array.2026The following program segment swaps the first element and the second element of the given array without using the third variable. Fill in the blanks with appropriate java statements:
void swap()
{
int x[] = {4, 8, 19, 24, 15};
(1) __________;
(2) __________;
x[0] = x[0] / x[1];
System.out.println(x[0] + " " + x[1]);
}2026Write a program to accept the designations of 100 employees in a single dimensional array. Accept the designation from the user and print the total number of employees with the designation given by the user as input.
Example:
Trainee | Manager | Chef | Manager | Director | Manager
Input: Manager Output: 32026Write a program to accept a two-dimensional integer array of order 4 x 5 as input from the user. Check if it is a Sparse Matrix or not. A matrix is considered to be sparse if the total number of zero elements is greater than the total number of non-zero elements. Print appropriate messages.
Example:
4 3 0 1 0
1 0 0 2 0
1 0 1 0 0
0 3 2 0 0
Number of zero elements = 11
Number of non zero elements = 9
Matrix is a Sparse Matrix2026