Bird
0
0

You want to call a method calculateSum that returns an integer sum of two numbers. How do you call it and store the result in a variable named result?

hard📝 Application Q8 of 15
Java - Methods and Code Reusability
You want to call a method calculateSum that returns an integer sum of two numbers. How do you call it and store the result in a variable named result?
AcalculateSum(5, 10) = result;
Bint result = calculateSum(5, 10);
Cint result = calculateSum;
Dresult = calculateSum(5, 10);
Step-by-Step Solution
Solution:
  1. Step 1: Understand method call with return value

    To store a returned value, assign the method call to a variable of matching type.
  2. Step 2: Check correct syntax

    int result = calculateSum(5, 10); correctly declares int result and assigns calculateSum(5, 10) to it.
  3. Final Answer:

    int result = calculateSum(5, 10); -> Option B
  4. Quick Check:

    Store return value by assignment to variable [OK]
Quick Trick: Assign method call to variable with matching type [OK]
Common Mistakes:
  • Trying to assign variable to method call
  • Omitting parentheses in method call
  • Assigning method name without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes