-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGuess-The-Output.js
More file actions
4412 lines (3484 loc) Β· 118 KB
/
Copy pathGuess-The-Output.js
File metadata and controls
4412 lines (3484 loc) Β· 118 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* π‘"JavaScript-with-JC" - Guess the Output? */
// π MCQ-1
function MCQ1() {
const person = {
name: "Jayesh",
displayName1: function () {
console.log("name1", this.name);
},
displayName2: () => {
console.log("name2", this.name);
},
};
person.displayName1();
person.displayName2();
// πA) name1 Jayesh , name2 Jayesh
// π‘B) name1 Jayesh , name2 undefined
// πC) name1 Jayesh , name2
// πD) name1 , name2 Jayesh
/*
In window browser answer is C) name1 Jayesh , name2 because arrow function inherits "this" from its outer function where "this" is window.
and window has by default property name that is used for setting or returning the name of a window and in above case window.name is empty string.
In other compilers answer is B) name1 Jayesh , name2 undefined because arrow function inherits "this" from its outer function where "this" refers to global object.
*/
}
// MCQ1();
// π MCQ-2
function MCQ2() {
let name = "Jayesh";
function printName() {
if (name === "Jayesh") {
let name = "JC";
console.log(name);
}
console.log(name);
}
printName();
// πA) Jayesh π‘B) Jayesh, JC
// πC) JC, JC πD) JC, Jayesh
/* Answer is D) JC, Jayesh because let variables are block scope, name inside if condition will shadow outer name*/
}
// MCQ2();
// π MCQ-3
function MCQ3() {
var player = "Virat";
function displayPlayer() {
if (player === "Virat") {
var player = "VK";
console.log(player);
}
console.log(player);
}
displayPlayer();
// πA) VK, Virat π‘B) VK, VK
// πC) undefined πD) VK, undefined
/*
Answer is C) undefined because var variables are functional scope, When displayPlayer fn starts executing, Execution context of
displayPlayer will be created in callstack and at the memory creation phase var variable player is initialized as undefined.
hence if condition will become false and It will print only undefined.
*/
}
// MCQ3();
// π MCQ-4
function MCQ4() {
const person = {
name: "Jayesh",
age: 24,
};
const getAge = person.age;
delete person.age;
console.log(person);
console.log(getAge);
// πA) { name: 'Jayesh', age: 24 }, null
// π‘B) { name: 'Jayesh' }, 24
// πC) { name: 'Jayesh' }, undefined
// πD) { name: 'Jayesh', age: 24 }, 24
/*
Answer is B) { name: 'Jayesh' }, 24 because delete keyword deletes property of an object and we are setting getAge as pass by value.
*/
}
// MCQ4();
// π MCQ-5
function MCQ5() {
// No Strict Mode
name = "Jayesh"; // window.name ( property of window object )
console.log(delete name);
const displayName = (function (name) {
console.log(delete name); // Local variable of function
return name;
})("JC");
console.log(displayName);
// πA) true, false, JC
// π‘B) true, true, undefined
// πC) false, false, JC
// πD) false, true, undefined
/*
Answer is A) true, false, JC because delete keyword deletes only property of an object.
delete keyword can not delete local variables ( declared with var, let, and const ) and functions.
delete keyword can delete global variables as they are property of window object.
*/
}
// MCQ5();
// π MCQ-6
function MCQ6() {
const arr = [];
for (var i = 0; i < 5; i++) {
arr[i] = function () {
return i;
};
}
console.log(arr[0]());
console.log(arr[4]());
// πA) 0, 4 π‘B) 4, 4
// πC) 5, 5 πD) TypeError
/*
Answer is C) 5, 5 because variables declared with var keyword are function-scoped or globally-scoped but not blocked scoped.
Inner function will form the closure and points to the updated value of i that is 5.
In the case of Let variable, as they are blocked scoped so inner function will hold different values of i from 0 to 4.
*/
/* π In the case of Let variable */
const arrBlock = [];
for (let i = 0; i < 5; i++) {
arrBlock[i] = function () {
return i;
};
}
console.log(arrBlock[0]()); // 0
console.log(arrBlock[4]()); // 4
}
// MCQ6();
// π MCQ-7
function MCQ7() {
let person = { name: "Jayesh" };
const personArray = [person];
person = null;
console.log(personArray);
personArray = [];
console.log(personArray);
// πA) [ { name: "Jayesh" } ], []
// π‘B) [ { name: "Jayesh" } ] , TyperError
// πC) [ null ], TypeError
// πD) [ {} ], []
/*
Answer is B) [ { name: "Jayesh" } ] , TyperError because person = null will only disconnect the person variable from value { name: "Jayesh"} which is stored in memory, personArray[0] will still point to same value { name: "Jayesh"}.
and personArray = [] at this line TyperError as const variable can't be redeclared and throws Uncaught TypeError: Assignment to constant variable.
*/
}
// MCQ7();
// π MCQ-8
function MCQ8() {
const value = { number: 10 };
const addition = (x = { ...value }) => {
console.log((x.number += 5));
};
addition();
addition();
addition(value);
addition(value);
// πA) 15, 20, 25, 30 π‘B) 15, 15, 20, 25
// πC) 15, 15, 15, 15 πD) 15, 15, 15, 20
/*
Answer is D) 15, 15, 15, 20 because when we call addition function 3rd time with passing value object as an argument, then x will take value as pass by reference and will update number property of original object ( value in this case ) to 15.
Hence, while calling addition function 4th time will console 15 + 5 => 20.
*/
}
// MCQ8();
// π MCQ-9
function MCQ9() {
function makePerson() {
return {
userName: "Jayesh",
ref: this,
};
}
const person = makePerson();
console.log(person.ref.userName);
// πA) Jayesh π‘B) ""
// πC) undefined πD) TypeError
/*
Answer is C) undefined because "this" keyword in makePerson function will refer to the window object,
person.ref.userName is same as Window.userName and no property named with userName is present in window object, Hence It will console undefined.
*/
// π We can get "Jayesh" as an output by doing small change in above question :-
function makePerson2() {
return {
userName: "Jayesh",
// π Here, We have assigned a function to ref property of an object, and function's "this" will point to the returned object.
ref: function () {
return this;
},
};
}
const person2 = makePerson2();
console.log(person2.ref().userName); // Jayesh
}
// MCQ9();
// π MCQ-10
function MCQ10() {
const user = {
userName: "Jayesh",
displayName: function () {
console.log(this.userName);
},
};
setTimeout(user.displayName, 1000);
// πA) Jayesh π‘B) undefined
// πC) "" πD) TypeError
/*
Answer is B) undefined because setTimeout is using user.displayName as a callback function rather than object method.
callback function's "this" will refer to the window object and It will console undefined as there is no property such as userName in the window object.
*/
// π We can get "Jayesh" as an output by wrapping the user.displayName() inside a function :-
setTimeout(function () {
user.displayName(); // Here, displayName is called by user object ( object method ). Hence, "this" will refer to user object.
}, 1000);
}
// MCQ10();
// π MCQ-11
function MCQ11() {
const series = { name: "JavaScript-with-JC" };
function getSatus(postNumber) {
return `${this.name} π ${postNumber}`;
}
console.log(getSatus.call(series, 50));
console.log(getSatus.bind(series, 50));
// πA) JavaScript-with-JC π 50, undefined
// π‘B) JavaScript-with-JC π 50, JavaScript-with-JC π 50
// πC) JavaScript-with-JC π 50, [Function: bound getSatus]
// πD) JavaScript-with-JC π 50, TypeError
/*
Answer is C) JavaScript-with-JC π 50, [Function: bound getSatus] because call, apply and bind methods are used for function borrowing in JavaScript.
The call method immediately invokes the borrowed function where as bind method does not invoke the borrowed function immediately, bind method returns a copy of borrowed function
that can be called later on with or without passing new arguments to it.
*/
// π We can get 'JavaScript-with-JC π 50, JavaScript-with-JC π 50' as an output by calling borrowed function of bind method :-
console.log(getSatus.call(series, 50)); // JavaScript-with-JC π 50
console.log(getSatus.bind(series, 50)()); // JavaScript-with-JC π 50
}
// MCQ11();
// π MCQ-12
function MCQ12() {
var name = "Jayesh";
function displayName() {
console.log(this.name);
}
const person = {
name: "JC",
method(fn) {
fn();
},
};
person.method(displayName);
// πA) JC π‘B) Jayesh
// πC) undefined πD) TypeError
/*
Answer is B) Jayesh because displayName function is passed to person object method as a callback function.
"this" keyword in displayName function will refer to window object and window object has a property "name" with value "Jayesh". Hence, It will console Jayesh as an output.
*/
// π We can get JC as an output by attaching call method with fn() inside person method :-
const person2 = {
name: "JC",
method(fn) {
fn.call(this); // borrowing function and passing "this" of person2 object.
},
};
person2.method(displayName); // JC
}
// MCQ12();
// π MCQ-13
function MCQ13() {
var length = 4;
function callback() {
console.log(this.length);
}
const object = {
length: 5,
method: function () {
arguments[0]();
},
};
object.method(callback, 2, 3);
// πA) 2 π‘B) 3
// πC) 4 πD) 5
/*
Answer is B) 3 because arguments keyword is an array of arguments passed to the function.
Here while calling object.method(), we are passing three arguments callback fn(), 2 and 3.
If we try to console arguments it will look like this π
Arguments(3) [Ζ, 2, 3, callee: Ζ, Symbol(Symbol.iterator): Ζ]
0: Ζ callback()
1: 2
2: 3
callee: Ζ ()
length: 3
Symbol(Symbol.iterator): Ζ values()
[[Prototype]]: Object
As we can clearly see, arguments is having length property that is equal to number of arguments passed to function.
So, arguments[0] is nothing but the first argument passed to function that is callback function in this case.
As we know, Everything in JavaScript is an object ( arguments is also an object which has length property with value 3 )
arguments[0]() function's "this" will refer to arguments object. Hence, It will console 3 as an output.
*/
}
// MCQ13();
// π MCQ-14
function MCQ14() {
var name = "Jayesh";
function displayName() {
console.log(this.name);
}
const person = {
name: "JC",
method: displayName.bind(this),
};
person.method();
// πA) Jayesh π‘B) JC
// πC) undefined πD) TypeError
/*
Answer is A) Jayesh because "this" inside the definition for person object does not refer to person object.
"this" will refer to the window object here, and binding displayName function with passing window's this
as a context will return a copy of bound function that is stored in method property of person object.
So, While calling person.method() will console Jayesh as an output.
*/
// π We can get JC as an output by wrapping displayName.bind(this) inside a function because "this" inside the normal function of an object refers to the object :-
const person2 = {
name: "JC",
method: function () {
return displayName.bind(this); // Here, "this" refers to the person2 object
},
};
person2.method()(); // JC
}
// MCQ14();
// π MCQ-15
function MCQ15() {
function show() {
console.log(this.name);
}
const person1 = { name: "Jc" };
const person2 = { name: "Jayesh" };
show = show.bind(person1).bind(person2);
show();
// πA) Jayesh π‘B) undefined
// πC) JC πD) TypeError
/*
Answer is C) JC because a function which is bound with bind keyword can not be re-bound with other new context, bind chaining does not exist.
once the function is bound to a particular object, It will always be bound to that object no matter how many times it's further bounded.
*/
}
// MCQ15();
// π MCQ-16
function MCQ16() {
let person1 = {
name: { firstName: "Jayesh" },
age: 24,
};
let person2 = { ...person1 };
person2.name.firstName = "Virat";
person2.age = 33;
console.log(person1.name.firstName);
console.log(person1.age);
// πA) Jayesh, 33 π‘B) Jayesh, 24
// πC) Virat, 33 πD) Virat, 24
/*
Answer is D) Virat, 24 because The spread operator makes deep copies of data if the data is not nested.
When we have nested data in an array or object the spread operator will create a deep copy of the top most data
and a shallow copy of the nested data.
person1 and person2 is pointing to different memory address but person1.name and person2.name is pointing to the same memory address
We Can do Deep copy of nested objects by using:-
1) const copyObj = JSON.parse(JSON.stringify(originalObj))
2) const copyObj = structuredClone(originalObj);
*/
}
// MCQ16();
// π MCQ-17
function MCQ17() {
for (var i = 0; i < 5; i++) {
setTimeout(
(i) => {
console.log(i);
},
1000,
i
);
}
// πA) 0 1 2 3 4 π‘B) 5 5 5 5 5
// πC) 4 4 4 4 4 πD) 0 1 2 3 4 5
/*
Answer is A) 0 1 2 3 4 because as we are passing i ( 0 to 4 ) value as an argument to setTimeout callback function
therefore this will console different values of i from 0 to 4.
if there was no argument passed to setTimeout callback function then the output would be 5 5 5 5 5 because variables declared
with var keyword are function-scoped or globally-scoped but not blocked scoped. Inner function i would point to the updated value of i that is 5.
*/
}
// MCQ17();
// π MCQ-18
function MCQ18() {
console.log(1);
async function fetchData() {
console.log(2);
let result = await Promise.resolve(3);
console.log(result);
}
fetchData();
console.log(4);
// πA) 1 2 3 4 π‘B) 1 4 2 3
// πC) 1 2 4 3 πD) 1 3 4 2
/*
Answer is C) 1 2 4 3 beacause promise is used to handle the asynchronous result of an operation and
callback functions attached to the promises are stored into microtask queue.
So, first synchronous code will be executed i.e 1,2,4 and once callstack is empty, event loop pushes the microtask queue's task into callstack
callstack will start executing the task and It will console 3 at last.
*/
}
// MCQ18();
// π MCQ-19
function MCQ19() {
console.log("start");
const promise = new Promise((resolve) => {
console.log(1);
resolve(2);
console.log(3);
});
promise.then((result) => {
console.log(result);
});
console.log("end");
// πA) start end 1 3 2 π‘B) start 1 3 end 2
// πC) start end 1 2 3 πD) start 1 end 2 3
/*
Answer is B) start 1 3 end 2 beacause The function we pass into the Promise constructor runs synchronously,
but anything that depends on its resolution ( resolve or reject ) will be called asynchronously.
Even if the promise resolves immediately, any handlers ( callback attached to promise then and catch ) will execute asynchronously.
const promise = new Promise((resolve) => {
console.log(1); // runs synchronously
resolve(2); // called asynchronously by then callback
console.log(3); // runs synchronously
});
*/
}
// MCQ19();
// π MCQ-20
function MCQ20() {
console.log("First");
const promise = new Promise((resolve) => {
console.log("Second");
});
promise.then((result) => {
console.log(result);
});
console.log("Third");
// πA) First Second undefined Third π‘B) First Third Second
// πC) First Second Third undefined πD) First Second Third
/*
Answer is D) First Second Third because as there is no resolve in Promise constructor, So it will not go inside of .then block.
*/
}
// MCQ20();
// π MCQ-21
function MCQ21() {
const fetchData = function () {
return new Promise((resolve, reject) => {
reject();
});
};
fetchData()
.then(() => {
console.log("Success 1");
})
.catch(() => {
console.log("Error 1");
})
.then(() => {
console.log("Success 2");
});
// πA) Error 1 TypeError π‘B) Error 1
// πC) Error 1 Success 2 πD) undefined
/*
Answer is C) Error 1 Success 2 because in promise chaining .then method below .catch method will be called if in .catch method we are not
returning rejected promise ( by default implicitly returns a promise that is handled by it's below .then method )
*/
}
// MCQ21();
// π MCQ-22
function MCQ22() {
function foo() {
let a = (b = 0);
a++;
return a;
}
foo();
console.log(typeof a);
console.log(typeof b);
// πA) undefined number π‘B) ReferenceError number
// πC) undefined undefined πD) number number
/*
Answer is A) undefined number because variable a is declared with let it is blocked scope and will be "not defined" outside function foo().
The typeof operator returns "undefined" even for βundeclaredβ (or βnot definedβ) variables.
Notice that there was no error thrown when we executed typeof a, even though a is an undeclared variable.
This is a special safety guard in the behavior of typeof.
and variable b is a just global scope variable hence it will be available outside function foo() also.
*/
}
// MCQ22();
// π MCQ-23
function MCQ23() {
console.log("start");
setTimeout(() => {
console.log("first");
}, 0);
Promise.resolve("second").then((res) => console.log(res));
console.log("end");
// πA) start end first second π‘B) start first second end
// πC) start end second first πD) start first end second
/*
Answer is C) start end second first because callback function attached to Promises added into microtask queue
whereas callback function of setTimeout added into callback ( macroTask ) queue.
microTask queue has more priority than callback ( macroTask ) queue.
*/
}
// MCQ23();
// π MCQ-24
function MCQ24() {
const person1 = {
name: "Jayesh",
age: 24,
};
const person2 = person1;
person2.name = "Virat";
console.log(person1.name);
console.log(person2.name);
// πA) Jayesh Virat π‘B) Virat Virat
// πC) Virat Jayesh πD) Jayesh Jayesh
/*
Answer is B) Virat Virat because objects are passed as a reference, person1 and person2 will hold the same memory address
and altering any property of person2 will modify property of person1 as well.
*/
}
// MCQ24();
// π MCQ-25
function MCQ25() {
let p = new Promise((resolve, reject) => {
reject(Error("Fails!"));
});
p.catch((error) => {
console.log(error.message);
}).then((result) => {
console.log(result);
});
// πA) Fails! undefined π‘B) Fails!
// πC) Fails! TypeError πD) Fails! Fails!
/*
Answer is A) Fails! undefined because promise is rejecting so .catch callback will execute and console "Fails" first.
In promise chaining .then method below .catch method will be called if in .catch method we are not
returning rejected promise ( by default implicitly it returns a promise that is handled by it's below .then method ).
as .catch method is not returning anything, result of .then method will be undefined.
The Error() constructor creates an error object. Error() can be called with or without new. Both create a new Error instance.
Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions.
Error.message in user-created Error objects is the string provided as the constructor's first argument that is "Fails!" in our case.
*/
}
// MCQ25();
// π MCQ-26
function MCQ26() {
const person = {
name: "JC",
displayName() {
console.log(this.name);
},
};
const jayesh = Object.create(person);
person.displayName();
jayesh.displayName();
// πA) JC undefined π‘B) undefined JC
// πC) undefined undefined πD) JC JC
/*
Answer is D) JC JC because Object.create() method creates a new object, using an existing object as the prototype
of the newly created object. Object.create() is used for Prototypal inheritance. jayesh.__proto__ will point to person object.
If child object does not have property then it will inherit the property from it's parent ( prototype ) object.
*/
}
// MCQ26();
// π MCQ-27
function MCQ27() {
const fetchData = function () {
return new Promise((res) => res("One"));
};
let isLoading = true;
fetchData()
.then((result) => {
console.log(result);
})
.finally(() => {
console.log("Two");
isLoading = false;
});
console.log(isLoading);
// πA) One Two true π‘B) One Two false
// πC) true One Two πD) false One Two
/*
Answer is C) true One Two because first synchronous code will be executed i.e isLoading ( true ).
once callstack is empty and promise is resolved, callback function attached to the promise .then method() will execute
and eventually .finally method will be executed.
*/
}
// MCQ27();
// π MCQ-28
function MCQ28() {
const calc = (a) => {
return (b) => {
if (b) return calc(a + b);
return a;
};
};
console.log(calc(1)(2)(3)(4)());
// πA) 3 π‘B) 10
// πC) 5 πD) 1
/*
Answer is B) 10 because of Infinite Currying.
Currying is a technique to convert multiple arguments function into a single argument functions (unary functions) in a sequence.
There are two ways to perform currying :-
1) Using Closures ( Used in above question )
2) Using Bind
*/
}
// MCQ28();
// π MCQ-29
function MCQ29() {
const person1 = {
name: "Jayesh",
};
const person2 = {
name: "Virat",
};
const person = Object.assign(person1, person2);
console.log(person.name);
console.log(person1.name);
// πA) Virat Virat π‘B) Virat Jayesh
// πC) Jayesh Virat πD) Jayesh Jayesh
/*
Answer is A) Virat Virat because Object.assign() method copies all own properties from one or more source objects to a target object.
It returns the modified target object. Merging objects with same properties overwritten by other objects that have the same properties later in the parameters order.
In the above question, person1 is target object that is merged with person2 and as both objects have same property name
that's why "Jayesh" is overwritten by "Virat". person and person1 points to the same memory address. comparing person === person1 results true.
Note:- While Cloning an object using Object.assign makes deep copies of data if the data is not nested. When we have nested data in an object then
It will create a deep copy of the top most data and a shallow copy of the nested data.
Cloning an Object :-
const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
Syntax-
Object.assign(target, ...sources)
*/
}
// MCQ29();
// π MCQ-30
function MCQ30() {
const promise = () => Promise.resolve("Success");
function first() {
promise().then((res) => console.log(res));
console.log("First");
}
async function second() {
const res = await promise();
console.log(res);
console.log("Second");
}
first();
second();
// πA) First Success Second Success
// π‘B) First Second Success Success
// πC) Second Success First Success
// πD) First Success Success Second
/*
Answer is D) First Success Success Second because :-
1) While calling first function, callback function attached to then method of promise will be stored into microtask queue
and next synchronous code i.e console.log("First") will be executed in callstack.
2) While calling second function which is async, The keyword await in async second function makes JavaScript wait until
the promise settles and returns its result. So, as there are two resolved promises callback function stored in microtask queue.
first promise .then method will console "Success", after that await promise of second promise will console "Success".
3) At last, Synchronous code after the await keyword in async function will be executed i.e console.log("Second");
*/
}
// MCQ30();
// π MCQ-31
function MCQ31() {
console.log("start");
async function getData() {
console.log("JC");
return "Jayesh";
}
getData().then((res) => console.log(res));
console.log("end");
// πA) start end JC Jayesh π‘B) start JC Jayesh end
// πC) start JC end Jayesh πD) start Jayesh JC end
/*
Answer is C) start JC end Jayesh because async function always returns a promise. If no promise is return other values are wrapped in a resolved promise automatically.
So in the above question return "Jayesh" would be same as Promise.resolve("Jayesh");
First, All the synchronous code will be executed i.e start JC end and later on callback function attached to promise that is stored in microtask queue will be executed by callstack.
Hence, The Final Result will be:- start JC end Jayesh
*/
}
// MCQ31();
// π MCQ-32
function MCQ32() {
const p1 = Promise.resolve("First");
const p2 = Promise.reject("Second");
const p3 = Promise.resolve("Third");
const runPromises = async () => {
try {
const res = await Promise.all([p1, p2, p3]);
console.log(res);
} catch (err) {
console.log(err);
}
};
runPromises();
// πA) [ First, Third ] π‘B) Second
// πC) [ First ] πD) [ Second ]
/*
Answer is B) Second because Promise.all() executes all passed promises concurrently and improves the performance of the application.
π Promise.all() Cases :-
1) If all promises resolve, returns the array of results of all promises resolved.
2) If any promise fails, returns the rejected promise error.
3) If passed empty [], returns empty [].
In the above question, promise p2 is rejected. Hence, Promise.all() will return the rejected promise error "Second".
*/
}
// MCQ32();
// π MCQ-33
function MCQ33() {
const inc = async (x) => {
x = x + (await 1);
return x;
};
const increment = async (x) => {
x = x + 1;
return x;
};
inc(1).then((x) => {
increment(x).then((x) => console.log(x));
});
// πA) 1 π‘B) 2
// πC) 3 πD) 4
/*
Answer is C) 3 because first promise return by async function "inc" will resolve and return ( 1 + 1 ) 2 as a result in .then method.
secondly, promise return by async function "increment" will resolve and return ( 2 + 1 ) 3 as a result in .then method.
Note:- await keyword in async function waits for the promise to fullfilled but if the value is not a Promise ( In above question await 1 ),
it converts the value to a resolved Promise, and waits for it. So. await 1 would be same as Promise.resolve(1).
*/
}
// MCQ33();
// π MCQ-34
function MCQ34() {
displayName();
var displayName = function () {
console.log("Jayesh");
};
function displayName() {
console.log("JC");
}
displayName();
// πA) Jayesh Jayesh π‘B) JC JC
// πC) Jayesh JC πD) JC Jayesh
/*
Answer is D) JC Jayesh because of Hoisting When javaScript engine starts executing the code, It creates the global execution
context in callstack, Each context in callstack has two phases:- 1) Memory Creation and 2) Code Execution
at the Memory Creation, for var displayName = func(){} displayName will be undefined but below in the code declared function displayName() {}
will override the displayName value to function displayName() { console.log("JC") }
displayName : displayName() {
console.log("JC");
}
at the Code Execution, first displayName() function will console "JC" and at the next line displayName will be assigned
function () { console.log("Jayesh") } as value;
displayName : displayName() {
console.log("Jayesh");
}
Hence, last displayName() function will console "Jayesh"
*/
}
// MCQ34();
// π MCQ-35
function MCQ35() {
const fetchData = function () {
return new Promise((res, reject) => {
reject("Error");
});
};
fetchData()
.then(null, (err) => {
console.log("First");
console.log(err);
})
.catch(() => {
console.log("Second");
console.log(err);
});
// πA) First Error π‘B) Second Error
// πC) Second undefined πD) ReferenceError
/*
Answer is A) First Error because then() method takes up to two arguments: callback functions for the fulfilled and rejected
cases of the Promise.
Syntax of then :- then(onFulfilled) or then(onFulfilled, onRejected).
then(
(value) => fulfillment handler