JavaScript Questions & Answers – Parsing Values using JavaScript – I

«
»

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Parsing Values using JavaScript – I”.

1. What is the function of the XML parser?
a) Converts XML document to XML DOM object
b) Converts XML DOM object to XML document
c) Converts XML DOM object to a comment
d) Compiles the html document
View Answer

Answer: a
Explanation: An XML parser converts an XML document into an XML DOM object – which can then be manipulated with JavaScript. All major browsers have a built-in XML parser to access and manipulate XML.
advertisement

2. What is the purpose of the method ActiveXObject()?
a) Used to call automation object
b) Used to reference automation object
c) Used to instantiate automation object
d) Used to call & reference automation object
View Answer

Answer: c
Explanation: The ActiveXObject.prototype object allows adding properties and methods to the ActiveXObject object that can be used with instances of the ActiveXObject object like any predefined property or method. The ActiveXObject() object is used only to instantiate Automation objects, and has no members.

3. What is the purpose of the url json?
a) Belongs to JSON object
b) Reference JSON formatted data
c) Belongs to JSON
d) Compiles json data
View Answer

Answer: b
Explanation: The URL JSON is assumed to reference a file of JSON-formatted data. The value passed to the callback is the object obtained by parsing the URL contents with jQuery.parseJSON(). jQuery.getJSON() uses this type. If the type is “json” and the URL or data string contains “=?”, the type is converted to “jsonp”.

4. What is the parameter of the method Date.parse()?
a) date
b) string
c) datestring
d) string
View Answer

Answer: c
Explanation: The parse method is defined as Date.parse(datestring). The parse() method parses a date string and returns the number of milliseconds between the date string.

5. Which is the function in JavaScript that will print the current page in JavaScript?
a) print()
b) printcurrent()
c) print(now)
d) print(this)
View Answer

Answer: a
Explanation: The print() method prints the contents of the current window. The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.

6. To which of the following object does the print() method belong to?
a) window
b) document
c) hash
d) string
View Answer

Answer: a
Explanation: The method print() belongs to the window object. The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.

7. What will happen if the radix parameter of the parseInt() function is omitted?
a) Runs in assumption
b) Throws exception
c) Aborts
d) Taken as 0
View Answer

Answer: a
Explanation: The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.

  • If the string begins with “0x”, the radix is 16 (hexadecimal)
  • If the string begins with “0”, the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)

8. What will be the radix value if the string begins with 0x?
a) 13
b) 14
c) 15
d) 16
View Answer

Answer: d
Explanation: If the string begins with 0x, then the radix value will be 16. If the string begins with “0”, the radix is 8 (octal). If the string begins with any other value, the radix is 10 (decimal).

9. What is the function of the parseInt() method?
a) Parses a data type and stores in an integer
b) Parses a string and returns an integer
c) Parses an integer and returns a string
d) Parses a string and return an object
View Answer

Answer: b
Explanation: The function parseInt() method parses a string and returns an integer. If the string begins with 0x, then the radix value will be 16. If the string begins with “0”, the radix is 8 (octal).
advertisement

10. What does it indicate when the radix value is 16?
a) String begins with 0x
b) String begins with 0
c) String begins with 0P
d) String begins with FF
View Answer

Answer: a
Explanation: If the string begins with “0x”, the radix is 16 (hexadecimal). If the string begins with “0”, the radix is 8 (octal).

11. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
var str = "HELLO, LOOK AT YOU!"; 
var patt1 = /\BLO/;
var result = str.search(patt1);
document.getElementById("demo").innerHTML = result;
</script>

a) 4
b) 7
c) 3
d) 1
View Answer

Answer: c
Explanation: The \B metacharacter is used to find a match, but where it is NOT at the beginning/end of a word. It is found in the regex library.

12. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "welcome to.\0JavaScript test."; 
   var patt1 = /\0/;
   var result = str.search(patt1);
   document.getElementById("demo").innerHTML = result;
}
</script>

a) 12
b) 13
c) 10
d) 11
View Answer

Answer: d
Explanation: The \0 metacharacter is used to find NUL character. \0 returns the position where the NUL character was found. If no match is found, it returns -1.

13. What will be the output of the following JavaScript code?

advertisement
<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "hello\f world."; 
   var patt1 = /\f/;
   var result = str.search(patt1);
   document.getElementById("demo").innerHTML = result;
}
</script>

a) 6
b) 5
c) 8
d) -1
View Answer

Answer: b
Explanation: The \f metacharacter is used to find a form feed character. \f returns the position where the form feed character was found. If no match is found, it returns -1.

14. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "hello \r world."; 
   var patt1 = /\r/;
   var result = str.search(patt1);
   document.getElementById("demo").innerHTML = result;
}
</script>

a) 6
b) 5
c) 3
d) -1
View Answer

Answer: a
Explanation: The \r metacharacter is used to find a carriage return character. \r returns the position where the carriage return character was found. If no match is found, it returns -1.

15. What will be the output of the following JavaScript code?

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "hello to \r world."; 
   var patt1 = /\t/;
   var result = str.search(patt1);
   document.getElementById("demo").innerHTML = result;
}
</script>

a) 6
b) 8
c) 9
d) -1
View Answer

Answer: c
Explanation: The \t metacharacter is used to find a tab character. \t returns the position where the tab character was found. If no match is found, it returns -1.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn