Bird
0
0

What will be the output of this Java code?

medium📝 Predict Output Q13 of 15
Java - Memory Management Basics
What will be the output of this Java code?
public class Test {
  public static void main(String[] args) {
    int a = 5;
    int b = 10;
    int c = add(a, b);
    System.out.println(c);
  }
  public static int add(int x, int y) {
    return x + y;
  }
}
A15
B510
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Trace method calls and parameters

    Main calls add(5, 10), passing values 5 and 10 as local variables on the stack.
  2. Step 2: Calculate return value

    Add method returns 5 + 10 = 15, which is stored in c and printed.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    5 + 10 = 15 [OK]
Quick Trick: Add method returns sum; print result [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Confusing method parameters with global variables
  • Expecting runtime or compile errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes