Skip to content
Laviyx Learn

Try

Java Arrays and Strings Practice

Practise Java arrays, 2D arrays, String methods, StringBuilder and wrapper classes on everyday tasks.

Beginner8 problemsUpdated

Review first: Arrays, 2D Arrays, Strings, StringBuilder, Wrapper Classes and Autoboxing

  1. 1.Marks statistics

    Beginner

    Given int[] marks = {56, 91, 78, 34, 88};, print the total, the average and the highest mark.

    Show hint

    Loop once, adding to a total and tracking the highest so far. Cast before dividing for the average.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int[] marks = {56, 91, 78, 34, 88};
            int total = 0;
            int highest = marks[0];
            for (int m : marks) {
                total += m;
                if (m > highest) highest = m;
            }
            System.out.println("Total: " + total);
            System.out.println("Average: " + (double) total / marks.length);
            System.out.println("Highest: " + highest);
        }
    }
    Output
    Total: 347
    Average: 69.4
    Highest: 91
  2. 3.Row and column totals

    Beginner

    For the grid {{3, 1, 4}, {1, 5, 9}, {2, 6, 5}}, print each row's total on one line, then each column's total on the next.

    Show hint

    For column totals, put the column loop on the outside.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int[][] grid = {{3, 1, 4}, {1, 5, 9}, {2, 6, 5}};
    
            StringBuilder rows = new StringBuilder();
            for (int[] row : grid) {
                int sum = 0;
                for (int v : row) sum += v;
                rows.append(sum).append(' ');
            }
            System.out.println(rows.toString().trim());
    
            StringBuilder cols = new StringBuilder();
            for (int c = 0; c < grid[0].length; c++) {
                int sum = 0;
                for (int[] row : grid) sum += row[c];
                cols.append(sum).append(' ');
            }
            System.out.println(cols.toString().trim());
        }
    }
    Output
    8 15 13
    6 12 18
  3. 4.Count the vowels

    Beginner

    Count the vowels (either case) in "Programming In Java Is Fun".

    Show hint

    Lower-case the text, loop over toCharArray(), and check "aeiou".indexOf(c) >= 0.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            String text = "Programming In Java Is Fun";
            int count = 0;
            for (char c : text.toLowerCase().toCharArray()) {
                if ("aeiou".indexOf(c) >= 0) count++;
            }
            System.out.println(count);
        }
    }
    Output
    8
  4. 5.Palindrome check

    Beginner

    Write static boolean isPalindrome(String s) that ignores spaces and case, and test it with "Never odd or even" and "Java".

    Show hint

    Clean the string, then compare it with new StringBuilder(cleaned).reverse().toString() using equals.

    Show solution
    Java
    public class Main {
        static boolean isPalindrome(String s) {
            String cleaned = s.replace(" ", "").toLowerCase();
            return cleaned.equals(new StringBuilder(cleaned).reverse().toString());
        }
    
        public static void main(String[] args) {
            System.out.println(isPalindrome("Never odd or even"));
            System.out.println(isPalindrome("Java"));
        }
    }
    Output
    true
    false
  5. 6.Split an email address

    Beginner

    Split "asha.rao@example.com" into the user part and the domain, and print the domain in upper case.

    Show hint

    indexOf('@') finds the split point; substring extracts each part.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            String email = "asha.rao@example.com";
            int at = email.indexOf('@');
            String user = email.substring(0, at);
            String domain = email.substring(at + 1);
            System.out.println(user);
            System.out.println(domain.toUpperCase());
        }
    }
    Output
    asha.rao
    EXAMPLE.COM
  6. 7.Build a string efficiently

    Beginner

    Use a StringBuilder to build "1, 2, 3, 4, 5, 6, 7, 8, 9, 10" and print it with its length.

    Show hint

    Append a separator before every number except the first.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= 10; i++) {
                if (i > 1) sb.append(", ");
                sb.append(i);
            }
            System.out.println(sb);
            System.out.println(sb.length());
        }
    }
    Output
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    29
  7. 8.Comparing Integer objects

    Beginner

    Box 1000 into two Integer variables and compare them with == and with equals. Then do the same with 100. Explain the results.

    Show hint

    Integer caches values from -128 to 127.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            Integer a = 1000;
            Integer b = 1000;
            Integer c = 100;
            Integer d = 100;
            System.out.println((a == b) + " " + a.equals(b));
            System.out.println((c == d) + " " + c.equals(d));
        }
    }
    Output
    false true
    true true

    == compares references. Small values come from the Integer cache, so c and d are the same object; 1000 is boxed into two different objects. Always use equals to compare wrapper values.