WorksheetsJavaScript Day-3
Total questions: 25
Worksheet time: 18mins
An array is a collection of __________.
single value
multiple values
only numbers
only strings
What will console.log(fruits[0]) output if let fruits = ["apple", "banana"];?
(a)
function createCounter() {
let count = 1;
return {
increment: (val) => {
count*val
console.log(count*val)
},
decrement: (val) => {
let final = val/2 == 2 ? val + 2 : val + 4 ;
console.log(final)
},
};
}
const counter = createCounter();
counter.increment(10);
counter.increment();
counter.decrement(4);
counter.decrement(30);
(a)
How do you add a new property gender to object person?
How do you access the name of the first object in array users?
Which method finds the first object matching a condition?
find()
filter()
map()
forEach()
let person = { name: "John", age: 25 };
let entries = Object.entries(person);
let newObj = Object.fromEntries(entries.reverse());
console.log(Object.keys(newObj)[0]);
Waht will be the Output ?
(a)
In JavaScript objects, keys are typically (a) and values can be any data type.
Lexical scope means __________.
Scope determined at runtime
Scope determined where function is defined
Scope determined where function is called
No scope
In lexical scope, inner functions can access __________ variables.
Only local
Only global
Outer and global
None
What is a closure in JavaScript?
A function inside a loop
A function that remembers outer variables
A global variable
A global variable
What is the output?
function outer() { let x = 5; return () => x + 2; }
console.log(outer()());
5
2
7
undifined
Closures are useful for data (a) .
What is output?
for(var i=0;i<3;i++){
setTimeout(()=>console.log(i),1000);
}
0 1 2
3 3 3
0 0 0
Error
Closures unintentionally modifying outer variables is a problem when using __________.
Global variables
Block-scoped variables
Function parameters
Constants
Each closure returned by a function has its own (a) variables.
How do you add a method to a constructor’s prototype?
Person.method = function(){}
Person.prototype.method = function(){}
Person.__proto__ = function(){}
Person.__proto__ = function(){}
let matrix = [
[19, 24, 36],[43, 25, 12],[18, 36, 19]
];
matrix[2][1] = 10,
matrix[1][0] = 32,
console.log(matrix[2][0])
console.log(matrix[1][2])
console.log(matrix[1][0])
console.log(matrix[2][2])
What will be the output ?
(a)
All functions automatically have a (a) property.
ES6 Classes are __________ in JavaScript.
Functions
Syntactic sugar over prototypes
Objects
Arrays
What is the output?
class Test {
method() { console.log("Hi"); }
}
let t = new Test();
t.method();
Hi
Error
undifined
null
What will happen if super() is not called in subclass constructor?
Works normally
Throws ReferenceError
Ignored
Undefined behavior
let students = [
{ name: "Suresh", grades: [85, 90, 78 , 90 , 75] },
{ name: "Ramesh", grades: [92, 88, 85 , 60 , 86] }
];
for(i=0;i<students.length ; i++){
for(j=0; j<students[i].grades.length; j++){
grade =students[i].grades[j]
if(grade > 90){
students[i].grades[j] = "Grade A1"
}else if(grade <=90 && grade >85 ){
students[i].grades[j] = "Grade A2"
}else if(grade <=85 && grade >75 ){
students[i].grades[j] = "Grade B1"
}
else if(grade <=75 && grade >60 ){
students[i].grades[j] = "Grade B2"
}else{
students[i].grades[j] = "Grade c"
}
}
} How many Grade B1s did Suresh get, and how many Grade B2s did Ramesh get?
(a)
Prototype inheritance is implemented using __________.
Classes
Objects
Functions
Interfaces
Object.create() creates an object with a specified (a) .
