Java Multiple Choice Questions & Answers on Type Conversions, Promotions and Castings for Freshers

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
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
3. What is the error in this code?
byte b = 50;
b = b * 50;

a) b can not 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 can not 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 result is also of type int.
4. If an expression contains double, int, float, long, then whole expression will 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 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
6. What is the output of this program?
  1.     class char_increment {
  2.         public static void main(String args[]) 
  3.         {
  4.             char c1 = 'D';
  5.             char c2 = 84;
  6.             c2++;
  7.             c1++;
  8.             System.out.println(c1 + " "  + c2);
  9.         } 
  10.     }
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.
7. What is the output of this program?
  1.     class conversion {
  2.         public static void main(String args[]) 
  3.         {
  4.             double a = 295.04;
  5.             int  b = 300;
  6.             byte c = (byte) a;
  7.             byte d = (byte) b;
  8.             System.out.println(c + " "  + d);
  9.         } 
  10.     }
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.
8. What is the output of this program?
  1.     class A {
  2.         final public int calculate(int a, int b) { return 1; } 
  3.     } 
  4.     class B extends A { 
  5.         public int calculate(int a, int b) { return 2; } 
  6.     } 
  7.      public class output {
  8.         public static void main(String args[]) 
  9.         { 
  10.             B object = new B(); 
  11.             System.out.print("b is " + b.calculate(0, 1));  
  12.         } 
  13.     }
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”?
  1.     class main_arguments {
  2.         public static void main(String [] args) 
  3.         {
  4.             String [][] argument = new String[2][2];
  5.             int x;
  6.             argument[0] = args;
  7.             x = argument[0].length;
  8.             for (int y = 0; y < x; y++) 
  9.                 System.out.print(" " + argument[0][y]);              
  10.         }
  11.     }
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.
10. What is the output of this program?
  1.     class c {    
  2.         public void main( String[] args ) 
  3.         {  
  4.             System.out.println( "Hello" + args[0] ); 
  5.         } 
  6.     }
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.

Related

Multiple Choice Questions 5933721942395176350

Post a Comment

emo-but-icon

item