Which of the following modifier combination is legal for top level class declaration

Which of the following modifier combination is legal for top level class declaration

14. 

Which two of the following are legal declarations for nonnested classes and interfaces?

  1. final abstract class Test {}
  2. public static interface Test {}
  3. final public class Test {}
  4. protected abstract class Test {}
  5. protected interface Test {}
  6. abstract public class Test {}
[A]. 1 and 4
[B]. 2 and 5
[C]. 3 and 6
[D]. 4 and 6

Answer: Option C

Explanation:

(3), (6). Both are legal class declarations.

(1) is wrong because a class cannot be abstract and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.


Kapil said: (Dec 30, 2010)  
Class(NonNested) can be public,default,final or abstract.

But class can't marked as both fianl and abstract


Amit said: (Feb 12, 2014)  
If interface is marked as static then there would be no meaning of using interface. It is meant to be implemented by many classes or interfaces. If we use "static" then there will be only one copy of it which we don't want in anyway!


Ivan said: (Aug 19, 2014)  
Final can not be used with the combination interface, its gives an error saying "illegal combination of modifiers".


Rishi Pathak said: (Nov 26, 2014)  
This wrong! Interface can't be final. Suppose I Say Final Interface :-).

Then we will not be able to Provide body to the method declared in Interface. This will lead to uselessness of interface.

So Java Compiler will say WTF! This Is An "illegal combination of modifiers".


Aayush said: (Jun 22, 2015)  
Please clear about (4) option. How class can't be protected?


Agata said: (Jan 28, 2018)  
public static interface Test {}

This code will compile. Answer number (2) should be valid, although I have no idea how to use such construction.


Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.




Which of the following modifier combination is legal for top level class declaration

6. 

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. abstract
C. protected
D. synchronized
E. default access

Answer: Option E

Explanation:

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.


7. 

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: Option D

Explanation:

All the given statements are legal declarations.


8. 

Which cause a compiler error?

A. int[ ] scores = {3, 5, 7};
B. int [ ][ ] scores = {2,7,6}, {9,3,45};
C. String cats[ ] = {"Fluffy", "Spot", "Zeus"};
D. boolean results[ ] = new boolean [] {true, false, true};
E. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

Answer: Option B

Explanation:

Option B generates a compiler error: <identifier> 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} };

The only access modifier (other than friendly or package) that can be applied to a top-level class is public.

Which of the following modifiers can be applied to top level class?

What modifiers may be used with a top-level class? public, abstract and final can be used for top-level class.

Which of the following is a valid top level class declaration?

Which of the following is a valid top level class declaration? Explanation: Option E is the correct answer.

Which of these access modifiers can be used for a top level interface?

There are only two use cases for class visibility at the top level (a) Visible everywhere (b) Visible only within the package. Hence only two modifiers ( public and default).