Top

Declarations and Access Control - General Questions

Answer: A

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

Option B is incorrect. It generates a compiler error: incompatible types because the array variable declaration is not correct. The array construction expects a reference type, but it is supplied with a primitive type in the declaration.

Option C is incorrect. It generates a compiler error: incompatible types because a string literal is not assignable to a character type variable.

Option D is wrong, it generates a compiler error expected. The compiler thinks that you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create a 3 x 3 two-dimensional array.

Enter details here

2.

Which of the following code fragments inserted, will allow to compile?

public class Outer 
{ 
    public void someOuterMethod() 
    {
        //Line 5 
    } 
    public class Inner { } 
    
    public static void main(String[] argv) 
    {
        Outer ot = new Outer(); 
        //Line 10
    } 
} 

Answer: A

Option A compiles without problem.

Option B gives error - non-static variable cannot be referenced from a static context.

Option C package ot does not exist.

Option D gives error - non-static variable cannot be referenced from a static context.

Enter details here

Answer: A

(A) is valid interface declarations.

(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.

Enter details here

4.

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) }}

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.

Enter details here

5.

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

Answer: C

(1), (2) and (6) are valid array declarations.

Option (3) is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a

Option (4) is not a correct array declaration. The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a []

Option (5) is not a correct array declaration. 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[ ] .

Enter details here

Answer: B

Option B generates a compiler error: expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array.

To correct the problem and make option B compile you need to add an extra pair of curly brackets:

int [ ] [ ] scores = { {2,7,6}, {9,3,45} };

Enter details here

7.

public class Test { }

What is the prototype of the default constructor?

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.

Enter details here

8.

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?

Answer: E

default access is the "package oriented" access modifier.

Option A and C are wrong because public and protected are less restrictive. Option B and D are wrong because abstract and synchronized are not access modifiers.

Enter details here

9.

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

Answer: D

The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.

Enter details here

10.

Which three are valid method signatures in an interface?

1.private int getArea();

2.public float getVol(float x);

3.public void main(String [] args);

4.public static void main(String [] args);

5.boolean setFlag(Boolean [] test);

Answer: B

(2), (3), and (5). These are all valid interface method signatures.

(1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.

Enter details here

Loading…
Tags: Declarations and Access Control - General Questions Questions and Answers || Declarations and Access Control - General Questions MCQ Questions and Answers || Declarations and Access Control - General Questions GK Questions and Answers || Declarations and Access Control - General Questions GK MCQ Questions || Declarations and Access Control - General Questions Multiple Choice Questions and Answers || Declarations and Access Control - General Questions GK || GK on Declarations and Access Control - General Questions || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming