Questions
Questions
INTERVIEW QUESTIONS
Page |2
Contents
1. TUPLES.................................................................................................................................................4
1.1 TUPLE VS ARRAY.........................................................................................................................4
2. VALUE TYPE AND REFERENCE TYPE....................................................................................................6
3. DELEGATES..........................................................................................................................................8
4. BOXING AND UNBOXING..................................................................................................................10
5. MANAGED AND UNMANAGED CODE...............................................................................................11
6. DIFF BETWEEN STRUCT AND CLASS..................................................................................................12
7. ENUM................................................................................................................................................14
8. CONSTANT VS READONLY.................................................................................................................16
9. MVC FILTERS.....................................................................................................................................17
10. ATTRIBUTE ROUTING IN MVC.......................................................................................................18
11. ACTION FILTERS TYPE IN MVC AND RETURN TYPE.......................................................................19
12. WHY MAIN METHOD SHOULD BE STATIC.....................................................................................20
13. TYPE OF CONSTRUCTORS IN CLASS EXPLAIN................................................................................21
14. STATE MANAGEMENT TECHNIQUES.............................................................................................22
15. SEALED CLASS AND PROPERTIES...................................................................................................23
16. DISPOSE AND FINALIZE.................................................................................................................25
17. DELEGATES AND EVENTS..............................................................................................................26
18. C# COLLECTIONS............................................................................................................................27
19. INTERFACE CAN HAVE SAME METHOD.........................................................................................29
20. THREADS.......................................................................................................................................30
21. WEB CONFIG VS MACHINE CONFIG VS APP CONFIG....................................................................35
22. SQL STATEMENT DDL, DML, TCL, DQL, DCL..................................................................................37
23. CLUSTERED AND NON-CLUSTERED INDEX....................................................................................38
24. COLUMN CONSTRAINTS IN SQL....................................................................................................39
25. NORMALIZATION IN SQL...............................................................................................................40
26. SELECTORS IN JQUERY AS WELL CSS.............................................................................................41
The element Selector............................................................................................................................41
The #id Selector....................................................................................................................................41
The .class Selector.................................................................................................................................41
27. FUNCTION VS STORE PROCEDURE IN SQL....................................................................................42
28. THROW EX VS THROW DIFFERENCE IN C#....................................................................................43
29. CAN WE HAVE MULTIPLE EXCEPTION BLOCK...............................................................................44
30. CUSTOM ATTRIBUTES IN C# .........................................................................................................45
Page |3
1. TUPLES
Tuple:
enum MyType {
case A, B, C
}
func foo() -> (MyType, Int, String) {
// ...
return (.B, 42, "bar")
}
let (type, amount, desc) = foo()
Array:
func foo() -> [Any] {
// ...
return [MyType.B, 42, "bar"]
}
reference type
public class Student{
public string StudentName { get; set; }
}
public class Program{
public static void ChangeReferenceType(Student std2){
std2.StudentName = "Steve";
}
public static void Main(){
Student std1 = new Student();
std1.StudentName = "Bill";
ChangeReferenceType(std1);
Console.WriteLine(std1.StudentName);
}
}
output:
Steve
Page |8
output:
Steve
Note: here we are passing string not the class
Page |9
3. DELEGATES
using System;
class TestDelegate {
static int num = 1;
Output:
Value of Num: 26
Value of Num: 130
P a g e | 11
boxing and unboxing is that boxing is a conversion from a value type to an object
type, whereas the term unboxing refers to the conversion from an object type to
a value type.
int i = 10;
object o = i; //performs boxing
https://www.tutorialsteacher.com/Content/images/articles/csharp/boxing.PNG
object o = 10;
int i = (int)o; //performs unboxing
int i = 10;
object o = i; // boxing
double d = (double)o; // runtime exception
int i = 10;
object o = i; // boxing
double d = (double)(int)o; // valid
https://www.tutorialsteacher.com/Content/images/articles/csharp/
unboxing.PNG
P a g e | 12
Managed code is the one that is executed by the CLR of the .NET framework while
unmanaged or unsafe code is executed by the operating system.
One major difference between structs and classes is that structs are value types,
while classes are reference types. This means that structs are copied by value
when they are passed around, while classes are copied by reference.
Class Structure
Classes are of reference types. Structs are of value types.
All the reference types are allocated on heap All the value types are allocated on stack
memory. memory.
Class is generally used in large programs. Struct are used in small programs.
Structure does not contain parameter less
constructor or destructor, but can contain
Classes can contain constructor or destructor.
Parameterized constructor or static
constructor.
Classes used new keyword for creating Struct can create an instance, with or
instances. without new keyword.
A Struct is not allowed to inherit from
A Class can inherit from another class.
another struct or class.
The data member of struct can’t be
The data member of a class can be protected.
protected.
Function member of the class can be virtual Function member of the struct cannot be
or abstract. virtual or abstract.
struct Employee {
public int id;
}
using System;
namespace VariablesDemo
{
internal class Example
{
int x = 10;
static void Main(string[] args)
{
Example e1 = new Example(); //e1 is Instance of class Example
Example e2 = e1; //e2 is Reference of class Example
Console.ReadKey();
}
}
}
7. ENUM
An enum is a special "class" that represents a group
of constants (unchangeable/read-only variables).
Example:
enum WeekDays
{
Monday, // 0
Tuesday, // 1
Wednesday, // 2
Thursday, // 3
Friday, // 4
Saturday, // 5
Sunday // 6
}
enum Categories
{
Electronics, // 0
Food, // 1
Automotive = 6, // 6
Arts, // 7
BeautyCare, // 8
Fashion // 9
}
8. CONSTANT VS READONLY
In C#, constant fields are created using const keyword. ReadOnly is a runtime
constant. Const is a compile time constant. The value of readonly field can be
changed.
using System.IO;
using System;
public class Program {
public const int VALUE = 10;
public readonly int value1;
Program(int value){
value1 = value;
}
public static void Main() {
Console.WriteLine(VALUE);
Program p1 = new Program(11);
Console.WriteLine(p1.value1);
}
}
P a g e | 18
9. MVC FILTERS
Attribute routing uses attributes to define routes. Attribute routing gives you
more control over the URIs in your web API. For example, you can easily create
URIs that describe hierarchies of resources.
Example:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "about", id = UrlParameter.Optional } //
Parameter defaults
);
P a g e | 20
Refer
https://www.c-sharpcorner.com/article/action-results-and-action-filters-in-asp-
net/
P a g e | 21
using system;
namespace demo {
class helloworld {
static void main(string[] args) {
console.writeline("hello world");
console.readkey();
}
}
}
P a g e | 22
Sealed classes are used to restrict the users from inheriting the class. A class can
be sealed by using the sealed keyword. The keyword tells the compiler that the
class is sealed, and therefore, cannot be extended. No class can be derived from
a sealed class.
// C# program to
// define Sealed Class
using System;
class Printer {
// Display Function for
// Dimension printing
public virtual void show()
{
Console.WriteLine("display dimension : 6*6");
}
// Display Function
public virtual void print()
{
Console.WriteLine("printer printing....\n");
}
}
// inheriting class
class LaserJet : Printer {
// Sealed Display Function
// for Dimension printing
sealed override public void show()
{
Console.WriteLine("display dimension : 12*12");
}
// Function to override
// Print() function
override public void print()
{
Console.WriteLine("Laserjet printer printing....\n");
}
}
// Officejet class cannot override show
// function as it is sealed in LaserJet class.
P a g e | 26
// Driver Class
class Program {
// Driver Code
static void Main(string[] args)
{
Printer p = new Printer();
p.show();
p.print();
The C# dispose() and finalize() methods are used to liberate the unmanaged
resources kept by an object. The dispose() method is described within the
IDisposable interface, but the finalize() method is described within the class
object.
Dispose () use to remove the unreachable object from memory or object which
has no references. Dispose (). This method call by Developer. Finalize () clean up
the object from memory which has no references.
BASIS FOR
COMPARISON DISPOSE( ) FINALIZE( )
The method dispose( ) is The method finalize( ) id
defined in the interface defined in java.lang.object
Defined IDisposable interface. class.
public void Dispose( ){ protected void finalize( ){
// Dispose code here // finalization code here
Syntax } }
The method finalize( ) is
The method dispose( ) is invoked by the garbage
Invoked invoked by the user. collector.
Method finalize( ) is used to
Method dispose( ) is used to free unmanaged resources
free unmanaged resources before the object is
Purpose whenever it is invoked. destroyed.
The method dispose( ) is to be The method finalize( ) is to be
implemented whenever there implemented for unmanaged
Implementation is a close( ) method. resources.
The method dispose( ) is The method finalize( ) is
Access specifier declared as public. declared as private.
The method dispose( ) is faster
and instantly disposes an The method finalize is slower
Action object. as compared to dispose
The method disposes( )
performs the instantaneous The method finalize( ) being
action hence, does not effect slower affects the
Performance the performance of websites. performance of the websites.
P a g e | 28
Defining an event is a two-step process. First, you need to define a delegate type
that will hold the list of methods to be called when the event is fired. Next, you
declare an event using the event keyword. To illustrate the event, we are creating
a console application. In this iteration, we will define an event to add that is
associated with a single delegate DelEventHandler.
18. C# COLLECTIONS
C# collection types are designed to store, manage and manipulate similar data
more efficiently. Data manipulation includes adding, removing, finding, and
inserting data in the collection. Collection types implement the following common
functionality:
Non-generic Generic
Syntax :
al.Add(d);
Example:
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
public void Hello()
{
Console.WriteLine("Hello to all");
}
}
20. THREADS
A thread is defined as the execution path of a program. Each thread defines a unique
flow of control. If your application involves complicated and time consuming operations,
then it is often helpful to set different execution paths or threads, with each thread
performing a particular job.
Threads are lightweight processes. One common example of use of thread is
implementation of concurrent programming by modern operating systems. Use of
threads saves wastage of CPU cycle and increase efficiency of an application.
The following table shows some most commonly used properties of the Thread
class −
CurrentCulture
Gets or sets the culture for the current thread.
CurrentPrinciple
Gets or sets the thread's current principal (for role-based security).
CurrentThread
Gets the currently running thread.
CurrentUICulture
P a g e | 33
Gets or sets the current culture used by the Resource Manager to look up culture-
specific resources at run-time.
ExecutionContext
Gets an ExecutionContext object that contains information about the various
contexts of the current thread.
IsAlive
Gets a value indicating the execution status of the current thread.
IsBackground
Gets or sets a value indicating whether or not a thread is a background thread.
IsThreadPoolThread
Gets a value indicating whether or not a thread belongs to the managed thread
pool.
ManagedThreadId
Gets a unique identifier for the current managed thread.
Name
Gets or sets the name of the thread.
Priority
Gets or sets a value indicating the scheduling priority of a thread.
ThreadState
Gets a value containing the states of the current thread.
The following table shows some of the most commonly used methods of the
Thread class −
Allocates an unnamed data slot on all the threads. For better performance, use
fields that are marked with the ThreadStaticAttribute attribute instead.
Reads the value of a field. The value is the latest written by any processor in a
computer, regardless of the number of processors or the state of processor cache.
This method has different overloaded forms. Only some are given above.
Web.config
App.config
It is also a special type of configuration file which is basically used with Windows
Services, Windows application, Console Apps or it can be WPF application or any
others.
It parses at compile time; it means if you edit the app.config when program is
running, then you need to restart the application to reload the configuration
setting into the program.
When you run the application which contains the app.config, at the time of
compilation a copy of app.config with different name moves into build folder for
running the application, So that's why we need to restart the program if any
changes made in app.config.
It is not added auto when you create a project, to add this you need to go to
solution explorer and choose Add New Item and choose “Application
Configuration File”. Windows application always contains the App.config file into
the project.
P a g e | 38
Machine.config
It is a special type of configuration file which creates into the OS when you install
visual studio. This stores machine level configuration setting. Only one
machine.config file exists into the system and it stores highest level of
configuration settings.
Path of Machine.config
32-bit System
%windir%\Microsoft.NET\Framework\[version]\config\machine.config
64-bit System
%windir%\Microsoft.NET\Framework64\[version]\config\machine.config
P a g e | 39
A table can have only one clustered index. A table can have multiple non-clustered indexes.
The clustered index has the inherent ability to A non-Clustered index does not have the inherent
store data on the disk. ability to store data on the disk.
Non-Clustered index storescontainThe non-
Clustered both value and a pointer to the actual
Clustered index store pointers to block not data. row that holds data.
In Non-Clustered index leaf nodes are not the
actual data itself rather they only contain included
In Clustered index leaf nodes are actual data itself. columns.
In a Clustered index, Clustered key defines the In a Non-Clustered index, the index key defines the
order of data within a table. order of data within the index.
A Clustered index is a type of index in which table A Non-Clustered index is a special type of index in
records are physically reordered to match the which the logical order of the index does not match
index. the physical stored order of the rows on the disk.
Candidate Key
A candidate key is a set of one or more columns that can identify a record
uniquely in a table, and YOU can use each candidate key as a Primary Key.
Super key
Super key is a set of over one key that can identify a record uniquely in a table,
and the Primary Key is a subset of Super Key.
P a g e | 43
jQuery selectors are one of the most important parts of the jQuery library. jQuery
selectors allow you to select and manipulate HTML element(s).
jQuery selectors are used to "find" (or select) HTML elements based on their
name, id, classes, types, attributes, values of attributes and much more. It's based
on the existing CSS Selectors, and in addition, it has some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
$("#test").hide();
$(".test").hide();
try
{
Console.WriteLine("Enter First Number");
Number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second Number");
Number2 = int.Parse(Console.ReadLine());
Result = Number1 / Number2;
Console.WriteLine($"Result: {Result}");
}
catch (DivideByZeroException DBZE)
{
Console.WriteLine("Second Number Should Not Be Zero");
}
catch (FormatException FE)
{
Console.WriteLine("Enter Only Integer Numbers");
}
P a g e | 47
Here both the methods are used to convert the string but the basic difference
between them is: "Convert" function handles NULLS, while "i.ToString()" does
not it will throw a NULL reference exception error. So as good coding practice
using "convert" is always safe.
string s;
object o = null;
s = o.ToString();
//returns a null reference exception for s.
string s;
object o = null;
s = Convert.ToString(o);
//returns an empty string for s and does not throw an exception.
P a g e | 49
SOLID principles in C#
The following are the design flaws that cause damage to software, mostly.
SOLID principles are the design principles that enable us to manage several
software design problems. Robert C. Martin compiled these principles in the
1990s. These principles provide us with ways to move from tightly coupled code
and little encapsulation to the desired results of loosely coupled and encapsulated
real business needs properly. SOLID is an acronym for the following.
O: Open/Closed Principle
The Open/closed Principle says, "A software module/class is open for extension
and closed for modification."
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.
Inheritance
Polymorphism
Polymorphism means one name, many forms. One function behaves in different
forms. In other words, "Many forms of a single object is called Polymorphism."