This set of 30 multiple-choice questions (MCQs) covers key topics in C# database programming, including ADO.NET, Entity Framework, LINQ to SQL, CRUD operations, and using stored procedures. These MCQs are designed to enhance your knowledge of database connectivity, object-relational mapping (ORM), and data manipulation techniques in C#.
1. ADO.NET (SQL Server Connectivity)
Which of the following namespaces is used for ADO.NET functionality in C#?
A) System.Data
B) System.Data.SqlClient
C) System.Data.SqlServer
D) System.Data.OleDb
What class is used to establish a connection to a SQL Server database in ADO.NET?
A) SqlCommand
B) SqlDataAdapter
C) SqlConnection
D) SqlDataReader
What is the purpose of a SqlCommand object in ADO.NET?
A) To execute a query against the database
B) To manage database connections
C) To store data temporarily
D) To map database tables to objects
Which of the following methods is used to execute a query and retrieve a result set in ADO.NET?
A) ExecuteReader()
B) ExecuteNonQuery()
C) ExecuteScalar()
D) ExecuteQuery()
What type of data does SqlDataReader return in ADO.NET?
A) Structured data like tables and columns
B) Simple single value
C) Single record only
D) XML data
2. Entity Framework and LINQ to SQL
Which of the following is an ORM (Object-Relational Mapping) framework in C#?
A) ADO.NET
B) Entity Framework
C) SQL Server
D) LINQ to XML
What is the primary advantage of using Entity Framework in C#?
A) Direct SQL scripting for data manipulation
B) Reduces the need for manual SQL query writing
C) Allows only non-relational databases
D) Provides better performance for large datasets
Which feature of LINQ to SQL allows for writing SQL queries directly in C# code?
A) SQL Queries
B) LINQ Queries
C) Entity Queries
D) Stored Procedures
In Entity Framework, which class is used to represent the database context?
A) DbSet
B) DbContext
C) DataContext
D) SqlContext
How does Entity Framework handle database schema changes over time?
A) By manually updating the database schema
B) By using migrations
C) By removing old database records
D) By reinitializing the database
3. CRUD Operations
What does CRUD stand for in database programming?
A) Create, Read, Update, Delete
B) Create, Read, Undo, Delete
C) Connect, Retrieve, Use, Delete
D) Create, Register, Undo, Delete
Which ADO.NET method is used to insert data into a database?
A) ExecuteReader()
B) ExecuteNonQuery()
C) ExecuteScalar()
D) InsertData()
What type of SQL command is typically used to update existing records in a database?
A) SELECT
B) INSERT
C) UPDATE
D) DELETE
In Entity Framework, which method is used to add a new record to a database table?
A) Add()
B) Insert()
C) Create()
D) SaveChanges()
What does the Delete method do in Entity Framework?
A) Marks an entity as deleted without removing it
B) Removes the entity from the DbSet and the database
C) Deletes all records in a table
D) Temporarily disables an entity in the database
4. Using Stored Procedures with C#
Which ADO.NET class is used to execute a stored procedure?
A) SqlCommand
B) SqlConnection
C) SqlDataReader
D) SqlAdapter
What is the purpose of a stored procedure in SQL Server?
A) To optimize performance by reducing network traffic
B) To store data persistently
C) To perform one or more SQL operations as a unit
D) To execute a query against a specific table
How do you pass parameters to a stored procedure in ADO.NET?
A) By using the AddWithValue method of SqlCommand
B) By using a SqlParameter object
C) By appending parameters to the query string
D) By using the ExecuteParameter method
Which of the following is used to call a stored procedure in C# using Entity Framework?
A) ExecuteStoredProcedure()
B) SqlQuery()
C) ExecuteSqlCommand()
D) FromSqlRaw()
What type of parameter is used for output values in a stored procedure?
A) Input Parameter
B) Output Parameter
C) Return Parameter
D) Both B and C
5. Advanced Topics and Optimization
Which of the following can improve performance when retrieving large data sets from a database in C#?
A) Using stored procedures
B) Using asynchronous queries
C) Using ExecuteScalar()
D) Using direct database connections
Which ADO.NET method is used to retrieve the first column of the first row in a result set?
A) ExecuteReader()
B) ExecuteScalar()
C) ExecuteNonQuery()
D) ExecuteQuery()
What is the purpose of a DbContext in Entity Framework?
A) To manage database connections
B) To define entity mappings to database tables
C) To manage entity state changes
D) All of the above
How can you retrieve a single record from a database using Entity Framework?
A) By using FirstOrDefault()
B) By using Find()
C) By using Single()
D) All of the above
What is the default behavior when a record is deleted using Entity Framework?
A) The record is only deleted in memory
B) The record is marked as deleted but remains in the database
C) The record is removed from both the DbSet and the database
D) The record is not affected unless changes are saved
Which of the following commands is used to update the database schema using Entity Framework migrations?
A) Update-Database
B) Migrate-Database
C) Create-Database
D) Apply-Migration
What does the ExecuteReader() method return in ADO.NET?
A) A scalar value
B) A DataSet
C) A DataReader
D) A DataTable
Which method is used to execute a command in Entity Framework to perform a database update?
A) ExecuteCommand()
B) SaveChanges()
C) CommitChanges()
D) UpdateData()
In ADO.NET, which class is used to retrieve data and fill a DataSet?
A) SqlDataReader
B) SqlCommand
C) SqlDataAdapter
D) SqlConnection
In LINQ to SQL, how do you execute a query that returns multiple records?
A) By using ToList()
B) By using SingleOrDefault()
C) By using FirstOrDefault()
D) By using First()
Answer Key
Qno
Answer (Option with Text)
1
B) System.Data.SqlClient
2
C) SqlConnection
3
A) To execute a query against the database
4
A) ExecuteReader()
5
A) Structured data like tables and columns
6
B) Entity Framework
7
B) Reduces the need for manual SQL query writing
8
B) LINQ Queries
9
B) DbContext
10
B) By using migrations
11
A) Create, Read, Update, Delete
12
B) ExecuteNonQuery()
13
C) UPDATE
14
A) Add()
15
B) Removes the entity from the DbSet and the database
16
A) SqlCommand
17
C) To perform one or more SQL operations as a unit
18
B) By using a SqlParameter object
19
D) FromSqlRaw()
20
D) Both B and C
21
B) Using asynchronous queries
22
B) ExecuteScalar()
23
D) All of the above
24
D) All of the above
25
C) The record is removed from both the DbSet and the database