Anonymous

Register for more FREE stuff!

my subscriptions

Arrays

Question 1

[Maximum mark: 3]



Create a Java program which will take the array student_names with values: Jack, Mike, Katy, Alice. Your program should print the first name in that array.


Your program should print:

  • "The first name in this array is {name}."

Answers and Explanations

Question 2

[Maximum mark: 4]



Create a Java program which will take two arrays, students_names and students_grades.


The students_names array should have the following values: Katy, Alice, Jordan, Jack, Matt


The students_grades array should have the following values: 6,6,5,4,7.


Print the first name in this array in the person's respective grade.


Your program should print:

  • "The first name in this array is {name} with the {grade}."

Answers and Explanations

Question 3

[Maximum mark: 4]



Create a Java program which will take one array called cities, with values: London, Paris, Amsterdam, Lisbon.


a) Print all values in the given order (starting with London).

b) Print all values in the reverse order (starting with Lisbon).

Answers and Explanations

Question 4

[Maximum mark: 5]



Consider two arrays employees and salaries. Salaries are displayed monthly in USD.

Employees Salaries ($)
Alex 2200
Tom 1900
Scott 3300
Miley 2150
Jackson 1450

a) Create a Java program which will calculate the sum of all money that the company has to spend on its employees in a year.


Your program should print:

  • "The sum spent on all employees in a year is {sum}$."

b) Change your previous program to get the average monthly salary of all employees.


Your program should print:

  • "The average monthly salary of all employees is {average}$."

Answers and Explanations

Question 5

[Maximum mark: 7]



Consider two arrays companies and incomes. Incomes of companies are given in billion USD.

Companies Incomes
Samsung 220.1
HP 16.7
Sony 215.4
Bosch 67.9
Siemens 31.2
Apple 358.4

The government is interested in calculating the average revenue it is going to get from collecting income taxes on those companies. It is known that there are the following tax brackets:

Income Tax
below 1 billion 12%
1 billion to 50 billion 15%
50 billion to 100 billion 20%
more than 100 billion 24%

Assume that the company pays the tax simply as the amount of their income multiplied by the tax rate. So for example, Samsung would pay \( 220.1 \times 0.24 = 52.8 \text{billion} \).



Create a Java program which will calculate the average tax amount which the companies will have to pay.


Your program should print:

  • "The average tax amount is {average} billion dollars."

Answers and Explanations

Question 6

[Maximum mark: 5]



Consider an array of numbers as shown below:

  • numbers: {2,3,7,8,5,10,12,45,34,12}

Create a Java program which will find the number of numbers divisible by 2 and divisible by 3.


Your program should print:

  • "There are {div_two} numbers divisible by 2 and {div_three} numbers divisible by 3."

Answers and Explanations

Question 7

[Maximum mark: 7]



Consider an array of temperatures recorded over a week in degrees Celsius:

  • temperatures: {15.5, 18.2, 19.8, 22.1, 20.5, 16.3, 14.8}

a) Create a Java program which will find and print the highest and lowest temperatures of the week.


Your program should print:

  • "The highest temperature of the week is {highest}°C."
  • "The lowest temperature of the week is {lowest}°C."

b) Modify your program to calculate the average temperature of the week. If the average temperature is above 18°C, print "It was a warm week.". Otherwise, print "It was a cool week."


Your program should print:

  • "The average temperature of the week is {average}°C. It was a warm week." or "The average temperature of the week is {average}°C. It was a cool week."

Answers and Explanations

Question 8

[Maximum mark: 6]



Consider arrays representing sales figures for different products in a store. Your task is to create a Java program that:


  • Uses predefined arrays for sales amounts, product names
  • Finds and prints the product with the highest sales amount.
  • Calculates and prints the total sales across all products.

The parallel arrays look as follows:

  • products: {"Laptop", "Headphones", "Monitor", "Mouse", "Keyboard"}
  • sales: {1200.0, 850.5, 1120.75, 750.0, 980.25}

Your program should print:

  • "The product with the highest sales is {product}.
  • "The total sales across all products is {sum}.

Answers and Explanations

Question 9

[Maximum mark: 6]



Consider arrays representing grades, subjects, and students. Your task is to create a Java program that:


  • Uses predefined arrays for grades, subjects, and students.
  • Finds and prints the student with the highest grade in the subject "Mathematics".
  • Calculates and prints the total number of students who have grades recorded for the subject "Mathematics".

The parallel arrays look as follows:

  • grades: {5, 6, 4, 5, 7, 5, 4}
  • subjects: {"English", "Mathematics", "English", "Biology", "Mathematics", "Chemistry", "Mathematics"}
  • students: {"John", "Alice", "Mark", "Tom", "Sue", "Mike", "Sarah"}

Your program should print:

  • "The student with the highest grade in Mathematics is {student} with a grade of {grade}."
  • "Total number of students with grades in Mathematics: {count}."

Answers and Explanations

Question 10

[Maximum mark: 6]



Consider the following array of integers:

  • numbers: {2,4,5,1,8,11,13, 22,17}

a) Write a Java program which calculates the average of all odd numbers from the given array.


Your program should print:

  • "The average of all odd numbers from this set is {average}."

b) Write a Java program which compares the averages of odd and even numbers in this array and prints which one is bigger, and by how much


Your program should print:

  • The {odd or even} numbers have a greater average, and they differ by {difference}.

Answers and Explanations