JavaScript quiz: browser-side

This quiz of 19 questions is from Evgenii Bazhanov's project. Special thanks to him and all of the contributors!

I selected the questions related to browser-side (client-side) JavaScript.

If you like this quiz, you should consider getting on my mailing list.

Front 01 (Q16)

Which property references the DOM object that dispatched an event?

  1. self
  2. object
  3. target
  4. source

Solution

  1. target

Relevant resource(s):

Front 02 (Q36)

Which statement is true about the async attribute for the HTML script tag?

  1. It can be used for both internal and external JavaScript code.
  2. It can be used only for internal JavaScript code.
  3. It can be used only for internal or external JavaScript code that exports a promise.
  4. It can be used only for external JavaScript code.

Solution

  1. It can be used only for external JavaScript code.

Relevant resource(s):

Front 03 (Q48)

What's one difference between the async and defer attributes of the HTML script tag?

  1. The defer attribute can work synchronously.
  2. The defer attribute works only with generators.
  3. The defer attribute works only with promises.
  4. The defer attribute will asynchronously load the scripts in order.

Solution

  1. The defer attribute will asynchronously load the scripts in order.

Relevant resource(s):

Front 04 (Q50)

<p class="pull">lorem ipsum</p>

Which statement references the DOM node created by the previous code?

  1. document.querySelector('class.pull')
  2. document.querySelector('.pull');
  3. document.querySelector('pull')
  4. document.querySelector('#pull')

Solution

  1. document.querySelector('.pull');

Relevant resource(s):

Front 05 (Q61)

Which method cancels event default behavior?

  1. cancel()
  2. stop()
  3. preventDefault()
  4. prevent()

Solution

  1. preventDefault().

Relevant resource(s):

Front 06 (Q62)

Which method do you use to attach one DOM node to another?

  1. attachNode()
  2. getNode()
  3. querySelector()
  4. appendChild()

Solution

  1. appendChild()

Relevant resource(s):

Front 07 (Q70)

button.addEventListener(
  'click',
  function (e) {
    button.className = 'clicked';
  },
  false,
);

You've written the event listener shown above for a form button, but each time you click the button, the page reloads. Which statement would stop this from happening?

  1. e.blockReload();
  2. button.preventDefault();
  3. button.blockReload();
  4. e.preventDefault();

Solution

  1. e.preventDefault();

Relevant resource(s):

Front 08 (Q72)

Which statement selects all img elements in the DOM tree?

  1. Document.querySelector('img')
  2. Document.querySelectorAll('<img>')
  3. Document.querySelectorAll('img')
  4. Document.querySelector('<img>')

Solution

  1. document.querySelectorAll('img')

Relevant resource(s):

Front 09 (Q75)

Which event is fired on a text field within a form when a user tabs to it, or clicks or touches it?

  1. focus
  2. blur
  3. hover
  4. enter

Solution

  1. focus

Relevant resource(s):

Front 10 (Q87)

Which tag pair is used in HTML to embed JavaScript?

  1. <script></script>
  2. <js></js>
  3. <javascript></javascript>
  4. <code></code>

Solution

  1. <script></script>

Relevant resource(s):

Front 11 (Q88)

If your app receives data from a third-party API, which HTTP response header must the server specify to allow exceptions to the same-origin policy?

  1. Security-Mode
  2. Access-Control-Allow-Origin
  3. Different-Origin
  4. Same-Origin

Solution

  1. Access-Control-Allow-Origin

Relevant resource(s):

Front 12 (Q108)

//HTML Markup
<div id="A">
  <div id="B">
    <div id="C">Click Here</div>
  </div>
</div>
//JavaScript
document.querySelectorAll('div').forEach((e) => {
  e.onclick = (e) => console.log(e.currentTarget.id);
});

What is the output that is printed when the div containing the text "Click Here" is clicked?

  1. C B A
  2. A
  3. C
  4. A B C

Solution

  1. C B A

Relevant resource(s):

Front 13 (Q113)

<h1 class="content">MisterDa 1</h1>
<div class="content">
  <span class="content">MisterDa 2</span>
</div>

Which statement can be used to select the element from the DOM containing the text "MisterDa 2" from the previous markup?

  1. document.querySelector("div.content")
  2. document.querySelector("span.content")
  3. document.querySelector(".content")
  4. document.querySelector("div.span")

Solution

  1. document.querySelector("span.content")

Front 14 (Q133)

How would you add a data item named animal with a value of sloth to local storage for the current domain?

  1. LocalStorage.setItem('animal', 'sloth');
  2. document.localStorage.setItem('animal', 'sloth');
  3. localStorage.setItem({ animal: 'sloth' });
  4. localStorage.setItem('animal','sloth');

Solution

  1. localStorage.setItem('animal', 'sloth');

Relevant resource(s):

Front 15 (Q138)

Which document method is not used to get a reference to a DOM node?

  1. document.getNode();
  2. document.getElementsByClassName();
  3. document.querySelectorAll();
  4. document.querySelector();

Solution

  1. document.getNode();

Front 16 (Q146)

<script type="text/javascript">a = 5 + "9"; console.log(a);</script>

What will be the output of the previous code snippet?

  1. Compilation Error.
  2. 14.
  3. Runtime Error.
  4. 59

Solution

  1. 59

Relevant resource(s):

Front 17 (Q147)

Which of the following methods can be used to display data in some form using Javascript?

  1. document.write()
  2. console.log()
  3. window.alert()
  4. All of the other solutions.

Solution

  1. All of the other solutions.

Note that using document.write() is discouraged.

Front 18 (Q149)

Which statement is applicable to the defer attribute of the HTML <script> tag?

  1. defer allows the browser to continue processing the page while the script loads in the background.
  2. defer causes the script to be loaded from the backup content delivery network (CDN).
  3. defer blocks the browser from processing HTML below the tag until the script is completely loaded.
  4. defer lazy loads the script, causing it to download only when it is called by another script on the page.

Solution

  1. defer allows the browser to continue processing the page while the script loads in the background.

Relevant resource(s):

Front 19 (Q154)

<h2 id="cleverest">girls</h2>

How would you change the color of this header to pink?

  1. document.getElementByName("cleverest").style.color = "pink";
  2. document.getElementsByTagName("h2").style.color = "pink";
  3. document.getElementByName("h2").style.color = "pink";
  4. document.getElementById("cleverest").style.color = "pink";

Solution

  1. document.getElementById("cleverest").style.color = "pink";

Relevant resource(s):