Java Questions & Answers – Type Conversions, Promotions and Castings

This section of our 1000+ Java MCQs focuses on Type conversions, promotions and castings of Java Programming Language.

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
View Answer

Answer: b
Explanation: None.

2. What is the prototype of the default constructor of this Java class?

    public class prototype { }

a) prototype( )
b) prototype(void)
c) public prototype(void)
d) public prototype( )
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

3. What will be the error in the following Java 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
View Answer

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.
advertisement

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
View Answer

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

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

Answer: a
Explanation: Truncate means to trim some digits of a floating type number or a double type number from right. We can also truncate the decimal portion completely making it an integer type number.
advertisement

6. What will be the output of the following Java code?

  1.     class char_increment 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             char c1 = 'D';
  6.             char c2 = 84;
  7.             c2++;
  8.             c1++;
  9.             System.out.println(c1 + " "  + c2);
  10.         } 
  11.     }

a) E U
b) U E
c) V E
d) U F
View Answer

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 will be the output of the following Java code?

  1.     class conversion 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             double a = 295.04;
  6.             int  b = 300;
  7.             byte c = (byte) a;
  8.             byte d = (byte) b;
  9.             System.out.println(c + " "  + d);
  10.         } 
  11.     }

a) 38 43
b) 39 44
c) 295 300
d) 295.04 300
View Answer

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 will be the output of the following Java code?

  1.     class A 
  2.     {
  3.         final public int calculate(int a, int b) { return 1; } 
  4.     } 
  5.     class B extends A 
  6.     { 
  7.         public int calculate(int a, int b) { return 2; } 
  8.     } 
  9.      public class output 
  10.      {
  11.         public static void main(String args[]) 
  12.         { 
  13.             B object = new B(); 
  14.             System.out.print("b is " + b.calculate(0, 1));  
  15.         } 
  16.     }

a) b is : 2
b) b is : 1
c) Compilation Error
d) An exception is thrown at runtime
View Answer

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 will be the output of the following Java program, if we run as “java main_arguments 1 2 3”?

  1.     class main_arguments 
  2.     {
  3.         public static void main(String [] args) 
  4.         {
  5.             String [][] argument = new String[2][2];
  6.             int x;
  7.             argument[0] = args;
  8.             x = argument[0].length;
  9.             for (int y = 0; y < x; y++) 
  10.                 System.out.print(" " + argument[0][y]);              
  11.         }
  12.     }

a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
View Answer

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 will be the output of the following Java program?

  1.     class c 
  2.     {    
  3.         public void main( String[] args ) 
  4.         {  
  5.             System.out.println( "Hello" + args[0] ); 
  6.         } 
  7.     }

a) Hello c
b) Hello
c) Hello world
d) Runtime Error
View Answer

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

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.