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
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.
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
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:
- A value of 0 indicates that the connection has not yet been established.
- A value of 1 indicates that the connection is established and communication is possible.
- A value of 2 indicates that the connection is going through the closing handshake.
- 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
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:
- open
- close
- message
- error
4. Which method is used to close the WebSocket?
a) Socket.flush()
b) Socket.close()
c) Socket.Close()
d) Socket.dispose()
View Answer
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
Explanation: The client and server communicate over a long-lived TCP socket following rules defined by the WebSocket protocol.
6. Which of the following is not a socket property?
a) onopen
b) readyState
c) onmessage
d) ready
View Answer
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?
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
Explanation: HTTP defines what actions Web servers and browsers should take in response to various commands. The above code creates an HTTP Server.
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
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
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
Explanation: There is no WebSocket event named deny. The four WebSocket events are
- open
- close
- message
- 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
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
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
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
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
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.
- Check Programming Books
- Practice Information Science MCQs
- Practice MCA MCQs
- Apply for Computer Science Internship
- Practice Programming MCQs