Write a program to accept a two-dimensional integer array of order as input from the user. Check if it is a Sparse Matrix or not. A matrix is considered to be a sparse, if the total number of zero elements is greater than the total number of non-zero elements. Print appropriate messages.
Official Solution
Correct Option: (1)
Step 1: Understand the concept of a sparse matrix. A sparse matrix is a matrix in which the number of zero elements is greater than the number of non-zero elements. Here, we have to accept a integer matrix from the user, count the number of zero and non-zero elements, and then decide whether the matrix is sparse or not.
Step 2: Declare the required array and variables. We need a two-dimensional integer array of size to store the matrix elements. We also need two counter variables: one to count zero elements and another to count non-zero elements.
Step 3: Accept the matrix elements from the user. Using nested loops, we can input all the elements of the matrix row by row. Since the matrix has rows and columns, the outer loop will run times and the inner loop will run times.
Step 4: Count zero and non-zero elements. While traversing the matrix, each element is checked. If the element is equal to , then the zero counter is increased. Otherwise, the non-zero counter is increased. In this way, at the end of traversal, we get the total number of zero and non-zero elements.
Step 5: Compare the two counts. After counting, we compare the total number of zero elements with the total number of non-zero elements. If the number of zero elements is greater, then the matrix is a sparse matrix. Otherwise, it is not.
Step 6: Write the complete Java program.
import java.util.Scanner; class SparseMatrix { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a[][] = new int[4][5]; int i, j; int zero = 0, nonzero = 0; System.out.println("Enter the elements of the matrix:"); for(i = 0; i < 4; i++) { for(j = 0; j < 5; j++) { a[i][j] = sc.nextInt(); if(a[i][j] == 0) zero++; else nonzero++; } } System.out.println("Number of zero elements = " + zero); System.out.println("Number of non-zero elements = " + nonzero); if(zero > nonzero) System.out.println("It is a Sparse Matrix."); else System.out.println("It is not a Sparse Matrix."); } }
Step 7: Conclude the logic of the program. Thus, the program first stores all elements of the matrix, then counts the zero and non-zero values, and finally checks whether the matrix satisfies the condition of a sparse matrix. If zero elements are more than non-zero elements, the appropriate message is displayed.
02
PYQ 2026
medium
computer-applicationsID: icse-cla
For the array \texttt{U[][] = \{\{4, 5\, \{7, 2\}, \{19, 4\}, \{7, 43\}\}}, find the maximum element and the index of the minimum element.}
Official Solution
Correct Option: (1)
Step 1: Write all the elements of the array.
The given 2D array is: Step 2: Find the maximum element.
Now compare all the elements:
The greatest value among these is: Step 3: Find the minimum element.
The smallest value in the array is: Step 4: Find the index of the minimum element.
In Java, array indexing starts from .
The element is present in the second row and second column, so its index is: Step 5: State the final answer.
Therefore:
and
03
PYQ 2026
easy
computer-applicationsID: icse-cla
Write 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.
Official Solution
Correct Option: (1)
Step 1: Understand the requirement of the question. In this question, we have to store the designations of employees in a one-dimensional array. After storing all the designations, we need to accept one designation from the user and count how many times that designation appears in the array. Finally, we print that total count.
Step 2: Declare the required variables and array. We need a string array to store the designations of employees. We also need one variable to store the designation entered by the user for searching, and one counter variable to count the number of matches.
Step 3: Accept the designations of 100 employees. Using a loop, we input all designations one by one and store them in the array. Since designations are words such as Manager, Trainee, Director, etc., we use a String array.
Step 4: Search the given designation in the array. After taking the input designation to be searched, we traverse the array again. Every time the entered designation matches an element of the array, we increase the counter by . For string comparison in Java, we use the equalsIgnoreCase() method so that the comparison works properly even if the user enters the designation in a different letter case.
Step 5: Display the final result. At the end of the loop, the counter contains the total number of employees having the required designation. This value is then printed as the output.
Step 6: Write the complete Java program.
import java.util.Scanner; class EmployeeDesignation { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String desig[] = new String[100]; String search; int i, count = 0; for(i = 0; i < 100; i++) { System.out.print("Enter designation of employee " + (i + 1) + ": "); desig[i] = sc.nextLine(); } System.out.print("Enter designation to be searched: "); search = sc.nextLine(); for(i = 0; i < 100; i++) { if(desig[i].equalsIgnoreCase(search)) { count++; } } System.out.println("Total number of employees = " + count); } }
Step 7: Explain the example given in the question. For example, if the designations stored are Trainee, Manager, Chef, Manager, Director, Manager and the input designation is Manager, then the word Manager appears times. Therefore, the output will be .
04
PYQ 2026
hard
computer-applicationsID: icse-cla
Write the statement that swaps the first element and the second element using the third variable. Fill in the blanks.
Official Solution
Correct Option: (1)
Step 1: Understand the concept of swapping.
To swap two values, we use a third temporary variable to store one value so that it is not lost during assignment. Step 2: Store the first element in the third variable.
First, store the value of the first element in the temporary variable: Step 3: Copy the second element into the first.
Now assign the value of the second element to the first element: Step 4: Copy the temporary value into the second.
Finally, assign the stored value from the temporary variable to the second element: Step 5: Write the final statements.
Therefore, the required swapping statements are: \texttt{temp = first;}
\texttt{first = second;}
\texttt{second = temp;}
05
PYQ 2026
medium
computer-applicationsID: icse-cla
An array with 3 elements is arranged in ascending order as follows: Name the technique used:
1
Bubble sort
2
Linear Search
3
Selection sort
4
Binary Search
Official Solution
Correct Option: (1)
Step 1: Observe the sequence of swaps carefully. The given array changes as follows:
In the first step, the adjacent elements and are compared and swapped because . In the second step, the adjacent elements and are compared and swapped because . So, the array gets sorted by repeatedly comparing and swapping adjacent elements. Step 2: Recall the idea of Bubble Sort. Bubble sort is a sorting technique in which adjacent elements are compared one by one, and if they are in the wrong order, they are swapped. In each pass, the largest element gradually moves toward the end of the array, just like a bubble rising upward. Here, first moves one position to the right and then again to the right until it reaches its correct place:
This exactly matches the working of Bubble sort. Step 3: Compare with the other options.
(A) Bubble sort: Correct, because adjacent elements are swapped repeatedly.
(B) Linear Search: Incorrect, because linear search is used for finding an element, not sorting.
(C) Selection sort: Incorrect, because selection sort selects the minimum element and places it in the correct position, rather than swapping adjacent elements repeatedly.
(D) Binary Search: Incorrect, because binary search is a searching method used on sorted arrays, not a sorting technique.
Step 4: Conclusion. Since the array is sorted by comparing and swapping neighboring elements step by step, the technique used is Bubble sort. Final Answer:Bubble sort.
06
PYQ 2026
medium
computer-applicationsID: icse-cla
The 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?
1
25
2
200
3
50
4
100
Official Solution
Correct Option: (4)
Step 1: Find the total number of elements in the two-dimensional array. The question says that the sales data of salesmen and products is stored in a two-dimensional array. This means the array has:
elements in total. So, the array contains integer values. Step 2: Recall the memory occupied by one integer. In Java, an int data type occupies bytes of memory. Therefore, each element of the array will take:
Step 3: Calculate the total memory occupied. Since there are integer elements, total memory occupied by the array is:
bytes. Step 4: Match the answer with the given options.
(A) 25: Incorrect, this is only the number of elements.
(B) 200: Incorrect.
(C) 50: Incorrect.
(D) 100: Correct.
Hence, the array occupies 100 bytes. Final Answer: bytes.