Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.

Unit 2 MCQs JavaScript Programming Fundamentals

INT219 — Front End Web Developer 59 questions. Pick an option to check it.

Read the notes Subjective questions

0 of 59 answered 0 right 0 wrong , 59 left

  1. Question 1 of 59 JavaScript syntax and data types Easy

    How do you write a single-line comment in JavaScript?

  2. Question 2 of 59 JavaScript syntax and data types Easy

    Which of the following is NOT a primitive data type in JavaScript?

  3. Question 3 of 59 Variables and scope Easy

    Which keyword is used to declare a variable that cannot be reassigned?

  4. Question 4 of 59 Variables and scope Easy

    What is the scope of a variable declared with the let keyword inside a code block {}?

  5. Question 5 of 59 Operators and expressions Easy

    What is the result of the expression 5 + '5' in JavaScript?

  6. Question 6 of 59 Operators and expressions Easy

    Which operator is used for strict equality (checks both value and type)?

  7. Question 7 of 59 Control flow statements Easy

    Which statement is used to execute a block of code only if a specified condition is true?

  8. Question 8 of 59 Functions and arrow functions Easy

    Which syntax is correct for declaring a basic named function in JavaScript?

  9. Question 9 of 59 Arrays and objects Easy

    How do you access the first element of an array named fruits?

  10. Question 10 of 59 Arrays and objects Easy

    Which of the following correctly defines an empty object literal?

  11. Question 11 of 59 Basic event handling Easy

    Which JavaScript event is triggered when a user clicks an HTML element?

  12. Question 12 of 59 Introduction to browser interaction Easy

    Which browser-specific function is used to display a simple message in a popup dialog box?

  13. Question 13 of 59 JavaScript syntax and data types Easy

    What will typeof true return?

  14. Question 14 of 59 Variables and scope Easy

    Before ES6 (2015), which was the only keyword available for declaring variables in JavaScript?

  15. Question 15 of 59 Operators and expressions Easy

    What does the modulus operator (%) do?

  16. Question 16 of 59 Control flow statements Easy

    Which keyword is used to stop the execution of a loop or a switch statement?

  17. Question 17 of 59 Functions and arrow functions Easy

    What is the correct syntax for a simple arrow function that takes no arguments?

  18. Question 18 of 59 Arrays and objects Easy

    Which array method adds one or more elements to the end of an array and returns the new length?

  19. Question 19 of 59 Control flow statements Easy

    How do you correctly start a for loop that counts from 0 to 4?

  20. Question 20 of 59 Basic event handling Easy

    What is the modern, recommended way to attach an event handler to an HTML element using JavaScript?

  21. Question 21 of 59 Operators and expressions Medium

    What will be logged to the console after running the following code snippet?

    JavaScript
    let a = '5';
    let b = 3;
    
    console.log(a + b);
    console.log(a - b);

  22. Question 22 of 59 Variables and scope Medium

    What is the output of the following code, which demonstrates a classic closure and scope issue with var?

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

  23. Question 23 of 59 Functions and arrow functions Medium

    Consider the following object. What will person.greet() log to the console?

    JavaScript
    const person = {
      name: 'Alice',
      greet: () => {
        console.log('Hello, ' + this.name);
      }
    };
    
    person.greet();

  24. Question 24 of 59 Arrays and objects Medium

    What is the difference in the final state of the original array after running code A versus code B?

    JavaScript
    // Code A
    const original = [1, 2, 3, 4, 5];
    const newA = original.slice(1, 3);
    
    // Code B
    const original = [1, 2, 3, 4, 5];
    const newB = original.splice(1, 3);

  25. Question 25 of 59 Control flow statements Medium

    What will be the final value of count after this loop completes?

    JavaScript
    let count = 0;
    for (let i = 0; i < 5; i++) {
      if (i % 2 === 0) {
        continue;
      }
      count++;
    }

  26. Question 26 of 59 Operators and expressions Medium

    What will the values of resultA and resultB be?

    JavaScript
    const value = 0;
    const resultA = value || 'default';
    const resultB = value ?? 'default';

  27. Question 27 of 59 Basic event handling Medium

    Imagine a <div> containing a <button>. Both have click event listeners attached. If a user clicks the button, in what order do the event handlers typically fire by default?

    HTML
    <div id="parent">
      <button id="child">Click Me</button>
    </div>
    
    <script>
      document.getElementById('parent').addEventListener('click', () => console.log('Parent clicked'));
      document.getElementById('child').addEventListener('click', () => console.log('Child clicked'));
    </script>

  28. Question 28 of 59 Introduction to browser interaction Medium

    Which code snippet correctly creates a new <p> element with the text "Hello World" and appends it to a div with the id container?

    HTML
    <div id="container"></div>

  29. Question 29 of 59 Variables and scope Medium

    What does the following code log to the console?

    JavaScript
    let x = 10;
    function test() {
      let x = 20;
      console.log(x);
    }
    test();
    console.log(x);

  30. Question 30 of 59 JavaScript syntax and data types Medium

    What is the result of typeof null in JavaScript?

  31. Question 31 of 59 Arrays and objects Medium

    Given the array const letters = ['a', 'b', 'c', 'd'];, what will be the value of the rest variable after this destructuring assignment?

    JavaScript
    const [first, , third, ...rest] = letters;

  32. Question 32 of 59 Control flow statements Medium

    What is the output of this switch statement due to fall-through?

    JavaScript
    let level = 2;
    let access = '';
    
    switch (level) {
      case 1:
        access += 'User ';
      case 2:
        access += 'Editor ';
      case 3:
        access += 'Admin ';
        break;
      case 4:
        access += 'SuperAdmin ';
    }
    console.log(access);

  33. Question 33 of 59 Functions and arrow functions Medium

    What is logged to the console when the following function is called as calculate(5)?

    JavaScript
    const calculate = (x, y = x * 2) => {
      return x + y;
    }

  34. Question 34 of 59 Operators and expressions Medium

    What are the final values of x and y after this code runs, considering short-circuiting and the post-increment operator?

    JavaScript
    let x = 0;
    let y = x++ || 10;

  35. Question 35 of 59 Introduction to browser interaction Medium

    What is the key difference between setting element.innerHTML and element.textContent?

    JavaScript
    const div = document.getElementById('myDiv');
    const myString = '<strong>Hello</strong>';
    
    // What happens differently here?
    div.innerHTML = myString;
    div.textContent = myString;

  36. Question 36 of 59 Basic event handling Medium

    What is the primary purpose of event.preventDefault() when used inside an event handler for a form's submit event?

  37. Question 37 of 59 Arrays and objects Medium

    What is the content of obj after this code is executed?

    JavaScript
    const propName = 'status';
    const value = 'active';
    
    const obj = {
      id: 123,
      [propName]: value
    };

  38. Question 38 of 59 Variables and scope Medium

    What will be logged to the console?

    JavaScript
    console.log(message);
    var message = 'Hello, Hoisting!';

  39. Question 39 of 59 JavaScript syntax and data types Medium

    Which of the following comparisons will result in true?

  40. Question 40 of 59 Functions and arrow functions Medium

    What is the output of the following Immediately Invoked Function Expression (IIFE)?

    JavaScript
    const result = (function(a, b) {
      let sum = a + b;
      return sum;
    })(5, 10);
    
    console.log(result);

  41. Question 41 of 59 Variables and scope Hard

    Consider the following JavaScript code snippet. What will be logged to the console?

    JavaScript
    for (var i = 0; i < 3; i++) {
      setTimeout(() => console.log(i), 10);
    }
    
    for (let j = 0; j < 3; j++) {
      setTimeout(() => console.log(j), 10);
    }

  42. Question 42 of 59 Operators and expressions Hard

    What is the result of the following expression in a modern JavaScript engine?

    JavaScript
    let x = 0;
    let y = null;
    
    const result = y || x ?? 'default';

  43. Question 43 of 59 Arrays and objects Hard

    Given the following code, what is the output of console.log(config.settings.retries) after the attemptChange function is called?

    JavaScript
    'use strict';
    
    const config = {
      id: 1,
      settings: {
        retries: 3,
        timeout: 5000
      }
    };
    
    Object.freeze(config);
    
    function attemptChange(obj) {
      try {
        obj.id = 2; // This will fail silently in non-strict mode, but throw in strict mode
        obj.settings.retries = 5; // This modification is on a nested object
      } catch (e) {
        console.log('Error caught');
      }
    }
    
    attemptChange(config);
    console.log(config.settings.retries);

  44. Question 44 of 59 Functions and arrow functions Hard

    What is logged to the console when user.greet() is executed?

    JavaScript
    function User(name) {
      this.name = name;
      this.greet = () => {
        const nestedGreet = () => {
          console.log(this.name);
        };
        nestedGreet();
      };
    }
    
    const user = new User('Alice');
    user.greet();

  45. Question 45 of 59 Control flow statements Hard

    What will be the output of the following code?

    JavaScript
    let result = '';
    outer_loop:
    for (let i = 0; i < 3; i++) {
      result += `o${i}`;
      for (let j = 0; j < 3; j++) {
        if (i === 1 && j === 1) {
          continue outer_loop;
        }
        result += `i${j}`;
      }
    }
    console.log(result);

  46. Question 46 of 59 JavaScript syntax and data types Hard

    What are the values of res1 and res2 after this code executes?

    JavaScript
    const res1 = {} + [];
    const res2 = [] + {};
    
    console.log(`res1: ${res1}, res2: ${res2}`);

  47. Question 47 of 59 Basic event handling Hard

    Consider the following HTML and JavaScript. If a user clicks on the p element, in what order will the messages be logged to the console?

    HTML
    <div id="div1">
      <div id="div2">
        <p id="p_element">Click me</p>
      </div>
    </div>
    
    javascript
    const div1 = document.getElementById('div1');
    const div2 = document.getElementById('div2');
    const p = document.getElementById('p_element');
    
    div1.addEventListener('click', () => console.log('div1 capture'), true);
    div1.addEventListener('click', () => console.log('div1 bubble'));
    
    div2.addEventListener('click', () => console.log('div2 bubble'));
    
    p.addEventListener('click', () => console.log('p target'));

  48. Question 48 of 59 Variables and scope Hard

    What is the output of this code?

    JavaScript
    function checkTDZ() {
      console.log(a);
      let a = 10;
    }
    
    try {
      checkTDZ();
    } catch (e) {
      console.log(e.name);
    }

  49. Question 49 of 59 Arrays and objects Hard

    What does the following code log to the console?

    JavaScript
    const arr = [1, 2, 3];
    
    arr.forEach((num, index) => {
      console.log(num);
      if (index === 0) {
        arr.push(4);
        arr.splice(1, 1);
      }
    });

  50. Question 50 of 59 Functions and arrow functions Hard

    What is the result of the following code snippet?

    JavaScript
    const myObject = {
      data: 'some data',
      getData: function() {
        const getArrow = () => arguments[0];
        return getArrow('arg1', 'arg2');
      }
    };
    
    console.log(myObject.getData('outer_arg1'));

  51. Question 51 of 59 Introduction to browser interaction Hard

    Suppose you have the following code. In what order will the messages be logged to the console?

    JavaScript
    console.log('A: Script Start');
    
    setTimeout(() => {
      console.log('B: setTimeout');
    }, 0);
    
    Promise.resolve().then(() => {
      console.log('C: Promise.then 1');
    }).then(() => {
      console.log('D: Promise.then 2');
    });
    
    console.log('E: Script End');

  52. Question 52 of 59 Operators and expressions Hard

    What is the value of result in the following code?

    JavaScript
    let a = 1;
    const arr = [2, 3, 4];
    const result = arr[a] + (a += 1) + arr[a++];

  53. Question 53 of 59 Arrays and objects Hard

    What is the output of this code?

    JavaScript
    const obj = Object.create(null);
    obj.prop = 'exists';
    
    let hasOwn = false;
    try {
      hasOwn = obj.hasOwnProperty('prop');
    } catch(e) {
      hasOwn = e.name;
    }
    
    const hasIn = 'prop' in obj;
    
    console.log(`${hasOwn}, ${hasIn}`);

  54. Question 54 of 59 JavaScript syntax and data types Hard

    What is the result of the expression 0.1 + 0.2 === 0.3 and why?

  55. Question 55 of 59 Control flow statements Hard

    What will be logged to the console?

    JavaScript
    const obj = { key: 'value' };
    
    switch (obj) {
      case { key: 'value' }:
        console.log('Case 1');
        break;
      case obj:
        console.log('Case 2');
        break;
      default:
        console.log('Default');
    }

  56. Question 56 of 59 Basic event handling Hard

    You have an async function as an event listener for a checkbox. What is a potential issue with the event.preventDefault() call in this code?

    JavaScript
    checkbox.addEventListener('click', async (event) => {
      const userIsSure = await confirmAction(); // An async function that shows a dialog
      if (!userIsSure) {
        event.preventDefault();
      }
    });

  57. Question 57 of 59 Functions and arrow functions Hard

    What is the result of attempting to instantiate an arrow function with the new keyword?

    JavaScript
    const ArrowConstructor = () => {
      this.value = 42;
    };
    
    let instance;
    try {
      instance = new ArrowConstructor();
    } catch (e) {
      instance = e.name;
    }
    
    console.log(instance);

  58. Question 58 of 59 Arrays and objects Hard

    What will be logged by console.log(result)?

    JavaScript
    const data = {
      id: 1,
      user: 'admin',
      details: {
        lastLogin: new Date(),
        roles: ['read', 'write']
      },
      getSummary: function() { return `${this.user}`; },
      temp: undefined
    };
    
    const result = JSON.parse(JSON.stringify(data));
    
    console.log(typeof result.details.lastLogin);

  59. Question 59 of 59 Operators and expressions Hard

    What is the output of the following code snippet?

    JavaScript
    let x = '10';
    let y = 10;
    
    console.log(x == y);
    console.log(x === y);
    console.log(y == [10]);
    console.log('1,0' == [1,0]);

That's unit 2

Answer a few questions and your score shows up here.