Question on Type Conversions, Promotions and Castings



Question on Type Conversions, Promotions and Castings

1. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type
b) The destination type is larger than source type
c) The destination type can be larger or smaller than source type
d) None of the mentioned

Answer: b
Explanation: None.

2. What is the prototype of the default constructor of this class?
    public class prototype { }
a) prototype( )
b) prototype(void)
c) public prototype(void)
d) public prototype( )

Answer: d
Explanation: None.

3. What is the error in this code?
    byte b = 50;
    b = b * 50;
a) b cannot contain value 100, limited by its range
b) * operator has converted b * 50 into int, which can not be converted to byte without casting
c) b cannot contain value 50
d) No error in this code

Answer: b
Explanation: While evaluating an expression containing int, bytes or shorts, the whole expression is converted to int then evaluated and the result is also of type int.

4. If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?
a) long
b) int
c) double
d) float

Answer: c
Explanation: If any operand is double the result of an expression is double.

5. What is Truncation is Java?
a) Floating-point value assigned to an integer type
b) Integer value assigned to floating type
c) Floating-point value assigned to an Floating type
d) Integer value assigned to floating type

Answer: a
Explanation: None.

6. What is the output of this program?
    class char_increment
    {
        public static void main(String args[])
        {
            char c1 = 'D';
            char c2 = 84;
            c2++;
            c1++;
            System.out.println(c1 + " "  + c2);
        }
    }
a) E U
b) U E
c) V E
d) U F

Answer: a
Explanation: Operator ++ increments the value of character by 1. c1 and c2 are given values D and 84, when we use ++ operator their values increments by 1, c1 and c2 becomes E and U respectively.
output:
$ javac char_increment.java
$ java char_increment
E U

7. What is the output of this program?
    class conversion
    {
        public static void main(String args[])
        {
            double a = 295.04;
            int  b = 300;
            byte c = (byte) a;
            byte d = (byte) b;
            System.out.println(c + " "  + d);
        }
    }
a) 38 43
b) 39 44
c) 295 300
d) 295.04 300

Answer: b
Explanation: Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable. b contains 300 which is larger than byte’s range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44

8. What is the output of this program?
    class A
    {
        final public int calculate(int a, int b) { return 1; }
    }
    class B extends A
    {
        public int calculate(int a, int b) { return 2; }
    }
     public class output
     {
        public static void main(String args[])
        {
            B object = new B();
            System.out.print("b is " + b.calculate(0, 1)); 
        }
    }
a) b is : 2
b) b is : 1
c) Compilation Error
d) An exception is thrown at runtime

Answer: c
Explanation: The code does not compile because the method calculate() in class A is final and so cannot be overridden by method of class b.

9. What is the output of this program, if we run as “java main_arguments 1 2 3”?
    class main_arguments
    {
        public static void main(String [] args)
        {
            String [][] argument = new String[2][2];
            int x;
            argument[0] = args;
            x = argument[0].length;
            for (int y = 0; y < x; y++)
                System.out.print(" " + argument[0][y]);             
        }
    }
a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3

Answer: d
Explanation: In argument[0] = args;, the reference variable arg[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.
Output:
$ javac main_arguments.java
$ java main_arguments
1 2 3

10. What is the output of this program?
    class c
    {   
        public void main( String[] args )
        { 
            System.out.println( "Hello" + args[0] );
        }
    }
a) Hello c
b) Hello
c) Hello world
d) Runtime Error

Answer: d
Explanation: A runtime error will occur owning to the main method of the code fragment not being declared static.
Output:
$ javac c.java
Exception in thread "main" java.lang.NoSuchMethodError: main
Question on Type Conversions, Promotions and Castings Question on Type Conversions, Promotions and Castings Reviewed by Unknown on February 24, 2019 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.