Viva
Viva
1. Basics of Scala
applyFunction(square, 5) // Output: 25
lazy val x = {
println("Computing x")
10
• lazy val: Evaluated only when accessed for the first time.
Q: What are traits, and how do they differ from abstract classes?
A: Traits are like interfaces with the ability to include concrete methods. Unlike abstract classes, traits
can be mixed into multiple classes. Abstract classes can have constructors, but traits cannot.
2. Collections
• Immutable collections cannot be modified after creation (e.g., List). Instead, they create new
collections when operations are performed.
val result = list.flatMap(x => List(x, x * 10)) // Output: List(1, 10, 2, 20, 3, 30)
Concurrency and Parallelism in Scala
1. Basics
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
Thread.sleep(1000)
42
futureResult.onComplete {
2. Actors
import akka.actor._
actor ! "Hello"
3. Akka Framework
4. Advanced Concepts
• Future: Represents a computation that is being executed and whose result will be available in
the future.
• Promise: A writable, one-time container that completes a Future. It allows the user to explicitly
fulfill or fail the associated Future.
Example:
import scala.concurrent.Promise
future.map(println) // Prints 42
1. Basics of MongoDB
"_id": "1",
"name": "Alice",
"age": 25,
2. CRUD Operations
Q: How do you insert a document into a MongoDB collection?
A:
Documents can be inserted using insertOne or insertMany. Example:
3. Indexes
• Single field index: Indexes only one field (e.g., { name: 1 }).
4. Aggregation
• $group: Groups documents and performs aggregate calculations like sum or average.
Example:
db.collection.aggregate([
]);
db.orders.aggregate([
$lookup: {
from: "products",
localField: "productId",
foreignField: "_id",
as: "productDetails"
]);
Q: Explain the role of the primary and secondary nodes in a MongoDB replica set.
A:
• Secondary nodes: Replicate data from the primary and handle read operations when configured.
1. Introduction to Neo4j
Example:
MATCH (p:Person)-[:FRIENDS_WITH]->(f:Person)
• Create a node:
• Create a relationship:
• CREATE (a)-[:FRIENDS_WITH]->(b);
3. Graph Traversals
RETURN p;
MATCH (p:Person)-[:FRIENDS_WITH]->(f:Person)
Q: What are graph algorithms, and how are they used in Neo4j?
A:
Graph algorithms analyze relationships and structures in the graph to uncover insights like shortest
paths, centrality, and community detection. They are used in use cases like recommendation systems
and fraud detection.
Example:
The PageRank algorithm measures the importance of nodes in a network.