Java MCQs on Type conversions, promotions and castings of Java Programming Language.
1. Which of these is necessary condition for automatic type conversion in Java? Answer: b 2. What is the prototype of the default constructor of this Java class? a) prototype( ) Answer: d 3. What will be the error in the following Java code? a) b cannot contain value 100, limited by its range Answer: b 4. If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types? Answer: c 5. What is Truncation is Java? Answer: a 6. What will be the output of the following Java code? a) E U 7. What will be the output of the following Java code? a) 38 43 8. What will be the output of the following Java code? a) b is : 2 Answer: c 9. What will be the output of the following Java program, if we run as “java main_arguments 1 2 3”? a) 1 1 10. What will be the output of the following Java program? a) Hello c
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
Clarification: None. public class prototype { }
b) prototype(void)
c) public prototype(void)
d) public prototype( )
Clarification: None.
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
Clarification: 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.
a) long
b) int
c) double
d) float
Clarification: If any operand is double the result of an expression is double.
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
Clarification: None.
class char_increment
{
public static void main(String args[])
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
System.out.println(c1 + " " + c2);
}
}
b) U E
c) V E
d) U F
Clarification: 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
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);
}
}
b) 39 44
c) 295 300
d) 295.04 300
Clarification: 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
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));
}
}
b) b is : 1
c) Compilation Error
d) An exception is thrown at runtime
Clarification: The code does not compile because the method calculate() in class A is final and so cannot be overridden by method of class b.
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]);
}
}
b) 1 0
c) 1 0 3
d) 1 2 3
Clarification: 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
class c
{
public void main( String[] args )
{
System.out.println( "Hello" + args[0] );
}
}
b) Hello
c) Hello world
d) Runtime Error
Clarification: 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
