MCQ Single Best Answer Easy

QIf q is 8 and r is 3, what is the result of the expression q -= r using the compound assignment operator in Java?

ID: #20157 Assignment Operators Java 118 views
Question Info
#20157Q ID
EasyDifficulty
Assignment Operators JavaTopic

Choose the Best Option

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

  • A 5
  • B 11
  • C 24
  • D 2
Correct Answer: Option A

Explanation

In Java, the expression q -= r is a compound assignment operator, which subtracts the value of r from q and assigns the result back to q. It is equivalent to the expression q = q - r.

If q is initially some numeric value and r is another numeric value, the result of the expression q -= r would be:


q = q - r;

Here's an example in Java:


public class CompoundAssignmentExample {
    public static void main(String[] args) {
        // Declare variables
        int q = 8;
        int r = 3;

        // Compound assignment
        q -= r;

        // Display the result
        System.out.println("The value of q after q -= r is: " + q);
    }
}

In this example, if q is initially 8 and r is 3, after the operation, the value of q will be 5 (8 - 3).

Share This Question

Challenge a friend or share with your study group.