MCQs
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
What is the output of this program?
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));
}
}
The code does not compile because the method calculate() in class A is final and so cannot
be overridden by method of class b.
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
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
None.
If any operand is double the result of expression is double.
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.
None.
None.