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.
-
-
- 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);
- Give an example for each of the following type conversions and include a comment to indicate how the conversion takes place.
- Explicit type conversion
- Automatic type conversion
- Explicit casts
- Automatic conversions for strings
- Write a line of code to divide a variable num of type double by zero. What is the output?
- What do each of the following print? Explain each outcome.
- System.out.println(2 + “bc”); prints: 2bc
- System.out.println(2 + 3 + “bc”); prints: 5bc
- System.out.println((2+3) + “bc”); prints: 5bc
- System.out.println(“bc” + (2+3)); prints: bc5
- System.out.println(“bc” + 2 + 3); prints: bc23
- Explain how to use Quadratic.java to find the square root of a number.
- What do each of the following print?
- System.out.println(‘b’);
- System.out.println(‘b’ + ‘c’);
- System.out.println((char) (‘a’ + 4));
- 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.
- System.out.println(a);
- System.out.println(a + 1);
- System.out.println(2 – a);
- System.out.println(-2 – a);
- System.out.println(2 * a);
- System.out.println(4 * a);
- In which way are the variables “x” and “a” alike in the following code snippet?
-