Questions Answers (OOPs, C#, DB Interview)
Questions Answers (OOPs, C#, DB Interview)
Questions Answers (OOPs, C#, DB Interview)
HAVING clause
A: 1. WHERE clause is used for filtering rows and it applies on each and every row, while HAVING clause is used to filter groups
in SQL.
2. When WHERE and HAVING clauses are used together in a SELECT query with aggregate function, WHERE clause is applied
first on individual rows and only rows which pass the condition are included for creating groups. Once a group is created, HAVING
clause is used to filter groups based upon the condition specified.
An abstract class can have all access modifiers for member An interface is not allowed to have access modifiers for
declaration of functions, subs, and properties. member declaration of functions, subs, properties, etc. All the
members are assumed as implicitly public.
A class can at most use only one abstract class. A class may inherit any number of interfaces.
Multiple inheritance is not supported in abstract class. An interface may support multiple inheritance.
Members of an abstract class cannot be static unless it’s a Members of an interface cannot be static.
Complete Member.
An abstract class has pre-defined fields and constraints. Fields cannot be defined in interfaces.
It can have both abstract and non-abstract methods. It can only have abstract methods.
Cookie- Cookie is a small and an important part of ASP.NET. In this store user information, session and application. It can be
created constant and temporary and they work with browser request. Cookies are stored on client side. The server can read
cookies and abstract data.
Types Cookie:- 1) Persistence- This type of cookie works with Date and time. 2)Non-Persistence- This is a temporary cookie. It
is created with access application and discards the close application.
Control state- Control state technique is developed to maintain data work properly in order. We can use view state but
suppose view state is disabled by the user, the control will not work as expected. For expected results of the control we have to
use Control State. In application, the Viewstate is by default true. Sometimes we need to use custom control to manage
application properly.
Hidden Field- Hidden fields are used to store value to client side. Hidden field is not displayed on the browser, but it works on
a request.
Viewstate- Viewstate is a very useful client side property. It is used for page level state management. Viewstate stores any
type of data and used for sending and receiving information.
Query String- Query string stores the value in URL.
SessionState ViewState
If you want to store information that you want to access If you want to store information that you want to access
on different pages then you can use SessionState. only with in the page(same page) then you can use
ViewState.
Data stored in the SessionState will be cleared when the Data stored in the ViewState will send back to the server
session expired(usually after 20 min of inactivity when when postback action performed (no expiration).
using session inproc).
SessionState is used to save the user specific data like ViewState is used primarily by Server controls to retain
UserID, User Role, etc state only on pages that post data back to themselves. And
when you want to save the page level data you can use
ViewState
Q: What is the base class in .net from which all the classes are derived from?
A: System.Object
Consumes more memory during concatenation Consumes less memory during concatenation
Overrides equals() and hashcode() methods of the object Doesn't override the equals() and hashcode() methods of the
class object class
eg: eg:
All the reference types are allocated on the heap memory. All the value types are allocated on stack memory.
Allocation of large reference type is cheaper than allocation of Allocation and deallocation is cheaper in value type as
large value type. compared to reference type.
Class has limitless features. Struct has limited features.
Class is generally used in large programs. Struct are used in small programs.
Classes can contain constructor or destructor. Structure does not contain constructor or destructor.
Classes used new keyword for creating instances. Struct can create an instance, without new keyword.
A Class can inherit from another class. A Struct is not allowed to inherit from another struct or class.
The data member of a class can be protected. The data member of struct can’t be protected.
Function member of the class can be virtual or abstract. Function member of the struct cannot be virtual or abstract.
Two variables of class can contain the reference of the same Each variable in struct contains its own copy of data(except in
object and any operation on one variable can affect another ref and out parameter variable) and any operation on one
variable. variable can not affect another variable.
Class :-
In OOP languages it is mandatory to create a class for representing data.
A class is a blueprint of an object that contains variables for storing data and functions to perform operations on the data.
A class will not occupy any memory space and hence it is only a logical representation of data.
To create a class, you simply use the keyword "class" followed by the class name:
Ex:- Class Employee
{
Object :-
"An object is an instance of a class"
A class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for
the class, that is called an object.
When an object is created using the new operator, memory is allocated for the class in the heap, the object is called an instance
and its starting address will be stored in the object in stack memory.
When an object is created without the new operator, memory will not be allocated in the heap, in other words an instance will not
be created and the object in the stack contains the value null.
When an object contains null, then it is not possible to access the members of the class using that object.
Syntax :- Employee objEmp = new Employee();
Abstraction:-
Abstraction is "To represent the essential feature without representing the background details."
Abstraction lets you focus on what the object does instead of how it does it.
Abstraction provides you a generalized view of your classes or objects by providing relevant information.
Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable
manner.
Real-world Example of Abstraction
Suppose you have an object Mobile Phone.
Abstraction means putting all the variables and methods in a class that are necessary.
For example: Abstract class and abstract method.
Abstraction is a common thing.
Encapsulation:-
Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.
Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that
object.
Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating
members and functions.
Encapsulation means hiding the internal details of an object, in other words how an object does something.
Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.
Encapsulation is a technique used to protect the information in an object from another object.
Hide the data for security such as making the variables private, and expose the property to access the private data that will
be public.
Inheritance:-
Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing
us to extend an existing class.
The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code and it is possible by
using inheritance.
Class B:A
{
}
Class C:A
{
}
Class D:A
{
}
Multilevel inheritance:-
When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.
Ex:- class A
{
}
Class B:A
{
}
Class C:B
{
}
1. C# does not support multiple inheritances of classes, the same thing can be done using interfaces.
2. Private members are not accessed in a derived class when one class is derived from another.
Polymorphism:-
Polymorphism is a Greek word meaning "One name many forms". In other words, one object has many forms or has one
name with multiple functionalities. "Poly" means many and "morph" means forms. Polymorphism provides the ability to class
multiple implementations with the same name. It is one principle concept in Object Oriented Programming after encapsulation and
inheritance.
There are basically the following two types of polymorphism in C#:
Method overloading:-
the method / function has the same name but different signatures. It is also known as Compile Time Polymorphism because the
decision of which method is to be called is made at compile time. Overloading is the concept in which method names are the
same with a different set of parameters.
Here the compiler checks the number of parameters passed and the type of parameter and make the decision of which method to
call and it throw an error if no matching method is found.
In the following example, the class has two methods with the same name "Add" but with different input parameters (the first
method has three parameters and the second method has two parameters).
1. public class TestData
2. {
3. public int Add(int a, int b, int c)
4. {
5. return a + b + c;
6. }
7. public int Add(int a, int b)
8. {
9. return a + b;
10. }
11. }
12. class Program
13. {
14. static void Main(string[] args)
15. {
16. TestData dataClass = new TestData();
17. int add2 = dataClass.Add(45, 34, 67);
18. int add1 = dataClass.Add(23, 34);
19. }
20. }
Operator overloading:-
The compiler requires an Area() method and it compiles successfully but the right version of the Area() method is not being
determined at compile time but determined at runtime. Finally the overriding methods must have the same name and signature
(number of parameters and type), as the virtual or abstract method defined in the base class method and that it is overriding in the
derived class.
A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The
compiler hides the function or method of the base class. This concept is known as shadowing or method hiding.
Q:What is LINQ?
A: The full form of LINQ is Language Integrated Query . It was introduced in .NET 3.5 and we can use it in C# or VB.NET based
on our requirements. LINQ can be used to query data from different data sources such as Collections, Generics, XML Documents,
ADO.NET Datasets, SQL Databases, Web-service etc. and It connects UI and data access layer.
Linq, as it appears in the System.Linq namespace and its a set of extension methods that allow you to query collections directly in
code. LINQ is an acronym for "Language Integrated Query". LINQ adds a rich, standardized query syntax in .NET programming
languages that allows developers to interact with any type of data.
Also we have benefit of intellisense as linq query is written in code behind and moreover it has full type checking at compile time.
So this helps us to avoid runtime errors as we can catch any error in compile time itself.
Advantages of LINQ
Following are the advantages of using linq in our applications.
Disadvantages of LINQ
Following are the drawbacks of using linq in our applications.
1. Since it is written in the code so we cannot make use of Cache execution plan which is a SQL feature as we do in stored
procedure.
2. Writing complex queries in LINQ is a bit tedious as compared to SQL.
3. If the query is not written properly then performance is degraded.
4. We have to build the project and deploy the DLL every time some change is done in the query.
1) Application Start - The life cycle of an ASP.NET application starts when a request is made by a user. This request is to the
Web server for the ASP.Net Application. This happens when the first user normally goes to home page for the application for the
first time. During this time, there is a method called Application_start which is executed by the web server. Usually, in this method,
all global variables are set to their default values.
2) Object creation - The next stage is the creation of the HttpContext, HttpRequest & HttpResponse by the web server. The
HttpContext is just the container for the HttpRequest and HttpResponse objects. The HttpRequest object contains information
about the current request, including cookies and browser information. The HttpResponse object contains the response that is sent
to the client.
3) HttpApplication creation - This object is created by the web server. It is this object that is used to process each subsequent
request sent to the application. For example, let's assume we have 2 web applications. One is a shopping cart application, and the
other is a news website. For each application, we would have 2 HttpApplication objects created. Any further requests to each
website would be processed by each HttpApplication respectively.
4) Dispose - This event is called before the application instance is destroyed. During this time, one can use this method to
manually release any unmanaged resources.
aàalĺlllllĺlď5) Application End - This is the final part of the application. In this part, the application is finally unloaded from memory.
● (INNER) JOIN: Returns records that have matching values in both tables
● LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
● RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
● FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
Q:What is Normalization?
A: Normalization is a database design technique which organizes tables in a manner that reduces redundancy and dependency
of data.It divides larger tables to smaller tables and links them using relationships.
Theory of Data Normalization in SQL is still being developed further. For example, there are discussions even on 6th Normal
Form. However, in most practical applications, normalization achieves its best in 3rd Normal Form. The evolution of Normalization
theories is illustrated below
Assume a video library maintains a database of movies rented out. Without any normalization, all information is stored in one table
as shown below.
Here you see Movies Rented column has multiple values.
1NF Example
What is a KEY?
A KEY is a value used to identify a record in a table uniquely. A KEY could be a single column or combination of multiple columns
Note: Columns in a table that are NOT used to identify a record uniquely are called non-key columns.
What is a Primary Key?
A composite key is a primary key composed of multiple columns used to identify a record uniquely
In our database, we have two people with the same name Robert Phil, but they live in different places.
Hence, we require both Full Name and Address to identify a record uniquely. That is a composite key.
● Rule 1- Be in 1NF
● Rule 2- Single Column Primary Key
It is clear that we can't move forward to make our simple database in 2nd Normalization form unless we partition the table above.
Database - Foreign Key
Foreign Key references the primary key of another Table! It helps connect your Tables
● A foreign key can have a different name from its primary key
● It ensures rows in one table have corresponding rows in another
● Unlike the Primary key, they do not have to be unique. Most often they aren't
● Foreign keys can be null even though primary keys can not
Why do you need a foreign key?
Suppose an idiot inserts a record in Table B such as
You will only be able to insert values into your foreign key that exist in the unique key in the parent table. This helps in referential
integrity.
The above problem can be overcome by declaring membership id from Table2 as foreign key of membership id from Table1
Now, if somebody tries to insert a value in the membership id field that does not exist in the parent table, an error will be shown!
What are transitive functional dependencies?
A transitive functional dependency is when changing a non-key column, might cause any of the other non-key columns to change
Consider the table 1. Changing the non-key column Full Name may change Salutation.
● Rule 1- Be in 2NF
● Rule 2- Has no transitive functional dependencies
To move our 2NF table into 3NF, we again need to again divide our table.
3NF Example
We have again divided our tables and created a new table which stores Salutations.
There are no transitive functional dependencies, and hence our table is in 3NF
In Table 3 Salutation ID is primary key, and in Table 1 Salutation ID is foreign to primary key in Table 3
EXAMPLE
public static class MyStaticClass
{
public static int myStaticVariable = 0;
public static void MyStaticMethod()
{
Console.WriteLine("This is a static method.");
}
public static int MyStaticProperty { get; set; }
}
class Program
{
Static void Main(string[]args)
{
Console.WriteLine(MyStaticClass.myStaticVariable); MyStaticClass.MyStaticMethod();
MyStaticClass.MyStaticProperty=100; Console.WriteLine(MyStaticClass.MyStaticProperty);
}
}
OUTPUT:
0
This is a static method.
100
In the above example, MyStaticClass is a static class with static variable, method and property. All the static members can be
access using class Name without creating an object of a class e.g. MyStaticClass.MyStaticMethod().
It is also possible to have static members in non-static classes just like a normal class. You can instantiate non static classes
using the new keyword as usual. However, the instance variable can only access the non-static methods and variables, it cannot
access the static methods and variables.
For example, consider the following myNonStaticClass with mix of static and non-static methods:
public class MyNonStaticClass
{
private static int myStaticVariable = 0;
public static void MyStaticMethod()
{
Console.WriteLine("This is static method.");
}
public void myNonStaticMethod()
{
Console.WriteLine("Non-static method");
}
}
In the above example, MyNonStaticClass can be instantiated and access the non-static members. However,
you cannot access static members. The following figure shows the debug view.
C# Static Constructor
A static or non-static class can have a static constructor without any access modifiers like public, private,
protected, etc.
A static constructor in a non-static class runs only once when the class is instantiated for the first time.
A static constructor in a static class runs only once when any of its static members accessed for the first
time.
public static class MyStaticClass
{
static MyStaticClass()
{
Console.WriteLine("Inside static constructor.");
}
public static int myStaticVariable = 0;
public static void myStaticMethod()
{
Console.WriteLine("This is static method.");
}
public static int MyStaticProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyStaticClass.myStaticVariable = 100;
MyStaticClass.MyStaticProperty = 200;
MyStaticClass.myStaticVariable = 300;
MyStaticClass.MyStaticProperty = 400;
}
}
OUTPUT:
Inside static constructor.
In the above example, the static members was accessed multiple times. However, static constructor got
called only once when any of its static members was accessed for the first time.
OUTPUT:
Inside static constructor
In the above example, we instantiate MyNonStaticClass three times but the static constructor got called only once when it
instantiated for the first time.
Differance:
Authentication Authorization
Authentication confirms your identity to grant access to Authorization determines whether you are authorized to
the system. access the resources.
It is the process of validating user credentials to gain It is the process of verifying whether access is allowed or
user access. not.
It determines whether user is what he claims to be. It determines what user can and cannot access.
Authentication usually requires a username and a Authentication factors required for authorization may
password. vary, depending on the security level.
Authentication is the first step of authorization so always Authorization is done after successful authentication.
comes first.
For example, students of a particular university are For example, authorization determines exactly what
required to authenticate themselves before accessing information the students are authorized to access on the
the student link of the university’s official website. This is university website after successful authentication.
called authentication.
Q:What is CLI?
A: A command-line interface or command language interpreter (CLI), also known as command-line user interface, console user
interface[1] and character user interface (CUI), is a means of interacting with a computer program where the user (or client) issues
commands to the program in the form of successive lines of text (command lines). A program which handles the interface is called
a command language interpreter or shell (computing).
Q. What is Serialization?
A: Serialization is the process of bringing an object into a form that it can be written on stream. It's the process of
converting the object into a form so that it can be stored on a file, database, or memory; or, it can be transferred
across the network. Its main purpose is to save the state of the object so that it can be recreated when needed.
Q. What is Deserialization ?
A: As the name suggests, deserialization is the reverse process of serialization. It is the process of getting back the
serialized object so that it can be loaded into memory. It resurrects the state of the object by setting properties, fields
etc.
Types :-
1)Binary Serialization
2)XML Serialization
3)JSON Serialization
Ref Out
The parameter or argument must be initialized first It is not compulsory to initialize a parameter or argument
before it is passed to ref. before it is passed to an out.
It is not required to assign or initialize the value of a A called method is required to assign or initialize a value
parameter (which is passed by ref) before returning to of a parameter (which is passed to an out) before
the calling method. returning to the calling method.
Passing a parameter value by Ref is useful when the Declaring a parameter to an out method is useful when
called method is also needed to modify the pass multiple values need to be returned from a function or
parameter. method.
It is not compulsory to initialize a parameter value before A parameter value must be initialized within the calling
using it in a calling m weethod. method before its use.
When we use REF, data can be passed bi-directionally. When we use OUT data is passed only in a
unidirectional way (from the called method to the caller
method).
Namespace
Namespaces is a logical group of related classes that can be used by any other language targeting the
Microsoft .Net framework . It is more used for logical organization of your classes. Namespaces are a way of
grouping type names and reducing the chance of name collisions.
The fully qualified name of a class is constructed by concatenating the names of all the namespaces that
contain the type. For e.g. the fully qualified name of the TextBox class is System.Windows.Forms.TextBox .
That means TextBox class is contained in the Forms namespace that is contained in the Windows
namespace that is contained in the root System namespace.
Assembly
An assembly is a collection of types and resources that are built to work together and form a logical unit of
functionality. It is an Output Unit, that is .exe or .dll file. It is a unit of Deployment and a unit of versioning and
also it contain MSIL (Microsoft Intermediate Language) code. Assemblies are self describing, it contains all
the metadata about the modules, types, and other elements in the form of a manifest.
Assemblies are of two types: Private and Shared Assemblies.
Private Assemblies
Private assembly is intended only for one application. The files of that assembly must be placed in the same
folder of the application.
Shared Assemblies
Shared assembly is to be made into a Shared Assembly, then the naming conventions are very strict since it
has to be unique across the entire system
Ans:- List - An array list that supports generic types and enforces type-safety. Since it is non-contiguous, it
can grow in size without re-allocating memory for the entire list. This is the more commonly used list
collection.
Hash table - A Hashtable is a collection of key/value pairs that are arranged based on the hash code of the
key. Or in other words, a Hashtable is used to create a collection which uses a hash table for storage. It is
the non-generic type of collection which is defined in
Hashtable is basically a List with no possibility of duplicates (and better performance in some scenarios)
Ans.
The is operator is used to check if the run-time type of an object is compatible with the
given type or not whereas as operator is used to perform conversion between compatible
reference types or Nullable types.
● The is operator is of boolean type whereas as operator is not of boolean type.
● The is operator returns true if the given object is of the same type whereas as operator
returns the object when they are compatible with the given type.
● The is operator returns false if the given object is not of the same type whereas as
operator return null if the conversion is not possible.
● The is operator is used for only reference, boxing, and unboxing conversions whereas as
operator is used only for nullable, reference and boxing conversions
Que. What is Page Load Sequence In Master Page And Is Child Pages?
Ans SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the
data in the table. If there is any violation between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints
apply to the whole table.
1. Declare Cursor: In this part we declare variables and return a set of values.
2. Open: This is the entering part of the cursor.
3. Fetch: Used to retrieve the data row by row from a cursor.
4. Close: This is an exit part of the cursor and used to close a cursor.
5. Deallocate: In this part we delete the cursor definition and release all the system resources associated with the
cursor.
Ans sql scalar functions: SQL scalar functions return value, based on the input value.
sql aggregate functions:SQL aggregate functions return a single value, calculated from
values in a column.
Ans
1) Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's
mandatory to assign a value to it. By default a const is static and we cannot change the value of a const
variable throughout the entire program.
2 Readonly is the keyword whose value we can change during runtime or we can assign it at run time but
only through the non-static constructor. Not even a method.
Ans:- Collection classes are specialized classes for data storage and retrieval. These classes provide support for
stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.
Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list
of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the
base class for all data types in C#.
Clustered Indexes:
1.The clustered indexes are indexes which are physically stored in order means it stores in ascending or
descending order in Database.
3.When primary key is created then clustered index has been automatically created in the table.
4.If table is under heavy data modifications the clustered indexes are preferable to use.
3.Foreign keys should be non clustered.When user wants to retrieve heavy data from fields other than primary
key the non clustered indexes are useful.
Ans: Generally SQL Server functions will return only one parameter value if we want to return multiple values
from function then we need to send multiple values in table format by using table valued functions.
@UserId INT
returns table as
return
Temporary tables are stored inside “tempdb,” which is a system database. Now, let’s take a look at how you can
use temporary data in a simple scenario.
Let’s create a temporary table that contains the name, age, and gender of all male students’ records from the
student table.
USE schooldb;
Take a look at the above query. We created a temporary table “#MaleStudents,” which stores names, ages, and
genders of all male students’ records from the student table. To define a temporary table, we use the INTO
statement after the SELECT statement. The name of a temporary table must start with a hash (#).
Ans : Definition:
FOR LOOP allows you to execute code repeatedly for a fixed number of times.
Syntax:
Syntax:
Difference:
FOR loop will execute code repeatedly as mintioned fixed number of times
CURSOR FOR LOOP will fetch and process/execute the code till end of the cursor data.
Overloaded constructors have the same name (name of the class) but the different number of
arguments. Depending upon the number and type of arguments passed, the corresponding
constructor is called.