JAVA – ASSIGNMENT 2
Name: Muhammed Niyasudheen K N
College: College of Applied Science, Nattika
Submission Date: 10 September 2025
Index
1. What are Arrays? Explain its types with syntax and example
2. Explain Control Statements with syntax and example
3. Explain Iteration Statements with syntax and example
4. Explain Operators in Java
5. Explain Class, Object creation with sample example
1. What are Arrays? Explain its types with syntax and example
An array is a collection of elements of the same data type stored in contiguous memory
locations. Arrays in Java are used to store multiple values in a single variable instead of
declaring separate variables for each value.
Types of Arrays in Java:
1. Single-Dimensional Array (1D Array):
Syntax:
int[] arr = new int[5];
Example:
int[] numbers = {10, 20, 30, 40};
2. Multidimensional Array:
Syntax:
int[][] arr = new int[3][3];
Example:
int[][] matrix = { {1, 2}, {3, 4} };
Diagrams:
1D Array
2D Array
2. Explain Control Statements with syntax and example
Control statements are used to control the flow of execution of a program. There are three
main types:
1. if Statement:
Syntax:
if (condition) {
// code
}
Example:
if (num > 0) {
System.out.println("Positive number");
}
2. if-else Statement:
Syntax:
if (condition) {
// code
} else {
// code
}
Example:
if (num % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
3. if-else-if Ladder:
Syntax:
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// code
}
Example:
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
4. switch Statement:
Syntax:
switch (variable) {
case value1:
// code
break;
default:
// code
}
Example:
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
default:
System.out.println("Invalid");
}
Jump Statements:
5. break:
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
6. continue:
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
3. Explain Iteration Statements with syntax and example
Iteration statements allow repetitive execution of code blocks. Types are:
1. for loop:
Syntax:
for (initialization; condition; update) {
// code
}
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
2. while loop:
Syntax:
while (condition) {
// code
}
Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
3. do-while loop:
Syntax:
do {
// code
} while (condition);
Example:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
4. Explain Operators in Java
Operators in Java are special symbols that perform operations on operands. Types:
1. Arithmetic Operators: +, -, *, /, %
2. Relational Operators: ==, !=, >, <, >=, <=
3. Logical Operators: &&, ||, !
4. Assignment Operators: =, +=, -=, *=, /=
5. Unary Operators: +, -, ++, --
6. Bitwise Operators: &, |, ^, ~, <<, >>
7. Conditional (Ternary) Operator: condition ? value1 : value2
8. instanceof Operator: Used to check object type
5. Explain Class, Object creation with sample example
A class is a blueprint for objects. An object is an instance of a class.
Syntax for class:
class ClassName {
// fields
// methods
}
Example:
class Student {
String name;
int age;
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s = new Student();
s.name = "John";
s.age = 20;
s.display();
}
}