Java Module 2 Answers Sachin
Java Module 2 Answers Sachin
BPLCK105C
MODULE 2
Example:
class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
class BitwiseExample {
public static void main(String args[]) {
int a = 5, b = 3; // Binary: 5 = 0101, 3 = 0011
System.out.println("a & b = " + (a & b)); // AND (0001 = 1)
System.out.println("a | b = " + (a | b)); // OR (0111 = 7)
System.out.println("a ^ b = " + (a ^ b)); // XOR (0110 = 6)
}
}
Output:
a&b=1
a|b=7
a^b=6
d) Example: Bitwise Shift Operators
class ShiftExample {
public static void main(String args[]) {
int x = 8; // Binary: 1000
System.out.println("x << 1 = " + (x << 1)); // Left shift (10000
= 16)
System.out.println("x >> 1 = " + (x >> 1)); // Right shift (0100
= 4)
}
}
Output:
x << 1 = 16
x >> 1 = 4
e) Explanation:
Bitwise operators perform operations on binary digits instead of
whole numbers.
Left shift (<<) multiplies by 2, while right shift (>>) divides by 2.
Bitwise NOT (~) inverts all bits, flipping 1 to 0 and vice versa.
4.Relational Operators
a) Definition:
Relational operators compare two values and return true or false.
b) Operators and Their Use:
a) Definition:
The switch statement is a multi-way branch statement that allows a
variable to be tested for equality against multiple values, making it
an alternative to long if-else-if chains.
b) Syntax:
switch (expression) {
case value1:
// Statements
break;
case value2:
// Statements
break;
default:
// Default case if no match found
}
class SwitchExample {
public static void main(String args[]) {
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");
}
}
}
Output:
Wednesday
1. while Loop
Definition:
Executes a block as long as the condition is true.
Syntax & Example:
int i = 5;
while (i > 0) {
System.out.println("Countdown: " + i);
i--;
}
Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
2. do-while Loop
Definition:
Executes the block at least once, even if the condition is false.
Syntax & Example:
int i = 5;
do {
System.out.println("Number: " + i);
i--;
} while (i > 0);
Output:
Number: 5
Number: 4
Number: 3
Number: 2
Number: 1
3. for Loop
Definition:
Executes a block a fixed number of times.
Syntax & Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
1. break Statement
Definition:
The break statement immediately exits a loop or a switch
statement, transferring control to the next statement outside the
loop.
Syntax & Example:
for (int i = 0; i < 10; i++) {
if (i == 5) break; // Exits loop when i is 5
System.out.println(i);
}
Output :
0
1
2
3
4
Key Points:
✅ Used to exit for, while, do-while, and switch statements.
✅ Stops execution of the loop immediately when a condition
is met.
✅ Can be labeled to break out of nested loops.
2. continue Statement
Definition:
The continue statement skips the current iteration of a loop and
moves to the next iteration.
Syntax & Example:
for (int i = 0; i < 5; i++) {
if (i == 2) continue; // Skips iteration when i is 2
System.out.println(i);
}
Output:
0
1
3
4
Key Points:
✅ Used to skip specific iterations of a loop.
✅ Moves control directly to the next iteration.
✅ Can be labelled to continue an outer loop in nested
structures.
3. return Statement
Definition:
The return statement exits from a method and optionally returns a
value to the calling method.
Syntax & Example:
class ReturnExample {
static int sum(int a, int b) {
return a + b; // Returns sum to caller
}
public static void main(String args[]) {
int result = sum(10, 5);
System.out.println("Sum: " + result);
}
}
Output:
Sum: 15
Use Cases:
✅ Exiting a method when a certain condition is met.
✅ Returning values from methods.
✅ Used in recursion for returning computed results.
SACHIN (1ST SEM)
DATA SCIENCE
ACET