Objective Type Questions
Question 1
A class encapsulates ............... .
- data
- methods
- functionality
- all the above
Answer
all the above
Reason — A class encapsulates characteristics, state (data) and behaviour (methods and functionality) of an entity in a single unit
Question 2
Through which access specifier, a class makes its element visible to all ?
- public
- private
- protected
- friendly
Answer
public
Reason — A class makes its element visible to all by using public specifier as a public data member and method can be accessed by any other class.
Question 3
If a local variable is having the same name as that of a global class element, then it
- hides the global variable
- gets hidden by global variable
- produces an error
- none of the above
Answer
hides the global variable
Reason — If a local variable is having the same name as that of a global class element, the system resolves the name to the most local scope available i.e., most local variable with the same name will be considered. The global variable will be hidden by the local variable.
Question 4
Java resolves duplicate variable name to ................ .
- global variable
- local variable
- most local scope variable
- all the above
Answer
most local scope variable
Reason — Java resolves duplicate variable name to most local scope variable. For example, if a local variable is having the same name as that of a global class element, then the most local variable with the same name will be considered. The global variable will be hidden by the local variable.
Question 5
A member method that returns the value of a private data member is called ............... .
- setter
- getter
- manager
- accessor
Answer
getter, accessor
Reason — Accessor methods are used to read values of private data members of a class which are not directly accessible in non-member methods.
Question 6
A member method that can change the value of a private data member is called ............... .
- setter
- getter
- manager
- accessor
Answer
setter
Reason — Setter/mutator methods allow us to change the value of a private data member as private data members cannot be accessed directly.
Assignment Questions
Question 1
What is encapsulation ?
Answer
The process of wrapping or grouping of data and functions in such a way that they are used as a single unit is known as encapsulation.
Question 2
What does a class encapsulate ?
Answer
A class encapsulates the characteristics, state and behaviour (data and functions) of an entity.
Question 3
How does a class enforce information hiding ?
Answer
A class enforces data hiding through the following access specifiers:
- public
- private
- protected
- default
Question 4
Name various visibility modifiers in a class.
Answer
The various visibility modifiers in a class are as follows:
- public — The public members are accessible in all the classes whether a subclass or any other class in same package or another package.
- private — The private members are accessible only inside their own class and nowhere else.
- protected — The protected members are accessible inside all the classes in their own package as well as in all subclasses of their class.
- default — The default members are accessible inside all the classes of the same package.
Question 5
Define scope and visibility.
Answer
Scope refers to the parts of the program where a particular piece of code or data item would be known and can be accessed.
Visibility is a related term and refers to whether we can use a variable from a given place in the program.
Question 6(a)
What will be the scope of a public class ?
Answer
The scope of a public class is public. It can be accessed from anywhere.
Question 6(b)
What will be the scope of a protected class ?
Answer
The scope of a protected class is protected. It can be accessed from all the classes within the same package as well as from the sub classes in the other packages.
Question 6(c)
What will be the scope of a default class ?
Answer
The scope of a default class is friendly or package. It can be accessed from all the classes within the same package.
Question 6(d)
What will be the scope of a private class?
Answer
A top-level class can't be declared as private. Only inner or nested classes can be private.
Question 7(a)
What will be the visibility of a public class ?
Answer
A public class is visible to all the classes, whether they are in the same package or in a different package.
Question 7(b)
What will be the visibility of a protected class ?
Answer
A protected class is visible to all the classes in the same package as well as to the classes outside the package that inherit the class.
Question 7(c)
What will be the visibility of a default class ?
Answer
The default class is visible to all the classes in the same package.
Question 7(d)
What will be the visibility of a private class ?
Answer
A private class is visible only within the same class. It is not visible to any sub-class or the classes in the same package.
Question 8
How does Java resolve variables having same name? Give code example.
Answer
Java resolve variables having same name to the most local scope available. Therefore, if a local variable is having the same name as that of a global class element, the most local variable with the same name will be considered. The global variable will be hidden by the local variable.
Consider the example given below:
public class VarHiding
{
// global variable "globalVar" is set to 0
public static int globalVar = 0;
public void checking( )
{
test(5);
System.out.println("First test :" + globalVar);
setGlobal(7);
System.out.println("Second test :" + globalVar);
}
public static void test(int globalVar)
// globalVar is local to method test
// test uses the same name as our globalVar variable
{
// All of these refer to local globalVar
globalVar = globalVar;
// Here local variable namely globaLVar will be
// considered
// Global variable having same name i.e.,
// globaLVar remains hidden, here
System.out.println("Inside of test :" + globalVar);
}
public static void setGlobal(int value)
// test two uses a different name. To resolve
// variable globalVar, we must go higher in the
// "stack"
{
// Refers to global globalVar
globalVar = value;
// Here global variable - globalVar will be considered.
System.out.println("Inside of test2 :" + globalVar) ;
}
}
The output produced by checking( ) function is as follows:
Inside of test :5
First test :0
Inside of test2 :7
Second test :7
Question 9
What are the getter and setter methods?
Answer
Getter methods are used to read values of private data members of a class which are directly not accessible in non-member methods. They do not modify the data members. They should have "public" access modifier and return type same as the data type of that instance variable. A getter method simply returns the instance variable's value.
Setter methods allow us to change the values of an instance variable of a class. They should have "public" access modifier and "void" return type.
Question 10
How many types of methods are generally there in a class ?
Answer
The member methods of a class can be categorized into following three categories :
- Accessor Methods — These are public member methods of the class that allow us to access the data members (instance variables) of object. They cannot change the value of data members. They are used to read values of private data members of a class which are directly not accessible in non-member methods.
- Mutator Methods — These member methods allow us to change the data members of an object. Any member method that changes the values of an instance variable of a class is a mutator method.
- Manager Methods — These are member methods with specific methods (e.g., constructors) that deal with initializing class instances.
Question 11
What are the advantages of encapsulation?
Answer
The advantages of encapsulation are as follows:
- Data Hiding — Encapsulation enables the programmer to hide desired data by making it private and give access only to the desired classes / methods. The programmer can hide how variables and data are stored.
- Implementation Logic are Hidden — User only knows that to update a data member's value, call its setter method and to read a data member's value, call its getter method but what these setter and getter methods are doing, is purely hidden from them.
- Flexibility — Since implementation details are hidden, it is easier to change the inner logic.
- More control in programmer's hand — With Java encapsulation, a programmer has full control over which values are allowed via setter and getter methods. This allows making the data read-only or write-only as per requirements. It lead to more flexibility and more control in programmer's hand.