Built-in Ex: Types Conversions

Screen Shot 2014-09-09 at 10.24.34 PM

Classwork:
Built-In Exercises – Type conversions and more

For this assignment write a java program, BuiltIn1_YI.java to produce the code necessary to answer all the questions clearly.

      1. In which way are the variables “x” and “a” alike in the following code snippet?
        int x;
        char a = 'z';
        System.out.println((int)a); 
        x = 't';
        System.out.println(x);
        
      2. Give an example for each of the following type conversions and include a comment to indicate how the conversion takes place.
        1. Explicit type conversion
        2. Automatic type conversion
        3. Explicit casts
        4. Automatic conversions for strings
      3. Write a line of code to divide a variable num of type double by zero. What is the output?
      4. What do each of the following print? Explain each outcome.
        1. System.out.println(2 + “bc”); prints: 2bc
        2. System.out.println(2 + 3 + “bc”); prints: 5bc
        3. System.out.println((2+3) + “bc”); prints: 5bc
        4. System.out.println(“bc” + (2+3)); prints: bc5
        5. System.out.println(“bc” + 2 + 3); prints: bc23
      5. Explain how to use Quadratic.java to find the square root of a number.
      6. What do each of the following print?
        1. System.out.println(‘b’);
        2. System.out.println(‘b’ + ‘c’);
        3. System.out.println((char) (‘a’ + 4));
      7. Suppose that a variable a is declared as int a = 2147483647 (or equivalently, Integer.MAX_VALUE). What do each of the following print? Explain each outcome.
        1. System.out.println(a);
        2. System.out.println(a + 1);
        3. System.out.println(2 – a);
        4. System.out.println(-2 – a);
        5. System.out.println(2 * a);
        6. System.out.println(4 * a);