Engineer bro!

Mon Nov 15 2021 (2 weeks ago)

Engineer by mistake!

I am a software engineer by passion

25 MCQs of JavaScript | SET - 1

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

Table of contents

Output - 1

function sayHi() {
  console.log(name);
  console.log(age);
  var name = 'Lydia';
  let age = 21;
}

sayHi();

Output - 2

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}

Output - 3

const shape = {
  radius: 10,
  diameter() {
    return this.radius * 2;
  },
  perimeter: () => 2 * Math.PI * this.radius,
};

console.log(shape.diameter());
console.log(shape.perimeter());

Output - 4

console.log(+true);
console.log(!'Lydia');

Which one is true?

const bird = {
  size: 'small',
};

const mouse = {
  name: 'Mickey',
  small: true,
};

Options

  • A: mouse.bird.size is not valid

  • B: mouse[bird.size] is not valid

  • C: mouse[bird["size"]] is not valid

  • D: All of them are valid

Output - 5

let c = { greeting: 'Hey!' };
let d;

d = c;
c.greeting = 'Hello';
console.log(d.greeting);

Output - 6

let c = { greeting: 'Hey!' };
let d;

d = c;
c.greeting = 'Hello';
console.log(d.greeting);

Output - 7

let a = 3;
let b = new Number(3);
let c = 3;

console.log(a == b);
console.log(a === b);
console.log(b === c);

Output - 8

class Chameleon {
  static colorChange(newColor) {
    this.newColor = newColor;
    return this.newColor;
  }

  constructor({ newColor = 'green' } = {}) {
    this.newColor = newColor;
  }
}

const freddie = new Chameleon({ newColor: 'purple' });
console.log(freddie.colorChange('orange'));

Output - 9

let greeting;
greetign = {}; // Typo!
console.log(greetign);

Output - 10

function bark() {
  console.log('Woof!');
}

bark.animal = 'dog';

Output - 11

Is there any error, mention the reason of your answer

function bark() {
  console.log('Woof!');
}

bark.animal = 'dog';

Output - 12

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person('Lydia', 'Hallie');
Person.getFullName = function() {
  return `${this.firstName} ${this.lastName}`;
};

console.log(member.getFullName());

Output - 13

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const lydia = new Person('Lydia', 'Hallie');
const sarah = Person('Sarah', 'Smith');

console.log(lydia);
console.log(sarah);

Output - 14

function Person(firstName, lastName) {
  console.log(this);
  this.firstName = firstName;
  this.lastName = lastName;
}

const member1 = new Person("Lydia", "Hallie");
const member2 = Person("Lydia", "Hallie");
// mention the reason

What are the three phases of event propagation?

  1. Target > Capturing > Bubbling

  2. Bubbling > Target > Capturing

  3. Target > Bubbling > Capturing

  4. Capturing > Target > Bubbling

All objects have prototypes

  1. true

  2. false

Output - 15

function sum(a, b) {
  return a + b;
}

sum(1, '2');

Output - 16

let number = 0;
console.log(number++);
console.log(++number);
console.log(number);

Output - 17

function getPersonInfo(one, two, three) {
  console.log(one);
  console.log(two);
  console.log(three);
}

const person = 'Lydia';
const age = 21;

getPersonInfo`${person} is ${age} years old`;

Output - 18

function checkAge(data) {
  if (data === { age: 18 }) {
    console.log('You are an adult!');
  } else if (data == { age: 18 }) {
    console.log('You are still an adult.');
  } else {
    console.log(`Hmm.. You don't have an age I guess`);
  }
}

checkAge({ age: 18 });

Output - 19

function getAge(...args) {
  console.log(typeof args);
}

getAge(21);

Output - 20

function getAge() {
  'use strict';
  age = 21;
  console.log(age);
}

getAge();

Output - 21

const sum = eval('10*10+5');

How long is cool_secret accessible?

sessionStorage.setItem('cool_secret', 123);
  1. Forever, the data doesn't get lost.

  2. When the user closes the tab.

  3. When the user closes the entire browser, not only the tab.

  4. When the user shuts off their computer.

Output - 22

var num = 8;
var num = 10;

console.log(num);

Output - 23

const obj = { 1: 'a', 2: 'b', 3: 'c' };
const set = new Set([1, 2, 3, 4, 5]);

obj.hasOwnProperty('1');
obj.hasOwnProperty(1);
set.has('1');
set.has(1);

Output - 24

const obj = { a: 'one', b: 'two', a: 'three' };
console.log(obj);

The JavaScript global execution context creates two things for you: the global object, and the "this" keyword.

  • true

  • false

Output - 25

for (let i = 1; i < 5; i++) {
  if (i === 3) continue;
  console.log(i);
}

Thank You ❤️

javascriptmcqset-1

Comments (0)

Discussions (0)


© 2021 dsabyte. All rights reserved