MCQs
Option A is correct - because the class that extends A is just simply overriding
method1.
Option B is wrong - because it can't override as there are less access privileges
in the subclass method1.
Option C is wrong - because to override it, the return type needs to be an integer.
The different return type means that the method is not overriding but the same
argument list means that the method is not overloading. Conflict - compile time
error.
Option D is wrong - because you can't override a method and make it a class
method i.e. using static.
However A, B and C are all wrong. Each of these would result in a narrowing
conversion. Whereas we want a widening conversion, therefore the only correct
answer is D. Don't be put off by the long cast, this applies only to the variable x
and not the rest of the expression. It is the variable y (of type double) that forces
the widening conversion to double.
Java's widening conversions are:
- From a byte to a SHORT, an int, a long, a float, or a double.
- From a short, an int, a long, a float, or a double.
- From a char to an int, a long, a float, or a double.
- From an int to a long, a float, or a double.
- From a long to a float, or a double.
- From a float to a double.
b >> 1 in if returns 5 which is equal to a i:e 5, therefore body
of if is executed and block second
is exited. Control goes to end of
the block second executing the last print statement, printing 10.
output:
$ javac Output.java
$ java Output
10
class objects are always passed by reference, therefore changes done are reflected back
on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b
are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes
20 & 10 respectively.
output:
$ javac Output.java
$ java Output
20 10
Operator short circuit and, &&, skips evaluating right hand
operand if left hand operand is false thus
division by zero in if
condition does not give an error.
output:
$ javac Output.java
$ java Output
2
None.
Whenever y is divisible by x remainder body of loop is skipped by
continue statement, therefore
if condition y == 8 is never true as when y
is 8, remainder body of loop is skipped by continue
statements of first
if. Control comes to print statement only in cases when y is odd.
output:
$ javac jump_statments.java
$ java jump_statments
1 3 5 7 9
What is the output of this program?
class static_out {
static int x;
static int y;
void add(int a, int b){
x = a + b;
y = x + b;
}
}
class static_use {
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
None.
output:
$ javac static_use.java
$ java static_use
6 6.4
(2), (3), and (5). These are all valid interface method signatures.
(1), is incorrect because an interface method must be public; if it is not explicitly
declared public it will be made public implicitly. (4) is incorrect because interface
methods cannot be static.
The only two real contenders are C and D. Protected access Option C makes a
member accessible only to classes in the same package or subclass of the class.
While default access Option D makes a member accessible only to classes in the
same package.