JAVASCRIPT FINAL EXAM 2026/2027 QUESTIONS AND
SOLUTIONS RATED A+
✔✔Math.random() - ✔✔Generates a random floating point number between 0 & 1. To
convert this to a number range that we want - for example a random number between 1
& 10. First we multiply the result of Math.random() by 10 (the amount of potential
numbers from 1 to 10) - this will give us numbers that range from 0 to 9.99999. Because
Math.random() won't ever return the whole number 1 and to remove the decimal places,
we use Math.ceil()to round the result of Math.random()*10 up to the nearest whole
number. See more on the Math object.
✔✔What type of language is JavaScript? - ✔✔It is a scripting language.
✔✔What is a scripting language? - ✔✔A scripting language is a lightweight
programming language.
✔✔What would be the result of the following code?
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script> - ✔✔It will write what is between the <h1> and <p> tags to the browser screen
formatted accordingly to the respective HTML tags
✔✔What happens if you call the "document.write" function after the document has fully
loaded? - ✔✔The html document will be deleted and replaced by your new content.
✔✔The alert() function is not much used in JavaScript. What is it is useful for? - ✔✔It is
often quite handy for trying out code.
✔✔What will this code do?
<p id="demo"> JavaScript can change the content of an HTML element.</p>
<script>
function myFunction() {
x=document.getElementById("demo"); // Find the element
x.innerHTML="Hello JavaScript!"; // Change the content
}
</script> - ✔✔It will change the text between the <p> tags to the content in quotes after
x.innerHTML
Hello JavaScript!
✔✔What does this piece of code tell the computer to do ?
x=document.getElementById("demo") - ✔✔Find the Element ("demo");
document.getElementById("some id")
(The "Id" could be anything else.)
, ✔✔What does this piece of code tell the computer to do?
x.innerHTML="Hello JavaScript!"; - ✔✔Change the content of the x variable to Hello
JavaScript!
✔✔parseInt: If the first character cannot be converted to a number, what is returned? -
✔✔NaN
✔✔What does parseFloat do? - ✔✔It parses its argument, and returns a floating point
number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal
point, or an exponent, it returns the value up to that point and ignores that character and
all succeeding characters. Leading and trailing spaces are allowed.
✔✔Math.random() - ✔✔This function returns a floating-point, pseudo-random number in
the range 0 to 1 — that is, from 0 (inclusive) up to but not including 1 (exclusive) —
which you can then scale to your desired range. The implementation selects the initial
seed to the random number generation algorithm; it cannot be chosen or reset by the
user.
✔✔What is the DOM? - ✔✔The DOM is a representation of a webpage that JavaScript
can use.
✔✔Object Prototype - ✔✔function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
✔✔Which index is zero based? (The first element is 0, the second is 1, and so on.) -
✔✔Array
✔✔pop() - ✔✔Removes the last element of an array, and returns that element
✔✔push() - ✔✔Adds new elements to the end of an array, and returns the new length
✔✔shift() - ✔✔Removes the first element of an array, and returns that element
✔✔splice() - ✔✔adds/removes items to/from an array, and returns the removed item(s)
✔✔unshift() - ✔✔Adds new elements to the beginning of an array, and returns the new
length
✔✔children - ✔✔children property returns a collection of an element's child elements,
as an HTMLCollection object
SOLUTIONS RATED A+
✔✔Math.random() - ✔✔Generates a random floating point number between 0 & 1. To
convert this to a number range that we want - for example a random number between 1
& 10. First we multiply the result of Math.random() by 10 (the amount of potential
numbers from 1 to 10) - this will give us numbers that range from 0 to 9.99999. Because
Math.random() won't ever return the whole number 1 and to remove the decimal places,
we use Math.ceil()to round the result of Math.random()*10 up to the nearest whole
number. See more on the Math object.
✔✔What type of language is JavaScript? - ✔✔It is a scripting language.
✔✔What is a scripting language? - ✔✔A scripting language is a lightweight
programming language.
✔✔What would be the result of the following code?
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script> - ✔✔It will write what is between the <h1> and <p> tags to the browser screen
formatted accordingly to the respective HTML tags
✔✔What happens if you call the "document.write" function after the document has fully
loaded? - ✔✔The html document will be deleted and replaced by your new content.
✔✔The alert() function is not much used in JavaScript. What is it is useful for? - ✔✔It is
often quite handy for trying out code.
✔✔What will this code do?
<p id="demo"> JavaScript can change the content of an HTML element.</p>
<script>
function myFunction() {
x=document.getElementById("demo"); // Find the element
x.innerHTML="Hello JavaScript!"; // Change the content
}
</script> - ✔✔It will change the text between the <p> tags to the content in quotes after
x.innerHTML
Hello JavaScript!
✔✔What does this piece of code tell the computer to do ?
x=document.getElementById("demo") - ✔✔Find the Element ("demo");
document.getElementById("some id")
(The "Id" could be anything else.)
, ✔✔What does this piece of code tell the computer to do?
x.innerHTML="Hello JavaScript!"; - ✔✔Change the content of the x variable to Hello
JavaScript!
✔✔parseInt: If the first character cannot be converted to a number, what is returned? -
✔✔NaN
✔✔What does parseFloat do? - ✔✔It parses its argument, and returns a floating point
number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal
point, or an exponent, it returns the value up to that point and ignores that character and
all succeeding characters. Leading and trailing spaces are allowed.
✔✔Math.random() - ✔✔This function returns a floating-point, pseudo-random number in
the range 0 to 1 — that is, from 0 (inclusive) up to but not including 1 (exclusive) —
which you can then scale to your desired range. The implementation selects the initial
seed to the random number generation algorithm; it cannot be chosen or reset by the
user.
✔✔What is the DOM? - ✔✔The DOM is a representation of a webpage that JavaScript
can use.
✔✔Object Prototype - ✔✔function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
✔✔Which index is zero based? (The first element is 0, the second is 1, and so on.) -
✔✔Array
✔✔pop() - ✔✔Removes the last element of an array, and returns that element
✔✔push() - ✔✔Adds new elements to the end of an array, and returns the new length
✔✔shift() - ✔✔Removes the first element of an array, and returns that element
✔✔splice() - ✔✔adds/removes items to/from an array, and returns the removed item(s)
✔✔unshift() - ✔✔Adds new elements to the beginning of an array, and returns the new
length
✔✔children - ✔✔children property returns a collection of an element's child elements,
as an HTMLCollection object