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
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
Explanation: None.
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
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
View Answer
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
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.
6. What will be the output of the following Java code?
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
View Answer
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?
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
View Answer
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?
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
View Answer
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”?
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
View Answer
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?
class c
{
public void main( String[] args )
{
System.out.println( "Hello" + args[0] );
}
}
a) Hello c
b) Hello
c) Hello world
d) Runtime Error
View Answer
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.
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy Programming Books
- Apply for Information Technology Internship
- Practice BCA MCQs
- Apply for Java Internship
- Buy Java Books