- A 7
- B 8
- C 15
- D 53
The addition operator (+) in Java is used to add two numeric values. In this case, 5 + 3 equals 8.
public class AdditionExample { public static void main(String[] args) { // Define the numbers int num1 = 5; int num2 = 3; // Add the numbers using the addition operator int result = num1 + num2; // Display the result System.out.println("Result of " + num1 + " + " + num2 + " is: " + result); } }
When you run this program, it will output:
Result of 5 + 3 is: 8
The multiplication operator (*) in Java is used to multiply two numeric values.
public class MultiplicationExample { public static void main(String[] args) { // Define the numbers int num1 = 5; int num2 = 3; // Multiply the numbers using the multiplication operator int result = num1 * num2; // Display the result System.out.println("Result of " + num1 + " * " + num2 + " is: " + result); } }
When you run this program, it will output:
Result of 5 * 3 is: 15
The modulus operator (%) in Java returns the remainder of the division of two numbers. In this case, 10 % 3 equals 1.
public class ModulusExample { public static void main(String[] args) { // Define the numbers int a = 10; int b = 3; // Calculate the result using the modulus operator int result = a % b; // Display the result System.out.println("Result of " + a + " % " + b + " is: " + result); } }
When you run this program, it will output:
Result of 10 % 3 is: 1
The compound assignment operator (+=) in Java is used to add the right operand to the left operand and assign the result to the left operand. In this case, x += 5 is equivalent to x = x + 5.
public class PlusEqualsExample { public static void main(String[] args) { // Define the initial value of x int x = 10; // Use the += operator to add 5 to x x += 5; // Display the updated value of x System.out.println("Value of x after x += 5: " + x); } }
When you run this program, it will output:
Value of x after x += 5: 15
The division operator (/) in Java is used to divide two numeric values. In this case, 8 / 2 equals 4, as the result of the division is a floating-point number.
public class DivisionExample { public static void main(String[] args) { // Define the values of a and b int a = 8; int b = 2; // Use the division operator to calculate a / b int result = a / b; // Display the result System.out.println("Result of a / b: " + result); } }
When you run this program, it will output:
Result of a / b: 4
Java does not have a built-in exponentiation operator. The ^ symbol in Java is the bitwise XOR operator, not an exponentiation operator.
public class BitwiseXORExample { public static void main(String[] args) { // Example 1: Using ^ for bitwise XOR int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result = a ^ b; // Binary: 0110 (Decimal: 6) System.out.println("Result of bitwise XOR: " + result); // Example 2: Using ^ for toggling a single bit int num = 8; // Binary: 1000 int mask = 1; // Binary: 0001 int toggled = num ^ mask; // Toggle the rightmost bit System.out.println("Toggled result: " + toggled); } }
The subtraction operator (-) in Java is used to subtract the right operand from the left operand. In this case, 15 - 7 equals 8.
public class SubtractionExample { public static void main(String[] args) { // Define the values of a and b int a = 15; int b = 7; // Use the subtraction operator to calculate a - b int result = a - b; // Display the result System.out.println("Result of a - b: " + result); } }
When you run this program, it will output:
Result of a - b: 8
The compound assignment operator (*=) in Java is used to multiply the right operand by the left operand and assign the result to the left operand. In this case, y *= 3 is equivalent to y = y * 3.
public class CompoundAssignmentExample { public static void main(String[] args) { // Define the initial value of y int y = 5; // Use the compound assignment with multiplication (y *= 3) y *= 3; // Display the result System.out.println("Value of y after y *= 3: " + y); } }
When you run this program, it will output:
Value of y after y *= 3: 15
When performing integer division in Java (/ with integer operands), the result is an integer, and any remainder is truncated. In this case, 20 / 4 equals 5.
public class IntegerDivisionExample { public static void main(String[] args) { // Define the values of p and q int p = 20; int q = 4; // Perform integer division (p / q) int result = p / q; // Display the result System.out.println("Result of p / q using integer division: " + result); } }
When you run this program, it will output:
Result of p / q using integer division: 5
This is because integer division in Java truncates the fractional part and returns the quotient as an integer. In this case, 20 / 4
results in 5
.
The compound assignment operator (%=) in Java is used to calculate the remainder of the division of the left operand by the right operand and assign the result to the left operand. In this case, z %= 6 is equivalent to z = z % 6.
public class ModulusAssignmentExample { public static void main(String[] args) { // Define the value of z int z = 17; // Perform modulus assignment (z %= 6) z %= 6; // Display the updated value of z System.out.println("Updated value of z after z %= 6: " + z); } }
When you run this program, it will output:
Updated value of z after z %= 6: 5
This is because the modulus assignment operator %=
calculates the remainder when the left operand is divided by the right operand and assigns the result to the left operand. In this case, 17 % 6
results in a remainder of 5
, so z
is updated to 5
.