1. List and explain the features of Object-Oriented Programming.
Object-Oriented Programming (OOP) has several features that help in writing clear, organized,
and reusable code.
1.) Encapsulation
Encapsulation means hiding the internal details of a class.
Binding data and methods together and restricting direct access to some components.
2.) Abstraction
Abstraction means Hiding unnecessary details and showing only essential features to
the user. It helps the user to focus on what an object does, not how it does it.
3.) Inheritance
Inheritance means one class can use/inherit the properties of another class. The new
class is called a child class, and the old one is called a parent class.
4.) Polymorphism
Polymorphism means one thing can behave in different ways. In Java, the same method
name can be used in different classes.
and There are 2 types: Compile-time Polymorphism (Method Overloading) and Runtime
Polymorphism (Method Overriding)
5.) Modularity
Modularity means breaking a program into small parts (called classes or modules).
Each class has a specific job. This makes the code easier to read and manage.
6.) Reusability
Reusability means we can use the same class in different programs without rewriting
the code. This saves time and effort.
7.) Maintainability
Maintainability means the code is easy to update and fix. If there is a problem, it’s easy
to find and correct it because the code is organized into classes.
Example for OOP Features:
// Abstraction
abstract class Animal {
protected String name; // protected (Access Specifier)
public abstract void sound(); // abstract method
}
// Inheritance
class Dog extends Animal {
private String breed; // Encapsulation
static String category = "Mammal"; // Static member/variable
// Default constructor
public Dog() {
this("Unknown");
}
// Parameterized constructor
public Dog(String breed) {
this.name = "Dog";
this.breed = breed;
}
// Normal method
public void displayInfo() {
System.out.println("Name: " + name + ", Breed: " + breed);
}
// Compile-time Polymorphism (Overloading)
public void sound(String mood) {
System.out.println(name + " is " + mood);
}
// Runtime Polymorphism (Overriding)
public void sound() {
System.out.println("Bark! Breed: " + breed);
}
// Static method
public static void showCategory() {
System.out.println("Category: " + category);
}
}
public class Test {
public static void main(String[] args) {
Dog d1 = new Dog(); // default constructor
Dog d2 = new Dog("Labrador"); // parameterized constructor
d1.displayInfo(); // method call
d1.sound(); // runtime polymorphism
d2.displayInfo(); // method call
d2.sound("Happy"); // compile-time polymorphism
Dog.showCategory(); // static method
}
}
Output
Name: Dog, Breed: Unknown
Bark! Breed: Unknown
Name: Dog, Breed: Labrador
Dog is Happy
Category: Mammal
2. Discuss the variables, data types and Operators available in Java with
examples.
1.) Variables in Java
A variable is a named memory location used to store data during program execution.
In Java, variables must be declared with a data type before they are used.
Variables can be local, instance, or static.
Types of Variables:
Local Variables – Declared inside a method or block, accessible only within it.
Instance Variables – Declared in a class but outside methods, belong to objects.
Static Variables – Declared using the static keyword, shared among all objects of the
class.
Example – Variables
class VariablesDemo {
int instanceVar = 10; // Instance Variable
static int staticVar = 20; // Static Variable
void display() {
int localVar = 30; // Local Variable
System.out.println("Instance: " + instanceVar);
System.out.println("Static: " + staticVar);
System.out.println("Local: " + localVar);
}
public static void main(String[] args) {
VariablesDemo obj = new VariablesDemo();
obj.display();
}
}
Output:
Instance: 10
Static: 20
Local: 30
2.) Data Types in Java
Data types define the kind of values a variable can hold.
Java supports two categories: Primitive and Non-Primitive.
A) Primitive Data Types
1. byte – 8-bit integer
2. short – 16-bit integer
3. int – 32-bit integer
4. long – 64-bit integer
5. float – 32-bit decimal
6. double – 64-bit decimal
7. char – Single 16-bit Unicode character
8. boolean – Holds true or false
B) Non-Primitive Data Types
Arrays, Strings, Classes, Interfaces.
Example – Data Types
class DataTypesDemo {
public static void main(String[] args) {
int num = 100;
double price = 99.99;
char grade = 'A';
boolean isPassed = true;
System.out.println(num + ", " + price + ", " + grade + ", " +
isPassed);
}
}
Output:
100, 99.99, A, true
3.) Operators in Java
Operators are symbols that perform operations on variables and values.
Java operators are grouped as:
A) Arithmetic Operators – +, -, *, /, %
B) Relational Operators – ==, !=, <, >, <=, >=
C) Logical Operators – &&, ||, !
D) Assignment Operators – =, +=, -=, *=, /=
E) Unary Operators – ++, --, +, -, !
F) Conditional Operator – ? : (Ternary)
G) Bitwise Operators – &, |, ^, ~, <<, >>, >>>
Example – Operators
class OperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b));
System.out.println("Greater: " + (a > b));
System.out.println("Logical AND: " + (a > 0 && b > 0));
int max = (a > b) ? a : b; // Ternary
System.out.println("Max: " + max);
}
}
Output:
Addition: 15
Greater: true
Logical AND: true
Max: 10
3. Explain control statements in Java with syntax and examples.
Definition
Control statements are used to control the flow of execution in a program based on
certain conditions or loops.
They help in making decisions, repeating tasks, or branching program execution.
In Java, control statements are classified into: Selection, Iteration, and Jump statements.
1.) Selection Statements
Used to choose a part of the program to execute based on a condition.
a) if Statement - Executes a block of code only if the condition is true.
Syntax:
if (condition) { // statements }
Example:
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5"); }
b) if-else Statement - Executes one block if the condition is true, otherwise executes another
block.
Syntax:
if (condition) { // true block }
else { // false block }
Example:
int num = 4;
if (num % 2 == 0) { System.out.println("Even"); }
else { System.out.println("Odd"); }
c) if-else-if Ladder - Multiple conditions are checked sequentially.
Syntax:
if (condition1) { // block1 }
else if (condition2) { // block2 }
else { // default block }
d) switch Statement - Used for multi-way branching based on a variable's value.
Syntax:
switch (variable) {
case value1: // statements; break;
case value2: // statements; break;
default: // statements;
}
Example:
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}
2.) Iteration (Looping) Statements
Used to repeat a set of statements multiple times.
a) while Loop - Executes a block while the condition is true.
Syntax:
while (condition) {
// loop body
}
b) do-while Loop - Executes the loop body at least once, then checks the condition.
Syntax:
do {
// loop body
} while (condition);
c) for Loop - Repeats a block of code a fixed number of times.
Syntax:
for (initialization; condition; update) {
// loop body
}
d) for-each Loop - Iterates over elements in arrays or collections.
Syntax:
for (datatype var : array) {
// loop body
}
3.) Jump Statements
Used to change the normal flow of control.
a) break - Exits the loop or switch immediately.
b) continue - Skips the current iteration and proceeds with the next iteration.
c) return – Return a final result and Exits from the method.
Example Program for Control Statements
public class ControlExample {
public static void main(String[] args) {
// if-else
int num = 5;
if (num > 0) System.out.println("Positive");
else System.out.println("Negative");
// for loop with continue
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip 3
System.out.print(i + " ");
}
// switch
int choice = 2;
switch (choice) {
case 1: System.out.println("\nOne"); break;
case 2: System.out.println("Two"); break;
default: System.out.println("Other");
}
}
}
Output:
Positive
1 2 4 5
Two
4. Discuss the use and types of access specifiers in Java with examples.
Access specifiers (or access modifiers) in Java define the visibility and accessibility of classes,
methods, and variables.
They control which parts of a program can access a particular member.
Types of Access Specifiers
Specifier Description Scope
public Accessible from anywhere in the program. Global
Accessible within the same package and
protected Package + Inheritance
subclasses (even in different packages).
default (no keyword) Accessible only within the same package. Package
private Accessible only within the same class. Class only
Syntax:
access_specifier data_type variableName;
access_specifier returnType methodName() {
// code
}
Example for Access Specifiers:
class Demo {
public int pub = 1; // Public
private int pri = 2; // Private
protected int pro = 3; // Protected
int def = 4; // Default
public void show() {
System.out.println("Public: " + pub);
System.out.println("Private: " + pri);
System.out.println("Protected: " + pro);
System.out.println("Default: " + def);
}
}
public class AccessTest {
public static void main(String[] args) {
Demo obj = new Demo();
System.out.println(obj.pub); // Accessible
// System.out.println(obj.pri); // Not accessible
System.out.println(obj.pro); // Accessible (same package)
System.out.println(obj.def); // Accessible (same package)
obj.show(); // Access private via method
}
}
Output:
1
3
4
Public: 1
Private: 2
Protected: 3
Default: 4
5. Explain constructors and methods in Java along with their definition
and usage with examples.
1. Constructors in Java
A constructor is a special method in Java that is automatically called when an object is
created and It is used to initialize objects.
A constructor has the same name as the class and does not have a return type.
Usage:
To assign initial values to object variables.
To execute code automatically during object creation.
Types of Constructors:
Default Constructor – No parameters, initializes default values.
Parameterized Constructor – Accepts parameters to initialize variables with specific
values.
Syntax:
class ClassName {
ClassName() {
// default constructor body
}
ClassName(parameters) {
// parameterized constructor body
}
}
2. Methods in Java
A method is a block of code that performs a specific task and is executed when called.
Methods help in code reusability and modularity.
They can take parameters and return values.
Usage:
To organize code into reusable blocks.
To perform operations multiple times without rewriting code.
Types of Methods:
1. Instance Methods – Belong to an object, require object creation to call.
2. Static Methods – Belong to the class, can be called without creating an object.
Syntax:
returnType methodName(parameters) {
// method body
}
Example program for Constructors and Methods:
// Abstraction
abstract class Animal {
protected String name; // protected (Access Specifier)
public abstract void sound(); // abstract method
}
// Inheritance
class Dog extends Animal {
private String breed; // Encapsulation
static String category = "Mammal"; // Static member/variable
// Default constructor
public Dog() {
this("Unknown");
}
// Parameterized constructor
public Dog(String breed) {
this.name = "Dog";
this.breed = breed;
}
// Normal method
public void displayInfo() {
System.out.println("Name: " + name + ", Breed: " + breed);
}
// Compile-time Polymorphism (Overloading)
public void sound(String mood) {
System.out.println(name + " is " + mood);
}
// Runtime Polymorphism (Overriding)
public void sound() {
System.out.println("Bark! Breed: " + breed);
}
// Static method
public static void showCategory() {
System.out.println("Category: " + category);
}
}
public class Test {
public static void main(String[] args) {
Dog d1 = new Dog(); // default constructor
Dog d2 = new Dog("Labrador"); // parameterized constructor
d1.displayInfo(); // method call
d1.sound(); // runtime polymorphism
d2.displayInfo(); // method call
d2.sound("Happy"); // compile-time polymorphism
Dog.showCategory(); // static method
}
}
Output
Name: Dog, Breed: Unknown
Bark! Breed: Unknown
Name: Dog, Breed: Labrador
Dog is Happy
Category: Mammal