JavaScript Practice Test — Questions and Answers
Question 1: Which standard construct for data validation is available in the development environment?
- Validation constructs
- Super controlled loop constructs
- case sensitivity check
- All of the above (Correct answer)
Correct answer: All of the above
Data validation in a development environment involves various techniques. This can include using specific validation constructs (like HTML5 input types and attributes, or JavaScript validation libraries), super controlled loop constructs for iterating and checking data, and ensuring case sensitivity checks where appropriate for data integrity. All these methods contribute to robust data validation.
Question 2: In NetScape, the main purpose of a "Live Wire" is to
- Support only non relational database
- create linkage between client side and server side
- To interpret JavaScript code
- permit server side, Javascript code, to connect to RDBMS (Correct answer)
Correct answer: permit server side, Javascript code, to connect to RDBMS
Netscape LiveWire was an early server-side JavaScript platform. Its primary purpose was to enable JavaScript code running on the server to interact with relational database management systems (RDBMS). This allowed developers to build dynamic web applications by connecting server-side JavaScript logic to databases for data storage and retrieval.
Question 3: When the page has finished parsing, which attribute is utilized to specify that the script be executed? (only for external scripts)
- type
- parse
- a sync
- defer (Correct answer)
Correct answer: defer
The `defer` attribute on a `<script>` tag (for external scripts) tells the browser to download the script in parallel with parsing the HTML document, but to execute it only after the HTML document has been fully parsed. This ensures that the script doesn't block the rendering of the page and that the DOM is ready when the script runs.
Question 4: The script tag must be included in
- head and body (Correct answer)
- title and head
- D head
- all the mentioned
Correct answer: head and body
The `<script>` tag can be placed in either the `<head>` section or the `<body>` section of an HTML document. Placing it in the `<head>` allows scripts to be loaded before the page content, while placing it at the end of the `<body>` is a common practice to ensure the HTML content is parsed and rendered before the script executes, preventing rendering blocks.
Question 5: To add external JS code in your HTML document, which of the following attributes is used?
- ext
- link
- src (Correct answer)
- script
Correct answer: src
To link an external JavaScript file to an HTML document, the `<script>` tag uses the `src` attribute. The `src` attribute specifies the URL (path) of the external script file. For example, `<script src="my-script.js"></script>` tells the browser to load and execute the JavaScript code from `my-script.js`.
Question 6: The keyword or property you use to refer to an object that was used to invoke them is
- this (Correct answer)
- object
- to
- from
Correct answer: this
In JavaScript, the `this` keyword refers to the object that is currently executing the code or that invoked the function. Its value depends on the context in which it is used. For example, inside an object method, `this` refers to the object itself, allowing access to its properties and other methods.
Question 7: What is the basic difference between JavaScript and Java?
- Functions are considered as fields
- There is no difference
- Functions are values, and there is no hard distinction between methods and fields (Correct answer)
- Variables are specific
Correct answer: Functions are values, and there is no hard distinction between methods and fields
JavaScript treats functions as first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions, much like any other value. This functional programming paradigm, combined with its prototype-based inheritance, means there isn't a strict separation between methods (functions associated with objects) and fields (data properties) as seen in class-based languages like Java.
Question 8: The definition of augmenting classes is.
- objects inherit prototype properties in static state
- objects inherit prototype properties even in dynamic state (Correct answer)
- objects inherit prototype properties only in dynamic state
Correct answer: objects inherit prototype properties even in dynamic state
Augmenting classes in JavaScript refers to the ability to add new properties or methods to an object's prototype *after* the object has been created. Because JavaScript uses prototype-based inheritance, any changes made to a prototype are immediately reflected in all objects that inherit from it, even those already instantiated. This dynamic nature allows for flexible extension of objects at runtime.
Question 9: The JSON() method's property is
- it is invoked automatically by the JSON.stringify() method (Correct answer)
- it cannot be invoked in any form
- it can be invoked manually as object.JSON()
- it will be automatically invoked by the compiler
Correct answer: it is invoked automatically by the JSON.stringify() method
The `toJSON()` method is a special method that an object can define. When `JSON.stringify()` is called on an object, it automatically looks for and invokes a `toJSON()` method on that object if it exists. This allows the object to customize its JSON serialization representation, returning a value that will be serialized instead of the original object.
Question 10: We state that a class B can extend another class A when it can.
- B is the superclass and A is the subclass
- A is the superclass and B is the subclass (Correct answer)
- Both A and B are the subclass
- Both A and B are the superclass
Correct answer: A is the superclass and B is the subclass
In object-oriented programming, when class B `extends` class A, it means that class B inherits properties and methods from class A. In this relationship, class A is referred to as the superclass (or parent class), and class B is the subclass (or child class). The subclass B can then add its own unique properties and methods or override those inherited from the superclass A.
Question 11: Which of the following functions allows you to register a function that will only be invoked once?
- setTotaltime()
- setInterval()
- setTimeout() (Correct answer)
- none of the above
Correct answer: setTimeout()
The `setTimeout()` function in JavaScript is used to execute a function or a piece of code once after a specified delay. It schedules the function to run only a single time. In contrast, `setInterval()` repeatedly executes a function at a fixed interval.
Question 12: When uncaught JavaScript exceptions occur, which handler method is used?
- Onerror (Correct answer)
- Onhalt
- none of the mentioned
- Both onhalt and onerror
Correct answer: Onerror
The `onerror` event handler in JavaScript is a global event handler that is triggered when an uncaught error occurs on the page. It provides a mechanism to catch and handle errors that are not caught by `try...catch` blocks. This allows developers to log errors, display user-friendly messages, or perform other recovery actions.
Question 13: Which property is utilized to get information about the browser's vendor and version?
- version
- navigator (Correct answer)
- modal
- browser
Correct answer: navigator
The `navigator` object in JavaScript provides information about the browser, including its name, version, and user-agent string. It is a property of the global `window` object. Developers often use the `navigator` object to detect browser capabilities or to tailor content for specific browsers.
Question 14: The location property belongs to which object?
- Position
- Element
- Location
- Window (Correct answer)
Correct answer: Window
The `location` property is part of the `Window` object in the browser's Document Object Model (DOM). It provides information about the current URL and allows for navigation to different URLs. While `document.location` also exists, it is a read-only alias for `window.location`, making `Window` the primary owner of this property.
Question 15: Which of the following objects has the URL property?
- Location
- Document (Correct answer)
- Element
- All of the above
Correct answer: Document
The `Document` object in the browser's DOM has a `URL` property that returns the complete URL of the current document. This property is read-only and provides the full address of the web page. It is distinct from `window.location.href`, though both provide URL information.
Question 16: Which of the following protocols is stateless?
- XHTML
- HTTP (Correct answer)
- HTML
- All of the above
Correct answer: HTTP
HTTP (Hypertext Transfer Protocol) is a stateless protocol, meaning that each request from a client to a server is treated as an independent transaction. The server does not retain any information about previous requests from the same client. This design simplifies server implementation but often requires mechanisms like cookies or session IDs to maintain state across multiple requests.
Question 17: What does the WebSocket attribute Socket.readyState with a value of 2 mean?
- Established connection and communication is possible
- Closed connection
- Handshake connection (Correct answer)
- Unestablished connection
Correct answer: Handshake connection
The `readyState` property of a WebSocket object indicates the connection's current state. A value of 2 signifies the `CLOSING` state, meaning the connection is in the process of being shut down. This involves a closing handshake between the client and the server to gracefully terminate the connection.
Question 18: What is the total number of WebSocket events?
- 4 (Correct answer)
- 1
- 6
- 2
Correct answer: 4
WebSockets have four primary events that represent different stages of the connection lifecycle. These events are `open` (when the connection is established), `message` (when data is received), `error` (when an error occurs), and `close` (when the connection is closed). These events allow developers to handle various aspects of WebSocket communication.
Question 19: Which of the following isn't a property of a socket?
- onmessage
- ready (Correct answer)
- onopen
- readyState
Correct answer: ready
The `WebSocket` object has several standard properties and event handlers, including `onmessage`, `onopen`, `onclose`, `onerror`, and `readyState`. The option `ready` is not a standard property or event handler of the WebSocket API. It might be confused with `readyState`, which tracks the connection's status.
Question 20: What is the content of a History object?
- Attribute values
- Property
- URL (Correct answer)
- Parameters
Correct answer: URL
The `History` object in the browser's DOM manages the browser's session history, which consists of the URLs visited by the user in the current tab or frame. It allows navigation back and forth through the history stack. While it doesn't directly expose all URLs, it provides methods like `back()`, `forward()`, and `go()` to navigate through the history of URLs.
Question 21: What do you call the events that the Node object generates?
- emitters (Correct answer)
- highevents
- generator
- dispatcher
Correct answer: emitters
In Node.js, objects that emit named events are instances of the `EventEmitter` class. These objects are commonly referred to as "event emitters." They allow for a publish/subscribe pattern where listeners can register to be notified when specific events occur.
Question 22: What function was used to remove all name event handlers?
- removeAllListeners(name) (Correct answer)
- removerListener(name,f)
- deleteListener(name,f)
- deleteAllListeners(name)
Correct answer: removeAllListeners(name)
The `EventEmitter` class in Node.js provides the `removeAllListeners([eventName])` method. When called with an `eventName`, it removes all listeners for that specific event. If called without any arguments, it removes all listeners for all events on the emitter.
Question 23: An event emitter is which of the following?
- process (Correct answer)
- once
- on
- listener
Correct answer: process
In Node.js, the global `process` object is an instance of `EventEmitter`. It emits various events related to the Node.js process lifecycle, such as `exit`, `uncaughtException`, and `SIGINT`. This allows developers to hook into and respond to critical process-level events.
Question 24: Which of the following POSIX signals causes events to occur?
- SIGINT (Correct answer)
- SIGDOWN
- SIGSHORT
- SIGFLOAT
Correct answer: SIGINT
In Node.js, the `process` object emits a `SIGINT` event when the process receives a `SIGINT` signal, typically generated by pressing `Ctrl+C` in the terminal. This allows applications to gracefully handle termination requests, perform cleanup, or prompt the user before exiting. Other POSIX signals like `SIGHUP` or `SIGTERM` also cause events.
Question 25: When the getter and setter methods are available, what does RHINO do?
- Java classes & JavaScript properties
- It becomes JavaScript properties (Correct answer)
- Java classes are used to avoid the
- None of the above
Correct answer: It becomes JavaScript properties
Rhino is a JavaScript engine written in Java. When Java classes are exposed to JavaScript in Rhino, and those Java classes have standard getter and setter methods (e.g., `getX()` and `setX()`), Rhino automatically maps these to JavaScript properties. This allows JavaScript code to access and modify Java object properties using standard dot notation, like `javaObject.x`, instead of explicitly calling `javaObject.getX()` or `javaObject.setX(value)`.
Which standard construct for data validation is available in the development environment?