Single Choice Easy

QIf a is 5 and b is 2, what is the value of the expression a *= b using the compound assignment operator in Java?

ID: #20155 Assignment Operators Java 130 views
Question Info
#20155Q ID
EasyDifficulty
Assignment Operators JavaTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A 7
  • B 10
  • C 5
  • D 2
Correct Answer

Explanation

In Java, the expression a *= b is a compound assignment operator, which multiplies the value of a by the value of b and assigns the result back to a. It is equivalent to the expression a = a * b.

If a is initially 5 and b is 2, the result of the expression a *= b would be:


a = a * b;

After the operation, the value of a will be 10 (5 * 2).

Here's an example in Java:


public class CompoundAssignmentExample {
    public static void main(String[] args) {
        // Declare variables
        int a = 5;
        int b = 2;

        // Compound assignment
        a *= b;

        // Display the result
        System.out.println("The value of a after a *= b is: " + a);
    }
}

When you run this Java program, it will output "The value of a after a *= b is: 10".

Share This Question

Challenge a friend or share with your study group.