Multiple choice technology programming languages

Given: public class Pass { public static void main(String [] args) { int x 5; Pass p = new Pass(); p.doStuff(x); System.out.print(” main x = “+ x); } void doStuff(int x) { System.out.print(” doStuff x = “+ x++); } } What is the result?

  1. doStuffx = 5 main x = 5

  2. Compilation fails.

  3. doStuffx = 6 main x = 5

  4. An exception is thrown at runtime

  5. doStuffx = 6 main x = 6

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Java uses pass-by-value for primitive types. When x=5 is passed to doStuff(), the method receives a copy of x (value 5). The post-increment x++ prints 5 first, then increments the local parameter to 6. The original x in main remains 5 because primitives are passed by value, not by reference.

AI explanation

Java passes primitives by value, so the copy of x inside doStuff() is independent of x in main(). The post-increment x++ prints the current value (5) before incrementing, so doStuff prints 'doStuff x = 5' and the local increment never reaches main's x. Back in main(), x is still 5, so the final output is 'doStuff x = 5 main x = 5'.