Access Specifier : ICSE Class 10 Board Exam Questions and Answers

2 Marks Questions

Question 1 : Consider the following class

public class MyClass

{

  public static int x=3, y=4;

  public int a = 2, b=3;

}

i) Name the variables for which each object of the class will have its own distinct copy.

ii) Name the variables that are common to all object of the class.

Answer :

i) ‘a’ and ‘b’

ii) ‘x’ and ‘y’

Question 2 : Differentiate between public and private modifiers for members of a class.

Answer : Difference between public and private modifiers:

Public Modifier Private Modifier
It must open access level. It is lowest access level.
It can be accessed by any class, package. It can be accessed only by the member within class.

Question 3 : Differentiate between private and protected visibility modifiers.

Answer : Difference between private and protected visibility modifiers:

Private Modifier Protected Modifier
It can be accessed by the same class to which the methods and fields belong. It can be accessed by the subclass.
Private method and fields are not visible within subclass. Protected method and fields are visible within subclass.

Question 4 : Explain any two types of access specifier.

Answer : Any two types of access specifier are :

i) Public : It allows the variable and method visible to all classes outside and within class or in other package.

ii) Private : It allows the variables and methods visible to only that class and to which the method and fields belong.

Question 5 : What is meant by private visibility of a method?

Answer : Private visibility of a method means that methods is accessible only in same class and can not be accessed by child class or in other class.

Question 6 : What is the purpose of static methods and static variables?

Answer : Static variables are used when there is a requirement to share a variable between multiple objects of a class instead of creating separate copies for each object.

Static method are used to be loaded in the memory without creating any object of that class in which method is defined.

Question 7 : Explain about friendly modifier.

Answer : Friendly modifier does not have a keyword. If you don’t specify any access modifier, by default it is friendly.

e.g.

class Demo

{

   int a; //No modifier is used

}

In the above example, access specifier is not used. So we will consider it to be friendly modifier.

Question 8 : What is the default value of local variable?

Answer : The local variables are not initialised to any default value, neither primitives not object references.

Question 9 : Can a private member of a public class can be accessed by 

i) same class?

ii) other class in the same package?

iii) class of some other package?

Answer : i) Yes, private member of a public class can be accessed by same class.

ii) No, other classes are unable to access private member of a public class.

iii) No, other package’s class can not access private member of a public class.