Automatic type promotion questions on java and answer with solution

Java objective questions on automatic type promotion and answers  

(1)

public class TypeConversion {
    public static void main(String[] args) {
         {
             final int a='\15';
             System.out.println(a);
         }
         int a=25;byte b=0100;
         a=a+b;
         System.out.println(a);
    }
}

What will output when you compile and run the above code?

(a)15
   125
(b)13
   89
(c)Run time exception
(d) Compiler error

(2)

public class TypeConversion {
    public static void main(String[] args) {
         float f=12e-1F;
         final long l=12L;
         f=f+l;
         System.out.println(f);
    }
}

What will output when you compile and run the above code?

(a)18.9
(b)13.2
(c)Run time exception
(d) Compiler error





Answer: (b)

(3)

public class TypeConversion {
    public static void main(String[] args) {
         char a=5;
         short b=-++a;
         System.out.println(b);
    }
}

What will output when you compile and run the above code?

(a)-4
(b)-5
(c)-6
(d) Compiler error





Answer: (d)

(4)

public class TypeConversion {
    public static void main(String[] args) {
         boolean b=11>=11;
         String str=(String)b;
         System.out.println(str);
    }
}

What will output when you compile and run the above code?

(a)true
(b)false
(c)”true
(d) Compiler error





Answer: (d)

(5)

public class TypeConversion {
    public static void main(String[] args) {
         byte b=016;
         short s=0x16;
         char c='\16';
         int a=b+s+c;
         System.out.println(a);
    }
}

What will output when you compile and run the above code?

(a)48
(b)50
(c)Run time exception
(d) Compiler error





No comments: