Engineer bro!

Wed Nov 17 2021 (1 week ago)

Engineer by mistake!

I am a software engineer by passion

25 MCQs of JavaScript | SET - 2

In this article, I have listed 25 javascript MCQs for you to practice. Comment your answer with proper justification.

Table of contents

Q1

String.prototype.giveLydiaPizza = () => {
  return 'Just give Lydia pizza already!';
};

const name = 'Lydia';

console.log(name.giveLydiaPizza())

Q2

const a = {};
const b = { key: 'b' };
const c = { key: 'c' };

a[b] = 123;
a[c] = 456;

console.log(a[b]);

Q3

const foo = () => console.log('First');
const bar = () => setTimeout(() => console.log('Second'));
const baz = () => console.log('Third');

bar();
foo();
baz();

Q4 - What is the event.target when clicking the button?

<div onclick="console.log('first div')">
  <div onclick="console.log('second div')">
    <button onclick="console.log('button')">
      Click!
    </button>
  </div>
</div>

Q5 - When you click the paragraph, what's the logged output?

<div onclick="console.log('div')">
  <p onclick="console.log('p')">
    Click here!
  </p>
</div>

Q6

const person = { name: 'Lydia' };

function sayHi(age) {
  return `${this.name} is ${age}`;
}

console.log(sayHi.call(person, 21));
console.log(sayHi.bind(person, 21));

Q7

function sayHi() {
  return (() => 0)();
}

console.log(typeof sayHi());

Q8 - Which of these values are falsy?

0;
new Number(0);
('');
(' ');
new Boolean(false);
undefined;

Q9

console.log(typeof typeof 1);

Q10

const numbers = [1, 2, 3];
numbers[10] = 11;
console.log(numbers);

Q11

(() => {
  let x, y;
  try {
    throw new Error();
  } catch (x) {
    (x = 1), (y = 2);
    console.log(x);
  }
  console.log(x);
  console.log(y);
})();

Q12 - Everything in JavaScript is either a...

  • A: primitive or object

  • B: function or object

  • C: trick question! only objects

  • D: number or object

Q13

let and = [[0, 1], [2, 3]].reduce(
  (acc, cur) => {
    return acc.concat(cur);
  },
  [1, 2],
);

console.log(ans)

Q14

!!null;
!!'';
!!1;

Q15 - What does the setInterval method return in the browser?

setInterval(() => console.log('Hi'), 1000);

Q16

console.log([...'Lydia']);

Q17

function* generator(i) {
  yield i;
  yield i * 2;
}

const gen = generator(10);

console.log(gen.next().value);
console.log(gen.next().value);

Q18

const firstPromise = new Promise((res, rej) => {
  setTimeout(res, 500, 'one');
});

const secondPromise = new Promise((res, rej) => {
  setTimeout(res, 100, 'two');
});

const ans = Promise.race([firstPromise, secondPromise]).then(res => console.log(res));

console.log(ans)

Q19

let person = { name: 'Lydia' };
const members = [person];
person = null;

console.log(members);

Q20

const person = {
  name: 'Lydia',
  age: 21,
};

for (const item in person) {
  console.log(item);
}

Q21

console.log(3 + 4 + '5');

Q22

const num = parseInt('7*6', 10);
console.log(num)

Q23

let ans = [1, 2, 3].map(num => {
  if (typeof num === 'number') return;
  return num * 2;
});

console.log(ans)

Q24

function getInfo(member, year) {
  member.name = 'Lydia';
  year = '1998';
}

const person = { name: 'Sarah' };
const birthYear = '1997';

getInfo(person, birthYear);

console.log(person, birthYear);

Q25

function greeting() {
  throw 'Hello world!';
}

function sayHi() {
  try {
    const data = greeting();
    console.log('It worked!', data);
  } catch (e) {
    console.log('Oh no an error:', e);
  }
}

sayHi();

Thank You ❤️

25-mcq-jsset-2

Comments (0)

Discussions (0)


© 2021 dsabyte. All rights reserved