VidyaNITI Education Presents
LT_MA E _ _E
( Jaua Progratntning
)
Software Testing & Quality Assurance
( Optimization Technique )
� - - - - - - - - - - -�-
( Research Methodology )
( Electiue 1 )
Electiue 2
( )
GRAB CLEAN & EXAM-FOCUSED SOLVED
PAPERS TO BOOST YOUR SCORE
CLICK HERE
INSTALL APP NOW
UNIT
1 Basics of Java
◀ Index ▶
[Link]. Contents Page No.
1 Class and objects 3
Solved Previous Year Questions 15
2 Abstraction, polymorphism, inheritance, and 19
encapsulation
Solved Previous Year Questions 31
3 Abstract Class, Interface 40
Solved Previous Year Questions 44
4 Garbage Collector 49
Solved Previous Year Questions 49
5 Lambda expression 51
Solved Previous Year Questions 53
Click on the page number to directly jump to that page.
Previous Year Question
1. Class and Objects
● Q. Define class student with suitable attributes and methods. Display the
student details. [5] (Nov 2026)
2. Abstraction, Polymorphism, Inheritance, and Encapsulation
● Q. Define a class calculation to implement method overloading for addition
of two integers and two double variables. [5] (April 2025)
● Q. Define class person with suitable data member and methods. And extend
this class in Manager class. Display manager details. [5] (April 2025)
● Q. Explain use of final keyword for class, data members, methods.
[5] (Nov 2026)
3. Abstract Class, Interface
● Q. Create an abstract class shape with method area(). Inherit this class for
creating square class. Display area of square. [5] (Nov 2026)
4. Garbage Collector
● Q. What is garbage collection? Explain with required method. [5] (April 2025)
5. Lambda Expression
● Q. Write a function using lambda expression to calculate power of
number (xʸ). [5] (April 2025)
● Q. Write a function using lambda expression to calculate cube of a
given number. [5] (Nov 2026)
◀️ Class ▶️
Definition
A class in Java is a blueprint or template used to create objects.
It defines the properties (variables) and behaviour (methods) that
the objects of that class will have.
In simple words, a class describes what data an object will store
and what actions it can perform.
Explanation :
- Java is an object-oriented programming language (OOP).
- In object-oriented programming, programs are organized using
classes and objects.
- A class acts like a design or plan for creating objects.
- To understand this concept easily, think about the example of a
building plan.
- Before building a house, an architect prepares a blueprint or
design.
This blueprint contains information such as:
- number of rooms
- structure of the building
- location of doors and windows
- Using that blueprint, many houses can be built.
Similarly in Java:
Class → blueprint or design
Object → actual thing created using that design
For example, suppose we create a class called Student.
The class Student may define that every student has:
- Roll Number
- Name
- Course
- Marks
It may also define actions such as:
enter student details
display student details
But the class itself does not represent a real student.
It only describes what information a student should contain.
When we create an object of the class Student, that object will
represent a real student such as:
- Rahul
- Amit
- Sneha
Each object will have its own values for roll number, name and
marks.
Therefore, the class provides the structure, while objects store
the actual data.
Components of a Class
A class mainly contains two important parts.
1. Data Members (Variables)
Data members are variables declared inside a class.
They are used to store the properties or information of an object.
For example, in a Student class:
- rollNo
-name
- marks
These variables store the information about a student.
2. Methods (Functions)
- Methods are functions defined inside a class.
- They describe the behaviour or actions of the object.
- Methods perform operations on the data stored in the variables.
For example:
getData() → used to accept student information
display() → used to display student details
Thus, methods help us to manipulate and use the data of the class.
Why We Use Classes :
- Classes are used in Java programs for several reasons.
- They help organize large programs in a structured way.
- They allow us to represent real-world objects in programming.
- They improve code readability and maintainability.
- They make it possible to reuse code by creating multiple
objects.
- Using classes makes programs more clear, modular and easy to
manage.
Syntax of a Class :
The general syntax for defining a class in Java is:
class ClassName
{
// data members
// methods
}
Here:
- class is a keyword used to declare a class.
- ClassName is the name of the class.
- Inside the class we define variables and methods.
Important Points for Exam :
- A class is declared using the class keyword.
- A class is a blueprint for creating objects.
- A class contains data members and methods.
- Objects of a class store actual values of the data members.
- Classes are the basic building blocks of Java programs.
◀️ Object in Java ▶️
Definition :
- An object in Java is an instance of a class.
- It represents a real-world entity and contains the actual values
of the data members defined in the class.
In simple words, an object is created from a class and is used to
access the variables and methods of that class.
Explanation :
- As studied earlier, a class is only a blueprint or design.
- It defines what data and behaviour an object will have, but it
does not store real data by itself.
- To store real data and perform operations defined in the class,
we must create objects.
- Therefore, an object is the actual working unit of a class.
Example to Understand the Concept :
- Suppose we create a class called Student.
- The Student class may contain the following data members:
- Roll Number
- Name
- Marks
- This class only defines the structure of a student, but it does not
represent any specific student.
- When we create objects of this class, each object represents a
real student.
For example:
Student 1 → Rahul
Student 2 → Sneha
Student 3 → Amit
Each object will have its own values for roll number, name and
marks.
Therefore :
Class → Describes the structure
Object → Stores actual data and performs actions
Creation of an Object
- In Java, objects are created using the new keyword.
The new keyword performs two important tasks:
- It allocates memory for the object.
- It returns a reference to the created object.
Syntax for Creating an Object :
ClassName objectName = new ClassName();
Explanation :
ClassName → Name of the class
objectName → Name of the object
new → Keyword used to create the object
ClassName() → Constructor used to initialize the object
Example of Object Creation :
Suppose there is a class named Student.
To create an object of this class, we write:
Student s1 = new Student();
Explanation of the Example :
Student → Name of the class
s1 → Name of the object
new Student() → Creates a new object of the class
Now the object s1 can be used to store student information and
call methods defined in the Student class.
Accessing Data Members Using Object :
After creating an object, we can access the variables and methods
of the class using the dot operator (.).
Syntax :
[Link]
[Link]()
Example :
[Link] = 101;
[Link] = “Rahul”;
[Link]();
Here :
[Link] → Accesses the variable rollNo
[Link] → Accesses the variable name
[Link]() → Calls the method display()
The dot operator (.) connects the object with the variables and
methods of the class.
# Multiple Objects of a Class
- A single class can create multiple objects.
- Each object has its own separate copy of data members.
Example:
Class → Student
Objects created from it:
s1 → Rahul
s2 → Sneha
s3 → Amit
- Although all objects belong to the same class, each object stores
different data values.
- This allows a program to represent multiple real-world entities
using the same class structure.
Example Program:
class Student
{
int rollNo;
String name;
void display()
{
[Link](“Roll No: “ + rollNo);
[Link](“Name: “ + name);
}
}
class Test
{
public static void main(String args[])
{
Student s1 = new Student();
[Link] = 101;
[Link] = “Rahul”;
[Link]();
}
}
Output :
Roll No: 101
Name: Rahul
Important Points for Exam :
- An object is an instance of a class.
- Objects store the actual values of variables defined in the
class.
- Objects are created using the new keyword.
- The dot (.) operator is used to access variables and methods
through an object.
- A single class can create multiple objects.
- Each object contains its own copy of data members.
◀️ Solved Prvious Year Questions ▶️
Q. Define class Student with suitable attributes and methods. Display
the student details. [5 Marks] (Nov 2026)
Ans:
In this question, we need to create a class named Student.
The class should contain attributes (data members) to store
student information and methods to perform operations such as
displaying the student details.
First, we define the data members inside the class. These variables
store the information of the student.
For example :
- Roll Number
- Name
- Course
- Marks
After defining the data members, we create a method that will
display the student details.
Then in the main method, we create an object of the Student class
and assign values to the data members.
Finally, we call the method to display the details.
Java Program :
class Student
{
int rollNo;
String name;
String course;
double marks;
void display()
{
[Link](“Roll Number: “ + rollNo);
[Link](“Name: “ + name);
[Link](“Course: “ + course);
[Link](“Marks: “ + marks);
}
public static void main(String args[])
{
Student s1 = new Student();
[Link] = 101;
[Link] = “Rahul”;
[Link] = “MCA”;
[Link] = 85.5;
[Link]();
}
}
Output :
Roll Number: 101
Name: Rahul
Course: MCA
Marks: 85.5
Program Explanation :
Step 1: A class named Student is created.
Step 2: Data members are declared inside the class:
- rollNo
- name
- course
- marks
These variables store the student information.
Step 3: A method named display() is created to print the student
details.
Step 4: Inside the main() method, an object s1 of the Student class
is created.
Step 5: Values are assigned to the data members using the object.
Step 6: The display() method is called using the object to print the
student details.
◀️ Encapsulation in Java ▶️
Definition :
- Encapsulation is one of the important concepts of Object
Oriented Programming (OOP).
- Encapsulation means wrapping data (variables) and methods
(functions) together into a single unit called a class and
controlling the access to that data.
In simple words, encapsulation protects the data of a class by
preventing direct access and allowing it to be accessed only
through methods.
Explanation :
- In many programs, data is very important and should not be
accessed or modified directly by every part of the program.
- If data is freely accessible, it may cause incorrect changes,
errors, or security problems.
- Encapsulation solves this problem by hiding the internal data of a
class and allowing access only through specific methods.
This means the data of a class is kept safe and controlled.
Encapsulation is usually implemented by:
- declaring variables as private
- providing public methods to access and update the data
These public methods are commonly called:
Getter methods – used to read the data
Setter methods – used to modify the data
This approach ensures that the data is used in a controlled and
secure way.
Encapsulation in Java :
- In Java, encapsulation is achieved by using access modifiers.
The most common way is :
- Declare the variables as private so they cannot be accessed
directly from outside the class.
- Provide public methods to access and modify those variables.
Example idea :
Variables → private
Methods → public
This allows controlled access to the data.
Advantages of Encapsulation :
- It protects data from unauthorized access.
- It improves security of the program.
- It makes programs more organized and modular.
- It allows controlled access to variables.
- It improves code maintainability.
◀️ Polymorphism in Java ▶️
- Polymorphism is an important concept of Object-Oriented
Programming (OOP).
- The word Polymorphism comes from two Greek words:
Poly → Many
Morph → Forms
Therefore, polymorphism means “one name having many forms.”
In Java, polymorphism means that a single method or function
can perform different tasks depending on the situation or input
parameters.
Explanation :
In many programs, we may need to perform similar operations on
different types of data.
Instead of creating separate method names for each operation,
Java allows us to use the same method name but with different
implementations.
This concept is known as polymorphism.
For example, suppose we want to perform addition. Addition can
be performed for different types of numbers such as:
- Two integer numbers
- Two decimal numbers
- Instead of creating different methods like addInt() or
addDouble(), Java allows us to use the same method name add()
with different parameters.
- Depending on the type and number of parameters, the correct
method will be executed.
- Thus, the same method name can perform different operations,
which represents polymorphism.
Types of Polymorphism in Java
Java mainly supports two types of polymorphism.
1. Compile-Time Polymorphism
Compile-time polymorphism is achieved using method overloading.
In method overloading :
The method name remains the same
But the parameters are different
The compiler decides which method to execute during compile
time.
Example :
add(int a, int b)
add(double a, double b)
Both methods have the same name but different parameter types,
so the compiler can identify which method should be used.
2. Run-Time Polymorphism :
Run-time polymorphism is achieved using method overriding.
In method overriding :
A child class provides a new implementation of a method that is
already defined in the parent class.
The method that will be executed is decided during program
execution (run time).
Advantages of Polymorphism :
- It increases code flexibility.
- It improves code readability.
- It allows methods to perform different tasks using the same
name.
- It reduces the need to create many different method names