JavaScript Questions & Answers – Web Sockets

«
»

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Web Sockets”.

1. Which of the following is a stateless protocol?
a) HTML
b) XHTML
c) HTTP
d) XML
View Answer

Answer: c
Explanation: A stateless protocol does not require the server to retain information or status about each user for the duration of multiple requests. HTTP is a stateless protocol, which means that the connection between the browser and the server is lost once the transaction ends.
advertisement

2. What does the value 2 of the WebSocket attribute Socket.readyState indicate?
a) Closed connection
b) Handshake connection
c) Unestablished connection
d) Established connection and communication is possible
View Answer

Answer: b
Explanation: The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. The readonly attribute readyState represents the state of the connection. It can have the following values:

  1. A value of 0 indicates that the connection has not yet been established.
  2. A value of 1 indicates that the connection is established and communication is possible.
  3. A value of 2 indicates that the connection is going through the closing handshake.
  4. A value of 3 indicates that the connection has been closed or could not be opened.

3. How many WebSocket events are available?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: c
Explanation: Web sockets are defined as a two-way communication between the servers and the clients, which mean both the parties, communicate and exchange data at the same time. There are fourWebSocket events namely:

  1. open
  2. close
  3. message
  4. error
Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

4. Which method is used to close the WebSocket?
a) Socket.flush()
b) Socket.close()
c) Socket.Close()
d) Socket.dispose()
View Answer

Answer: b
Explanation: The Socket.close() is used to close the WebSocket. The Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.

5. How does the client and the server communicate following the rules defined by the WebSocket protocol?
a) Long-lived TCP Socket
b) Short-lived TCP Socket
c) UDP Socket
d) HTTP Socket
View Answer

Answer: a
Explanation: The client and server communicate over a long-lived TCP socket following rules defined by the WebSocket protocol.
Take JavaScript Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement

6. Which of the following is not a socket property?
a) onopen
b) readyState
c) onmessage
d) ready
View Answer

Answer: d
Explanation: There is no Socket property called ready. Various Socket properties include onopen, readystate, ready, binaryTree, onclose, onerror etc.

7. What does the following JavaScript code snippet do?

advertisement
var httpserver = new http.Server();

a) Create an HTTP Server
b) Create HTTP Connection between Client and Server
c) HTTP Server & Connection
d) Create a connection between two servers
View Answer

Answer: a
Explanation: HTTP defines what actions Web servers and browsers should take in response to various commands. The above code creates an HTTP Server.
advertisement

8. How can we check the subprotocol being used by the client?
a) subprotocol property
b) protocol property
c) clientprotocol property
d) client property
View Answer

Answer: b
Explanation: A WebSocket is a standard bidirectional TCP socket between the client and the server. The socket starts out as an HTTP connection and then “Upgrades” to a TCP socket after an HTTP handshake. Once the connection is established, the client can determine which subprotocol is in use by checking the protocol property of the socket.

9. How will you transmit data using the connection?
a) send(data)
b) Socket.send(“data”)
c) Socket.send(data)
d) Socket(data)
View Answer

Answer: c
Explanation: The Socket.send(data) method transmits data using the connection. This Socket method sends data to connected socket.

10. Which of the following is not a WebSocket event?
a) open
b) close
c) error
d) deny
View Answer

Answer: d
Explanation: There is no WebSocket event named deny. The four WebSocket events are

  1. open
  2. close
  3. message
  4. error

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

11. 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 = /\v/;
   var result = str.search(patt1);
   document.getElementById("demo").innerHTML = result;
}
</script>

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

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

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

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "Hello World!"; 
   var patt1 = /\127/g;
   var result = str.match(patt1);
   if(result) 
  	result=true;
   else
  	result=false;
   document.getElementById("demo").innerHTML = result;
}
</script>

a) true
b) false
c) error
d) null
View Answer

Answer: a
Explanation: The \xxx character is used to find the Latin character specified by an octal number xxx. If no match is found, it returns null.

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

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "Hello World!"; 
   var patt1 = /\x57/g;
   var result = str.match(patt1);
   if(result) 
  	result=true;
   else
  	result=false;
 
   document.getElementById("demo").innerHTML = result;
}
</script>

a) error
b) false
c) true
d) undefined
View Answer

Answer: c
Explanation: The \xdd character is used to find the Latin character specified by a hexadecimal number dd. The character ‘W’ is represented as x57 in hexadecimal.

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

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "Visit W3Schools. Hello World!"; 
   var patt1 = /\u0057/g;
   var result = str.match(patt1);
   var result = str.match(patt1);
   if(result) 
  	result=true;
   else
  	result=false;
   document.getElementById("demo").innerHTML = result;
}
</script>

a) true
b) false
c) error
d) undefined
View Answer

Answer: a
Explanation: The \udddd character is used to find the Unicode character specified by a hexadecimal number dddd. If no match is found, it returns null.

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

<p id="demo"></p>
<script>
function myFunction() 
{
   var str = "100%!";
   var patt1 = /\W/g;
   var result = str.match(patt1);
   var result = str.match(patt1);
   if(result) 
  	result=true;
   else
  	result=false;
   document.getElementById("demo").innerHTML = result;
}
</script>

a) false
b) true
c) error
d) undefined
View Answer

Answer: b
Explanation: The \W metacharacter is used to find a non-word character. A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.

Sanfoundry Global Education & Learning Series – Javascript Programming.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.