Assignment: Arrays in Java
Assignment: Arrays in Java
Objective:
The objective of this assignment is to thoroughly test your understanding of arrays in Java, including single-dimensional arrays, multidimensional arrays, jagged arrays, and anonymous arrays. You will be required to write code snippets and solve problems using these array structures.
Part 1: Single-Dimensional Arrays
Question 1: Array Initialization and Access
Write a Java program that creates an array of 10 integers, initializes it with values from 1 to 10, and prints all the elements.
Example Output:
Question 2: Sum and Average of Array Elements
Write a Java program that calculates the sum and average of all elements in a single-dimensional array of 5 integers entered by the user.
Example Output:
Question 3: Find Maximum and Minimum Values in an Array
Write a Java program that finds and prints the maximum and minimum values in an array of integers.
Example Output:
Question 4: Reverse an Array
Write a Java program that reverses the elements of a given array and prints the reversed array.
Example Output:
Question 5: Array Element Frequency
Write a Java program that counts the frequency of each element in an array of integers and prints the result.
Example Output:
Question 6: Find Second Largest Element in an Array
Write a Java program that finds the second largest element in a single-dimensional array.
Example Output:
Question 7: Remove Duplicates from Array
Write a Java program that removes duplicate elements from an array and prints the unique elements.
Example Output:
Question 8: Find Common Elements Between Two Arrays
Write a Java program that finds and prints the common elements between two arrays.
Example Output:
Question 9: Rotate an Array
Write a Java program that rotates an array by a given number of positions to the right.
Example Output:
Question 10: Find Missing Number in an Array
Write a Java program that finds the missing number in an array containing numbers from 1 to n with one missing number.
Example Output:
Part 2: Multidimensional Arrays
Question 11: Matrix Addition
Write a Java program that adds two 2x2 matrices and prints the resulting matrix.
Example Output:
Question 12: Matrix Multiplication
Write a Java program that multiplies two 2x2 matrices and prints the resulting matrix.
Example Output:
Question 13: Transpose of a Matrix
Write a Java program that computes the transpose of a 3x3 matrix and prints the transposed matrix.
Example Output:
Question 14: Sum of Diagonals in a Matrix
Write a Java program that calculates and prints the sum of the primary and secondary diagonals of a 3x3 matrix.
Example Output:
Question 15: Matrix Subtraction
Write a Java program that subtracts one 3x3 matrix from another and prints the resulting matrix.
Example Output:
Question 16: Check if a Matrix is Symmetric
Write a Java program that checks if a given matrix is symmetric (i.e., equal to its transpose).
Example Output:
Question 17: Spiral Traversal of a Matrix
Write a Java program that prints the elements of a 3x3 matrix in spiral order.
Example Output:
Question 18: Find Maximum Element in Each Row of a Matrix
Write a Java program that finds and prints the maximum element in each row of a 3x3 matrix.
Example Output:
Question 19: Sum of Each Row and Column in a Matrix
Write a Java program that calculates and prints the sum of each row and each column in a 3x3 matrix.
Example Output:
Question 20: Identity Matrix Check
Write a Java program that checks if a given 3x3 matrix is an identity matrix (i.e., 1s on the diagonal and 0s elsewhere).
Example Output:
Part 3: Jagged Arrays
Question 21: Jagged Array Creation and Access
Write a Java program that creates a jagged array with 3 rows, where the first row has 2 columns, the second row has 3 columns, and the third row has 4 columns. Initialize the array with values and print the elements.
Example Output:
Question 22: Triangle Matrix using Jagged Array
Write a Java program that uses a jagged array to represent a triangular matrix and fills it with values. Print the triangular matrix.
Example Output:
Question 23: Jagged Array for Student Marks
Write a Java program that stores the marks of students in different subjects using a jagged array and prints them.
Example Output:
Question 24: Jagged Array for Employee Salaries
Write a Java program that stores the salaries of employees in different departments using a jagged array and prints them.
Example Output:
Question 25: Jagged Array with User Input
Write a Java program that allows the user to input values for a jagged array with 3 rows, where the first row has 2 columns, the second row has 3 columns, and the
third row has 4 columns. Print the array after input.
Example Output:
Part 4: Anonymous Arrays
Question 26: Sum Using Anonymous Array
Write a Java program that calculates the sum of an anonymous array passed to a method.
Example Output:
Question 27: Average Using Anonymous Array
Write a Java program that calculates the average of an anonymous array of double values passed to a method.
Example Output:
Question 28: Maximum Element Using Anonymous Array
Write a Java program that finds the maximum element in an anonymous array passed to a method.
Example Output:
Question 29: Minimum Element Using Anonymous Array
Write a Java program that finds the minimum element in an anonymous array passed to a method.
Example Output:
Question 30: Sort Using Anonymous Array
Write a Java program that sorts an anonymous array passed to a method using bubble sort and prints the sorted array.
Example Output:
Part 5: Array Manipulation
Question 31: Insert Element in Array
Write a Java program that inserts an element at a specific position in an array and shifts the other elements to the right.
Example Output:
Question 32: Remove Element from Array
Write a Java program that removes an element from a specific position in an array and shifts the remaining elements to the left.
Example Output:
Question 33: Split an Array
Write a Java program that splits an array into two halves and prints both halves.
Example Output:
Question 34: Merge Two Arrays
Write a Java program that merges two arrays into a single array and prints the merged array.
Example Output:
Question 35: Array Rotation by k Positions
Write a Java program that rotates an array by k
positions to the right and prints the rotated array.
Example Output:
Question 36: Replace All Negative Numbers in Array
Write a Java program that replaces all negative numbers in an array with 0 and prints the updated array.
Example Output:
Question 37: Find All Prime Numbers in an Array
Write a Java program that finds and prints all the prime numbers in an array.
Example Output:
Question 38: Count Even and Odd Numbers in an Array
Write a Java program that counts the number of even and odd numbers in an array and prints the result.
Example Output:
Question 39: Find the Longest Increasing Subsequence in an Array
Write a Java program that finds and prints the longest increasing subsequence in an array.
Example Output:
Question 40: Move All Zeros to End of Array
Write a Java program that moves all zeros in an array to the end while maintaining the order of other elements.
Example Output:
Part 6: Array Sorting and Searching
Question 41: Bubble Sort
Write a Java program that sorts an array of integers using the bubble sort algorithm.
Example Output:
Question 42: Selection Sort
Write a Java program that sorts an array of integers using the selection sort algorithm.
Example Output:
Question 43: Insertion Sort
Write a Java program that sorts an array of integers using the insertion sort algorithm.
Example Output:
Question 44: Merge Sort
Write a Java program that sorts an array of integers using the merge sort algorithm.
Example Output:
Question 45: Quick Sort
Write a Java program that sorts an array of integers using the quick sort algorithm.
Example Output:
Question 46: Linear Search
Write a Java program that searches for an element in an array using linear search and returns its index.
Example Output:
Question 47: Binary Search
Write a Java program that searches for an element in a sorted array using binary search and returns its index.
Example Output:
Question 48: Find the kth Smallest Element in an Array
Write a Java program that finds the kth smallest element in an array.
Example Output:
Question 49: Find the kth Largest Element in an Array
Write a Java program that finds the kth largest element in an array.
Example Output:
Question 50: Find the Majority Element in an Array
Write a Java program that finds the majority element in an array (an element that appears more than n/2 times).
Example Output:
Submission Guidelines:
Submit your answers in a well-documented Java code file.
Provide comments in your code explaining the logic you used.
Ensure your code is clean, properly formatted, and includes error handling where necessary.
Evaluation Criteria:
Correctness of logic and implementation.
Efficient use of array structures.
Code readability and documentation.
Handling of edge cases and errors.
Efficiency of the solution.
Good luck!
Last updated