Wednesday , May 22 2019

Express and Node JS Key Programs

Share on WhatsApp
Share via email Share

Node is ideal for the following types of applications? (You can select more than one option)

  1. Data intensive applications
  2. Computation intensive applications
  3. Real-time applications
  4. I/O based applications

var http = require(‘http’);

http.createServer().listen(3000);

function server(req, res) {

    res.writeHead(200, { ‘Content-Type’: ‘text/plain’ });

    res.end(‘XYZ\n’);

};

console.log(‘ABC’);

When you run the above node code the following will happen?

(You can select more than one option)

  1. Print ABC on the server console
  2. Print XYZ on the server console
  3. When the browser loads http://localhost:3000 it will show XYZ
  4.  When the browser loads http://localhost:3000 it will show ABC
  5. When the browser loads http://localhost:3000 it will wait for the response

<!DOCTYPE html>

<html>

<head>

    <title></title>

</head>

<body>

    <b>Hello World</b>

</body>

</html>

var http = require(‘http’);

var fs = require(‘fs’);

var options = { encoding: “utf-8” };

http.createServer((req, res) => {

    res.writeHead(200, { ‘Content-Type’: ‘text/html’ });

    fs.readFile(“index.html”, options, (err, data) => {

        if (err) {

            console.error(“Error reading file!”);

            return;

        }

        res.end(data);

    });

}).listen(3000);

Given the above html in index.html file and the above node app in the same director, if we run the node program and a browser points to http://localhost:3000 the following will happen?

(You can select more than one option)

  1. Hello World in bold will show in the browser
  2. Nothing will show in the browser
  3. An exception will be thrown on the server
  4. The server will send the file index.html to the client
  5. There will be JavaScript compiler error

The following code is in converter.js file:

var Converter1 = {

    rate: 100,

    setRate: (rate) => {

        Converter1.rate = rate;

    },

    rupeeToDollar: (rupee) => {

        return rupee / Converter1.rate;

    }

}

exports.Converter1 = Converter1

                If we put the following app.js file in the same directory as converter.js and run the code with node.js:

                                var { Converter1 } = require(‘./converter.js’);

Converter1.setRate(50);

console.log(Converter1.rupeeToDollar(200));

What will be printed in the console?

  1. 2
  2. 4
  3. JavaScript error
  4. Nothing will be printed on the console

var a = 0;

function asyncFunction(callback) {

    setTimeout(function () {

        a += 5;

        callback();

        a += 9;

    }, 200);

}

a += 2;

asyncFunction(function () {

    console.log(a);

});

a += 3;



  1. If we run the above nodejs program, what will print on the console?
  2. 0
  3. 2
  4. 3
  5. 5
  6. 10
  7. 19

var a = 0;

function asyncFunction(callback) {

    setTimeout(function () {

        a += 5;

        callback();

        a += 9;

    }, 200);

}

a += 2;

(function (a) {

    asyncFunction(function () {

        console.log(a);

    })

})(a);

a += 3;

If we run the above node.js program, what will print on the console?

  1. 0
  2. 2
  3. 3
  4. 5
  5. 10
  6. 19

function async(arg, callback) {

    setTimeout(() => { callback(arg * 2); }, 1000);

}

function final() {

    console.log(‘Done’, results);

}

var items = [1, 2, 3, 4, 5, 6];

var results = [];

var running = 0;

var limit = 2;

function launcher() {

    while (running < limit && items.length > 0) {

        var item = items.shift();

        async(item, function (result) {

            results.push(result);

            running–;

            if (items.length > 0) {

                launcher();

            } else if (running == 0) {

                final();

            }

        });

        running++;

    }

}

launcher();

Which of the following statements is true about the above Nodejs program?

(You can select more than one option)

  1. Runs a number of operations sequentially
  2. Runs a number of operations in parallel
  3. Only starts one async operation at a time (no concurrency)
  4. Starts all async operations in parallel (full concurrency)
  5. Starts a limited number of operations in parallel (partial concurrency, full concurrency control)
  6. Ensures that the async function complete in order
  7. No guarantee of order, only that all the operations have been completed

function async(arg, callback) {

    setTimeout(() => { callback(arg * 2); }, 1000);

}

function final() { console.log(‘Done’, results); }

var items = [1, 2, 3, 4, 5, 6];

var results = [];

