Learn Java Easy. Chapter 16 Exercises

Chapter 16 Exercises

Exercises Topic 16 – For-Each Loops in Java

Exercises Topic 16

Exercise 1. Iterate over an array of integers and print each number on a new line.

 

Solution:

 

public class Main {

       public static void main(String[] args) { 

                  int[] numbers = {1, 2, 3, 4, 5};

                  for (int number : numbers) { 

                         System.out.println(number);

                   }

         }

}

Exercise 2. Iterate over a String array and print each string on a new line.

 

Solution:

 

public class Main {

       public static void main(String[] args) {

                String[] words = {"Hello", "World", "Java", "Programming"};

                 for (String word : words) { 

                        System.out.println(word);

                  }

        }

}

Exercise 3. Sum all elements of an integer array and print the result.

 

Solution:

 

public class Main {

          public static void main(String[] args) {

                 int[] numbers = {1, 2, 3, 4, 5}; 
                 int sum = 0;

                 for (int number : numbers) { 

                      sum += number;

                 }

                System.out.println("Sum: " + sum);

          }

}

Exercise 4. Find the maximum value in an integer array and print the result.

 

Solution:

 

public class Main {

         public static void main(String[] args) { 

                 int[] numbers = {1, 2, 3, 4, 5};

                 int max = numbers[0];

                 for (int number : numbers) {

                       if (number > max) { 

                              max = number;

                       }

                  }

                  System.out.println("Maximum: " + max);

         }

}

Exercise 5. Count how many times a specific number appears in an integer array.

 

Solution:

 

public class Main {

      public static void main(String[] args) {

             int[] numbers = {1, 2, 3, 2, 4, 2, 5}; 

             int numberToFind = 2;

             int count = 0;

             for (int number : numbers) {

                   if (number == numberToFind) { 

                       count++;

                   }

              }

             System.out.println("The number " + numberToFind + " appears " + count + " times.");

       }

}

Exercise 6. Print the positions of a specific number in an integer array.

 

Solution:

 

public class Main {

    public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 2, 4, 2, 5}; 
        int numberToFind = 2;

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == numberToFind) {
                System.out.println("The number " + numberToFind + " is at position " + i);
            }
        }

    }

}

Exercise 7: Create an integer array and calculate the average of its elements.

 

Solution:

 

public class Main {

    public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 4, 5};
        int sum = 0;

        for (int number : numbers) {
            sum += number;
        }

        double average = (double) sum / numbers.length;
        System.out.println("Average: " + average);
    }

}

Exercise 8. Iterate over a String array and convert each string to uppercase, then print them.

 

Solution:

 

public class Main {

    public static void main(String[] args) {

        String[] words = {"hello", "world", "java", "programming"};
        
        for (String word : words) {
            System.out.println(word.toUpperCase());
        }

    }

}

As we have seen, to convert text to uppercase in Java, we use the toUpperCase() method of the String class. This method takes a text string and returns a new string with all letters converted to uppercase. Here’s a simple explanation of how to do it. Let’s see another simple example:

String text = "hello world";

String textInUppercase = text.toUpperCase(); System.out.println(textInUppercase);

Exercise 9. Iterate over an integer array and multiply each element by 2, printing the results.

 

Solution:

 

public class Main {

    public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 4, 5};

        for (int number : numbers) {
            System.out.println(number * 2);
        }

    }

}

Exercise 10. Iterate over an integer array and print only the even numbers.

 

Solution:

public class Main {

    public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        int evenCount = 0;

        for (int number : numbers) {
            if (number % 2 == 0) {
                evenCount++;
            }
        }

        System.out.println("Number of even elements: " + evenCount);
    }

}

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