Tuesday 24 July 2012

Declarations and Access Control - Objective Questions -Part 4

1)

What will be the output of the program?

class Base
{
    Base()
    {
        System.out.print("Base");
    }
}
public class Alpha extends Base
{
    public static void main(String[] args)
    {
        new Alpha(); /* Line 12 */
        new Base(); /* Line 13 */
    }
}

A. Base
B. BaseBase
C. compilation fails
D. The code runs with no output

Answer: B

Option B is correct. It would be correct if the code had compiled, and the subclass Alpha had been saved in its own file. In this case Java supplies an implicit call from the sub-class constructor to the no-args constructor of the super-class therefore line 12 causes Base to be output. Line 13 also causes Base to be output.

2)

interface DoMath { double getArea(int rad); }
interface MathPlus { double getVol(int b, int h);}

which code fragment inserted at end of the program, will allow to compile?

A. class AllMath extends DoMath { double getArea(int r); }
B. interface AllMath implements MathPlus { double getVol(int x, int y); }
C. class AllMath implements MathPlus { double getArea(int rad); }
D. abstract class AllMath implements DoMath, MathPlus { }

Answer: D

An interface can extends one or more interfaces.A class can implements one or more interfaces.If a class implements an interface either it should implement all the functions in interface or declare the class itself as abstract.

3)

Given a method in a class, what access modifier do you use to restrict access to that
method to only the other members of the same class?

A. public
B. default access
C. protected
D. private

Answer: D

public -access everywhere.
default -only with the package.
protected -within the package and sub classes of other packages.
private  -only with in the class.

4)

Which statement is wrong?

A. The default constructor has the same access as its class.
B. The default constructor invokes the no-arg constructor of the superclass.
C. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
D. The compiler creates a default constructor only when there are no other constructors for
   the class.

Answer: C

C is wrong .The compiler creates a default constructor only when there are no other constructors for the class.

5)

You want subclasses in any package to have access to members of a superclass. Which is the
most restrictive access that accomplishes this objective?

A. public
B. private
C. protected
D. default

Answer: C

private makes a member accessible only from within its own class
protected makes a member accessible only to classes in the same package or subclass of the class
default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.public means that all other classes regardless of the package that they belong to, can access the member

6)

class B {
  public static void main(String[] args) {
    A a = new A();
    a.m1();  // 1
    a.m2();  // 2
    a.m3();  // 3
    a.m4();  // 4
}}
Assume that the code appears in a single file named A.java.
What is the result of attempting to compile and run the program?

A. Prints: A.m1, A.m2, A.m3, A.m4,
B. Compile-time error at 2,3&4
C. Compile-time error at 3
D. None of the above

Answer: C

Both class A and B are declared in the same package, so class B has access to the public, protected, and
package access methods of class A.

No comments:

Post a Comment