Class X Notes
Class X Notes
Ans: The wrapping up to data and methods into a single units (called class) is known as
encapsulation. For example an engine of car or any vehicle contains many small
parts, which enables the entire machinery system to work. Encapsulation
property hides the inner working of
objects from the real world.
Q. What does a class encapsulate?
Ans: A class encapsulate Data Members that contains the information necessary to represent
the class and Member Functions that perform operations on the data member.
Q. How does a class enforce information hiding?
Ans: Classes enforce information hiding by means of access specifier.
Q. What is initial class?
Ans: A java program contains many classes. But one class in a Java program contains the
main() method. This class is called initial class.
Functions/Methods
A. Fill in the blanks:
ENCAPSULATION
Question 1
............... is the technique of binding both data and methods together to keep them safe from
unauthorised access and misuse.
1. Abstraction
2. Inheritance
3. Encapsulation
4. Polymorphism
Answer
Encapsulation
Reason — Encapsulation is the technique of binding both data and methods together to keep them
safe from unauthorised access and misuse.
Question 2
1. public
2. protected
3. private
4. All of these
Answer
All of these
Question 3
A member variable declared with a public access specifier has visibility in ............... .
1. Class
2. Package
3. Subclass
4. All of these
Answer
All of these
Reason — A member variable declared with a public access specifier has visibility in the class, its sub
classes and the package as well.
Question 4
A member variable declared with a private access specifier has visibility only in the ............... .
1. Class
2. Package
3. Subclass
4. All of these
Answer
Class
Reason — A member variable declared with a private access specifier has visibility only in the class in
which it is declared.
Question 5
4. Class only
Answer
Reason — A member variable declared with no access specifier has visibility in the same class and
other classes of the same package only.
Question 6
Answer
needs an instance to access it
Reason — An instance variable needs an instance (object) to access it as each instance of the class
has a separate copy of the instance variable.
Question 7
Answer
Reason — A static variable is preceded by static keyword in the declaration. It belongs to the class
and hence, it is called a class variable and is accessed via the class name.
Question 8
............... is the feature by means of which one class acquires the properties of another class.
1. Abstraction
2. Inheritance
3. Encapsulation
4. Polymorphism
Answer
Inheritance
Reason — Inheritance is the feature by means of which one class acquires the properties of another
class.
Question 9
1. Parent class
2. Base class
3. Super class
4. All of these
Answer
All of these
Reason — The class that gets inherited is known as a parent class, base class and super class as well.
Question 10
When many sub classes are inherited from a single base class, it is known as ............... .
1. Hierarchical inheritance
2. Multiple inheritance
3. Single inheritance
4. Multilevel inheritance
Answer
Hierarchical inheritance
Reason — When many sub classes are inherited from a single base class, it is known as Hierarchical
inheritance.
Assignment Questions
Question 1
Answer
A class encapsulates state and behavior by combining data and functions into a single unit. The state
of an object is represented by its member variables and behaviour is represented by member
methods. By combining state and behavior within a single unit, the class encapsulates the
implementation details, allowing the outside world to interact with the object through a well-
defined interface.
Question 2
Answer
1. public
2. private
3. protected
Question 3
1. public
2. private
3. protected
4. no modifier specified
Answer
1. public — The class members declared with the public access specifier can be accessed from
outside the class.
2. private — The class members declared with the private access specifier can be accessed only
within the class in which they are declared.
3. protected — The class members declared with the protected access specifier can be
accessed within the same package only. They are not accessible from outside the package,
except if the sub class is in the other package.
Question 4
Answer
1. It hides the implementation details of the class from other objects, which makes it easier to
change the implementation without affecting the rest of the system.
2. It provides encapsulation as the class defines a clear and well-defined interface for
interacting with its state.
3. It protects the state of the object by preventing external objects from modifying the state in
an unintended way.
Question 5
Answer
The scope of a variable refers to that part of the program in which the variable is accessible.
Question 6
1. Local variables
2. Parameter variables
3. Instance variables
4. Class variables
Answer
1. Local variables — The scope of local variables is limited to the method or the block they are
declared in. So, local variables are accessible only to the method or block in which they are
declared. They are not accessible to the rest of the class.
2. Parameter variables — The scope of parameter variables is limited to the method where
they are being used. Therefore, parameter variables are visible only to the method in which
they are used. They are not accessible to the rest of the class.
3. Instance variables — The scope of an instance variable is within the object that it belongs to,
and it can be accessed from any method of the class that it belongs to, within the visibility
restrictions defined by its access modifiers.
4. Class variables — A class variable can be accessed from any instance of the class using the
class name followed by the variable name. It is shared among all instances of a class. It is
available as a single copy to all instances of the class.
Question 7
Answer
A variable declared in a block is visible (accessible) in the block and in all the sub-blocks. However, it
is not visible outside the block.
Consider the given example:
void Add() {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);
else { //Block 2
int diff = a - b;
System.out.println(diff);
Here, the variables a and b will be accessible throughout the block and its sub-blocks (Block 1 and
Block 2) but the variable sum will be accessible only in Block 1 and the variable diff will be accessible
only in Block 2.
Question 8
Is it legal to define local variables with the same identifier in nested blocks?
Answer
No, it is illegal to define local variables with the same identifier in nested blocks
The local variable declaration space of a block includes any nested blocks. Thus, within a nested
block it is not possible to declare a local variable with the same name as a local variable in an
enclosing block.
Question 9
Answer
Static variables in Java are used when we want to create a variable that is common to all objects of a
class, rather than having a separate instance of the variable for each object. This is useful in
situations where we need to keep track of information that applies to a class as a whole, rather than
to individual objects.
1. Counter to keep track of the number of instances of a class that have been created.
Question 10
What is the mechanism that allows one class to extend another class?
Answer
Inheritance is the mechanism that allows one class to extend another class.
Question 11
Answer
A class that is an extension of another class is known as a derived class or child class or sub-class