Engineer bro!

Wed Mar 16 2022 (6 months ago)

Engineer by mistake!

I am a software engineer by passion

25 MCQs of JavaScript | SET - 4

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

Table of contents

Q1 - Is this a pure function?

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

Q2

const add = () => {
  const cache = {};
  return num => {
    if (num in cache) {
      return `From cache! ${cache[num]}`;
    } else {
      const result = num + 10;
      cache[num] = result;
      return `Calculated! ${result}`;
    }
  };
};

const addFunction = add();
console.log(addFunction(10));
console.log(addFunction(10));
console.log(addFunction(5 * 2));

Q3

const myLifeSummedUp = ['☕', '💻', '🍷', '🍫'];

for (let item in myLifeSummedUp) {
  console.log(item);
}

for (let item of myLifeSummedUp) {
  console.log(item);
}

Q4

const list = [1 + 2, 1 * 2, 1 / 2];
console.log(list);

Q5

function sayHi(name) {
  return `Hi there, ${name}`;
}

console.log(sayHi());

Q6

var status = '😎';

setTimeout(() => {
  const status = '😍';

  const data = {
    status: '🥑',
    getStatus() {
      return this.status;
    },
  };

  console.log(data.getStatus());
  console.log(data.getStatus.call(this));
}, 0);

Q7

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

let city = person.city;
city = 'Amsterdam';

console.log(person);

Q8

function checkAge(age) {
  if (age < 18) {
    const message = "Sorry, you're too young.";
  } else {
    const message = "Yay! You're old enough!";
  }

  return message;
}

console.log(checkAge(21));

Q9

fetch('https://www.website.com/api/user/1')
  .then(res => res.json())
  .then(res => console.log(res));

Q10

function getName(name) {
  const hasName = //
}

Q11

console.log('I want pizza'[0]);

Q12

function sum(num1, num2 = num1) {
  console.log(num1 + num2);
}

sum(10);

Q13

// module.js
export default () => 'Hello world';
export const name = 'Lydia';

// index.js
import * as data from './module';

console.log(data);

Q14

class Person {
  constructor(name) {
    this.name = name;
  }
}

const member = new Person('John');
console.log(typeof member);

Q15

let newList = [1, 2, 3].push(4);

console.log(newList.push(5));

Q16

function giveLydiaPizza() {
  return 'Here is pizza!';
}

const giveLydiaChocolate = () =>
  "Here's chocolate... now go hit the gym already.";

console.log(giveLydiaPizza.prototype);
console.log(giveLydiaChocolate.prototype);

Q17

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

for (const [x, y] of Object.entries(person)) {
  console.log(x, y);
}

Q18

function getItems(fruitList, ...args, favoriteFruit) {
  return [...fruitList, ...args, favoriteFruit]
}

getItems(["banana", "apple"], "pear", "orange")

Q19

function nums(a, b) {
  if (a > b) console.log('a is bigger');
  else console.log('b is bigger');
  return
  a + b;
}

console.log(nums(4, 2));
console.log(nums(1, 2));

Q20

class Person {
  constructor() {
    this.name = 'Lydia';
  }
}

Person = class AnotherPerson {
  constructor() {
    this.name = 'Sarah';
  }
};

const member = new Person();
console.log(member.name);

Q21

const info = {
  [Symbol('a')]: 'b',
};

console.log(info);
console.log(Object.keys(info));

Q22

const getList = ([x, ...y]) => [x, y]
const getUser = user => { name: user.name, age: user.age }

const list = [1, 2, 3, 4]
const user = { name: "Lydia", age: 21 }

console.log(getList(list))
console.log(getUser(user))

Q23

const name = 'Lydia';

console.log(name());

Q24

const output = `${[] && 'Im'}possible!
You should${'' && `n't`} see a therapist after so much JavaScript lol`;

Q25

const one = false || {} || null;
const two = null || false || '';
const three = [] || 0 || true;

console.log(one, two, three);

Thank You ❤️

Software EngineeringNodeJsJavaScript

Comments (0)

Discussions (0)


© 2021 dsabyte. All rights reserved