Scientech Easy Access Modifiers Interview Questions Answer: Basic | Scientech Easy

Sunday, May 20, 2018

Access Modifiers Interview Questions Answer: Basic

Java modifiers, Java private class, core java interview questions,java interview questions and answers,access modifiers,frequently asked core java interview questions interview questions in java with answers,

  Access Modifiers questions asked in Interview:

    1. How many types of Modifiers in Java?
    Ans: There are two types of Modifiers in Java:
         a) Access Modifiers
         b) Non- access Modifiers
    2. What are the access modifiers in java?
    Ans: Access Modifiers are the modifiers which
       which is used to restrict the visibility of a class,                     field, method, and constructor.
       Java supports four access modifiers:
       a) Private: Private members of a class can be accessible only within the class. It cannot be accessed from outside the class.
 b) Default: Default members of a class are accessible within the same package. Default members are visible only within the package. They cannot be accessed from outside the package.
c) Protected: Protected members of a class are visible within the package but they can be inherited to subclasses outside the package.
d) Public: Public members are visible anywhere. They can be inherited from any subclass.

3. Can we have a private constructor in java?
Ans: Private constructor is used if you do not want to create the object of that class from other class and cannot create the subclass of that class.
Private constructors are used in singleton design pattern, Factory method design pattern.

4) Which access specifier can be used with a class?
5) Can we instantiate the object of derived class if parent constructor is 'protected'?
Ans: No, we cannot instantiate the object of derived class if parent constructor is protected.

6) What are non-access modifiers in java?
Ans: There are 4 non-access modifiers in java.
 a) Static: This modifier is used to specify that a member is a class member or instance member.
 b) Final: Final is a keyword which is used to restrict the users. In other words, It is used to restrict further modification of a class, method or field.
c) Abstract: Abstract is a keyword which is used with class or method. An abstract class or abstract method is used for further modification.
d) Synchronized: It is used to achieve the thread safeness. Only one thread can enter in a synchronized method or block at given time.

7) Can we declare a class as private?
Ans: No, we can declare a class as private but an inner class can be private. Inner class means class as a member of another class.

8)Can we declare an abstract method as 'private'?
Ans: No, an abstract method can be private. They must be public or protected or default so that they can be further modified.

9) Can we declare a class as 'protected'?
Ans: No, we cannot declare a class as protected. An inner class can be protected but not outer class.

10) Can a method or a class be final and abstract at the same time?
Ans: No, it is not possible. A class or a method cannot be final or abstract at the same time. because final method
or final class cannot be further modified whereas an abstract method or an abstract class must be modified further.

11) Why access modifiers are used?
Ans: The access modifiers are used to restrict the access of class and its members. Access modifiers are used to reduce the visibility of the members of a class.

12) Which is the default access modifier?
Ans: 'Internal' is the default access modifier if no access modifier is mentioned with class or its members. Internal is a keyword which is used for declaration.

13) What is the default access modifier for Interfaces?  
Ans: Public is the default access modifier for Interfaces. No other access modifier is allowed for them.

14) Can we define struct members as protected?
Ans: No,  we cannot define struct members as protected because structs do not support Inheritance.

15) What is the default access modifier for a class, an Interface, and struct declared directly with a namespace?
Ans: Internal

16) What is the access modifier for enumeration?
Ans: Enumeration members are always public. No other access modifiers are allowed.

17) What is the role of private constructor in java?
Ans: If you declare any constructor of a class as a private then you cannot create the object of the class from outside the class. In other words, we cannot create the subclass of that class. 

18) Which is the least restrictive access modifier in java?
Ans: Public. 

 19) Which is the most restrictive access modifier in java?
Ans: Private.

20) Which access modifier is also called Universal access modifier?
Ans: Public. 

I hope this 20 Access Modifiers Interview Questions will help you in the interview. You always remember that when your concepts are clear then you may answer any questions. First, you understand concepts then go for interview questions.




Popular