Unit 1
Q2 (a) Compare C, C++ and Java.
(5 Marks Answer)
Feature C C++ Java
Programming Procedural Object-Oriented + Fully Object-Oriented
Paradigm Procedural
Platform Platform dependent Platform dependent Platform independent
Dependency (via JVM)
Memory Manual (using Manual (using Automatic Garbage
Management malloc/free) new/delete) Collection
Inheritance Not supported Supported (multiple Supported (multiple
inheritance via inheritance via
classes) interfaces)
Compilation Directly compiled to Compiled to machine Compiled to bytecode
machine code code and executed by JVM
Header Files Uses header files Uses header files like Uses packages like
like <stdio.h> <iostream> [Link].*
Pointers Fully supported Supported Not supported directly
(for safety)
Exception Not available Available Strongly supported
Handling using try, catch,
finally
Explanation:
● C is a structured programming language, mainly used for system-level programming.
● C++ introduced the concept of classes, inheritance, and polymorphism to make
programming modular.
● Java simplified memory handling and made programs portable and secure by removing
low-level features like pointers.
Q2 (b) Write a Java program to find the size of datatypes.
(5 Marks – Simple Java Program)
class DataTypeSize {
public static void main(String[] args) {
[Link]("Size of byte: " + ([Link] / 8) + "
bytes");
[Link]("Size of short: " + ([Link] / 8) + "
bytes");
[Link]("Size of int: " + ([Link] / 8) + "
bytes");
[Link]("Size of long: " + ([Link] / 8) + "
bytes");
[Link]("Size of float: " + ([Link] / 8) + "
bytes");
[Link]("Size of double: " + ([Link] / 8) + "
bytes");
[Link]("Size of char: " + ([Link] / 8) + "
bytes");
}
}
Output:
Size of byte: 1 bytes
Size of short: 2 bytes
Size of int: 4 bytes
Size of long: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 2 bytes
Explanation:
● Java provides wrapper classes (Byte, Short, Integer, etc.) that contain the constant
SIZE, which represents the number of bits.
● Dividing by 8 gives the size in bytes.
Q3 (a) Write a short note on:
1. Java statements
2. Command line arguments
3. Comments
(5 Marks Answer)
1. Java Statements
● A statement is a complete instruction in Java that performs an action.
● Every statement ends with a semicolon ( ; ).
● Types of Java statements:
Declaration Statement – Declares variables.
int a, b;
○
Expression Statement – Assigns or performs operations.
a = b + 5;
○
Control Statement – Controls the flow of execution.
if (a > b) [Link]("A is greater");
○
Looping Statement – Repeats a block of code.
for(int i=0; i<5; i++) { ... }
○
2. Command Line Arguments
● Command line arguments are values passed to the main() method when the program
is executed.
● These arguments are stored in the String array args[] of the main method.
Example:
class CommandLineDemo {
public static void main(String[] args) {
[Link]("First Argument: " + args[0]);
}
}
Execution:
java CommandLineDemo Hello
Output:
First Argument: Hello
●
3. Comments
● Comments are used to improve readability and ignore code during compilation.
● Types of Comments in Java:
1. Single-line comment: // comment here
2. Multi-line comment: /* comment here */
3. Documentation comment: /** comment here */
Example:
// This is a single-line comment
/* This is a multi-line comment */
/**
* This is a documentation comment
*/
●
Q3 (b) What is the difference between while and do-while loop?
(5 Marks Answer)
Feature while loop do-while loop
Condition Condition is checked before executing Condition is checked after
Checking the loop body. executing the loop body.
Execution Loop may not execute even once if Loop executes at least once
Guarantee condition is false initially. even if condition is false.
Syntax while(condition) { do { statements; }
statements; } while(condition);
Use Case When the number of iterations is When the loop must execute at
unknown and pre-check needed. least once.
Example:
// while loop example
int i = 1;
while(i <= 3) {
[Link]("While loop: " + i);
i++;
}
// do-while loop example
int j = 1;
do {
[Link]("Do-while loop: " + j);
j++;
} while(j <= 3);
Output:
While loop: 1
While loop: 2
While loop: 3
Do-while loop: 1
Do-while loop: 2
Do-while loop: 3
Q4 (a) Examine difference between break and continue statement.
(5 Marks Answer)
Feature break continue
Definition Used to terminate the loop or Used to skip the current iteration and
switch statement completely. continue with the next iteration of the
loop.
Control Transfers control outside the Transfers control to the beginning of the
Transfer loop or switch. next iteration.
Usage Commonly used inside switch, Commonly used inside loops only (for,
for, while, and do-while while, do-while).
loops.
Effect on Stops the loop immediately. Skips the remaining statements in the
Loop current iteration.
Example See below See below
Example of break:
class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3)
break; // exits the loop when i = 3
[Link](i);
}
}
}
Output:
1
2
👉 The loop stops when i == 3.
Example of continue:
class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue; // skips iteration when i = 3
[Link](i);
}
}
}
Output:
1
2
4
5
👉 The loop skips 3 but continues running.
Q4 (b) Develop a Java program to determine whether a given integer is an
Even number or not.
(5 Marks – Simple Java Program)
import [Link];
class EvenOrOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
int num = [Link]();
if (num % 2 == 0)
[Link](num + " is an Even number.");
else
[Link](num + " is an Odd number.");
}
}
Output:
Enter an integer: 6
6 is an Even number.
Explanation:
● % (modulus operator) is used to find the remainder.
● If num % 2 == 0, the number is even; otherwise, it’s odd.
🧩 Types of Decision-Making Statements in Java
1. if statement
2. if-else statement
3. nested if statement
4. if-else-if ladder
5. switch statement
1. if Statement
✅ Syntax:
if (condition) {
// statements to execute if condition is true
}
✅ Example:
int num = 10;
if (num > 0) {
[Link]("Number is positive");
}
Output:
Number is positive
Explanation:
The condition (num > 0) is true, so the statement inside the if block executes.
2. if-else Statement
✅ Syntax:
if (condition) {
// executes when condition is true
} else {
// executes when condition is false
}
✅ Example:
int num = -5;
if (num >= 0)
[Link]("Positive number");
else
[Link]("Negative number");
Output:
Negative number
3. Nested if Statement
✅ Syntax:
if (condition1) {
if (condition2) {
// executes when both conditions are true
}
}
✅ Example:
int num = 15;
if (num > 0) {
if (num % 2 == 0)
[Link]("Positive Even number");
else
[Link]("Positive Odd number");
}
Output:
Positive Odd number
4. if-else-if Ladder
✅ Syntax:
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;
✅ Example:
int marks = 75;
if (marks >= 90)
[Link]("Grade A");
else if (marks >= 75)
[Link]("Grade B");
else if (marks >= 50)
[Link]("Grade C");
else
[Link]("Fail");
Output:
Grade B
Explanation:
The program checks conditions sequentially until one is true, then executes that block.
5. switch Statement
✅ Syntax:
switch(expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}
✅ Example:
int day = 3;
switch(day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
default: [Link]("Invalid day");
}
Output:
Wednesday
Explanation:
The value of day matches case 3, so that block executes.
🧠 Summary Table
Statement Description Example Use
if Executes code when condition is true Check positive
number
if-else Executes one block for true, another for false Check even/odd
nested Checks multiple conditions inside one Check sign and parity
if another
if-else- Tests multiple conditions sequentially Grading system
if
switch Compares one value with many options Menu or day selection
Q6 (a) Describe abstraction and encapsulation.
(5 Marks Answer)
1️⃣ Abstraction
Definition:
Abstraction means hiding the internal implementation and showing only the necessary
details to the user.
Example:
When you use a mobile phone, you press buttons to make calls — but you don’t know how the
internal circuits work.
Similarly, in Java, we can hide implementation details using abstract classes and interfaces.
✅ Syntax:
abstract class Shape {
abstract void draw(); // abstract method
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
}
class Test {
public static void main(String[] args) {
Shape s = new Circle();
[Link]();
}
}
Output:
Drawing Circle
Explanation:
● The user only calls draw() method — internal logic is hidden.
● This is the concept of abstraction.
2️⃣ Encapsulation
Definition:
Encapsulation means wrapping data (variables) and methods (functions) into a single unit
— like a capsule that contains medicine.
In Java, encapsulation is achieved using classes, and data hiding is done using the
private keyword.
✅ Example:
class Student {
private int rollNo;
public void setRollNo(int r) {
rollNo = r; // setter method
}
public int getRollNo() {
return rollNo; // getter method
}
}
class Main {
public static void main(String[] args) {
Student s = new Student();
[Link](101);
[Link]("Roll Number: " + [Link]());
}
}
Output:
Roll Number: 101
Explanation:
● Data (rollNo) is hidden from direct access.
● Controlled access is given through methods → Encapsulation.
Difference between Abstraction and Encapsulation
Feature Abstraction Encapsulation
Definition Hiding implementation details Binding data & methods together
Achieved Abstract classes & Interfaces Classes & Access Modifiers
By
Focus On “what” a system does On “how” it hides data
Example abstract class Shape private variables with
getters/setters
Q6 (b) Illustrate various operators with suitable examples.
(5 Marks Answer)
Definition:
Operators in Java are symbols that perform operations on variables and values.
Types of Operators
Type Example Description
1. Arithmetic Operators +, -, *, /, % Used for mathematical
calculations
2. Relational Operators <, >, <=, >=, ==, != Compare two values
3. Logical Operators &&, `
4. Assignment =, +=, -=, *=, /= Assign values to variables
Operators
5. Increment/Decrement ++, -- Increase or decrease value by 1
6. Conditional (Ternary) condition ? expr1 : Short form of if-else
expr2
7. Bitwise Operators &, ` , ^, <<, >>`
✅ Example Program
class OperatorExample {
public static void main(String[] args) {
int a = 10, b = 5;
// Arithmetic
[Link]("Addition: " + (a + b));
// Relational
[Link]("a > b: " + (a > b));
// Logical
[Link]("(a > b) && (a != 0): " + ((a > b) && (a !=
0)));
// Assignment
a += 2;
[Link]("After a += 2: " + a);
// Ternary
String result = (a % 2 == 0) ? "Even" : "Odd";
[Link]("a is " + result);
}
}
Output:
Addition: 15
a > b: true
(a > b) && (a != 0): true
After a += 2: 12
a is Even
Q7. Discuss about Object-Oriented Programming (OOP)
Principles.
(10 Marks Answer)
🌟 Definition:
Object-Oriented Programming (OOP) is a programming paradigm that organizes software
design around objects, which are instances of classes.
It helps in modular, reusable, and maintainable code.
Java is a pure object-oriented language, and it supports the four main principles of OOP:
🧩 1. Encapsulation
Definition:
Encapsulation means binding data and methods into a single unit (class).
It helps to hide internal details and only expose necessary information.
Example:
class Student {
private int marks;
public void setMarks(int m) {
marks = m; // setter
}
public int getMarks() {
return marks; // getter
}
}
class Main {
public static void main(String[] args) {
Student s = new Student();
[Link](95);
[Link]("Marks: " + [Link]());
}
}
Output:
Marks: 95
👉 Data is hidden using private and accessed via public methods — showing
encapsulation.
🧩 2. Inheritance
Definition:
Inheritance allows a class (subclass) to inherit properties and behavior from another class
(superclass).
It promotes code reusability and supports hierarchical relationships.
Syntax:
class Parent {
void display() {
[Link]("This is Parent class");
}
}
class Child extends Parent {
void show() {
[Link]("This is Child class");
}
}
class Main {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
[Link]();
}
}
Output:
This is Parent class
This is Child class
👉 The Child class inherits features of Parent class.
🧩 3. Polymorphism
Definition:
Polymorphism means “many forms.”
It allows one interface to be used for different types of actions.
There are two types:
● Compile-time polymorphism (Method Overloading)
● Runtime polymorphism (Method Overriding)
Example – Method Overloading:
class Display {
void show(int a) {
[Link]("Integer: " + a);
}
void show(String b) {
[Link]("String: " + b);
}
}
class Main {
public static void main(String[] args) {
Display d = new Display();
[Link](10);
[Link]("Java");
}
}
Output:
Integer: 10
String: Java
Example – Method Overriding:
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Animal a = new Dog();
[Link]();
}
}
Output:
Dog barks
👉 Same method behaves differently — Polymorphism.
🧩 4. Abstraction
Definition:
Abstraction means showing only essential details and hiding the internal implementation.
Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
}
class Main {
public static void main(String[] args) {
Shape s = new Circle();
[Link]();
}
}
Output:
Drawing Circle
👉 The internal logic of draw() is hidden — only the behavior is visible.
🧠 Other Supporting OOP Concepts
Concept Description
Class Blueprint for creating objects (defines data and
behavior).
Object Instance of a class (real-world entity).
Constructor Special method to initialize objects.
Message Communication between objects using methods.
Passing
✅ Advantages of OOP
1. Reusability – Code can be reused using inheritance.
2. Security – Data hiding ensures protection.
3. Modularity – Code divided into objects for easy maintenance.
4. Flexibility – Easy to modify and extend.
5. Scalability – Suitable for large and complex applications.
Q8. Explain about iteration statements with suitable
examples.
(10 Marks Answer)
🌟 Definition:
Iteration statements (also called looping statements) in Java are used to execute a block of
code repeatedly as long as a given condition is true.
They help in reducing code repetition and make programs more efficient.
🧩 Types of Iteration Statements in Java
1. while loop
2. do-while loop
3. for loop
4. enhanced for loop (for-each loop)
1️⃣ while Loop
✅ Syntax:
while (condition) {
// statements to be executed
}
✅ Explanation:
● The condition is checked before entering the loop.
● If the condition is true, the body executes; otherwise, it stops.
✅ Example:
class WhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("Count: " + i);
i++;
}
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Note:
If the condition is false initially, the loop body may not execute even once.
2️⃣ do-while Loop
✅ Syntax:
do {
// statements
} while (condition);
✅ Explanation:
● The body executes first, then the condition is checked.
● Ensures that the loop runs at least once.
✅ Example:
class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
[Link]("Value: " + i);
i++;
} while (i <= 3);
}
}
Output:
Value: 1
Value: 2
Value: 3
Note:
The loop executes once even if the condition is false initially.
3️⃣ for Loop
✅ Syntax:
for (initialization; condition; increment/decrement) {
// statements
}
✅ Explanation:
● The for loop is used when the number of iterations is known in advance.
● It has three parts: initialization, condition, and update.
✅ Example:
class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("Number: " + i);
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
4️⃣ Enhanced for Loop (for-each Loop)
✅ Syntax:
for (datatype variable : array) {
// statements
}
✅ Explanation:
● Introduced in Java 5, mainly used for iterating over arrays or collections.
● Simplifies accessing elements without using indexes.
✅ Example:
class ForEachExample {
public static void main(String[] args) {
int numbers[] = {10, 20, 30, 40};
for (int n : numbers) {
[Link]("Value: " + n);
}
}
}
Output:
Value: 10
Value: 20
Value: 30
Value: 40
🧠 Comparison Table
Loop Type Condition Check Executes At Least Best Used For
Once
while Before loop body No Unknown number of iterations
do-while After loop body Yes Execute at least once
for Before loop body No Known number of iterations
for-each For each element Yes Arrays and collections
✅ Advantages of Iteration Statements
1. Reduces repetitive code.
2. Makes programs concise and readable.
3. Easier to manage repetitive tasks (like printing, summing, counting).
4. Enhances program efficiency.
5. Supports both fixed and dynamic repetitions.
Q9. Illustrate various OOP (Object-Oriented
Programming) concepts.
(10 Marks Answer)
🌟 Definition:
Object-Oriented Programming (OOP) is a programming model that organizes software design
around objects instead of functions and logic.
Objects represent real-world entities, having data (attributes) and methods (behavior).
Java is a pure object-oriented language, meaning almost everything in Java revolves around
classes and objects.
🧩 Major OOP Concepts in Java
No. Concept Description
1 Class Blueprint or template that defines data and methods.
2 Object Instance of a class.
3 Encapsulation Binding data and methods together.
4 Abstraction Hiding implementation details and showing only necessary
features.
5 Inheritance Acquiring properties and behavior from another class.
6 Polymorphism One interface, many forms (method overloading/overriding).
7 Message Communication between objects via methods.
Passing
Let’s explain each concept clearly 👇
1️⃣ Class
Definition:
A class is a blueprint that defines the structure (variables) and behavior (methods) of objects.
Syntax:
class Student {
int rollNo;
String name;
void display() {
[Link](rollNo + " " + name);
}
}
2️⃣ Object
Definition:
An object is an instance of a class. It represents a real-world entity like a student, car, or
employee.
Example:
class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Object creation
[Link] = 101;
[Link] = "Ram";
[Link]();
}
}
Output:
101 Ram
3️⃣ Encapsulation
Definition:
It is the process of wrapping variables and methods into a single unit (class).
It also involves data hiding by making variables private and providing access via get and
set methods.
Example:
class Account {
private int balance = 1000;
public int getBalance() {
return balance;
}
public void setBalance(int amount) {
balance = amount;
}
}
4️⃣ Abstraction
Definition:
Abstraction means showing only essential features and hiding the implementation details.
Example:
abstract class Shape {
abstract void draw(); // abstract method
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
}
Explanation:
User only calls draw() — internal logic is hidden.
5️⃣ Inheritance
Definition:
Inheritance allows a class (child) to reuse code from another class (parent) using the
extends keyword.
Example:
class Animal {
void eat() { [Link]("Eating..."); }
}
class Dog extends Animal {
void bark() { [Link]("Barking..."); }
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
Output:
Eating...
Barking...
6️⃣ Polymorphism
Definition:
Polymorphism means many forms — one method behaves differently based on context.
Two types:
● Compile-time (Method Overloading)
● Runtime (Method Overriding)
Example – Overloading:
class Display {
void show(int a) { [Link]("Integer: " + a); }
void show(String s) { [Link]("String: " + s); }
}
Example – Overriding:
class Animal {
void sound() { [Link]("Animal sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Dog barks"); }
}
7️⃣ Message Passing
Definition:
Objects communicate with each other through method calls.
This allows modular and reusable code.
Example:
class Student {
void display() {
[Link]("Hello from Student class");
}
}
class Main {
public static void main(String[] args) {
Student s = new Student();
[Link](); // message passing
}
}
🧠 Advantages of OOP
1. Modularity – Code is divided into objects.
2. Reusability – Code can be reused via inheritance.
3. Data Security – Data is hidden using encapsulation.
4. Flexibility – Easier to modify and update.
5. Scalability – Suitable for large projects.
6. Maintainability – Clear structure reduces complexity.
Q10. Discuss about jump statements with an example.
(10 Marks Answer)
🌟 Definition:
Jump statements in Java are used to transfer control from one part of the program to
another.
They are mainly used to alter the normal flow of execution within loops or switch statements.
Java provides three types of jump statements:
1. break
2. continue
3. return
🧩 1️⃣ break Statement
Definition:
The break statement is used to terminate the loop or switch statement immediately.
When encountered, control jumps to the statement next to the loop or switch.
✅ Syntax:
break;
✅ Example:
class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3)
break; // exits the loop when i = 3
[Link]("i = " + i);
}
[Link]("Loop terminated.");
}
}
Output:
i = 1
i = 2
Loop terminated.
Explanation:
When i == 3, the break statement stops the loop entirely.
🧩 2️⃣ continue Statement
Definition:
The continue statement skips the current iteration of the loop and moves to the next
iteration.
It does not terminate the loop — only skips part of the loop body.
✅ Syntax:
continue;
✅ Example:
class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue; // skips printing when i = 3
[Link]("i = " + i);
}
}
}
Output:
i = 1
i = 2
i = 4
i = 5
Explanation:
When i == 3, continue skips the print statement but continues with the next iteration.
🧩 3️⃣ return Statement
Definition:
The return statement is used to exit from a method and optionally return a value to the
caller.
✅ Syntax:
return; // for void methods
return value; // for methods with a return type
✅ Example:
class ReturnExample {
static int add(int a, int b) {
return a + b; // returns sum to caller
}
public static void main(String[] args) {
int result = add(5, 10);
[Link]("Sum = " + result);
}
}
Output:
Sum = 15
Explanation:
The return statement sends the calculated sum back to the main method.
🧠 Comparison of Jump Statements
Statemen Function Used In Behavior
t
break Terminates the Loop, switch Exits immediately
loop/switch
continue Skips current iteration Loop Jumps to next iteration
return Exits from a method Methods Returns control (and optionally a
value)
✅ Advantages of Jump Statements
1. Helps control program flow efficiently.
2. Used for early termination of loops or methods.
3. Simplifies complex loop logic.
4. Makes programs more readable and controlled.