MCQs Of Day 22: MongoDB Replication
MCQs on MongoDB Replication
1. What is a MongoDB replica set?
o A) A single MongoDB node that handles all the requests
o B) A group of MongoDB servers that maintain the same data set
o C) A storage engine used for replication
o D) A query engine used for replication
Answer: B) A group of MongoDB servers that maintain the same data set
Explanation: A replica set in MongoDB is a group of MongoDB instances that maintain the same data set to provide redundancy and high availability.
2. What is the primary purpose of a MongoDB replica set?
o A) To replicate data to multiple databases
o B) To provide automatic failover and data redundancy
o C) To improve the query performance
o D) To store data on disk
Answer: B) To provide automatic failover and data redundancy
Explanation: Replica sets provide high availability and redundancy by replicating data across multiple MongoDB nodes, ensuring that if one node goes down, another can take over as the primary.
3. How many members are required for a MongoDB replica set?
o A) 1
o B) 2
o C) 3
o D) 5
Answer: C) 3
Explanation: While you can start a replica set with 2 members, at least 3 members are recommended to ensure the replica set has a primary and a majority of secondaries for elections and fault tolerance.
4. Which of the following is a default read preference in MongoDB?
o A) Primary
o B) Secondary
o C) Nearest
o D) Majority
Answer: A) Primary
Explanation: The default read preference is primary, meaning that all reads are directed to the primary node.
5. Which of the following is the correct command to initiate a replica set in MongoDB?
o A) rs.initiate()
o B) rs.start()
o C) mongod --replSet
o D) replicaSet.start()
Answer: A) rs.initiate()
Explanation: The command rs.initiate() is used to initiate a MongoDB replica set.
6. What happens when a primary node in a replica set goes down?
o A) Data is lost
o B) A secondary node is automatically promoted to primary
o C) The replica set becomes read-only
o D) All operations are blocked
Answer: B) A secondary node is automatically promoted to primary
Explanation: MongoDB automatically promotes a secondary node to primary when the original primary node goes down, ensuring high availability.
7. What is the default write concern in MongoDB?
o A) w: 0
o B) w: 1
o C) w: "majority"
o D) w: 2
Answer: B) w: 1
Explanation: The default write concern is w: 1, meaning the write operation is acknowledged by the primary node before returning.
8. What is the purpose of the rs.status() command in MongoDB?
o A) To check the health of a single node
o B) To view the status of the replica set
o C) To start the replica set
o D) To terminate the replica set
Answer: B) To view the status of the replica set
Explanation: The rs.status() command provides information about the current state of the replica set, including the primary and secondary nodes.
9. Which of the following write concerns ensures that a write operation is acknowledged by the majority of the replica set members?
o A) w: 0
o B) w: 1
o C) w: "majority"
o D) w: 2
Answer: C) w: "majority"
Explanation: w: "majority" ensures that a write operation is acknowledged by the majority of replica set members, providing stronger consistency.
10. What does the readConcern setting in MongoDB control?
- A) The number of replicas
- B) The data consistency level for read operations
- C) The write acknowledgment for read operations
- D) The replication lag between primary and secondary nodes
Answer: B) The data consistency level for read operations
Explanation: readConcern controls the consistency level of the data read from MongoDB. For example, readConcern: "majority" guarantees that the data returned is the most recent committed.
11. What does the readPref setting control in MongoDB?
- A) The type of replica set
- B) The node to which read operations are directed
- C) The maximum number of reads allowed
- D) The write acknowledgment level
Answer: B) The node to which read operations are directed
Explanation: readPref controls the node from which read operations are executed, with options such as primary, secondary, and nearest.
12. Which of the following is a valid read preference in MongoDB?
- A) PrimaryPreferred
- B) SecondaryOnly
- C) NearestOnly
- D) MajorityOnly
Answer: A) PrimaryPreferred
Explanation: PrimaryPreferred is a valid read preference that allows reads from the primary node but falls back to a secondary if the primary is unavailable.
13. Which command is used to add a new member to a MongoDB replica set?
- A) rs.add()
- B) rs.addMember()
- C) db.addReplica()
- D) db.addMember()
Answer: A) rs.add()
Explanation: The rs.add() command is used to add a new member to an existing MongoDB replica set.
14. What is the minimum number of nodes required for a MongoDB replica set to achieve automatic failover?
- A) 1
- B) 2
- C) 3
- D) 4
Answer: C) 3
Explanation: At least 3 nodes are required to ensure automatic failover, providing enough nodes for a majority to elect a new primary if the current one fails.
15. Which of the following is a potential downside of using a replica set with more nodes?
- A) Increased data consistency
- B) Increased network latency and overhead
- C) Increased fault tolerance
- D) Decreased disk space usage
Answer: B) Increased network latency and overhead
Explanation: More nodes in a replica set increase network communication overhead and may introduce latency, as the system needs to maintain consistency across all members.
16. What is the role of a secondary node in a MongoDB replica set?
- A) It handles all write operations
- B) It stores the original data
- C) It replicates data from the primary and can serve read operations
- D) It cannot serve read operations
Answer: C) It replicates data from the primary and can serve read operations
Explanation: Secondary nodes replicate data from the primary node and can also serve read operations, depending on the read preference.
17. Which command is used to remove a member from a MongoDB replica set?
- A) rs.remove()
- B) rs.delete()
- C) rs.removeMember()
- D) rs.drop()
Answer: A) rs.remove()
Explanation: The rs.remove() command is used to remove a member from a MongoDB replica set.
18. How do you check if the primary node has been successfully elected in a replica set?
- A) By using the rs.status() command
- B) By checking the logs only
- C) By using the db.isPrimary() command
- D) By using the rs.election() command
Answer: A) By using the rs.status() command
Explanation: The rs.status() command provides information about the election status and confirms the primary node in a replica set.
19. Which of the following is a valid write concern in MongoDB?
- A) w: 0
- B) w: "all"
- C) w: "every"
- D) w: "leader"
Answer: A) w: 0
Explanation: w: 0 is a valid write concern that indicates no acknowledgment is required for the write operation.
20. Which of the following MongoDB settings controls the replication delay between primary and secondary nodes?
- A) ReplicationLag
- B) SyncDelay
- C) oplog window
- D) ReplicaSync
Answer: C) oplog window
Explanation: The oplog window controls the replication delay between the primary and secondary nodes.
21. Which MongoDB command is used to view the configuration of a replica set?
- A) rs.config()
- B) db.config()
- C) rs.status()
- D) db.replicaConfig()
Answer: A) rs.config()
Explanation: The rs.config() command is used to view the current configuration of a MongoDB replica set.
22. In MongoDB, which member of a replica set is responsible for handling read and write operations?
- A) Secondary node
- B) Primary node
- C) Arbitrator node
- D) Both primary and secondary nodes
Answer: B) Primary node
Explanation: The primary node handles all write operations and can also handle read operations depending on the read preference.
23. What is the purpose of an arbitrator in a MongoDB replica set?
- A) To store data
- B) To participate in elections but not hold data
- C) To increase the number of voting members
- D) To handle read queries
Answer: B) To participate in elections but not hold data
Explanation: An arbitrator helps in elections but does not store data. It’s used to break ties in elections when there is an even number of voting members.
24. Which of the following is true about a MongoDB replica set with three nodes?
- A) The secondaries cannot be used for read operations
- B) If the primary node goes down, the system will not be available
- C) All nodes are capable of accepting write operations
- D) The system can automatically promote a secondary to primary if needed
Answer: D) The system can automatically promote a secondary to primary if needed
Explanation: If the primary node fails, a secondary node is automatically promoted to primary to ensure continued availability.
25. Which of the following is NOT a valid read preference in MongoDB?
- A) Primary
- B) Secondary
- C) PrimaryPreferred
- D) WritePreferred
Answer: D) WritePreferred
Explanation: There is no WritePreferred read preference in MongoDB. Valid read preferences include Primary, Secondary, PrimaryPreferred, SecondaryPreferred, and Nearest.
26. Which of the following is NOT a factor affecting replication in MongoDB?
- A) Network latency
- B) Disk I/O performance
- C) Number of replica set members
- D) Amount of data on disk
Answer: D) Amount of data on disk
Explanation: While the amount of data affects the replication lag, it does not directly affect replication performance like network latency, disk I/O, and the number of replica set members.
27. Which MongoDB command is used to force a secondary node to become the primary?
- A) rs.stepDown()
- B) rs.forcePrimary()
- C) rs.elect()
- D) rs.recover()
Answer: A) rs.stepDown()
Explanation: The rs.stepDown() command forces the current primary to step down and allows a secondary to be promoted to primary.
28. What is the default number of votes allowed for each member in a MongoDB replica set?
- A) 1
- B) 2
- C) 3
- D) 0
Answer: A) 1
Explanation: By default, each replica set member has 1 vote in the election process to become primary.
29. Which of the following is a characteristic of a MongoDB replica set member that is an “arbiter”?
- A) Stores a copy of data
- B) Handles read and write operations
- C) Participates in elections but does not store data
- D) Stores a subset of the data
Answer: C) Participates in elections but does not store data
Explanation: An arbitrator participates in replica set elections but does not store any data.
30. Which command can be used to check the replication lag in a MongoDB replica set?
- A) rs.replicaLag()
- B) rs.status()
- C) db.replicaLag()
- D) db.stats()
Answer: B) rs.status()
Explanation: The rs.status() command provides information about replication status and the lag between the primary and secondary nodes.
31. Which of the following is a reason to use MongoDB replication?
- A) To speed up the write operations
- B) To increase data availability and redundancy
- C) To decrease disk usage
- D) To improve the query performance
Answer: B) To increase data availability and redundancy
Explanation: MongoDB replication is used to provide high availability, data redundancy, and fault tolerance.
32. What happens when a secondary node falls behind the primary in terms of data replication?
- A) It will automatically catch up when the primary is restarted
- B) It will be permanently out of sync
- C) It will remain in sync until manually fixed
- D) It will eventually catch up as it replicates the missing data
Answer: D) It will eventually catch up as it replicates the missing data
Explanation: A secondary node that falls behind will continue to replicate from the primary until it catches up.
33. In a MongoDB replica set, which node cannot handle read operations?
- A) Primary node
- B) Secondary node
- C) Arbiter node
- D) All nodes can handle read operations
Answer: C) Arbiter node
Explanation: An arbiter does not store data and therefore cannot handle read operations.
34. Which of the following MongoDB settings allows you to control how many replica set members must acknowledge a write operation before it is considered successful?
- A) writeConcern
- B) readConcern
- C) writePref
- D) readPref
Answer: A) writeConcern
Explanation: writeConcern controls how many replica set members must acknowledge a write operation before it is considered successful.
35. What does the rs.reconfig() command do in MongoDB?
- A) Adds a new replica set
- B) Reconfigures the settings of an existing replica set
- C) Removes a replica set member
- D) Starts replication
Answer: B) Reconfigures the settings of an existing replica set
Explanation: rs.reconfig() is used to modify the configuration of an existing replica set.
36. In MongoDB, what is the term used for the data that is replicated across all nodes in a replica set?
- A) Oplog
- B) Shard
- C) ReplicaLog
- D) BackupSet
Answer: A) Oplog
Explanation: The oplog (operations log) is the data that is replicated across all nodes in a replica set to keep them in sync.
37. Which of the following operations is NOT replicated in a MongoDB replica set?
- A) Write operations
- B) Read operations
- C) Index operations
- D) Administrative commands
Answer: D) Administrative commands
Explanation: Administrative commands (e.g., database creation, user management) are not replicated in a MongoDB replica set.
38. What is the minimum number of voting members required in a MongoDB replica set for elections?
- A) 1
- B) 2
- C) 3
- D) 4
Answer: A) 1
Explanation: A MongoDB replica set requires at least 1 voting member to conduct elections.
39. Which of the following commands in MongoDB forces the primary node to step down and initiate an election?
- A) rs.stepDown()
- B) rs.forceStepDown()
- C) rs.step()
- D) rs.failover()
Answer: A) rs.stepDown()
Explanation: rs.stepDown() forces the current primary node to step down and triggers an election for a new primary.
40. Which of the following is NOT a feature of MongoDB replication?
- A) Automatic failover
- B) Data redundancy
- C) Load balancing
- D) Synchronous replication across all nodes
Answer: D) Synchronous replication across all nodes
Explanation: MongoDB replication is typically asynchronous, meaning there may be some lag between the primary and secondary nodes.
41. Which of the following best describes the nearest read preference in MongoDB?
- A) Reads from the nearest node, based on latency
- B) Reads only from the primary node
- C) Reads from the secondary node with the most recent data
- D) Reads from the node with the least number of operations
Answer: A) Reads from the nearest node, based on latency
Explanation: The nearest read preference directs read operations to the node with the lowest latency, regardless of whether it is primary or secondary.
42. What happens if you disable journaling in a MongoDB replica set?
- A) The replica set will still function normally
- B) Write operations will not be replicated
- C) Data durability may be compromised
- D) The system will only handle read operations
Answer: C) Data durability may be compromised
Explanation: Disabling journaling can affect data durability and increase the risk of data loss during failures.
43. What does a MongoDB replica set configuration with an even number of voting members result in?
- A) No primary node can be elected
- B) The replica set will automatically add a third voting member
- C) It may result in election ties, requiring an arbiter
- D) The replica set will work but with reduced reliability
Answer: C) It may result in election ties, requiring an arbiter
Explanation: Even numbers of voting members can lead to election ties, which is why an arbiter is often added.
44. Which MongoDB replica set status indicates that replication is catching up but has not yet fully synced?
- A) RECOVERING
- B) PRIMARY
- C) SECONDARY
- D) ARBITER
Answer: A) RECOVERING
Explanation: A node in the RECOVERING state is not yet fully synced and is catching up with the primary node.
45. What type of MongoDB replication setup is used to create a geographically distributed replica set?
- A) Sharded replica set
- B) Geospatial replica set
- C) Global replica set
- D) Multi-region replica set
Answer: D) Multi-region replica set
Explanation: A multi-region replica set allows for the deployment of replica set members in different geographic locations to ensure high availability and low-latency access.
46. Which command can you use to check the current replication lag in MongoDB?
- A) db.getReplicationStatus()
- B) rs.status()
- C) rs.replicationLag()
- D) db.replicationStatus()
Answer: B) rs.status()
Explanation: The rs.status() command provides replication status, including information about the replication lag.
47. What does MongoDB use to ensure consistency across replica set members?
- A) Write concern
- B) Read concern
- C) Consistency check
- D) Write preference
Answer: A) Write concern
Explanation: Write concern ensures that a write operation is acknowledged by a specific number of replica set members to guarantee consistency.
48. Which of the following MongoDB commands allows you to add a new member to a replica set?
- A) rs.add()
- B) rs.addMember()
- C) rs.insertMember()
- D) rs.reconfig()
Answer: A) rs.add()
Explanation: The rs.add() command is used to add a new member to an existing MongoDB replica set.
49. What happens if a secondary replica set member becomes too far behind the primary?
- A) It will be removed from the replica set automatically
- B) It will stay as secondary until manual intervention is performed
- C) It can be promoted to primary
- D) It will be permanently out of sync and require a full resync
Answer: D) It will be permanently out of sync and require a full resync
Explanation: If a secondary node falls too far behind, it may need to resync from the primary to catch up.
50. Which is the default write concern in MongoDB?
- A) Acknowledged by 1 node
- B) Acknowledged by all nodes
- C) No acknowledgment required
- D) Acknowledged by the primary node only
Answer: A) Acknowledged by 1 node
Explanation: The default write concern is w: 1, meaning the write operation needs to be acknowledged by just the primary node.
