Monday, 23 July 2012

Declarations and Access Control - Objective Questions -Part 2

1)

interface Base
{
    boolean m1 ();
    byte m2(short s);
}

which two code fragments will compile?

   1. interface Base2 implements Base {}
   2. abstract class Class2 extends Base
      { public boolean m1(){ return true; }}
   3. abstract class Class2 implements Base {}
   4. abstract class Class2 implements Base
      { public boolean m1(){ return (7 > 4); }}
   5. abstract class Class2 implements Base
      { protected boolean m1(){ return (5 > 7) }}

A. 1 & 2
B. 2 & 3
C. 3 & 4
D. 1 & 5

Answer: C

(3) is correct because an abstract class doesn't have to implement any or all of its interface's methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean).
(1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes don't extend interfaces. (5) is incorrect because interface methods are implicitly public, so the methods being implemented must be public.

2)

Which three form part of correct array declarations?

   1. public int a [ ]
   2. static int [ ] a
   3. public [ ] int a
   4. private int a [3]
   5. private int [3] a [ ]
   6. public final int [ ] a

A. 1, 3, 4
B. 2, 4, 5
C. 1, 2, 6
D. 2, 5, 6

Answer: C

At Option 3 ,The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a.At Option 4,The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a [].The compiler complains with 2 errors: ']' expected. A closing bracket is expected in place of the 3 and  expected A variable name is expected after a[ ].

3)

Which one creates an instance of an array?

int[ ] ia = new int[15];
float fa = new float[20];
char[ ] ca = "Some String";
int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

Answer: A

Option A is correct. It uses correct array declaration and correct array construction.

4)

Which is a valid declaration within an interface?

A. public static short stop = 23;
B. protected short stop = 23;
C. transient short stop = 23;
D. final void madness(short stop);

Answer: A

(B) and (C) are incorrect because interface variables cannot be either protected or transient. (D) is incorrect because interface methods cannot be final or static.

5)

Which cause a compiler error?

int[ ] scores = {3, 5, 7};
int [ ][ ] scores = {2,7,6}, {9,3,45};
String cats[ ] = {"Fluffy", "Spot", "Zeus"};
boolean results[ ] = new boolean [] {true, false, true}

Answer: B

The correct syntax is int [][]scores={{2,7,6},{9,3,45}};

6)

Which of the following techniques can be used to prevent the instantiation of a class by
any code outside of the class?

A. Do not declare any constructors.
B. Do not use a return statement in the constructor.
C. Declare all constructors using the keyword void to indicate that nothing is returned.
D. Declare all constructors using the private access modifier.

Answer: D

Private data members and member functions can be accessed only with in the declared class.

7)

What is the most restrictive access modifier that will allow members of one class to have
access to members of another class in the same package?

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

Answer: D

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

8)

public class Test { }

What is the prototype of the default constructor?

A. Test()
B. Test(void)
C. public Test()
D. public Test(void)

Answer: C

Option A and B are wrong because they use the default access modifier and the access modifier for the class is public  (remember, the default constructor has the same access modifier as the class).
Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

9)

What is the narrowest valid returnType for methodA in line 3?

public class ReturnIt
{
    returnType methodA(byte x, double y) /* Line 3 */
    {
        return (long)x / y * 2;
    }
}

A. int
B. byte
C. long
D. double

Answer: D

The result of expression '(long)x / y * 2' is of type double.(long)x casting is only for x not for the entire
expression.Java's widening conversions are:
- From a byte to a short, an int, a long, a float, or a double.


10)

Which of the following is/are legal method declarations?

   1. protected abstract void m1();
   2. static final void m1(){}
   3. synchronized public final void m1() {}
   4. private native void m1();

A. 1 and 3
B. 2 and 4
C. 1 only
D. All of them are legal declarations.

Answer: D

All the given statements are legal declarations.Abstract functions can have public,protected, default access.

No comments:

Post a Comment