Table of contents
Introduction
Namaste, In this blog I will discuss 10 code scenarios on Javascript .
Identify the output of the below program
let arr=[1,2,3,4,5]; console.log(arr.at(-1));
a.4
b.1
c.2
d.5
e.error
Answer:d
Reason:The value at index -1 is 5.
Identify the output of the below program
let emp = { no:100, name:"ram", salary:1000 }; console.log(Object.hasOwn(emp,'no'));
a.true
b.false
c.error
d.100
e.ram
Answer:a
Reason:The 'no' property belongs to emp object.
Identify the output of the below program
function connectToDatabase(usn,pwd) { let connectionError = new Error("Invalid USN/PWD"); let str = 'usn:' + usn +' pwd:' + pwd; connectionError.cause = new Error(str); throw connectionError; } function getDataFromDatabase() { try { connectToDatabase("ram","javascript"); }catch (error) { if (error.cause) { console.log(error.cause); } } } getDataFromDatabase();
a.no output
b.Invalid USN/PWD
c.db Connect
d.Error: usn:ram pwd:javascript
e.error
Answer:d
Reason:By computation the result is Error: usn:ram pwd:javascript.
Identify the output of the below program
class Emp { eno=100; ename="ram"; constructor() { } }; let obj = new Emp(); console.log(obj.eno,obj.ename);
a.100 ram
b.undefined undefined
c.ram
d.error
e.object object
Answer: a
Reason:The class variables can be initialized and accessed.
Identify the output of the below program
class Emp { #eno=100; ename="ram"; #constructor() { } }; let obj = new Emp(); console.log(obj.ename);
a.error
b.100
c.ram
d.undefined
e.null
Answer:a
Reason:Constructor cannot be private.
Identify the output of the below program
let arr=[1,2,3,4,5]; console.log(arr.at(3));
a.1
b.2
c.3
d.4
e.5
Answer:d
Reason: The value at index 3 is 4.
Identify the output of the below program
class Emp { #eno=100; ename="ram"; constructor() { } }; let obj = new Emp(); console.log(obj.eno);
a.error
b.100
c.undefined
d.null
e.ram
Answer:c
Reason:The private members cannot be accessed on object.
Identify the output of the below program
let emp = { no:100, name:"ram", salary:1000 }; console.log(Object.hasOwn(emp,'dept'));
a.error
b.ram
c.1000
d.false
e.true
Answer:d
Reason:The property 'dept' does not exists on emp object.
Identify the output of the below program
class Emp { #eno=100; ename="ram"; constructor() { } #fx() { console.log(this.#eno); } }; let obj = new Emp(); obj.fx();
a.100
b.undefined
c.null
d.error
e.ram
Answer:d
Reason: Private function when accessed on object yields into error.
Identify the output of the below program
let arr=[1,2,3,4,5]; console.log(arr.at(-5));
a.5
b.4
c.3
d.2
e.1
Answer:e
Reason:The value at index -5 is 1 .