Blog Detail

September 23, 2023

Java Data Types Exercises – Part 3

  • What is type casting in Java?



  • What will be the value of the roundedPi ?

                    class Main {
      public static void main(String[] args) {
        double pi = 3.14159;
        int roundedPi = (int) pi;  
    
        System.out.println("Rounded Pi: " + roundedPi);
      }
    }
  • What will be the value of the pi variable?

                    class Main {
      public static void main(String[] args) {
        double pi = 3.14159;
        int roundedPi = (int) pi;  
    
        System.out.println("Rounded Pi: " + roundedPi);
        System.out.println("Pi: " + pi);
      }
    }
    
  • Cast the string into integer.

                    class Main {
      public static void main(String[] args) {
        String resolution = "72";
        int result = ""
      }
    }
  • Cast the string into double

                    class Main {
      public static void main(String[] args) {
        String weight = "57.5";
        double result = "".parseDouble(weight);  
      }
    }
  • What is the output of this code?

                    class Main {
      public static void main(String[] args) {
        String bankBalance = "$2500.30";
        double result = Double.parseDouble(bankBalance);
    
        System.out.println(result);
      }
    }
  • What is the output of this code?

                    class Main {
      public static void main(String[] args) {
        String population = "1,000,000";
        double result = Integer.parseInt(population);
    
        System.out.println(result);
      }
    }
  • Debug this code.

                    class Main {
      public static void main(String[] args) {
        String capacity = "102";
        int result = ""(capacity) + 5;
    
        System.out.println(result);
      }
    }
  • Java Exercises for Data Types

    • If you’ve faced difficulties with these exercises, take a look at Data Types Module on my Java course.


    You can also share ‘Java Data Types’ Exercises on your:
    X (Twitter)TelegramLinkedinFacebook – or on your favorite platform…




    Happy Coding!
    Behnam Khani