In the name of ALLAH, the most beneficient, the most merciful

MCQs (JavaScripting)

Multiple Choice Questions

  1. var x = 25;
    var y = 12;
    var z = x < y;

    What value is stored in the variable z above?

    1. true
    2. false
    3. 12
    4. 25
  2. Which statement creates an array named colors and stores 4 elements?

    1. var colors = ["red", "orange", "yellow", "green"];
    2. var colors = array["red", "orange", "yellow", "green"];
    3. var colors = new array("red", "orange", "yellow", "green");
    4. var colors = array: ["red", "orange", "yellow", "green"];
  3. Which of the following statements declares a (non-global) variable named myNum and assigns it a value of 24?

    1. myNum = 24;
    2. variable myNum set to 24;
    3. num myNum 24;
    4. var myNum = 24;
  4. Consider the variable:
    var greeting = "Hello";
    greeting += "John";

    What value is stored in the greeting variable after the 2nd line of code?

    1. "Hello John"
    2. HelloJohn
    3. "John"
    4. "HelloJohn"
  5. A complete line of JavaScript code that can be executed by the computer is called a(n) ________.

    1. block
    2. statement
    3. executable
    4. variable
  6. Which message will be displayed on the document when the following code is executed?
    var x = 3;
    var y = 8;
    if(x < 5 && y > 6) {
    document.write("This test passed");
    }
    else{
    document.write("This test failed");
    }

    1. This test passed
    2. This test failed
    3. Neither message will be displayed
    4. Both messages will be displayed
  7. Which of the following statements declares a variable named highScore?

    1. new var highScore;
    2. variable highScore;
    3. var highScore;
    4. declare var highScore;
  8. Which JavaScript statement creates a new list item element and stores it in a new variable named listItem.

    1. var listitem = document.createElement('ul');
    2. var listitem = document.createElement('li');
    3. var listitem = document.createElement('ol');
    4. var listitem = document.Element('li');
  9. Which message will be displayed on the document when the following code is executed?
    var num = 24;
    if(num > 82)
    {
    document.write("zone 1");
    }
    else if(num >= 43)
    {
    document.write("zone 2");
    }
    else{
    document.write("zone 3");
    }

    1. zone 1
    2. zone 2
    3. zone 3
    4. none of the given
  10. Consider the following code:
    var book = {
    title: "Romeo and Juliet",
    authorFirstName: "William",
    authorLastName: "Shakespeare",
    publishDate: 1597
    };

    What line of code logs the value of the title property to the console?

    1. console.log(book.authorLastName);
    2. console.log(book.authorFirstName);
    3. console.log(book.title);
    4. console.log(book.publishDate);
  11. Which of the following for-loop declarations creates a loop that will execute 4 times?

    1. for(x = 1; x < 4; x++)
    2. for(x = 0; x < 4; x++)
    3. for(x = 1; x < 3; x++)
    4. for(x = 0; x < 5; x++)
  12. How can a function return a value?

    1. return the value in the body of the function with the return keyword
    2. include a return statement after the body of the function
    3. include the return keyword in the parameter list( ) use the function return (parameter1, parameter2) syntax
    4. none of the given
  13. Which message will be displayed on the document when the following code is executed?
    var x = 3;
    var y = 8;
    if(x % 2 == 1 && y > 6)
    {
    document.write("This test passed" );
    }
    else{
    document.write("This test failed");
    }

    1. This test passed
    2. This test failed
    3. Neither message will be displayed
    4. Both messages will be displayed
  14. Which of the following statements calls a function, giveMeANumber, and passes the number 4 as a parameter?

    1. giveMeANumber();
    2. giveMeANumber().4;
    3. giveMeANumber(4);
    4. giveMeANumber(==4);
  15. Which of the following statements defines a function named increment that returns a value and defines a parameter?

    1. increment(num){
      num++;}
      return num;
    2. increment(num){
      num++;
      return num;}
    3. function increment(num){
      num++;
      return num;}
    4. func increment(num){
      param: num;
      num++;
      return (num);}
  16. What belongs immediately after the if keyword in an if statement, within the parentheses?
    if ( )

    1. An else statement
    2. A return statement
    3. A condition
    4. A number
  17. Which JavaScript statement uses the document object to get a list of all of the h1 elements on a page and stores the collection in a variable named headings.

    1. var headings = document.getElementById("h1");
    2. var headings = document.getElementsByTagName("h1");
    3. var headings = document.getElementBySelector("h1");
    4. var headings = document.getElementsByClassName("h1");
  18. Consider the following code:
    var x = 2;
    var y = 6;
    var z = -3;

    Which of the following line of code will update the stored value of the variable y to 3?

    1. y = x
    2. y = -z;
    3. y = z
    4. y == 3
  19. A JavaScript variable that originally stored a number can later store a string value.

    1. True
    2. False
  20. Which of the following statements stores a value returned from a function?

    1. var value = myFunction;
    2. var value = myFunction();
    3. myFunction -> var value;
    4. var value = return myFunction;
  21. What causes a loop to be infinite?

    1. It does not include a statement that modifies the test/condition.
    2. It does not include a closing brace
    3. It does not have a return statement
    4. It does not have a proper condition
  22. Which of the following can be used to modify the text within a paragraph?

    1. element.displayText
    2. document.write
    3. element.write
    4. element.innerHTML
  23. What is the primary role of JavaScript?

    1. To translate Java for webpages
    2. To style web content
    3. To add dynamic behavior to webpages
    4. To store and retrieve data from databases
  24. Which of these is a JavaScript array?

    1. var c = 10;
    2. var a = ['sin', 'cos', 'tan'];
    3. var b = {min:-1, max:1};
    4. All of the given
  25. var x = 4;
    var y = 9;
    var z = x < y;

    What value is stored in the variable z above?

    1. 4
    2. 9
    3. true
    4. false
  26. Which statement retrieves the element with the value 32 from the array numbers?
    var numbers = [1, 3, 12, 42, 32, 4];

    1. numbers.get(32)
    2. numbers[32]
    3. numbers[4]
    4. numbers[5]
  27. What value is stored in the variable x after the following lines of code are executed?
    var x = 3;
    x*=2;

    1. 2
    2. 3
    3. 6
    4. 9
  28. Which of the following statements changes the CSS background-color of an element to "green"?

    1. element.background-color = "green";
    2. element.style.backgroundColor = "green";
    3. element.style.background-color = "green";
    4. element.set("background-color", "green");
  29. typeof -34.7

    1. number
    2. string
    3. boolean
  30. typeof "Hello, world"

    1. number
    2. string
    3. boolean
  31. A(n) ________ can hold a collection of named values, known as properties.

    1. data type
    2. object
    3. property
    4. temp
  32. Consider the code:
    var greeting = "Hello";

    var contact = {
    firstName: "Jane",
    lastName: "Doe",
    age: 35
    };

    What line of code update the greeting variable to store the value "Hello Jane"?

    1. greeting += firstName;
    2. greeting += contact.firstName;
    3. greeting += " " + contact.firstName;
    4. greeting = greeting + " " + contact.lastName;
  33. What is the result of the following statement?
    typeof 340.2

    1. decimal
    2. number
    3. string
    4. object
  34. What value is stored in the variable x after the following lines of code are executed?
    var x = 9;
    var y = 3;
    var z = x%y;

    1. 0
    2. 1
    3. 2
    4. 3
  35. Consider the following code:
    var x = 2;
    var y = 6;
    var z = -3;

    Which of the following line of code will update the stored value of the variable y to 3?

    1. y = x
    2. y /= 2;
    3. y = z
    4. y == 3
  36. JavaScript was created by Google in 2005 for the Gmail application that needed more interactivity

    1. No, JavaScript was invented in 1995 by Netscape Corp.
    2. Yes, Google proposed an open implementation of the language in 2003 to ECMA
  37. var language = "JavaScript";
    The data type of the language variable above is:

    1. number
    2. string
    3. boolean
    4. double
  38. Which JavaScript statement creates a new list item element and stores it in a new variable named listItem?

    1. var listitem = document.getElement("li");
    2. var listitem = document.createElement.li;
    3. var listitem = document.createElement("li");
    4. var listitem.document.createElement("li");
  39. Consider the following code:
    var x = 2;
    var y = 6;
    var z = -3;

    What is the datatype of the variable **z**?

    1. number
    2. negative
    3. int
    4. whole
  40. Which message will be displayed on the document when the following code is executed?
    var num = 40;
    if(num > 60) {
    document.write("zone 1");
    }
    else if(num > 25) {
    document.write("zone 2");
    }
    else{
    document.write("zone 3");
    }

    1. zone 1
    2. zone 2
    3. zone 3
    4. none of the above
  41. In JavaScript, in order to change the content of an HTML document or the CSS style of HTML elements, we use:

    1. The selector and the DOM API
    2. The HTML API
    3. The CSS API
    4. None of the given.
  42. var mySection = document.getElementById("mySection");
    var special = mySection.getElementsByClassName("special");

    What will the variable special contain after this code is executed?

    1. A collection of all elements that have the class "special"
    2. The first element in the document with the class "special"
    3. A collection of all elements with the class "special" that are children of the element with the id "mySection"
    4. A collection of all elements that are children of the element with the id "mySection"
  43. Consider the following code:
    var x = 2;
    var y = 6;
    var z = -3;

    Which of the following line of code will update the stored value of the variable y to 3?

    1. y = x
    2. y = y/x;
    3. y = z
    4. y == 3
  44. Which of the following statements changes the href attribute of an element to "www.microsoft.com"?

    1. element.sethref("www.microsoft.com");
    2. element.attribute.href = "www.microsoft.com";
    3. element.setAttribute("href", "www.microsoft.com");
    4. element.href("www.microsoft.com");
  45. Consider the following code:
    var book = {
    title: "Romeo and Juliet",
    authorFirstName: "William",
    authorLastName: "Shakespeare",
    publishDate: 1597
    };

    The code above creates a(n) ________ with the identifier book.

    1. property
    2. object
    3. value
    4. data type
  46. Which of the following does not output information to the user via the webpage?

    1. window.alert
    2. console.log
    3. document.write
    4. element.innerHTML
  47. Which of the following lines of code produces the value "William Shakespeare"?

    1. book.authorFirstName + book.authorLastName
    2. book.authorFirstName.authorLastName
    3. book.authorFirstName + " " + book.authorLastName
    4. book{authorFirstName+authorLastName}
  48. Which of the following statement sets a function called myHandler to be called when myButton is clicked?

    1. myButton.onclick = myHandler;
    2. myButton.click = myHandler;
    3. myButton.addEventListener("onClick", myHandler);
  49. typeof "390"
    What is the result of the this statement?

    1. boolean
    2. number
    3. string
    4. object
  50. Which line of code creates a variable named language that holds that value "JavaScript"?

    1. variable language: "JavaScript";
    2. var language: JavaScript;
    3. var language = JavaScript;
    4. var language = "JavaScript";
  51. JavaScript allows you to develop webpages that are ________. This means the webpage's content changes based on conditions and interactions.

    1. static
    2. dynamic
    3. encapsulated
    4. inherited
  52. var x = 25;
    var y = 12;
    var z = x < y;

    What is the data type of the variable z above?

    1. string
    2. number
    3. boolean
    4. object
  53. var x = 34;
    var y = 10;
    var z = x%y;

    What value is stored in the variable z after the following lines of code are executed?

    1. 2
    2. 4
    3. 5
    4. 3
  54. A ________ is a container that can store a value in your program that can be accessed or modified at a later point.

    1. temp
    2. block
    3. variable
    4. segment
  55. JavaScript statements end with ________.

    1. a semicolon (;)
    2. forward slashes (//)
    3. back slashes (\\)
    4. a period (.)
  56. Which of the following statement sets a function called myHandler to be called when myButton is clicked?

    1. myButton.click = myHandler;
    2. myButton.addEventListener("onClick", myHandler);
    3. myButton.addEventListener("click", myHandler);
  57. Which of the following statements invokes a function named myFunction that has no parameters?

    1. myFunction;
    2. myFunction();
    3. myFunction[];
    4. myFunction!
  58. Which statement retrieves the element "d" from the array myArray?
    var myArray = ["a", "b", "c", "d", "e"];

    1. myArray["d"]
    2. myArray[4]
    3. myArray[3]
    4. myArray.getElement(4)
  59. var x = 4;
    var y = 9;
    var z = x < y;

    What is the data type of the variable z above?

    1. boolean
    2. number
    3. string
    4. double
  60. JavaScript is a(n) ________.

    1. isolated language
    2. Java variant
    3. scripting language
    4. compiled language
  61. Which alert message(s) will be displayed when the following code runs?
    var num = 42;

    if(num <= 30)
    {
    document.write("Alert message #1");
    }
    else{
    document.write("Alert message #2");
    }

    1. "Alert message #1"
    2. "Alert message #2"
    3. Neither
    4. Both
  62. typeof "-34.7"

    1. number
    2. string
    3. boolean
  63. What is an infinite loop?

    1. A loop that contains many lines of code
    2. A loop that never stops iterating
    3. A loop that doesn't have a return statement
    4. A loop that doesn't have a closing brace
  64. Consider the following object:
    var book = {
    title: "Romeo and Juliet",
    author: "William Shakespeare",
    publishDate: 1597
    };

    Considering the book object above,
    which line of code will write the publish date of the book object to the browser console?

    1. console.log(book.publishDate);
    2. console.log(book.author);
    3. console.log(publishDate);
    4. console.log(book.title);
  65. JavaScript code is case sensitive.

    1. True
    2. False
  66. Consider the code:
    var greeting = "Hello";

    var contact = {
    firstName: "Jane",
    lastName: "Doe",
    age: 35
    };

    What line of code update the greeting variable to store the value "Hello Jane"?

    1. greeting += firstName;
    2. greeting += contact.firstName;
    3. greeting = greeting + " " + contact.firstName;
    4. greeting = greeting + " " + contact.lastName;
  67. In order to write JavaScript code that is embedded in an HTML file, your JavaScript code belongs ________.

    1. at the top of the HTML file
    2. within a script element
    3. within a javascript element
    4. at the end of the HTML file
  68. JavaScript can only run in a browser (It cannot run in other environments).

    1. True
    2. False
  69. What output will be displayed on the document when the following loop completes?
    for(x = 0; x < 4; x++){
    document.write(x + " ");
    }

    1. x x x x
    2. 0 1 2 3
    3. 0 1 2 3 4
    4. x x x x x
  70. Which of the following statement would return a collection of elements that have the class "special"?

    1. document.getElementsByClassName("special");
    2. document.querySelector(".special");
    3. document.getClass(".special");
    4. document.getElementByTagName("special");
  71. What will the value of x be when the following while loop stops executing?
    var x = 4;
    while(x > 0)
    {
    alert("Hello ");
    x--;
    }

    1. 4
    2. 0
    3. 1
    4. this is an infinite loop- will never stop executing
  72. Consider the following object:
    var book = {
    title: "Romeo and Juliet",
    author: "William Shakespeare",
    publishDate: 1597
    };

    Considering the book object above,
    author is a ________ of the book object.

    1. value
    2. property
    3. object
    4. data type
  73. var myVar = "23";
    What is the data type of the variable myVar?

    1. number
    2. string
    3. boolean
    4. double
  74. Which of the following for-loop declarations creates a loop that will execute 4 times?

    1. for(x = 1; x < 4; x++)
    2. for(x = 1; x < 5; x++)
    3. for(x = 1; x < 3; x++)
    4. for(x = 0; x < 5; x++)
  75. A JavaScript script will always be executed after the HTML page finishes loading.

    1. True
    2. False
  76. Which alert message(s) will be displayed when the following code runs?
    var num = 35;
    if(num <= 30) {
    document.write("Alert message #1");
    }
    else{
    document.write("Alert message #2");
    }

    1. "Alert message #1"
    2. "Alert message #2"
    3. Neither
    4. Both
  77. Which of the following are used to create a variable and assign it a value?

    1. data type and identifier
    2. identifier and a value
    3. value and an assignment operator (=)
    4. data type, identifier, value and an assignment operator (=)
  78. Assume that a variable has been declared and initialized with the following statement:
    var count = 4;
    Which of the following statements updates the variable with the value 24?

    1. var count = 24;
    2. update count 24;
    3. count == 24;
    4. count = 24;
  79. Which of the following keywords is used to define a function?

    1. function
    2. func
    3. new
    4. method
  80. Which of the following statements invokes a function named myFunction that accepts a numeric parameter?

    1. myFunction 4;
    2. myFunction(4);
    3. myFunction <-(4);
    4. myFunction[4];
  81. Which of the following statements declares a variable named score?

    1. score;
    2. variable score;
    3. var score;
    4. declare var score;
  82. Which of the following statement would return a collection of elements that have the class "special"?

    1. document.getElementByTagName("special");
    2. document.querySelector(".special");
    3. document.getClass(".special");
    4. document.querySelectorAll(".special");