JavaScript Quiz

1. What is the correct syntax for referring to an external script called "xxx.js"?

a) <script href="xxx.js">
b) <script name="xxx.js">
c) <script src="xxx.js">
d) <script file="xxx.js">

Answer:

c) <script src="xxx.js">

Explanation:

The correct syntax for referring to an external JavaScript file is <script src="xxx.js">.

2. How do you write a conditional statement for executing some statements only if "i" is NOT equal to 5?

a) if (i <> 5)
b) if i =! 5 then
c) if (i != 5)
d) if i <> 5

Answer:

c) if (i != 5)

Explanation:

In JavaScript, '!=' is used to test if a variable is not equal to a value. The correct syntax is if (i != 5).

3. How does a 'for' loop start?

a) for (i = 0; i <= 5; i++)
b) for (i <= 5; i++)
c) for i = 1 to 5
d) for (i = 0; i <= 5)

Answer:

a) for (i = 0; i <= 5; i++)

Explanation:

The correct syntax of a 'for' loop in JavaScript is for (initialization; condition; increment).

4. What is the correct way to write a JavaScript array?

a) var colors = 1 = ("red"), 2 = ("green"), 3 = ("blue")
b) var colors = (1:"red", 2:"green", 3:"blue")
c) var colors = ["red", "green", "blue"]
d) var colors = "red", "green", "blue"

Answer:

c) var colors = ["red", "green", "blue"]

Explanation:

In JavaScript, arrays are written with square brackets, and array elements are separated by commas.

5. Which event occurs when the user clicks on an HTML element?

a) onchange
b) onmouseclick
c) onclick
d) onmouseover

Answer:

c) onclick

Explanation:

The onclick event occurs when the user clicks on an HTML element.

6. How do you declare a JavaScript variable?

a) var carName;
b) variable carName;
c) v carName;
d) carName;

Answer:

a) var carName;

Explanation:

In JavaScript, a variable is declared using the 'var' keyword followed by the variable name.

7. What will the following code return: Boolean(10 > 9)?

a) NaN
b) false
c) true
d) undefined

Answer:

c) true

Explanation:

The expression 10 > 9 evaluates to true, and the Boolean() function converts it to a Boolean value, which is true.

8. Which operator is used to assign a value to a variable?

a) *
b) –
c) =
d) x

Answer:

c) =

Explanation:

The '=' operator is used in JavaScript to assign values to variables.

9. What is the correct syntax for adding comments in JavaScript?

a) <!–This is a comment–>
b) 'This is a comment
c) //This is a comment
d) /*This is a comment*/

Answer:

c) //This is a comment

Explanation:

In JavaScript, single-line comments are added with two forward slashes (//), and multi-line comments start with /* and end with */.

10. How do you create a function in JavaScript?

a) function myFunction()
b) function:myFunction()
c) function = myFunction()
d) create myFunction()

Answer:

a) function myFunction()

Explanation:

In JavaScript, a function is defined using the 'function' keyword, followed by a name, followed by parentheses ().

11. Which method is used to round a number to the nearest integer in JavaScript?

a) Math.round()
b) Math.ceil()
c) Math.floor()
d) round()

Answer:

a) Math.round()

Explanation:

Math.round() is used to round a number to its nearest integer in JavaScript.

12. How can you detect the client's browser name in JavaScript?

a) navigator.appName
b) browser.name
c) client.navName
d) window.browserName

Answer:

a) navigator.appName

Explanation:

navigator.appName returns the name of the browser the client is using.

13. Which event is fired when a form element loses focus?

a) onfocusout
b) onblur
c) onfocus
d) onchange

Answer:

b) onblur

Explanation:

The onblur event is fired when an element loses focus.

14. What method is used to parse a string to an int in JavaScript?

a) Integer.parse()
b) parseInteger()
c) parseInt()
d) toInt()

Answer:

c) parseInt()

Explanation:

parseInt() function parses a string argument and returns an integer of the specified radix or base.

15. What will be the output of the following code: console.log(typeof "John");

a) 'string'
b) 'text'
c) 'Str'
d) 'undefined'

