JavaScript Aptitude Test - 25 MCQ Questions with Answers

This post presents a JavaScript aptitude test with 25 multiple-choice questions for beginners. These beginner-level MCQ questions cover the fundamentals of JavaScript.

1. What is JavaScript?

a) A scripting language used primarily for server-side scripting
b) A markup language used to style HTML pages
c) A programming language used to create interactive effects within web browsers
d) A database query language

2. Which HTML tag is used to embed JavaScript code in a webpage?

a) <js>
b) <script>
c) <javascript>
d) <java>

3. How do you create a variable in JavaScript?

a) var myVariable = "value";
b) variable myVariable = "value";
c) v myVariable = "value";
d) $myVariable = "value";

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

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

5. Which of the following is not a valid JavaScript variable name?

a) 2names
b) _first_name
c) firstName
d) LastName

6. 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)
c) if (i !== 5)
d) b) and c) are correct

7. Which JavaScript method is used to access an element by its ID?

a) getElementById()
b) getElementByID()
c) getElementsById()
d) getElement(Id)

8. How do you call a function named "myFunction"?

a) call myFunction()
b) call function myFunction()
c) myFunction()
d) call myFunction

9. How does a FOR loop start?

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

10. 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**

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

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

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

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

13. What is the correct JavaScript syntax to change the content of the HTML element below?

a) document.getElement("p").innerHTML = "Hello World!";
b) document.getElementByName("p").innerHTML = "Hello World!";
c) document.getElementById("demo").innerHTML = "Hello World!";
d) document.getElementById("p").innerHTML = "Hello World!";

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

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

15. How do you create a function in JavaScript?

a) function:myFunction()
b) function myFunction()
c) function = myFunction()
d) function: myFunction{}

16. How do you round the number 7.25, to the nearest integer?

a) round(7.25)
b) Math.round(7.25)
c) Math.rnd(7.25)
d) round(7.25,0)

17. Which statement is used to stop a loop?

a) halt
b) exit
c) break
d) stop

18. How can you detect the client's browser name?

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

19. Which method converts JSON data to a JavaScript object?

a) JSON.toObject()
b) JSON.parse()
c) JSON.stringify()
d) JSON.toJS()

20. 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) ceil(x, y)

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

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

22. What is the correct JavaScript syntax for opening a new window called "window" and loading "page.html" into it?

a) window.new("page.html", "window");
b) window.open("page.html", "window");
c) new.window("page.html", "window");
d) open.window("page.html", "window");

23. Which of the following JavaScript methods is used to access one or more elements by class name?

a) document.getElementByClassName()
b) document.getElementsByClassName()
c) document.getElementByClass()
d) document.getClass()

24. What is the default behavior of the form element when the submit button is clicked?

a) Data is saved locally to the web browser
b) The form data is sent to the page itself
c) The form data is sent to a new page
d) The form is cleared

25. How do you declare a JavaScript variable that cannot be reassigned?

a) var myVar;
b) let myVar;
c) const myVar;
d) int myVar;

Comments