Programming Questions and Exercises : Arrays and ArrayList

Question 1

Write Java statements that do the following:
a) Declare an array numArray of 15 elements of type int.
b) Output the value of the tenth element of the array alpha.
c) Set the value of the fifth element of the array alpha to 35.
d) Set the value of the ninth element of the array alpha to the sum of the sixth and thirteenth elements of the array alpha.

Question 2

a) Write a statement that declares a string array initialized with the following strings:
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".
b) Write a loop that displays the contents of each element in the array that you declared.

Question 3

Write a program that creates an array of 10 elements size. Your program should prompt the user to input numbers in array and then display the sum of all array elements.

Question 4

The variable list1 and list2 are reference arrays that each have 5 elements. Write code that copies the values in list1 to list2. Values in list1 are input by user.

Question 5

Write a Java program to reverse the element of an integer 1-D array.

Question 6

Write a Java program to find the largest and smallest element of an array.

Question 7

Write a menu driven Java program with following option 
a. Accept elements of an array 
b. Display elements of an array 
c. Search the element within array given by user
d. Sort the array using bubble sort method 
Write methods for all options. The methods should be static and have one parameter name of the array.

Question 8

Suppose A, B, C are arrays of integers. The numbers in array A appear in ascending order while the numbers in array B appear in descending order. Write a user defined method in Java to produce third array C by merging arrays A and B in ascending order. Use A, B and C as arguments in the method.

Question 9

Write a menu driven program to do following operation on two dimensional array A of size m x n. You should use user-defined methods which accept 2-D array A, and its size m and n as arguments. The options are:

  • To input elements into matrix of size m x n
  • To display elements of matrix of size m x n
  • Sum of all elements of matrix of size m x n
  • To display row-wise sum of matrix of size m x n
  • To display column-wise sum of matrix of size m x n
  • To create transpose of matrix of size n x m

Question 10

Write user defined methods for square matrix to calculate

  • Left diagonal sum
  • Right diagonal sum

Question 11

Write a user-defined method in Java to display the multiplication of row element of two-dimensional array A[4][6] containing integer.

Question 12

Write a user defined method named Upper-half() which takes a two dimensional array A, with size N rows and N columns as argument and prints the upper half of the array.
e.g.,
2 3 1 5 0                              2 3 1 5 0
7 1 5 3 1                                 1 5 3 1
2 5 7 8 1     The output will be    1 7 8
0 1 5 0 1                                       0 1
3 4 9 1 5                                          5

Question 13

Write a method in Java which accepts a 2D array of integers and its size as arguments and displays the elements of middle row and the elements of middle column.
[Assuming the 2D Array to be a square matrix with odd dimension i.e. 3x3, 5x5, 7x7 etc...]
Example, if the array contents is
3  5  4
7  6  9
2  1  8
Output through the method should be :
Middle Row : 7 6 9
Middle column : 5 6 1

Question 14

Write code that creates an ArrayList that can hold string objects. Add the names of three cars to the ArrayList, and then display the contents of the ArrayList.