Answer:

a) 'string'

Explanation:

The typeof operator in JavaScript returns the type of a variable or an expression. In this case, "John" is a string, so it returns 'string'.

16. How do you find the number with the highest value of x and y?

a) Math.ceil(x, y)
b) top(x, y)
c) Math.max(x, y)
d) Math.highest(x, y)

Answer:

c) Math.max(x, y)

Explanation:

Math.max(x, y) is used to find the highest value between x and y in JavaScript.

17. What is the correct JavaScript syntax for opening a new window called "w2"?

a) w2 = window.new("http://www.example.com");
b) w2 = window.open("http://www.example.com");
c) w2 = new window("http://www.example.com");
d) w2 = open.window("http://www.example.com");

Answer:

b) w2 = window.open("http://www.example.com");

Explanation:

window.open() method is used to open a new browser window. The syntax w2 = window.open("http://www.example.com"); opens a new window with the specified URL.

18. JavaScript is the same as Java.

a) True
b) False
c) Depends on the context
d) Sometimes true

Answer:

b) False

Explanation:

JavaScript and Java are entirely different languages, both in concept and design. JavaScript is a scripting language used mainly for enhancing web pages and user interfaces, while Java is a programming language used for building more complex, standalone applications.

19. How do you declare a JavaScript array?

a) var colors = "red", "green", "blue";
b) var colors = (1:"red", 2:"green", 3:"blue");
c) var colors = ["red", "green", "blue"];
d) var colors = 1 = ("red"), 2 = ("green"), 3 = ("blue");

Answer:

c) var colors = ["red", "green", "blue"];

Explanation:

JavaScript arrays are declared with square brackets and the elements are separated by commas.

20. How can you add a comment in a JavaScript?

a) <!–This is a comment–>
b) 'This is a comment
c) //This is a comment
d) /* This is a comment */

Answer:

c) //This is a comment (for single-line) and d) /* This is a comment */ (for multi-line)

Explanation:

In JavaScript, single-line comments are made with two forward slashes (//), and multi-line comments are made with /* to start and */ to end.

21. What method is used to remove the last element from an array and return that element in JavaScript?

a) array.last()
b) array.pop()
c) array.pull()
d) array.removeLast()

Answer:

b) array.pop()

Explanation:

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

22. Which of the following is correct about features of JavaScript?

a) It can not handle dates and time.
b) JavaScript is a lightweight, interpreted programming language.
c) JavaScript is not object-oriented.
d) It cannot perform tasks related to the network.

Answer:

b) JavaScript is a lightweight, interpreted programming language.

Explanation:

JavaScript is indeed a lightweight, interpreted programming language that allows scripting on web pages, enhancing interactivity and functionality.

23. How can you detect if a variable is an array in JavaScript?

a) variable instanceof Array
b) Array.isArray(variable)
c) typeof variable == 'array'
d) variable.isArray()

Answer:

b) Array.isArray(variable)

Explanation:

Array.isArray() is a static method of Array that returns true if a variable is an array, otherwise false.

24. What will the following code return: Boolean(null);

a) true
b) false
c) null
d) undefined

Answer:

b) false

Explanation:

Boolean(null) will return false, as null is a falsy value in JavaScript.

25. What is the purpose of the 'this' keyword in JavaScript?

a) Refers to the current object
b) Refers to the previous object
c) Is a variable that stores data
d) None of the above

Answer:

a) Refers to the current object

Explanation:

In JavaScript, 'this' is a reference to the current object, the object that is currently being executed or used.

26. What does the 'isNaN' function do in JavaScript?

a) Converts a value to a number
b) Checks if a value is not a number
c) Checks if a value is a number
d) None of the above

Answer:

b) Checks if a value is not a number

Explanation:

The 'isNaN' function in JavaScript is used to determine whether a value is NaN (Not-a-Number) or not.

27. Which method is used to combine two or more arrays in JavaScript?

a) array1.join(array2)
b) array1.concat(array2)
c) array1.combine(array2)
d) array1.merge(array2)

Answer:

b) array1.concat(array2)

