Step 1: Understand the connection callback behavior
The connect method calls the callback with an error if connection fails.
Step 2: Analyze the error handling in the callback
If err exists, it logs 'Connection error'; otherwise, 'Connected'. Wrong password causes err.
Final Answer:
Connection error -> Option C
Quick Check:
Error in connect logs 'Connection error' [OK]
Hint: Error in connect callback logs 'Connection error' [OK]
Common Mistakes:
Assuming it logs 'Connected' even on failure
Expecting a syntax error from wrong password
Thinking no output occurs on failure
4. Identify the error in this Node.js database connection code:
const mysql = require('mysql');
const connection = mysql.createConnection({host: 'localhost', user: 'root', password: 'pass'});
connection.connect();
connection.query('SELECT * FROM users', (err, results) => {
if (err) throw err;
console.log(results);
});
connection.end();
medium
A. createConnection() requires a callback function.
B. query() method is used incorrectly with wrong parameters.
C. end() should be called before query() to close connection early.
D. Missing callback in connect() causes query to run before connection is ready.
Solution
Step 1: Check connection timing
connect() is asynchronous; without a callback, query may run before connection is ready.
Step 2: Identify the impact on query execution
Running query before connection is established can cause errors or unexpected behavior.
Final Answer:
Missing callback in connect() causes query to run before connection is ready. -> Option D
Quick Check:
connect() async needs callback before query [OK]
Hint: Always use callback or await after connect() before queries [OK]
Common Mistakes:
Calling end() too early before queries finish
Thinking createConnection needs a callback
Misusing query() parameters
5. You want to build a Node.js app that stores user preferences and updates them often. Which approach best ensures data is saved and updated correctly?
hard
A. Connect to the database once at app start, reuse connection for all queries, and close on app exit.
B. Open a new database connection for every query and close it immediately after.
C. Store preferences only in memory variables without database connection.
D. Use database connection only for reading data, not for saving updates.
Solution
Step 1: Consider connection efficiency and data consistency
Reusing one connection reduces overhead and keeps queries consistent.
Step 2: Evaluate options for frequent updates
Opening and closing connections for each query wastes resources; in-memory storage loses data on restart.
Final Answer:
Connect to the database once at app start, reuse connection for all queries, and close on app exit. -> Option A
Quick Check:
Reuse connection for frequent updates [OK]
Hint: Reuse one connection for many queries, close at app end [OK]
Common Mistakes:
Opening/closing connection for every query wastes resources
Not using database loses persistent data
Using DB only for reads misses updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.