Wednesday 25 July 2012

Object-Oriented concepts - Objective Questions - Part 1

1)

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
  public static void main(String args[]) {
    Base base = new Base();
    I1 i1 = base;           // 1
    Sub sub = (Sub)base;    // 2
}}

What is the result of attempting to compile and run the program?

A. Compile-time error at line 1
B. Compile-time error at line 2
C. Run-time error at line 2
D. None of the above

Answer: C

The compiler accepts the explicit cast at line 2, but an error is generated at run-time. Type Base is the super class of type Sub, so an instance of type Base can not be converted to the type of the subclass, Sub.


2)

What will be the output of the program?

class A
{
    final public int methodl(int a, int b) { return 0; }
}
class B extends A
{
    public int method1 (int a, int b) {return 1; }
}
public class Test
{
    public static void main(String args[])
    {
        B b=new B();
        System.out.println("x = " + b.method1(0, 1));
    }
}

A. x = 0
B. x = 1
C. Compilation fails.
D. An exception is thrown at runtime.


Answer: C

The code doesn't compile because the method in class A is final and so cannot be overridden.

3)

class Green {
  public static void main (String args[]) {
    int[] i = null;  // 1
    Cloneable c = i; // 2
    i = (int [])c;   // 3
}}

What is the result of attempting to compile and run the program?

A. Compile-time error at line 1.
B. Run-time error at line 2.
C. Run-time error at line 3.
D. None of the above

Answer: D

The null literal is converted to an int array type with the value null. All array types implement the Cloneable interface, so any array reference can be assigned to a reference of type Cloneable. The int array object referenced by the Cloneable reference, c, can be assigned to a reference of the int array type, int[].

4)

interface I1 {}  interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Red {
  public static void main(String args[]) {
    Sub s1 = new Sub();
    I2 i2 = s1;          // 1
    I1 i1 = s1;          // 2
    Base base = s1;      // 3
    Sub s2 = (Sub)base;  // 4
}}
A compile-time error is generated at which line?

A. 1
B. 2
C. 3&4
D. None of the above

Answer: D

Line 4 does not generate a runtime-time error. The reference named base actually refers to an instance of type Sub, so the reference may be cast to type Sub.

5)

public class Main{
 public static void main(String[] args) {
   B obj=new S();
   obj.dis(); }}
class B{
final void dis(){
System.out.println("From Base");
}}
class S extends B{
void dis(){
System.out.println("From Sub");
}}

What is the result of attempting to compile and run the program?

A. Prints From Base
B. Prints From Sub
C. Compile-time error
D. Run-time error

Answer: C

Final method dis() in class B can't be override in the sub class S.

6)

public class Main {
public static void main(String[] args) {

Main m = new Sub();
m.dis();
}
  void dis() {
System.out.println("From Main dis");
}
}
class Sub extends Main {
void dis() {
System.out.println("From Sub dis");
 }
}

A. Prints From Main dis
B. Prints From Sub dis
C. Compile-time error
D. Run-time error

Answer: B

The result is due to 'Dynamic method dispatch'.Dynamic method dispatch is run time polymorphism/late binding.
When you override a super class method in subclass the method which is to be executed is decided at the run time rather than at the compile time.So here during run-time the system will identify the type of the object 'm'  as 'Sub' class,and it will call Sub class dis() method.

7)

public class Main {
public static void main(String[] args) {
Main m = new Sub();
m.dis();
}
public void dis() {
System.out.println("From Main dis");
}
}
class Sub extends Main {
void dis() {
System.out.println("From Sub dis");
}
}

A. Prints From Main dis
B. Prints From Sub dis
C. Compile-time error
D. Run-time error

Answer: C

Cannot reduce the visibility of the inherited method ,dis() from Main .
Visibility level from High to Low

1.public
2.protected
3.default/package level
4.private

So when overriding a function the visibility of sub class function should be either same or greater than that of the base class method.

8)

public class Main{
public static void main(String args[]){
Base ob1=new Base();// line1
Base ob2=new Sub();// line 2
Sub ob3=new Sub(); //line 3
}}
abstract class Base{
public abstract void add();
}
 class Sub extends Base {
@Override
public void add() {}
}

A. Error at line 1
B. Error at line 2
C. Error at line 3
D. None of the above

Answer: A

Abstract class can't be instantiate ,so line 1 generate compile time error.But abstract class can be used for creating object reference as in line 2.

9)

interface A {int i = 1; int m1();}
interface B extends A {int i = 10; int m1();}
class C implements B {
  public int m1() {return ++i;}
  public static void main(String[] args) {
    System.out.print(new C().m1());
}}

What is the result of attempting to compile and run the program?

A. Prints: 2
B. Prints: 11
C. compile-time error
D. run-time error

Answer: C

Fields declared within an interface are implicitly public, final, and static. A compile-time error is generated in response to the attempt to increment the value of i.

10)

class GFC505 extends GFC504 {
  public void setName(String name) {this.name = name;}
  public String getName() {return name;}
}
class GFC504 extends GFC503 {
  private void setName(String name) {this.name = name;}
  private String getName() {return name;}
}
class GFC503 {String name;}

Which class is tightly encapsulated?

A. GFC503
B. GFC504
C. GFC505
D. None of the above

Answer: D

Class GFC503 is not tightly encapsulated; so no subclass of GFC503 is tightly encapsulated.

No comments:

Post a Comment