62). 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
Show Answer
Correct Answer: The destination type is larger than source type
Notes:
Answer: b
Explanation: None.
63). 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( )
Show Answer
Correct Answer: public prototype( )
Notes:
Answer: d
Explanation: None.
64). 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
Show Answer
Correct Answer: * operator has converted b * 50 into int, which can not be converted to byte without casting
Notes:
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.
65). If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?
byte b = 50;
b = b * 50;
[A]long
[B]int
[C]double
[D]float
Show Answer
Correct Answer: double
Notes:
Answer: c
Explanation: If any operand is double the result of an expression is double.
66). What is Truncation in Java?
byte b = 50;
b = b * 50;
[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
Show Answer
Correct Answer: Floating-point value assigned to an integer type
Notes:
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.
67). 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
Show Answer
Correct Answer: E U
Notes:
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
68). 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
Show Answer
Correct Answer: 39 44
Notes:
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
69). 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
Show Answer
Correct Answer: Compilation Error
Notes:
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.
70). 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
Show Answer
Correct Answer: 1 2 3
Notes:
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
71). 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
Show Answer
Correct Answer: Runtime Error
Notes:
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