Prerequisite: Basic understanding of JavaScript concepts
1. What is the HTML tag under which one can write the JavaScript code?
A) <javascript>
B) <scripted>
C) <script>
D) <js>
Ans: Option C
Explanation: If we want to write JavaScript code under the HTML tag, we will have to use the "script" tag.
2. Choose the correct JavaScript syntax to change the content of the following HTML code.
<p id="geek">GeeksforGeeks</p>
A) document.getElement("geek").innerHTML="I am a Geek";
B) document.getElementById("geek").innerHTML="I am a Geek";
C) document.getId("geek")="I am a Geek";
D) document.getElementById("geek").innerHTML=I am a Geek;
Ans: B
Explanation: The correct syntax to access the element is document.getElementById("geek"). Here we want to access the content written under that id, so we used .innerHTML to specify that and finally, we replaced the content with whatever is written inside the quotes.
3. Which of the following is the correct syntax to display "GeeksforGeeks" in an alert box using JavaScript?
A) alertbox("GeeksforGeeks");
B) msg("GeeksforGeeks");
C) msgbox("GeeksforGeeks");
D) alert("GeeksforGeeks");
Ans: D
Explanation: To display any text in the alert box, you need to write it as alert("GeeksforGeeks");.
4. What is the correct syntax for referring to an external script called "geek.js"?
A) <script src="geek.js">
B) <script href="geek.js">
C) <script ref="geek.js">
D) <script name="geek.js">
Ans: A
Explanation: The "src" term is used to refer to any JavaScript file.
5. The external JavaScript file must contain <script> tag. True or False?
A) True
B) False
Ans: B
Explanation: It is not necessary for any external javascript file to have <script> tag.
6. Predict the output of the following JavaScript code.
<script type="text/javascript">
a = 8 + "8";
document.write(a);
</script>
A) 16
B) Compilation Error
C) 88
D) Run Time Error
Ans: Option C
Explanation: In the above-given code, 8+"8" have first integer and second string data types. Rather than adding the two numbers, it concatenated the two.
7. Predict the output of the following JavaScript code.
<script type="text/javascript">
let a="GeeksforGeeks";
let x=a.lastIndexOf("G");
document.write(x);
</script>
A) 8
B) 0
C) 9
D) Error
Ans: A
Explanation: The index starts with 0 in JavaScript. Here, x searches for the last occurrence of "G" in the text.
8. Which of the following is not a reserved word in JavaScript?
A) interface
B) throws
C) program
D) short
Ans: C
Explanation: In JavaScript, interface, throws, and short are reserved keywords.
9. Predict the output of the following JavaScript code.
<script type="text/javascript" language="javascript">
let a = "GeeksforGeeks";
let result = a.substring(4, 5);
document.write(result);
</script>
A) sf
B) ks
C) s
D) k
Ans: C
Explanation: The substring command selects the substring starting from 4 to 5, excluding the 5th index. The indexing starts from 0. So, the output here is just "s" rather than sf.
10. Predict the output of the following JavaScript code.
<script type="text/javascript" language="javascript">
let x=5;
let y=6;
let res=eval("x*y");
document.write(res);
</script>
A) "30"
B) 30
C) 5*6
D) "5*6"
Ans: B
Explanation: eval command will evaluate the operation. Here it is 5*6=30.
Similar Reads
JavaScript Quiz These JavaScript quiz questions are designed to help you test and enhance your knowledge of JavaScript, ranging from basic concepts to advanced features. These topic-specific quizzes provide a comprehensive way to practice and assess your understanding of JavaScript concepts.The JavaScript quizzes a
2 min read
JavaScript Basic Quiz These JavaScript basic quiz questions are designed to help you test and enhance your foundational knowledge of JavaScript. Covering essential concepts, this quiz offers a great way to assess your understanding of the core features of JavaScript. The JavaScript quizzes are perfect for beginners and p
2 min read
Quiz App in JavaScript using ChatGPT AI is one of the most overhyped techs right now in the market. It is said that it will soon replace Software Engineers in creating complex software applications. In this article, we will test ChatGPT to create a Quiz application by giving ChatGPT the prompts and will see if it can actually make this
7 min read
Create a Quiz Application Using JavaScript In this article, we will learn how to create a quiz application using JavaScript. The quiz application will contain questions followed by a total score shown at the end of the quiz. The score will increase based on the correct answers given. Initially, there are only three questions, but you can inc
4 min read
JavaScript Set Coding Practice Problems Sets are an important data structure in JavaScript that store unique values of any type, whether primitive or object references. A Set is useful for scenarios where you want to eliminate duplicate values or check for membership efficiently. Sets are unordered collections, meaning the elements are no
2 min read
JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav
15+ min read