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.
//Declare an array numArray of 15 elements of type int
int[] numArray = new int[15];
//Output the value of the tenth element of the array alpha.
System.out.println(alpha[9]);
//Set the value of the fifth element of the array alpha to 35.
alpha[4] = 35;
//Set the value of the ninth element of the array alpha to the sum of
//the sixth and thirteenth elements of the array alpha
alpha[8] = alpha[5] + alpha[12];
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.
// Declare a string array and initialize
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday"};
// Displays the contents of each element
for (String day : days)
{
System.out.println(day);
}
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.
import java.util.Scanner;
public class ArraySum
{
public static void main(String[] args)
{
final int SIZE = 10;
// Create an array to hold numbers.
int[] numbers = new int[SIZE];
Scanner console = new Scanner(System.in);
System.out.println("Enter " + SIZE + " numbers.");
// Get employees' salary.
for (int i = 0; i < SIZE; i++)
{
numbers[i] = console.nextInt();
}
int sum = 0;
// Calculate the sum.
for (int i = 0; i < SIZE; i++)
{
sum += numbers[i];
}
System.out.println("Sum of numbers: " + sum);
}
}
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.
import java.util.Scanner;
public class ArrayCopy
{
public static void main(String[] args)
{
final int SIZE = 5;
int[] list1 = new int[SIZE];
int[] list2 = new int[SIZE];
Scanner console = new Scanner(System.in);
System.out.println("Enter " + SIZE + " numbers.");
// Get value in list1
for (int i = 0; i < SIZE; i++)
{
list1[i] = console.nextInt();
}
// Copy elements in list2.
for (int i = 0; i < SIZE; i++)
{
list2[i] = list1[i];
}
// Display elements of list2.
System.out.println("Elements of list2 are: ");
for (int i = 0; i < SIZE; i++)
{
System.out.println(list2[i]);
}
}
}
Question 5
Write a Java program to reverse the element of an integer 1-D array.
import java.util.Scanner;
public class ReverseArray
{
public static void main(String[] args)
{
int size;
Scanner console = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
size = console.nextInt();
int[] list = new int[size];
System.out.println("Enter " + list.length + " integers.");
for (int i = 0; i < list.length; i++)
{
list[i] = console.nextInt();
}
int temp;
// Reversing elements of the array.
for (int i = 0; i < list.length / 2; i++)
{
temp = list[i];
list[i] = list[list.length - i - 1];
list[list.length - i - 1] = temp;
}
System.out.println("Reverse array: ");
// Display the reverse array.
for (int i = 0; i < list.length; i++)
{
System.out.println(list[i]);
}
}
}
Question 6
Write a Java program to find the largest and smallest element of an array.
import java.util.Scanner;
public class MaxMinElement
{
public static void main(String[] args)
{
int size;
Scanner console = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
size = console.nextInt();
int[] list = new int[size];
System.out.println("Enter " + list.length + " integers.");
for (int i = 0; i < list.length; i++)
{
list[i] = console.nextInt();
}
int max = list[0];
int min = list[0];
for (int i = 1; i < list.length; i++)
{
if(list[i] > max)
{
max = list[i];
}
if(list[i] < min)
{
min = list[i];
}
}
System.out.println("Largest element: " + max
+ " Smallest element: " + min);
}
}
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.
import java.util.Scanner;
public class SearchSortArray
{
static Scanner console = new Scanner(System. in );
public static void main(String[] args)
{
int size;
System.out.print("Enter the size of the array: ");
size = console.nextInt();
int[] list = new int[size];
int option;
do
{
System.out.println("Menu ");
System.out.println("1. Read Array");
System.out.println("2. Display Array");
System.out.println("3. Search an Item in Array");
System.out.println("4. Sort the Array");
System.out.println("5. Exit");
System.out.print("Select an Option : ");
option = console.nextInt();
switch (option)
{
case 1:
readArray(list);
break;
case 2:
displayArray(list);
break;
case 3:
System.out.print("Enter the number you want to search: ");
int item = console.nextInt();
int index = searchArray(list, item);
if (index == -1)
{
System.out.println("Item not found");
}
else
{
System.out.println("Item found at position " + (index + 1));
}
break;
case 4:
System.out.println("Sorted array :");
sortArray(list);
displayArray(list);
break;
}
} while (option != 5);
}
static void readArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
array[i] = console.nextInt();
}
}
static void displayArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println();
}
static int searchArray(int[] array, int data)
{
for (int i = 0; i < array.length; i++)
{
if(array[i] == data)
{
return i;
}
}
return -1;
}
static void sortArray(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{
for(int j = 0; j < array.length -i-1; j++)
{
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
}
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.
answer :
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
answer :
Question 10
Write user defined methods for square matrix to calculate
Left diagonal sum
Right diagonal sum
answer :
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.
answer :
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
answer :
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
answer :
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.