You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
714
714
715
+
**41. What will be the output**
716
+
```js
717
+
functionmodify(obj) {
718
+
obj.name="Updated";
719
+
}
720
+
721
+
let person = { name:"Original" };
722
+
modify(person);
723
+
console.log(person.name);
724
+
725
+
functionreassign(obj) {
726
+
obj = { name:"New Object" };
727
+
}
728
+
729
+
reassign(person);
730
+
console.log(person.name);
731
+
```
732
+
<details>
733
+
<summary><b>View Answer</b></summary>
734
+
<ul>
735
+
<li><b>Output</b> : Output of the first console log will be "Updated". Output of the second console log will also be "Updated".</li>
736
+
<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>
737
+
</ul>
738
+
</details>
739
+
740
+
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
741
+
742
+
743
+
**42. What will be the output**
744
+
```js
745
+
let a={ x:1, y: {alpha:10,beta:20} };
746
+
let b = {...a};
747
+
b.x=101;
748
+
b.y.alpha=1001;
749
+
console.log(a.x);
750
+
console.log(a.y.alpha);
751
+
```
752
+
<details>
753
+
<summary><b>View Answer</b></summary>
754
+
<ul>
755
+
<li><b>Output</b> : First console log will output "1". Second console log will output "1001".</li>
756
+
<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>
757
+
</ul>
758
+
</details>
759
+
760
+
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
761
+
762
+
763
+
**43. What will be the output**
764
+
```js
765
+
greet();
766
+
functiongreet() {
767
+
console.log("Hello");
768
+
}
769
+
```
770
+
<details>
771
+
<summary><b>View Answer</b></summary>
772
+
<ul>
773
+
<li><b>Output</b> : The console will output `"Hello"`.</li>
774
+
<li><b>Reason</b> : Function declarations are hoisted, meaning the `greet` function is fully available before it's called. So when `greet();` is executed, it successfully prints `"Hello"` to the console.</li>
775
+
</ul>
776
+
</details>
777
+
778
+
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
779
+
780
+
781
+
782
+
**44. What will be the output**
783
+
```js
784
+
greet();
785
+
vargreet=function() {
786
+
console.log("Hi");
787
+
}
788
+
```
789
+
<details>
790
+
<summary><b>View Answer</b></summary>
791
+
<ul>
792
+
<li><b>Output</b> : The code will throw a TypeError.</li>
793
+
<li><b>Reason</b> : While `var greet` is hoisted, it is initialized with undefined at the time of execution. Therefore, calling greet(); before the assignment results in an error becuase undefined is not a function.</li>
794
+
</ul>
795
+
</details>
796
+
797
+
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
798
+
799
+
**45. What will be the output**
800
+
```js
801
+
console.log('Start');
802
+
803
+
setTimeout(() => {
804
+
console.log('setTimeout');
805
+
}, 0);
806
+
807
+
Promise.resolve().then(() => {
808
+
console.log('Promise');
809
+
});
810
+
811
+
console.log('End');
812
+
```
813
+
814
+
**45. What will be the output**
815
+
```js
816
+
console.log('Start');
817
+
818
+
setTimeout(() => {
819
+
console.log('setTimeout');
820
+
}, 0);
821
+
822
+
Promise.resolve().then(() => {
823
+
console.log('Promise');
824
+
});
825
+
826
+
console.log('End');
827
+
```
828
+
<details>
829
+
<summary><b>View Answer</b></summary>
830
+
<ul>
831
+
<li><b>Output</b> : The console will output the following order:
832
+
<ul>
833
+
<li>Start</li>
834
+
<li>End</li>
835
+
<li>Promise</li>
836
+
<li>setTimeout</li>
837
+
</ul>
838
+
</li>
839
+
<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.
840
+
</li>
841
+
</ul>
842
+
</details>
843
+
844
+
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
0 commit comments