Skip to content
Laviyx Learn

Try

Java Basics Practice

Practise Java variables, data types, arithmetic, integer division, casting, string conversion and formatted output.

Beginner7 problemsUpdated

Review first: Variables, Data Types, Operators, Type Casting, Constants and final

  1. 1.Split the bill

    Beginner

    A dinner costs 1850 rupees and 4 friends split it equally. Print each share with exactly two decimal places, like Each person pays 462.50.

    Show hint

    Divide as a double, and format with System.out.printf("%.2f%n", value).

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            double total = 1850;
            int people = 4;
            System.out.printf("Each person pays %.2f%n", total / people);
        }
    }
    Output
    Each person pays 462.50
  2. 2.Minutes to hours and minutes

    Beginner

    A film lasts 135 minutes. Print it as 2 h 15 min.

    Show hint

    Integer / gives whole hours; % gives the remaining minutes.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int minutes = 135;
            System.out.println(minutes / 60 + " h " + minutes % 60 + " min");
        }
    }
    Output
    2 h 15 min
  3. 3.Fix the average

    Beginner

    The average of the marks 7, 8 and 10 should be about 8.33, but int sum = 25; double avg = sum / 3; gives 8.0. Print the wrong result and the correct one.

    Show hint

    Cast one operand to double before dividing.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int sum = 7 + 8 + 10;
            double wrong = sum / 3;
            double right = (double) sum / 3;
            System.out.println(wrong);
            System.out.println(right);
        }
    }
    Output
    8.0
    8.333333333333334

    Java prints a double with as many digits as needed to identify it exactly, which is why the correct average shows many 3s. Use printf("%.2f", ...) to round the display.

  4. 4.Text to numbers

    Beginner

    Given String a = "42"; and String b = "8";, print what a + b gives, then convert both with Integer.parseInt and print their real sum.

    Show hint

    + joins strings. Convert first, then add.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            String a = "42";
            String b = "8";
            System.out.println(a + b);
            System.out.println(Integer.parseInt(a) + Integer.parseInt(b));
        }
    }
    Output
    428
    50
  5. 5.Avoid integer overflow

    Beginner

    A platform has 2_500_000 users who each watch 1_200 seconds a day. Print the total seconds per day correctly, and also print what happens if you multiply two ints.

    Show hint

    Cast to long before multiplying: (long) users * seconds.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int users = 2_500_000;
            int seconds = 1_200;
            long correct = (long) users * seconds;
            int overflowed = users * seconds;
            System.out.println(correct);
            System.out.println(overflowed);
        }
    }
    Output
    3000000000
    -1294967296

    3,000,000,000 is larger than Integer.MAX_VALUE (2,147,483,647), so the int multiplication wraps around to a negative number. Java does not report this error unless you use Math.multiplyExact.

  6. 6.Characters are numbers

    Beginner

    Given char letter = 'C';, print the next letter and the numeric code of letter.

    Show hint

    letter + 1 is an int; cast it back with (char).

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            char letter = 'C';
            char next = (char) (letter + 1);
            int code = letter;
            System.out.println(next + " " + code);
        }
    }
    Output
    D 67
  7. 7.Round, floor and ceil

    Beginner

    For the price 249.75, print Math.round, Math.floor, Math.ceil and the value cast to int.

    Show hint

    Math.round returns a long; floor and ceil return doubles; a cast truncates.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            double price = 249.75;
            System.out.println(Math.round(price));
            System.out.println(Math.floor(price));
            System.out.println(Math.ceil(price));
            System.out.println((int) price);
        }
    }
    Output
    250
    249.0
    250.0
    249