Skip to content

Added questions based on function modification, object shallow copies and event loop #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,97 @@ console.log(Object.keys(arr));

**[:top: Scroll to Top](#javascript-output-based-interview-questions)**

**41. What will be the output**
```js
function modify(obj) {
obj.name = "Updated";
}

let person = { name: "Original" };
modify(person);
console.log(person.name);

function reassign(obj) {
obj = { name: "New Object" };
}

reassign(person);
console.log(person.name);
```
<details>
<summary><b>View Answer</b></summary>
<ul>
<li><b>Output</b> : Output of the first console log will be "Updated". Output of the second console log will also be "Updated".</li>
<li><b>Reason</b> : JS does not allow true pass by reference. It uses call by value for primitives (numbers, strings, booleans, null, undefined and symbols) and call by sharing for objects and arrays. <br> If you modify an object's properties inside a function, the changes will reflect outside the function but if you reassign the object completely inside the function, the original reference will remain unchanged outside.</li>
</ul>
</details>

**[:top: Scroll to Top](#javascript-output-based-interview-questions)**


**42. What will be the output**
```js
let a={ x:1, y: {alpha:10,beta:20} };
let b = {...a};
b.x=101;
b.y.alpha=1001;
console.log(a.x);
console.log(a.y.alpha);
```
<details>
<summary><b>View Answer</b></summary>
<ul>
<li><b>Output</b> : First console log will output "1". Second console log will output "1001".</li>
<li><b>Reason</b> : The spread operator provides a shallow copy operation and any objects that are deeply nested in the variable a will still be shared between a as well as b. To make an actual deep copy, use the method structuredClone(a) </li>
</ul>
</details>

**[:top: Scroll to Top](#javascript-output-based-interview-questions)**


**43. What will be the output**
```js
console.log('Start');

setTimeout(() => {
console.log('setTimeout');
}, 0);

Promise.resolve().then(() => {
console.log('Promise');
});

console.log('End');
```

**44. What will be the output**
```js
console.log('Start');

setTimeout(() => {
console.log('setTimeout');
}, 0);

Promise.resolve().then(() => {
console.log('Promise');
});

console.log('End');
```
<details>
<summary><b>View Answer</b></summary>
<ul>
<li><b>Output</b> : The console will output the following order:
<ul>
<li>Start</li>
<li>End</li>
<li>Promise</li>
<li>setTimeout</li>
</ul>
</li>
<li><b>Reason</b> : The execution order is Synchronous code first, then Microtasks run and the Microtask queue is emptied, then the Macrotasks run in the Task queue/ Callback queue. All the callbacks in the .then(), .catch() and .finally() get into the microtask queue and the other asynchronous operations, go into the WebAPIs first and when they are completed, the callbacks in them go to task queue.
</li>
</ul>
</details>

**[:top: Scroll to Top](#javascript-output-based-interview-questions)**