Beginner array coding problems in Java

More Data Structure and Algorithms Coding Questions and answers in Java.

Q1. Write a program that allows you to create an integer array of 5 elements with the following values: int numbers[ ]={5,2,4,3,1}. The program computes the sum of first 5 elements and stores them at element 6, computes the average and stores it at element 7 and identifies the smallest value from the array and stores it at element 8.

A1. Remember that the arrays are zero based.

Output:

Q2. Other than looping through each element in an array and setting each one to null, is there a better way to do it?
A2. The Arrays and Collections utility classes in Java has very handy methods.

Q3. Can you complete the following method so that

An input of {10, 2, 10, 1} yields {2, 1, 0, 0}. The 10’s to be removed, and the remaining elements should shift left towards the start of the array as needed, and the empty spaces at the end of the array should be filled with 0’s.

A3.

Q4. Can you complete the following method so that

Given an array of numbers, return true if the array contains two 9’s next to each other, or there are two 9’s separated by one element.

{ 2, 9, 2, 2, 9, 9 } –> false

{ 2, 9, 2, 9, 9 } –> true

A4.

Q5 Given a number n, create and return a new string array of length n, containing the strings “0”,”1″ “2” .. through n-1. n may be 0, For example, if n = 4, returns {“0”, “1”, “2”, “3”}

A5


Java Interview FAQs

800+ Java Interview Q&As

Top