Depending on the language and how it passes in values, you'll have different answers. Assuming it's javascript the answer is 0,1. In other languages it might be 1, 1.
That's a tricky question, and if you're not good enough to understand why they're asking that then you fail!
This particular example is 0, [1], which is easily seen by pasting it in the console.
There may be some edge cases I'm not covering here, but JavaScript passes primitives by value (strings, numbers, etc) and non-primitives (objects, arrays, etc) by passing a copy of the reference.
What this means is if you reassign the reference inside of the method, it will only affect that scope, because the reference itself is a copy. If you modify the properties of the non-primitive, it will be modified, because the copy of the reference points to the same non-primitive.
Since primitives are passed by value then any modifications are not reflected outside of the method.
To the other poster below, in JavaScript, const with an array (object, etc) simply prevents reassignment of the variable, the array can still be modified.
This is more about knowing what const does to an array. Which is, IMHO, not how I would expect it to behave. So it's somewhat of a trick question. But I don't write too much JS so I don't know how well known that quirk would be.
a and b are shadowed in the foo function so the function only acts on the shadowed (and copied) variables. Once you return from foo, the const a and b are unchanged.
Not a great question as a pass or fail test imho because if you use shadowed names often enough to be able to parse this code in your head, you’re writing bad code. There is a reason why shadowing names is a bad practice: it’s hard to figure out what the end result is and account for side effects! But as a discussion on all the points above then it would teach the recruiter something about what the candidate knows rather than using this as a trivia question.
The const on b only applies to the object reference to b, and doesn't prevent foo from changing the contents of b. No doubt that's why the test question includes an array in the first place.
At least I assume that's how const works; I've not used const in JS.
That’s possible. In reality, if I ever came across that piece of code and had a bug to fix in it, the first thing I’d do is rename a and b in one scope or the other because it’s just asking for trouble otherwise and it’s not easy to understand what happens to a and b. That’s why I think using this question as a trivia question is bad. But the discussion we’re having about it clearly shows we both know what to look out for, that we’re competent, and is way more interesting than running JS code in our heads. Even if either of us gets the answer wrong, it doesn’t really matter as much as how we got there in my opinion.
That's a tricky question, and if you're not good enough to understand why they're asking that then you fail!