0% found this document useful (0 votes)
50 views

Classes and Objects - OCJP

Uploaded by

srinivas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Classes and Objects - OCJP

Uploaded by

srinivas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Polymorphism

Polymorphism

1. What is the result of compiling and running the following code?


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");
}
static void call(Number x, Number y) {
System.out.print("Number x, Number y");
}
public static void main(String[] args) {
int val = 3;
call(val, val);
}
}

No. Answers Correct


1 Long x, Long y
2 int... x
3 Number x, Number y
4 Compilation error
5 An exception is thrown at run time

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

2. What modification is necessary to produce the following output : SubType


class Type {
public Object getTypeName(){
return "Type";
}
}

class SubType extends Type {


public String getTypeName(){//line 8
return "SubType";
}
}

public class Tester {


public static void main(String[] args) {
Type first = new SubType(); //line 16
System.out.println(first.getTypeName()); //line 17
}
}

TalentASprint | © Copyright 2012 1


Polymorphism

No. Answers Correct


1 No modification is needed
2 At line 8 change ´the return type from String to Object to be a
correct overloading of getTypeName()
3 At line 16 change Type to SubType
4 At line 17 cast first.getTypeName() to String

3. Given the code. What is the result?


class Hotel {
public int bookings;
public void book() {
bookings++;
}
}
public class SuperHotel extends Hotel {
public void book() {
bookings--;
}
public void book(int size) {
book();
super.book();
bookings += size;
}
public static void main(String args[]) {
Hotel hotel = new SuperHotel();
hotel.book(2);
System.out.print(hotel.bookings);
}
}

No. Answers Correct


1 Compilation fails.
2 An exception is thrown at runtime.
3 0
4 1
5 2

6 -1
Explanation:
Class Hotel don't have any method with book(int) signature.

4. What will be the output :


class MySuper {
MySuper(){disp();}
void disp(){System.out.println("superclass");}
}
class MySub extends MySuper {
double i=Math.ceil(8.4f);
public static void main(String arg[]) {
MySuper obj= new MySub();
obj.disp();
}
void disp(){System.out.println(i);}
}

TalentASprint | © Copyright 2012 2


Polymorphism

No. Answers Correct


1 The program displays "superclass" followed by "9.0" as an output.
2 The program displays "superclass" followed by "superclass" as an output.
3 The program displays "9.0" followed by "9.0" as an output.
4 The program displays "0.0" followed by "9.0" as an output.

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 {

No. Answers Correct


1 Creature getIt() { return this;}
2 private Falcon getIt() { return new Falcon();}
3 Falcon getIt() {return this;}
4 public Bird getIt() {return this;}
5 Creature getIt(Creature c) {return this;}

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
}

TalentASprint | © Copyright 2012 3


Polymorphism

No. Answers Correct


1 public void call() throws IllegalArgumentException {}
2 void call() throws IllegalArgumentException,FileNotFoundException {}
3 void call() throws RuntimeException {}
4 void call() throws IllegalArgumentException,RuntimeException { }
5 private void call() {}

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));
}
}

No. Answers Correct


1 a) #1 & superclass method. No overloading or overriding
2 b) #2 & overriding
3 c) #2 & overloading
4 d) #3 & overriding
5 e) #3 & overloading
6 f) None of the above

TalentASprint | © Copyright 2012 4


Polymorphism

8. What is the o/p of the given code?


class Ret{
public long tryIt(){
long num = 25;
return num; // $1
}
}
class ReturnType extends Ret{
public long tryIt(){
char ch = 'a';
long num = 50;
return ch; // $2
}
public static void main(String []args){
Ret r = new ReturnType(); // $3
System.out.println(r.tryIt()); // $4
}
}

No. Answers Correct


1 a) 25
2 b) 50
3 c) Compilation Error at $2
4 d) Compilation Error at $3
5 e) Runtime Exception at $4
6 f) None of the above

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);
}
}

No. Answers Correct


1 Long x, Long y
2 int... x
3 Compilation error
4 An exception is thrown at run time

TalentASprint | © Copyright 2012 5


Polymorphism

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();
}
}

No. Answers Correct


1 Compile time error
2 an exception may be thrown
3 Hello
Hi everyOne

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);
}
}

TalentASprint | © Copyright 2012 6


Polymorphism

No. Answers Correct


1 33
2 34
3 44
4 43
5 a compilation error

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);
}
}

No. Answers Correct


1 will compile fine and will print "Derived"
2 will compile fine and will print "Base"
3 Won't compile because of line 1.Can be corrected by marking x as final, then the
output
will be "Derived"
4 Won't compile because of line 1.Can be corrected by marking x as final, then the
output will be "Base"

TalentASprint | © Copyright 2012 7

You might also like