QUESTION : 1
What is the result of compiling and running this program?
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay");
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
}
1. prints "Mammal eats food"
2. prints "Cattle eats hay"
3. prints "Horse eats hay"
4. Class cast Exception at runtime.
ANS : 1
The method that will be called is the one
from class Mammal. The reasons are quite obvious.
QUESTION : 2
Comsider the following class hierarchy.
1. interface A{
2. public void method1();
3. }
4. class One implements A{
5. public void method1(){
6. System.out.println("hello");
7. }
8. }
9. class Two extends One{}
10. public class Test extends Two{
11. public static void main(String[] args)
12. {
13. A a;
14. Two t = new Two();
15. a = t;
16. a.method1();
17. }
18. }
What will be the outcome on attempting to compile and run this ?
1. Compiles and runs printing out "hello".
2. Compilation error at line 16.
3. The compiler raises an objection to the assignment at line 15.
4. Throws a NoSuchMethodException at runtime.
ANS : 1
Object reference conversion is possible here.
The old type which is class can be assigned
to an interface type as long as the class implements
that interface.
QUESTION : 3
What will happen if you try to compile and run this ?
interface A{
public void innerMeth();
}
public class Test {
A a;
int memVar = 1;
void aMethod(){
a = new A(){
public void innerMeth(){
System.out.println(memVar);
} };
}
public static void main(String[] args){
Test t = new Test();
t.a.innerMeth();
}
}
1. Compiler error.
2. NoSuchMethodException at runtime.
3. Compiles and runs printing 1
4. Throws a NullPointerException at runtime.
ANS : 4
You will get a NullPointerException because the
inner class object gets assigned to the reference a
only after the aMethod() runs. You can prevent
the exception by calling t.aMethod() before the
inner anonymous class method is called.
QUESTION : 4
What will happen if you try to compile and run this code.
class Rectangle{
public int area(int length , int width) {
return length * width;
}
}
class Square extends Rectangle{
public int area(long length , long width) {
return (int) Math.pow(length ,2);
}
}
class Test{
public static void main(String args[]) {
Square r = new Square();
System.out.println(r.area(5 , 4));
}
}
1. Will not compile.
2. Will compile and run printing out 20
3. Runtime error
4. Will compile and run printing out 25
ANS : 1
This code will fail to compile because the
compiler cannot resolve the method call here.
QUESTION : 5
What will be the result of attempting to compile and run this.
class Base{}
class Derived extends Base{}
public class Test {
public static void main(String[] args){
Derived d = (Derived) new Base();
}
}
1. Will not compile
2. Compiles and runs without error.
3. Runtime error
ANS : 3
QUESTION : 6
What will this program print out ?
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
ANS : 4
QUESTION : 7
Almost the same code as in the previous question.
The only difference is the methods are static now.
What will it print now?
class Base{
static int value = 0;
Base(){
addValue();
}
static void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
static void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
ANS : 3
QUESTION : 8
What is the result of attempting to compile and run this ?
interface ITest{
public void setVal();
}
public class Test {
private String a;
void aMethod(){
final String b;
ITest it = new ITest() {
public void setVal(){
a = "Hello";
b = " World";
}};
it.setVal();
System.out.println(a + b);
}
public static void main(String[] args) {
Test t = new Test();
t.aMethod();
}
}
1. Code will not compile
2. Run time error
3. Will compile and run printing "Hello"
4. Will compile and run without any output
ANS : 1
QUESTION : 9
What is the result of attempting to compile and run this ?
class Base{
String s = "Base";
String show(){
return s;
}
}
class Derived extends Base{
String s = "Derived";
}
public class Test {
void print(Base b){
System.out.println(b.show());
}
void print(Derived d){
System.out.println(d.show());
}
public static void main(String[] args){
Test t = new Test();
Base b = new Derived();
t.print(b);
}
}
1. Code will not compile
2. Run time error
3. Will compile and run printing "Derived"
4. Will compile and run printing "Base"
ANS : 4
QUESTION : 10
What is the result of attempting to compile and run this ?
interface ITest{
public void setVal();
}
public class Test {
private String a;
void aMethod(){
final String b = " World";
ITest it = new ITest() {
public void setVal(){
a = "Hello" + b;
}};
it.setVal();
System.out.println(a);
}
public static void main(String[] args) {
Test t = new Test();
t.aMethod();
}
}
1. Code will not compile
2. Run time error
3. Will compile and run printing "Hello World"
4. Will compile and run printing "Hello"
ANS : 3
March 11, 2008
Object oriented programing in Java
No Comments Yet »
No comments yet.
RSS feed for comments on this post. TrackBack URI
