MCSD MCSD Programming in HTML5 3 — Questions and Answers
Question 1: Which JavaScript method removes and returns the last element of an array?
- shift()
- splice()
- pop() (Correct answer)
- slice()
Correct answer: pop()
`Array.prototype.pop()` removes the last element from an array and returns that element, mutating the original array.
Question 2: What is the purpose of the HTML5 `data-*` attribute?
- To define server-side data bindings
- To store custom private data on HTML elements accessible via JavaScript (Correct answer)
- To specify MIME type of embedded data
- To mark elements for CSS styling only
Correct answer: To store custom private data on HTML elements accessible via JavaScript
Custom `data-*` attributes allow developers to embed extra information on HTML elements without non-standard attributes, readable via `dataset` in JavaScript.
Question 3: Which CSS3 media query feature would you use to apply styles only when the viewport is narrower than 768px?
- @media screen and (max-width: 768px) (Correct answer)
- @media print and (width: 768px)
- @media screen and (min-width: 768px)
- @media all and (viewport: 768px)
Correct answer: @media screen and (max-width: 768px)
`@media screen and (max-width: 768px)` applies styles when the screen width is 768px or less, targeting mobile viewports.
Question 4: Which HTML5 API provides access to the device's geolocation?
- navigator.location
- navigator.geolocation (Correct answer)
- window.gps
- document.location
Correct answer: navigator.geolocation
The Geolocation API is exposed through `navigator.geolocation`, providing methods like `getCurrentPosition()` to retrieve coordinates.
Question 5: What does the `addEventListener` third parameter `true` signify?
- The listener fires only once
- The event is handled during the capture phase (Correct answer)
- The listener is asynchronous
- The default action is prevented
Correct answer: The event is handled during the capture phase
Passing `true` as the third argument to `addEventListener` registers the handler for the capture (top-down) phase rather than the default bubble phase.
Question 6: Which HTML5 form attribute ensures a field must be filled out before the form can be submitted?
- validate
- mandatory
- required (Correct answer)
- nonempty
Correct answer: required
The `required` attribute on an input element prevents form submission and shows a validation message if the field is left empty.
Question 7: In CSS Flexbox, which property on the flex container controls the direction of flex items?
- flex-wrap
- align-items
- flex-direction (Correct answer)
- justify-content
Correct answer: flex-direction
`flex-direction` sets the main axis direction (row or column) along which flex items are placed in the container.
Which JavaScript method removes and returns the last element of an array?