Data Access MCQs ASP.NET

What is the primary data access technology used for relational databases in ASP.NET?
a. ADO.NET
b. Entity Framework
c. Dapper
d. NHibernate

Answer: a. ADO.NET

Which class in ADO.NET provides a way to execute SQL queries and commands against a SQL Server database?
a. SqlCommand
b. SqlConnection
c. SqlDataAdapter
d. SqlDataReader

Answer: a. SqlCommand

Which ADO.NET class is used to establish a connection to a SQL Server database?
a. SqlConnection
b. SqlCommand
c. SqlDataAdapter
d. SqlDataReader

Answer: a. SqlConnection

What is the purpose of the SqlDataAdapter class in ADO.NET?
a. To fill a DataSet or DataTable with data from a database
b. To execute SQL commands against a database
c. To open and manage database connections
d. To read data from a database

Answer: a. To fill a DataSet or DataTable with data from a database

Which method of the SqlCommand class is used to execute a query that returns a single value?
a. ExecuteScalar()
b. ExecuteNonQuery()
c. ExecuteReader()
d. ExecuteXmlReader()

Answer: a. ExecuteScalar()

What is the purpose of the DataSet class in ADO.NET?
a. To hold data in memory in a tabular format
b. To execute SQL commands against a database
c. To manage database connections
d. To perform transactions

Answer: a. To hold data in memory in a tabular format

Which ADO.NET class is used to read data from a database in a forward-only, read-only manner?
a. SqlDataReader
b. SqlDataAdapter
c. SqlConnection
d. SqlCommand

Answer: a. SqlDataReader

What is the primary purpose of the Entity Framework (EF) in ASP.NET?
a. To provide an Object-Relational Mapping (ORM) framework
b. To execute raw SQL queries
c. To manage database connections
d. To fill DataSets and DataTables

Answer: a. To provide an Object-Relational Mapping (ORM) framework

Which method of the Entity Framework is used to save changes to the database?
a. SaveChanges()
b. SubmitChanges()
c. UpdateDatabase()
d. Commit()

Answer: a. SaveChanges()

What is a DbContext in Entity Framework?
a. A class that manages database connections and transactions
b. A class that represents a session with the database
c. A class that maps SQL queries to C# objects
d. A class that represents a database schema

Answer: b. A class that represents a session with the database

How can you define a one-to-many relationship in Entity Framework?
a. By using navigation properties
b. By using ForeignKey attributes
c. By configuring relationships in OnModelCreating
d. By defining DbSet properties

Answer: a. By using navigation properties

Which class in Entity Framework represents a collection of entities in the context?
a. DbSet<T>
b. DbContext
c. EntitySet
d. EntityCollection

Answer: a. DbSet<T>

What is the purpose of DataAnnotations in Entity Framework?
a. To provide metadata for model properties
b. To configure database connections
c. To execute SQL queries
d. To manage transactions

Answer: a. To provide metadata for model properties

Which attribute is used to specify a primary key in an Entity Framework model?
a. [Key]
b. [PrimaryKey]
c. [Identity]
d. [DatabaseGenerated]

Answer: a. [Key]

How can you perform raw SQL queries in Entity Framework?
a. By using the Database.SqlQuery<T> method
b. By using the DbContext.ExecuteSqlCommand method
c. By using the DbSet.SqlQuery method
d. By using the SqlCommand class directly

Answer: a. By using the Database.SqlQuery<T> method

What is the role of LINQ in data access with Entity Framework?
a. To query and manipulate data in a strongly-typed manner
b. To execute raw SQL commands
c. To manage database connections
d. To handle transactions

Answer: a. To query and manipulate data in a strongly-typed manner

Which Entity Framework approach allows for database schema updates based on model changes?
a. Code First Migrations
b. Database First Approach
c. Model First Approach
d. Code First Approach

Answer: a. Code First Migrations

What is the purpose of the Repository pattern in data access?
a. To abstract data access logic and provide a cleaner interface
b. To directly execute SQL commands
c. To manage database connections
d. To handle transactions

Answer: a. To abstract data access logic and provide a cleaner interface

How do you handle concurrency issues with Entity Framework?
a. By using ConcurrencyCheck or RowVersion attributes
b. By locking database tables
c. By using transactions
d. By retrying operations manually

Answer: a. By using ConcurrencyCheck or RowVersion attributes

Which method is used to update or insert a record in Entity Framework?
a. AddOrUpdate()
b. SaveChanges()
c. ExecuteNonQuery()
d. UpdateDatabase()

Answer: b. SaveChanges()

What is Dapper in the context of ASP.NET data access?
a. A micro ORM that provides fast and simple data access
b. A full-featured ORM with code-first support
c. A library for managing database connections
d. A tool for generating SQL queries

Answer: a. A micro ORM that provides fast and simple data access

Which method in Dapper is used to execute a SQL command and return the number of affected rows?
a. Execute()
b. Query()
c. QuerySingle()
d. ExecuteScalar()

Answer: a. Execute()

What is the purpose of the TransactionScope class in ADO.NET?
a. To manage transactions across multiple database operations
b. To execute SQL commands in a batch
c. To manage database connections
d. To handle concurrency issues

