Table of contents
Introduction
Namaste, In this blog I will discuss 10 code scenarios on Javascript .
Identify the output of the below program
let p1 = Promise.reject("Error 1"); let p2 = Promise.reject("Error 2"); Promise.any([p1,p2]) .then((value) => { console.log(value); })
a.Error 1
b.Error 2
c.Crash
d.No output
e.Error 1 Error 2
Answer:c
Reason:The above code does not handle an error situation hence crashes.
Identify the output of the below program
let pr = new Promise((resolve, reject) => { reject("fails"); }); let pr2 = new Promise((resolve, reject) => { setTimeout(reject, 500, "Completed"); }); let pr3 = new Promise((resolve, reject) => { setTimeout(reject, 100, "Fast"); }); Promise.any([pr, pr2, pr3]).then((value) => { console.log(value); });
a.fails
b.Completed
c.Fast
d.Crash
e.No output
Answer:d
Reason:All the promises are rejected and are also not handled hence application crashes.
Identify the output of the below program
class Emp { eno; ename; constructor(eno, ename) { this.eno = eno; this.ename = ename; } show() { console.log(this.eno, this.ename); } #update(eno, ename) { this.eno = eno; this.ename = ename; } }; let obj = new Emp(1, "Ram"); obj.update(2,"Gopi");
a.error
b.1 Ram
c.2 Gopi
d.no output
e.undefined
Answer:a
Reason:The private method cannot be accessed on an object.
Identify the output of the below program
let abc="ram" let ref = new WeakRef(abc); let wkref = ref.deref(); console.log(wkref);
a.ram
b.undefined
c.error
d.Object
e.null
Answer:c
Reason:Cannot create a weak reference for string.
Identify the output of the below program
let p1 = Promise.reject("Error 1"); let p2 = Promise.reject("Error 2"); let p3 = Promise.resolve("Success"); Promise.any([p1, p2, p3]) .then((value) => { console.log(value); }) .catch((error) => { console.log(error); });
a.Error 1
b.Error 2
c.Success
d.AggregateError
e.No output
Answer:c
Reason:The first completed promise is handled by Promise.any() function.
Identify the output of the below program
let abc= () => {} let ref = new WeakRef(abc); let wkref = ref.deref(); console.log(wkref);
a.error
b.function
c.Object
d.undefined
e.()=>{}
Answer:e
Reason:Function is an object hence weak reference can be created.
Identify the output of the below program
let pr = new Promise((resolve, reject) => { reject("fails"); }); let pr2 = new Promise((resolve, reject) => { setTimeout(resolve, 500, "Completed"); }); let pr3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, "Fast"); }); Promise.any([pr, pr2, pr3]).then((value) => { console.log(value); });
a.fails
b.Completed
c.Fast
d.Error
e.No output
Answer:c
Reason:The first completed promise is handled by Promise.any() function.
Identify the output of the below program
class Emp { eno; ename; constructor(eno, ename) { this.eno = eno; this.ename = ename; } #show() { console.log(this.eno, this.ename); } update(eno, ename) { this.eno = eno; this.ename = ename; this.#show(); } }; let obj = new Emp(1, "Ram"); obj.update(2,"Gopi");
a.1 Ram
b.2 Gopi
c.error
d.undefined
e.no output
Answer:b
Reason:The update() function calls the private function show().
Identify the output of the below program
let student = { name: 'Ram', age: 20, }; let ref = new WeakRef(student); let wkref = ref.deref(); console.log(wkref.name);
a.Ram
b.Error
c.undefined
d.20
e.garbage value
Answer:a
Reason:The original object is still alive hence de-referencing works.
Identify the output of the below program
```javascript
let p1 = Promise.reject("Error 1"); let p2 = Promise.reject("Error 2");
Promise.any([p1, p2]) .then((value) => { console.log(value); }) .catch((error) => { console.log(error); }); ```
a.Error 1
b.Error 2
c.AggregateError
d.No output
e.undefined
Answer:c
Reason:When all promises are rejected the catch is called which prints AggregateError.