Table of contents
Introduction
Namaste, In this blog I will discuss 8 code scenarios on Javascript .
Identify the output of the below program
class MC { static mcField = "Base"; }; class CC extends MC { }; console.log(CC.mcField);
a.error
b.undefined
c.null
d.Base
e.Child
Answer:d
Reason:A public static field is inherited.
Identify the output of the below program
class MC { static mcField = "Base"; static show() { console.log(MC.mcField); } }; class CC extends MC { }; CC.show();
a.Child
b.Base
c.undefined
d.null
e.error
Answer:b
Reason:A public static method is inherited by child class.
Identify the output of the below program
class MC { static mcField = "Base"; }; class CC extends MC { static show() { console.log(CC.mcField); } }; CC.show();
a.Base
b.Child
c.undefined
d.null
e.error
Answer:a
Reason:The public static field is inherited.
Identify the output of the below program
class MyClass { #privateField; constructor() { this.#privateField = 42; } get privateValue() { return this.#privateField; } set #privateValue(newValue) { this.#privateField = newValue; } } let instance = new MyClass(); instance.privateValue = 100;
a.error
b.undefined
c.null
d.42
e.100
Answer:a
Reason:A private property cannot be accessed on an object.
Identify the output of the below program
let desc="Description"; class Test { static [desc]="DB"; }; console.log(Test.Description);
a.Description
b.DB
c.error
d.undefined
e.null
Answer:b
Reason:Computed fields can be static.
Identify the output of the below program
class Myclass2 { #i=10; static #j; show() { console.log(#i in this); } }; let obj = new Myclass2(); obj.show();
a.10
b.0
c.true
d.false
e.error
Answer:c
Reason:The variable i belongs to Myclass2.
Identify the output of the below program
function resolveAfter2Seconds(x) { return new Promise((resolve) => { setTimeout(() => { resolve(x); }, 1000); }); } await resolveAfter2Seconds(10).then( arg => {console.log(arg);} );
a.1000
b.10
c.error
d.undefined
e.null
Answer:b
Reason:Top level await is supported by ES13
Identify the output of the below program
class Myclass2 { #i=10; static #j; show() { console.log(#j in Myclass2); } }; let obj = new Myclass2(); obj.show();
a.true
b.false
c.undefined
d.10
e.error
Answer:a
Reason:The static variable j belongs to Myclass2.