JavaScript Questions and Answers Part-14

1. What does the subexpression /java(script)?/ result in?
a) It matches “java” followed by the optional “script”
b) It matches “java” followed by any number of “script”
c) It matches “java” followed by a minimum of one “script”
d) It matches “java” followed by a single “script”

Answer: a
Explanation: paranthesis () check for the optional presence of the argument in the string. subexpression /java(script)?/ matches “java” followed by the optional “script”.

2. What is the most essential purpose of parentheses in regular expressions?
a) Define pattern matching techniques
b) Define subpatterns within the complete pattern
c) Define portion of strings in the regular expression
d) matching the complete string

Answer: b
Explanation: When a regular expression is successfully matched against a target string, it is possible to extract the portions of the target string that matched any particular paranthesized subpattern. The essential purpose of parantheses in regular expressions is to define subpatterns within the complete pattern.

3. The method that performs the search-and-replace operation to strings for pattern matching is _______
a) searchandreplace()
b) add()
c) edit()
d) replace()

Answer: d
Explanation: The replace() method performs a search-and-replace operation. It takes a regular expression as its first argument and a replacement string as its second argument.

4. What would be the result of the following statement in JavaScript using regular expression methods?
a) Returns [“123″”456″”789”]
b) Returns [“123″,”456″,”789”]
c) Returns [1,2,3,4,5,6,7,8,9]
d) Throws an exception

Answer: b
Explanation: The split() method can take regular expressions as its arguments. The split() method generally breaks the string on which it is called into an array of substrings, using the argument as a separator.

5. What will be the purpose of exec() in the following JavaScript code?
var pattern = /Java/g;
var text = "JavaScript is more fun than Java!";
var result;
while ((result = pattern.exec(text)) != null)
{
alert("Matched '" + result[0] + "'" +" at position " + result.index +"; next search begins at " + pattern.lastIndex);
}
a) Returns the same kind of array whether or not the regular expression has the global g flag
b) Returns different arrays in the different turns of iterations
c) Return a sub part of the array
d) Returns a null value

Answer: a
Explanation: exec() returns the same kind of array whether or not the regular expression has the global g flag. Recall that match() returns an array of matches when passed a global regular expression. exec(), by contrast, always returns a single match and provides complete information about that match. When exec() is called on a regular expression that has the g flag, it sets the lastIndex property of the regular expression object to the character position immediately following the matched substring. When exec() is invoked a second time for the same regular expression, it begins its search at the character position indicated by the lastIndex property. If exec() does not find a match, it resets lastIndex to 0.

6. What will be the output of the following JavaScript code?
console.log(Pattern.matches("[amn]", "abcd"));
a) true
b) false
c) undefined
d) a

Answer: b
Explanation: The pattern.matches method tests whether the regular expression matches the pattern. The above code will result into false as string “abcd” is not among a, m, or n.

7. What will be the output of the following JavaScript code?
console.log(Pattern.matches("[amn]?", "a"));
a) true
b) false
c) undefined
d) bcd

Answer: a
Explanation: The above code tests for single occurrence of the character in a particular string. The symbol ? is used along with the “[]” for testing single occurrence.

8. What will be the output of the following JavaScript code?
console.log(Pattern.matches("\\d", "1"));
a) true
b) false
c) undefined
d) 1

Answer: a
Explanation: The above code tests for single occurrence of digit. //d returns true when there is any digits occurring one time.

9. What will be the output of the following JavaScript code?
Console.log(Pattern.matches("[adf]+", "a"));
a) true
b) false
c) undefined
d) 0

Answer: a
Explanation: “+” sign in regex checks for more than one time occurrence of a particular character. It returns true if a character from the set is present more than once.

10. What will be the output of the following JavaScript code?
console.log(Pattern.matches("[^abc]", "aemngq"));
a) true
b) false
c) undefined
d) 1

Answer: b
Explanation: “^” is used as a negation operator. The above code will print false as “a” is present in the string passed in the argument.