function work(item) {

    if (item) {

        async(item, function (result) {

            results.push(result);

            return work(items.shift());

        });

    } else {

        return final();

    }

}

work(items.shift());

Which of the following statements is true about the above Nodejs program? (You can select more than one option)

  1. Runs a number of operations sequentially
  2. Runs a number of operations in parallel
  3. Only starts one async operation at a time (no concurrency)
  4. Starts all async operations in parallel (full concurrency)
  5. Starts a limited number of operations in parallel (partial concurrency, full concurrency control)
  6. Ensures that the async function complete in order
  7. No guarantee of order, only that all the operations have been completed

function async(arg, callback) {

    setTimeout(() => { callback(arg * 2); }, 1000);

}

function final() { console.log(‘Done’, results); }

var items = [1, 2, 3, 4, 5, 6];

var results = [];

items.forEach(function (item) {

    async(item, function (result) {

        results.push(result);

        if (results.length == items.length) {

            final();

        }

    })

});

  1. Runs a number of operations sequentially
  2. Runs a number of operations in parallel
  3. Only starts one async operation at a time (no concurrency)
  4. Starts all async operations in parallel (full concurrency)
  5. Starts a limited number of operations in parallel (partial concurrency, full concurrency control)
  6. Ensures that the async function complete in order
  7. No guarantee of order, only that all the operations have been completed

var http = require(‘http’);

var server = http.createServer((req, res) => {

    res.write(‘Hello World’);

});

server.listen(3000);

Given the above Nodejs program what will show on the browser if we point it to http://localhost:3000

  1. Hello World
  2. ‘Hello World’
  3. Nothing because we have not called the end() method on the response object
  4. http://helloworld
  5. There will be a JavaScript error

var http = require(‘http’);

var server = http.createServer((req, res) => {

    var body = ‘Hello World’;

    res.write(body);

    res.setHeader(‘Content-Length’, body.length.toString());

    res.setHeader(‘Content-Type’, ‘text/plain’);

    res.end();

});

server.listen(3000);

Given the above Nodejs program what will show on the browser if we point it to http://localhost:3000

  1. Hello World
  2. ‘Hello World’
  3. Nothing because there will be a runtime Error on the server: Error: Can’t set headers after they are sent.
  4. http://helloworld
  5. There will be a JavaScript error

If we run the following nodejs program:

var url = require(‘url’);

console.log(require(‘url’).parse(‘http://localhost:3000/1?p=2’).pathname);

What will print on the console?

  1. /1
  2. 1
  3. P
  4. 2
  5. 3000
  6. localhost

 Which command would you give to download the dependencies listed in package.json file?

  1. npm init
  2. node init
  3. npm install
  4. npm install packages
  5. npm start

In which directory does npm download the dependencies?

  1. node
  2. node_modules
  3. npm_modules
  4. npm

Which command would you give to create the package.json file?

  1. npm init
  2. node init
  3. npm install
  4. npm install packages
  5. npm start

Which of the following statements are true about middleware?

  1. It breaks your app into smaller bits of behavior. Generally, they are called one by one, in a sequence.
  2. It breaks your app into smaller bits of behavior. Generally, they are called one by one, in a sequence when the user visits a particular resource i.e. an URL
  3. It breaks your app up into smaller functions that are executed when the user visits a particular resource i.e. an URL.
  4. It is a minimal, unopinionated framework that’s flexible

Which of the following statements are true about Node? (You can select more than one option)

  1. A browser might request a 100 megabyte cat picture from your Node-powered web server. You begin to load this big photo off of the hard disk. As far as Node is concerned, the hard disk is an external resource, so the Node ask it for the file and then it can move onto other things while we wait for it to load.
  2. Node is never really doing two things at once
  3. When an external resource is working on something, Node is not held up waiting
  4. In Node asynchronous things are handled by callbacks

Which of the following statements are true about routers?

  1. It breaks your app into smaller bits of behavior. Generally, they are called one by one, in a sequence.
  2. It breaks your app into smaller bits of behavior. Generally, they are called one by one, in a sequence when the user visits a particular resource i.e. an URL
  3. It breaks your app up into smaller functions that are executed when the user visits a particular resource i.e. an URL.
  4. It is a minimal, unopinionated framework that’s flexible
Share on WhatsApp
Share via email Share

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Leave a Reply

Your email address will not be published.