Total Pageviews

Friday, 22 January 2016

Object Orientation – Interview Questions & Answers

Q.1 Given:
public abstract interface Frobnicate { public void twiddle(String s); }
Which is a correct class? (Choose all that apply.)
A. public abstract class Frob implements Frobnicate {
    public abstract void twiddle(String s) { }
}
B. public abstract class Frob implements Frobnicate { }
C. public class Frob extends Frobnicate {
    public void twiddle(Integer i) { }
}
D. public class Frob implements Frobnicate {
    public void twiddle(Integer i) { }
}
E. public class Frob implements Frobnicate {
    public void twiddle(String i) { }
    public void twiddle(Integer s) { }
}
Answer:
® ✓ B is correct, an abstract class need not implement any or all of an interface’s methods. E is correct, the class implements the interface method and additionally overloads the twiddle() method.
®˚ A is incorrect because abstract methods have no body. C is incorrect because classes implement interfaces they don’t extend them. D is incorrect because overloading a
method is not implementing it.
Q.2 Given:
class Top {
    public Top(String s) { System.out.print(“B”); }
}
public class Bottom2 extends Top {
    public Bottom2(String s) { System.out.print(“D”); }
    public static void main(String [] args) {
        new Bottom2(“C”);
         System.out.println(” “);
    } }
What is the result?
A. BD
B. DB
C. BDC
D. DBC
E. Compilation fails
Answer:
® ✓ E is correct. The implied super() call in Bottom2’s constructor cannot be satisfied because there isn’t a no-arg constructor in Top. A default, no-arg constructor is generated by the compiler only if the class has no constructor defined explicitly.
®˚ A, B, C, and D are incorrect based on the above.
Q.3 Given:
class Clidder {
    private final void flipper() { System.out.println(“Clidder”); }
}
public class Clidlet extends Clidder {
    public final void flipper() { System.out.println(“Clidlet”); }
    public static void main(String [] args) {
        new Clidlet().flipper();
} }
What is the result?
A. Clidlet
B. Clidder
C. Clidder
Clidlet
D. Clidlet
Clidder
E. Compilation fails
Answer:
® ✓ A is correct. Although a final method cannot be overridden, in this case, the method is private, and therefore hidden. The effect is that a new, accessible, method flipper is created. Therefore, no polymorphism occurs in this example, the method invoked is simply that of the child class, and no error occurs.
®˚ B, C, D, and E are incorrect based on the preceding.
Q.4 Which statement(s) are true? (Choose all that apply.)
A. Cohesion is the OO principle most closely associated with hiding implementation details
B. Cohesion is the OO principle most closely associated with making sure that classes now 
about other classes only through their APIs
C. Cohesion is the OO principle most closely associated with making sure that a class is  designed with a single, well-focused purpose
D. Cohesion is the OO principle most closely associated with allowing a single object to be seen as having many types
Answer:
® ✓ Answer C is correct.
®˚ A refers to encapsulation, B refers to coupling, and D refers to polymorphism.
Q.5 Given:
1. ClassA has a ClassD
2. Methods in ClassA use public methods in ClassB
3. Methods in ClassC use public methods in ClassA
4. Methods in ClassA use public variables in ClassB
Which is most likely true? (Choose the most likely.)
A. ClassD has low cohesion
B. ClassA has weak encapsulation
C. ClassB has weak encapsulation
D. ClassB has strong encapsulation
E. ClassC is tightly coupled to ClassA
Answer:
® ✓ C is correct. Generally speaking, public variables are a sign of weak encapsulation.
®˚ A, B, D, and E are incorrect, because based on the information given, none of these statements can be supported.

No comments: