Classes and Objects - OCJP
Classes and Objects - OCJP
Polymorphism
Explanation:
Var-args is always chosen last.
You cannot widen then autobox (cannot widen from int to long then autobox to Long), BUT you
can autobox then widen (autobox to Integer then widen to Number, Integer extends Number)
therefore “Number x, Number y” is printed
6 -1
Explanation:
Class Hotel don't have any method with book(int) signature.
5. Which statement(s), inserted independently at // insere code here, will compile? (choose
three) (choose 3)
class Creature {
Creature getIt() {
return this;
}
}
class Bird extends Creature {
// insere code here
}
class Falcon extends Bird {
Explanation:
When overriding you cannot reduce the access scope, you cannot change scope from package to
private or public to preotected.
Creature getIt(Creature c) is an overloading not an overriding and it is correctly written.
The return type of the overriding method must be the same as, or a subtype of, the return type
of the overridden method.
6. Which, inserted independently at //insert overridden call() here, will represent an overridden
call() and compile with no error? (choose 3)
class Base {
void call() throws IllegalArgumentException {
}
}
public class Derived extends Base {
//insert overridden call() here
}
Explanation:
An overriding method can throw the same exceptions, narrower exceptions, or no exceptions
and can throw any runtime exceptions whether they were listed in the overridden method or
not.
An overriding method cannot reduce the access scope of the overridden method but can widen
it.
7. o/p of this program is 35. Which method executes, & whether it is method Overloading or
method Overriding?
class BaseI{
public int doSum(short a, short b){ // #1
return(a+b);
}
}
public class DerivedI extends BaseI{
public int doSum(int a,short b){ // #2
return(a+b);
}
public int doSum(short a, short b){ // #3
return(a+b);
}
public static void main(String []args){
DerivedI d = new DerivedI();
short x =15;
short y =20;
System.out.println(d.doSum(x,y));
}
}
Explanation:
Output of this code is 97. we can return any value that can be implicitly converted to the declared
return type. Since char can be implicitly converted to long, returning 'a' gives 97; which is ASCII /
Unicode value of 'a'.
9. Missing QUE?
public class Tester {
static void call(Long x, Long y) {
System.out.print("Long x, Long y");
}
static void call(int... x) {
System.out.print("int... x");
}
public static void main(String[] args) {
int val = 3;
call(val, val);
}
}
Explanation:
Var-args is always chosen last, but since you cannot widen then autobox (cannot widen from int to
long then autobox to Long), int... x is printed
10. What is the result of compiling and running this program ?If compiles successfully assume
"java Dont" at command line
class Test{
Test(){
System.out.println("Hello");
}
public void showItem(){
}
}
abstract class A extends Test {
A(){
System.out.println("Hi everyOne");
}
abstract public void showItem();
}
class Dont extends A {
public void showItem(){
}
public static void main(String... args){
new Dont();
}
}
11. What is the result of compiling and running the following code?
class Base {
public final int getNext(int i) {
return ++i;
}
}
public class Derived extends Base {
public int getNext(int i) {
return i++;
}
public static void main(String[] args) {
int result = new Derived().getNext(3);
System.out.print(result);
result = new Base().getNext(3);
System.out.print(result);
}
}
Explanation:
Final methods cannot be overridden
12. What is the result of compiling and running the following code?
class Base {
public void method(final int x) {
System.out.print("Base");
}
}
public class Derived extends Base {
public void method(int x) { // line 1
System.out.print("Derived");
}
public static void main(String[] args) {
Base b = new Derived();
b.method(3);
}
}