Basic Javascript Quiz

Description: Basic Javascript Quiz
Number of Questions: 15
Created by:
Tags: javascript
Attempted 0/15 Correct 0 Score 0

Inside which HTML element do we put the JavaScript?


Correct Option: C
Explanation:

To include JavaScript code in an HTML document, we use the element. Therefore, the correct answer is:

The Answer is: C. <script>

What is the correct JavaScript syntax to write "Hello World"?

  1. response.write("Hello World")

  2. document.write("Hello World")

  3. ("Hello World")

  4. echo("Hello World")


Correct Option: B

How do you call a function named "myFunction"?

  1. call function myFunction

  2. myFunction()

  3. call myFunction()


Correct Option: B
Explanation:

To call a function named "myFunction", the user needs to use the following syntax:

myFunction();

Option A is incorrect because it is not the correct syntax for calling a function.

Option B is the correct answer because it calls the function "myFunction" using the proper syntax.

Option C is incorrect because it includes the word "call", which is not necessary when calling a function in most programming languages.

Therefore, the answer is: B. myFunction()

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

  1. if i==5 then

  2. if i=5 then

  3. if (i==5)

  4. if i=5


Correct Option: C
Explanation:

To write a conditional statement for executing some statements only if "i" is equal to 5, you need to use the correct syntax.

The correct syntax for a conditional statement in most programming languages is:

if (condition) {
    // statements to be executed if the condition is true
}

Now let's analyze each option and determine if it is the correct way to write the conditional statement:

A. if i==5 then - This option is incorrect because the syntax is incorrect. The correct syntax for the condition should be enclosed in parentheses, like if (i == 5).

B. if i=5 then - This option is incorrect because the syntax is incorrect. To check for equality, you need to use a double equals sign (==) instead of a single equals sign.

C. if (i==5) - This option is correct. It uses the correct syntax for a conditional statement. The condition i == 5 checks if the variable i is equal to 5.

D. if i=5 - This option is incorrect because the syntax is incorrect. To check for equality, you need to use a double equals sign (==) instead of a single equals sign.

Therefore, the correct option is:

The Answer is: C. if (i==5)

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

  1. if (i <> 5)

  2. if (i != 5)

  3. if =! 5 then

  4. if <> 5


Correct Option: B
  1. Two. The "for" loop and the "while" loop

  2. Four. The "for" loop, the "while" loop, the "do...while" loop, and the "loop...until" loop

  3. One. The "for" loop


Correct Option: A
  1. for (i = 0; i <= 5)

  2. for (i = 0; i <= 5; i++)

  3. for i = 1 to 5

  4. for (i <= 5; i++)


Correct Option: B

What is the correct way to write a JavaScript array?

  1. var txt = new Array(1:"tim",2:"shaq",3:"kobe")

  2. var txt = new Array="tim","shaq","kobe"

  3. var txt = new Array("tim","shaq","kobe")


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) var txt = new Array(1:"tim",2:"shaq",3:"kobe") This option is incorrect because when using the new Array() syntax, you should not specify the indices explicitly. Instead, you can initialize the array directly with the values.

Option B) var txt = new Array="tim","shaq","kobe" This option is incorrect because the syntax is incorrect. When creating a new array, you should use parentheses to enclose the values, not an equal sign.

Option C) var txt = new Array("tim","shaq","kobe") This option is correct because it uses the correct syntax for creating a JavaScript array. The values are enclosed in parentheses, and each value is separated by a comma.

The correct answer is C. This option is correct because it follows the correct syntax for creating a JavaScript array using the new Array() syntax.

How do you round the number 8.25, to the nearest whole number?

  1. Math.rnd(8.25)

  2. Math.round(8.25)

  3. round(8.25)

  4. rnd(8.25)


Correct Option: B
  1. Math.max(6,8)

  2. top(6,8)

  3. ceil(6,8)

  4. Math.ceil(6,8)


Correct Option: A

You work on a JavaScript project. How do you prompt users with messages and at the same time requesting user inputs?

  1. Alert()

  2. Display()

  3. Prompt()

  4. Confirm()


Correct Option: C

Which of the following is the correct syntax of FOR?

  1. for ( increment; initialize; test)

  2. for ( initialize; test), increment

  3. for ( initialize; test; increment)

  4. for ( test; initalize; increment)


Correct Option: C
Explanation:

To solve this question, the user needs to know the correct syntax of the FOR loop used in programming. The FOR loop is used for executing a block of code repeatedly for a fixed number of times, and it has three parts: initialization, test, and increment.

Now, let's discuss each option and explain why it is right or wrong:

A. for ( increment; initialize; test): This option is incorrect because the syntax of FOR loop starts with the initialization, followed by the test, and then the increment. In this option, the increment is mentioned first, which is not the correct order.

B. for ( initialize; test), increment: This option is incorrect because the correct syntax of the FOR loop requires three statements separated by semicolons. This option has only two statements with a comma in between, which is not the correct syntax.

C. for ( initialize; test; increment): This option is correct. The correct syntax for the FOR loop is to start with the initialization statement, followed by the test statement, and then the increment statement, all separated by semicolons. This option has all three statements in the correct order.

D. for ( test; initalize; increment): This option is incorrect because the correct syntax of the FOR loop requires the initialization statement to be mentioned before the test statement. In this option, the test statement is mentioned first, which is not the correct order.

Therefore, the answer is: C. for ( initialize; test; increment).

In your JavaScript code, how do you find out which character occurs at the 5th position in a string "How are you"?

  1. Substring()

  2. String()

  3. Stringlength()

  4. CharAt()


Correct Option: D
Explanation:

To find out which character occurs at the 5th position in a string "How are you" in JavaScript, you can use the charAt() method.

Now, let's go through each option and explain why it is right or wrong:

A. Substring(): This option is incorrect. The substring() method is used to extract a specified part of a string and return it as a new string. It does not directly give you the character at a specific position.

B. String(): This option is incorrect. The String() function is used to convert a value to a string. It does not provide a way to access specific characters in a string.

C. Stringlength(): This option is incorrect. There is no built-in method called Stringlength() in JavaScript. This option is not relevant to the question.

D. CharAt(): This option is correct. The charAt() method is used to return the character at a specified index in a string. It takes an index as a parameter and returns the character at that index. In this case, charAt(4) will return the character at the 5th position in the string "How are you".

The Answer is: D

Which of the following do you use for a multi-way branch?

  1. If

  2. Ifthen

  3. Ifelse

  4. switch

  5. for


Correct Option: D
Explanation:

To solve this question, the user needs to know the different programming constructs used for branching and looping.

Option A: if statement is used for a two-way branch, where the code executes only if the condition is true. It is not used for a multi-way branch.

Option B: ifthen is not a valid programming construct.

Option C: ifelse is used for a two-way branch, where the code executes one block if the condition is true and another block if the condition is false. It is not used for a multi-way branch.

Option D: switch statement is used for a multi-way branch, where the code executes different blocks of code based on different possible values of an expression.

Option E: for loop is used for repeating a block of code for a specific number of times or for iterating over a collection of items. It is not used for branching.

Therefore, the correct answer is:

The Answer is: D. switch

You want to design a form validation mechanism. Using string methods, which of the following are the steps involved ?

  1. Check for the presence of certain characters

  2. Check the position of substrings

  3. Test the length of data

  4. Check the variable type of the strings

  5. Either ABC


Correct Option: E
- Hide questions