Welcome to JavaScript String Quiz. This JavaScript String Quiz features 15+ multiple-choice questions designed to test your knowledge of JavaScript String and methods. After each question, we'll provide the correct answer along with an explanation to ensure you grasp the concepts. Ready to start? Let's dive in!
1. How do you declare a string variable in JavaScript?
a)
let str = 'Hello';
b)
var str = "Hello";
c)
const str = 'Hello';
d) All of the above
Answer:
d) All of the above
Explanation:
All three options are valid ways to declare a string variable in JavaScript. You can use single quotes (''), double quotes (""), or backticks (``) for string declaration.
2. What will be the length of the following string?
let myString = "Hello, World!";
a) 11
b) 12
c) 13
d) 14
Answer:
c) 13
Explanation:
The length of a string in JavaScript is determined by the number of characters it contains, including spaces and punctuation. In this case, the string "Hello, World!" contains 13 characters.
3. Which method is used to convert a string to all lowercase letters?
a) toLowerCase()
b) toLower()
c) lowerCase()
d) convertToLower()
Answer:
a) toLowerCase()
Explanation:
The toLowerCase() method is used to convert all the characters in a string to lowercase. It returns a new string with the converted characters.
4. Which method is used to remove leading and trailing whitespace from a string?
a) trim()
b) removeWhitespace()
c) strip()
d) deleteWhitespace()
Answer:
a) trim()
Explanation:
The trim() method is used to remove leading and trailing whitespace (spaces, tabs, and line breaks) from a string.
5. What will be the output of the following code?
'JavaScript'.charAt(0)
a. J
b. j
c. JavaScript
d. Error
Answer:
a. J
Explanation:
The charAt() function returns the character at a specified index in a string. Since JavaScript uses zero-based indexing, 'J' is at position 0.
6. What will 'hello'.concat(' world') return?
a. hello world
b. hello
c. world
d. Error
Answer:
a. hello world
Explanation:
The concat() method is used to join two or more strings.
7. What will 'JavaScript'.endsWith('script') return?
a. true
b. false
c. Error
d. JavaScript
Answer:
b. false
Explanation:
The endsWith() function is case-sensitive. Therefore, it would not match 'script' with 'Script' in 'JavaScript'.
8. What is the result of 'JavaScript'.indexOf('Script')?
a. 4
b. 5
c. 6
d. -1
Answer:
a. 4
Explanation:
The indexOf() function returns the index of the first occurrence of a specified text in a string. JavaScript starts counting from 0, so 'Script' starts at position 4.
9. What will 'JavaScript'.lastIndexOf('a') return?
a. 1
b. 2
c. 3
d. 7
Answer:
c. 3
Explanation:
The lastIndexOf() method in JavaScript returns the last occurrence of a specified value in a string. The string is searched from the end to the beginning, but returns the index from the beginning, at position 0. If we apply this method to 'JavaScript'.lastIndexOf('a'), the output will be 3. This is because the last occurrence of 'a' in 'JavaScript' is at index 3 (remember, JavaScript uses 0-based indexing).
10. What is the output of 'hello'.replace('l', 'r')?
a. herro
b. hello
c. herlo
d. Error
Answer:
c. herlo
Explanation:
The replace() method in JavaScript replaces the first occurrence of a specified value. So, if you run 'hello'.replace('l', 'r'), the output will be 'herlo'. The first occurrence of 'l' in the string 'hello' is replaced by 'r', hence producing 'herlo'.
11. What will 'JavaScript'.slice(4, 10) return?
a. Script
b. JavaS
c. Java
d. Script
Answer:
a. Script
Explanation:
The slice() method extracts a part of a string and returns the extracted part in a new string. The method takes 2 parameters: the starting index (inclusive), and the ending index (exclusive).
Comments
Post a Comment
Leave Comment