1) Discuss method overloading.
Write a program to overload a method area() to compute the
area of a triangle and a circle.
Method overloading is a feature in Java where multiple methods in the same class can have
the same name but differ in number of parameters,types of parameters and order of
parameters.
Overloading allows methods to perform different tasks based on the arguments passed, even though
they share the same name. It enhances code readability and reusability, as the same method name
can be used for different types of tasks.
class Shape {
public double area(double base, double height) {
return 0.5 * base * height;
public double area(double radius) {
return Math.PI * radius * radius;
}}
public class Main {
public static void main(String[] args) {
Shape shape = new Shape();
double triangleArea = shape.area(7,10);
System.out.println("Area of Triangle: " + triangleArea);
double circleArea = shape.area(7);
System.out.println("Area of Circle: " + circleArea);
}}
2) Define class. Give syntax and example
Class can be thought of as a user-defined data type. We can create variables (objects) of that
data type. So, we can say that class is a template for an object and an object is an instance of a
class. Most of the times, the terms object and instance are used interchangeably.The syntax is as
follows:-
class classname
{
type var1;
type var2;
.......
type method1(para_list)
{
//body of method1
}
type method2(para_list)
{
//body of method2
}
...........
}
For ex refer to above one
3) Briefly explain static members of the class with suitable examples.
In Java, static members (variables and methods) belong to the class rather than to any
specific object instance. These members are shared among all instances of the class and
can be accessed without creating an instance of the class.
There are two types of static members:
1. Static Variables (also called class variables)
2. Static Methods
class Counter {
static int count = 0;
Counter() {
count++;
static void displayCount() {
System.out.println("Total objects created: " + count);
}
public class Main {
public static void main(String[] args) {
// Creating objects of the Counter class
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter.displayCount(); // Output: Total objects created: 3
4) Create a Java class called Student with the following details as variables (USN, Name, Branch,
Phone Number). Write a Java program to create n student objects and print USN, Name,
Branch, and Phone number with suitable heading.
import java.util.Scanner;
class Student {
String usn;
String name;
String branch;
String phoneNumber;
public Student(String usn, String name, String branch, String phoneNumber) {
this.usn = usn;
this.name = name;
this.branch = branch;
this.phoneNumber = phoneNumber;
}
public void displayDetails() {
System.out.printf("%-15s %-20s %-15s %-15s\n", usn, name, branch,
phoneNumber);
}}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = sc.nextInt();
sc.nextLine();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for Student " + (i + 1) + ":");
System.out.print("Enter USN: ");
String usn = sc.nextLine();
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Branch: ");
String branch = sc.nextLine();
System.out.print("Enter Phone Number: ");
String phoneNumber = sc.nextLine();
students[i] = new Student(usn, name, branch, phoneNumber);
System.out.println("\nStudent Details:");
System.out.printf("%-15s %-20s %-15s %-15s\n", "USN", "Name", "Branch",
"Phone Number");
System.out.println("---------------------------------------------------------------");
for (Student student : students) {
student.displayDetails();
sc.close();
}}
5) Define a constructor. What are the salient features of constructor? Write a java program to
show these features.
A constructor in Java is a special type of method that is used to initialize an object when it is
created. It has the same name as the class and does not return any value .A constructor is
automatically called when an object of the class is created.
Salient Features of a Constructor
1. Same Name as the Class: A constructor must have the same name as the class in
which it resides.
2. No Return Type: Constructors do not have a return type, not even void.
3. Called Automatically: When an object of the class is created, the constructor is
called automatically.
4. Overloading: A class can have more than one constructor with different parameter
lists (constructor overloading).
5. Initialization: Constructors are primarily used to initialize instance variables of the
class.
6. Default Constructor: If no constructor is defined, Java provides a default
constructor. However, once a constructor is explicitly defined, the default constructor
is no longer available unless explicitly declared.
class Car {
String brand;
int year;
String model;
Car() {
this.brand = "Unknown";
this.year = 0;
this.model = "Unknown";
System.out.println("Default Constructor called: " + brand + " " + year + " " + model);
Car(String brand, int year, String model) {
this.brand = brand;
this.year = year;
this.model = model;
System.out.println("Parameterized Constructor called: " + brand + " " + year + " " +
model);
Car(String brand, int year) {
this.brand = brand;
this.year = year;
this.model = "Unknown";
System.out.println("Constructor Overloading called: " + brand + " " + year + " " +
model);
void displayCarInfo() {
System.out.println("Car Details: " + brand + " " + year + " " + model);
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
car1.displayCarInfo();
Car car2 = new Car("Toyota", 2021, "Camry");
car2.displayCarInfo();
Car car3 = new Car("Honda", 2020);
car3.displayCarInfo();
6) How do you overload a constructor? Explain with a program.
Constructor Overloading in Java is a feature that allows a class to have more than one constructor
with different parameter lists. The constructors are differentiated by the number of parameters, the
types of parameters, or both. Overloaded constructors allow objects to be initialized in different
ways.(and explain with respect to above program)
7) What are different parameter passing techniques in Java? Discuss the salient features of the
same.
1. Pass-by-Value for Primitive Data Types
When you pass a primitive data type (e.g., int, char, float, etc.) to a method in Java, the
method gets a copy of the actual value. Changes made to the parameter inside the method do
not affect the original value.
Example of Primitive Pass-by-Value:
class Test {
void changeValue(int num) {
num = 100; // Modifying the copy of the value
public class Main {
public static void main(String[] args) {
int a = 10;
Test test = new Test();
System.out.println("Before: " + a); // Output: 10
test.changeValue(a); // Passing the copy of 'a'
System.out.println("After: " + a); // Output: 10 (Original 'a' remains unchanged)
}
Salient Features:
Copies the actual value: A copy of the actual value is passed to the method.
No modification to original data: Any changes made to the parameter inside the method do
not affect the original value.
Efficient for small data types: Primitive data types are small in size, so copying them is
efficient.
2. Pass-by-Value for Object References
When you pass an object reference to a method, a copy of the reference is passed, not the
object itself. This means that the reference itself is copied, but it still points to the same object
in memory. Changes to the object's internal data will affect the original object because both
the original and the copied references point to the same object.
Example of Object Reference Pass-by-Value:
class Person {
String name;
Person(String name) {
this.name = name;
void changeName(Person p) {
p.name = "John"; // Modifies the original object because both references point to the
same object
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice");
System.out.println("Before: " + person.name); // Output: Alice
person.changeName(person); // Passing the copy of the reference
System.out.println("After: " + person.name); // Output: John (Original object is modified)
}
8) What are various access specifiers in Java? List out the behavior of each of them.
Types of Access Specifiers in Java
1. Public
2. Private
3. Protected
1. Public
Description: The public access specifier allows a class, method, or variable to be
accessed from anywhere, both within the same class, same package, or from any
other class in different packages.
Behavior:
o Accessible from any class, any package.
o Used when you want to make classes, methods, or variables available globally.
Example:
public class PublicClass {
public int publicVariable = 10;
public void publicMethod() {
System.out.println("Public Method");
2. Private
Description: The private access specifier restricts access to the declaring class
only. Methods, variables, or constructors marked as private can only be accessed
within the same class.
Behavior:
o Not accessible outside the class in which it is declared.
o Provides the highest level of encapsulation.
o Used when you want to hide the implementation details or restrict access to
sensitive data.
Example:
class PrivateClass {
private int privateVariable = 20;
private void privateMethod() {
System.out.println("Private Method");
public void accessPrivate() {
System.out.println(privateVariable); // Allowed within the same class
privateMethod(); // Allowed within the same class
3. Protected
Description: The protected access specifier allows the class members to be
accessed within the same package and by subclasses (derived classes) in other
packages.
Behavior:
o Accessible within the same package and subclasses in other packages (via
inheritance).
o Often used in inheritance to allow child classes access to parent class fields and
methods.
Example:
class ParentClass {
protected int protectedVariable = 30;
protected void protectedMethod() {
System.out.println("Protected Method");
class ChildClass extends ParentClass {
public void accessProtected() {
System.out.println(protectedVariable); // Allowed in subclass
protectedMethod(); // Allowed in subclass