Node JS Assignment- 1

Basic Questions

  1.  Write a Node.js script that prints the current file name and directory path using __filename and __dirname.
  2. Write a Node.js script that reads the command‑line argument (your name) and prints “Hello, <name>”.
  3. Write a Node.js script that prints process uptime in seconds using process.uptime().
  4. Write a Node.js script that prints Node.js versions of V8 and OpenSSL from process.versions.
  5. Write a Node.js script that demonstrates using setTimeout and setImmediate together to show their execution order.
  6. Write a Node.js script that prints “Hello, Node.js!”.
  7. Use console.log to print “Node.js Basics Assignment” five times.
  8. Print the current working directory using process.cwd().
  9. Print the platform (process.platform) your Node.js is running on.
  10. Print the architecture (process.arch) your Node.js is using.
  11. Log the Node.js runtime version using process.version.
  12. Print all environment variables using process.env.
  13. Print the global object in Node.js.
  14. Create a script that prints “Script executed at: <current time>”.
  15. Use console.error to print “This is an error message”.
  16. Use console.warn to print a warning message.
  17. Write a script that prints “Node.js event-driven” using setTimeout.
  18. Create a simple script that prints command-line arguments using process.argv.
  19. Create a script that prints “Node.js assignment completed” every 2 seconds using setInterval.
  20. Exit a Node.js script after printing “Exiting program” using process.exit().

Intermediate Questions

  1. Create a script that prints “Hello, <name>!” using a command-line argument.
  2. Write a script that calculates the sum of two numbers passed via command-line arguments.
  3. Create a Node.js script that uses setTimeout to print “Task 1 complete” after 3 seconds.
  4. Create a script that prints numbers 1 to 5 using a non-blocking loop with setTimeout.
  5. Write a Node.js program to print the current date and time using Date object.
  6. Create a script to compare process.argv.length with process.argv output.
  7. Write a script that prints “Event loop demo” in the correct asynchronous order using setImmediate and setTimeout.
  8. Write a Node.js script that uses process.memoryUsage() and prints total/used memory in MB.
  9. Write a Node.js script that takes two numbers from command‑line arguments and prints their product.
  10.  Write a Node.js script that creates a timer which prints “Working…” every 1 second and stops after 5 seconds.
  11. Print the platform and architecture in one message like “Running on <platform>-<arch>”.
  12. Write a script that prints the type of process object.
  13. Print the memory usage of a Node.js process using process.memoryUsage().
  14. Use a command-line argument to take a number n and print numbers 1 to n.
  15. Write a script that prints “Non-blocking example” along with the elapsed time using console.time and console.timeEnd.
  16. Write a Node.js script that prints the first 5 environment variables using process.env.
  17. Create a script that prints “Learning Node.js Event Loop” after a 2-second delay using setTimeout.
  18. Create a simple timer using setInterval that prints “Tick” every second for 5 seconds.
  19. Write a script that prints all process methods available in Node.js.
  20. Write a Node.js script that prints the current working directory every 2 seconds and stops after 3 times.

Advanced Questions

  1. Demonstrate event-driven architecture: create a simple EventEmitter and trigger a custom event “taskCompleted”.
  2. Write a script to demonstrate single-threaded event loop by running multiple asynchronous tasks and logging execution order.
  3. Compare blocking vs non-blocking I/O: read a file using fs.readFileSync and fs.readFile and log execution times.
  4. Write a Node.js script that creates an EventEmitter with two custom events (startTask, endTask) and handles them.
  5. Demonstrate asynchronous behavior using nested setTimeout calls.
  6. Create a script that prints environment variables only if a specific variable DEBUG=true is set.
  7. Write a Node.js script that simulates a simple backend workflow using asynchronous tasks (setTimeout) and logs progress.
  8. Implement a script that uses process.argv to take a filename as input and prints “Processing <filename>…”.
  9. Write a Node.js script that demonstrates async vs sync file read using fs.readFile and fs.readFileSync.
  10. Write a Node.js script that starts three asynchronous timers (setTimeout) with different delays and logs their completion times.