Explanation:

The concat() method is used to merge two or more arrays. This method does not change the existing arrays but returns a new array.

28. How do you find the element with an id of "demo" in JavaScript?

a) getElementById('demo')
b) querySelector('#demo')
c) Both a) and b)
d) find('demo')

Answer:

c) Both a) and b)

Explanation:

Both getElementById('demo') and querySelector('#demo') can be used to find an element with the id of "demo" in JavaScript.

29. What is the default behavior of a button inside a form element?

a) Opens a new webpage
b) Submits the form
c) Reloads the page
d) None of the above

Answer:

b) Submits the form

Explanation:

By default, a button inside a form element submits the form.

30. What does the 'addEventListener' method do in JavaScript?

a) Adds a new HTML element
b) Updates an existing HTML element
c) Listens for a specific event on an element and executes a block of code when that event is triggered
d) Removes an event listener from an element

Answer:

c) Listens for a specific event on an element and executes a block of code when that event is triggered

Explanation:

The addEventListener() method attaches an event handler to the specified element without overwriting existing event handlers.

31. Which JavaScript operator returns true if the two compared values are not equal?

a) ==
b) !=
c) ===
d) !==

Answer:

b) !=

Explanation:

In JavaScript, the '!=' operator is used to check if two values are not equal. It returns true if the operands are not equal.

32. How do you declare a JavaScript function named "myFunction"?

a) function:myFunction()
b) function = myFunction()
c) function myFunction()
d) function – myFunction()

Answer:

c) function myFunction()

Explanation:

A JavaScript function is declared with the 'function' keyword, followed by the name of the function and parentheses. The correct declaration is function myFunction().

33. What will the following JavaScript code return? console.log("Hello World".length);

a) Hello World
b) 11
c) 12
d) undefined

Answer:

b) 11

Explanation:

The 'length' property in JavaScript is used to find the length of a string. "Hello World" has 11 characters, including the space.

34. Which JavaScript method can be used to replace parts of a string?

a) replace()
b) substring()
c) split()
d) concat()

Answer:

a) replace()

Explanation:

The replace() method is used in JavaScript to replace parts of a string with another string.

35. What is the correct syntax for referring to an external script called "app.js" with a relative path?

a) <script src="app.js">
b) <script href="app.js">
c) <script ref="app.js">
d) <script link="app.js">

Answer:

a) <script src="app.js">

Explanation:

The correct syntax for including an external JavaScript file is using the <script src="app.js"> tag.

36. How do you write an IF statement in JavaScript to execute some code if "x" is NOT equal to 8?

a) if x != 8 then
b) if (x != 8)
c) if x <> 8
d) if (x not equal 8)

Answer:

b) if (x != 8)

Explanation:

In JavaScript, the correct syntax for an IF statement checking if "x" is not equal to 8 is if (x != 8).

37. Which JavaScript method is used for parsing a string to a floating point number?

a) Integer.parse()
b) parseFloat()
c) Number.parseFloat()
d) parseDouble()

Answer:

b) parseFloat()

Explanation:

The parseFloat() method parses a string and returns a floating point number. It's the correct method to use for converting strings to floats.

38. What is the purpose of JavaScript's 'console.log()' method?

a) To display a message in the web page's content
b) To send a message to the browser's console
c) To log variables in the server logs
d) To create a popup alert

Answer:

b) To send a message to the browser's console

Explanation:

console.log() is used in JavaScript to output a message to the web browser's console. It is commonly used for debugging purposes.

39. How do you create an object in JavaScript?

a) var obj = new Object();
b) var obj = new OBJECT();
c) var obj = create Object();
d) var obj = object();

Answer:

a) var obj = new Object();

Explanation:

In JavaScript, an object can be created using the new keyword followed by Object(). The syntax is var obj = new Object().

40. What will the following code output? console.log(typeof 42);

a) 'number'
b) 'integer'
c) 'float'
d) 'digit'

Answer:

a) 'number'

Explanation:

In JavaScript, typeof is an operator that returns the data type of its operand. For the number 42, it will return 'number'.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top