Answer: a. To manage transactions across multiple database operations

Which method of SqlCommand is used to execute a SQL query and return a DataReader?
a. ExecuteReader()
b. ExecuteNonQuery()
c. ExecuteScalar()
d. ExecuteXmlReader()

Answer: a. ExecuteReader()

What is the primary difference between Entity Framework and Dapper?
a. Entity Framework is a full ORM, while Dapper is a micro ORM
b. Dapper supports multiple databases, while Entity Framework supports only SQL Server
c. Entity Framework is faster than Dapper
d. Dapper supports LINQ queries, while Entity Framework does not

Answer: a. Entity Framework is a full ORM, while Dapper is a micro ORM

Which class in ADO.NET is used to populate a DataTable or DataSet with data from a database?
a. SqlDataAdapter
b. SqlCommand
c. SqlConnection
d. SqlDataReader

Answer: a. SqlDataAdapter

How can you configure Entity Framework to use a SQL Server database?
a. By specifying the connection string in DbContext
b. By configuring settings in web.config
c. By using OnConfiguring method in DbContext
d. By adding a ConnectionString attribute to the model

Answer: c. By using OnConfiguring method in DbContext

What is Lazy Loading in the context of Entity Framework?
a. Automatically loading related data when it is accessed for the first time
b. Loading all related data in one query
c. Manually loading related data on demand
d. Using eager loading to improve performance

Answer: a. Automatically loading related data when it is accessed for the first time

Which method of DataTable allows you to filter rows based on a condition?
a. Select()
b. Find()
c. Filter()
d. Search()

Answer: a. Select()

How do you map a SQL Server table to a C# class in Entity Framework?
a. By defining a class with properties that match the table columns
b. By using SQL Server Management Studio (SSMS)
c. By configuring the table schema in web.config
d. By executing a SQL query to create the class

Answer: a. By defining a class with properties that match the table columns

What is the purpose of the Include() method in Entity Framework?
a. To eagerly load related entities
b. To exclude properties from serialization
c. To specify SQL query parameters
d. To limit the columns returned in a query

Answer: a. To eagerly load related entities

Which method of DataSet is used to accept changes made to the data?
a. AcceptChanges()
b. Update()
c. SaveChanges()
d. Commit()

Answer: a. AcceptChanges()

What is the function of Stored Procedures in data access?
a. To encapsulate SQL logic and improve performance
b. To manage database connections
c. To handle transactions
d. To automatically generate database schemas

Answer: a. To encapsulate SQL logic and improve performance

Which Entity Framework method is used to create a new database based on the model?
a. Database.EnsureCreated()
b. Database.Create()
c. Database.Initialize()
d. Database.Generate()

Answer: a. Database.EnsureCreated()

How can you handle database schema changes with Entity Framework Code First?
a. By using Code First Migrations
b. By manually updating the database schema
c. By re-creating the database
d. By configuring schema changes in web.config

Answer: a. By using Code First Migrations

What is Eager Loading in Entity Framework?
a. Loading all related entities in a single query
b. Loading related entities on demand
c. Loading related entities when accessed for the first time
d. Using lazy loading for performance

Answer: a. Loading all related entities in a single query

Which class in ADO.NET provides a way to interact with an XML database?
a. XmlDataDocument
b. XmlReader
c. XmlWriter
d. XmlSchema

Answer: a. XmlDataDocument

What does the DbSet<T> class represent in Entity Framework?
a. A collection of entities of type T
b. A single entity of type T
c. A database connection for type T
d. A query result of type T

Answer: a. A collection of entities of type T

Which method is used to execute a SQL command and return a single row of data in Entity Framework?
a. FirstOrDefault()
b. Single()
c. SingleOrDefault()
d. First()

Answer: c. SingleOrDefault()

What is the purpose of Database First approach in Entity Framework?
a. To generate model classes based on an existing database
b. To create a new database based on model classes
c. To configure database connections
d. To manage transactions

Answer: a. To generate model classes based on an existing database

How do you configure the connection string for Entity Framework in ASP.NET Core?
a. By using IConfiguration in Startup.cs
b. By setting it in web.config
c. By defining it in appsettings.json
d. By specifying it in DbContext

Answer: a. By using IConfiguration in Startup.cs

Which method in SqlCommand is used to execute a command and return the results as a list of objects?
a. ExecuteReader()
b. ExecuteScalar()
c. ExecuteXmlReader()
d. ExecuteNonQuery()

Answer: a. ExecuteReader()

What does AsNoTracking() do in Entity Framework?
a. Disables change tracking for read-only queries
b. Enables change tracking for updated entities
c. Tracks changes for entities in a query
d. Prevents entities from being read

Answer: a. Disables change tracking for read-only queries

Which class in Entity Framework is used to configure the entity mappings and relationships?
a. ModelBuilder
b. DbSet
c. DbContext
d. EntityTypeConfiguration

Answer: a. ModelBuilder

What is the purpose of Projection in LINQ queries?
a. To shape the data into a specific format or type
b. To filter data based on criteria
c. To group data into collections
d. To sort data based on properties

Answer: a. To shape the data into a specific format or type