Tuesday 7 March 2017

C# basic interview questions Part-6

1. Ques: What is design patterns ? How many design Patterns in Software Architecture ?
Answer:
Design patterns provide general solution to commonly occurring design problem. Design Patterns provide efficient solutions to software design and programming problems that are re-usable in your code. Software Architects and developers use them to build high quality robust applications.

Creational Patterns : These patterns deals mainly with creation of objects and classes.
  • Abstract Factory Pattern: Create instances of classes belonging to different families
  • Builder Pattern: Separate representation and object construction
  • Factory Method Pattern: Create instances of derived classes
  • Prototype Pattern: Clone or copy initialized instances
  • Singleton Pattern: Class with only one single possible instance

Structural Patterns : These patterns deals with Class and Object Composition.
  • Adapter Pattern: Match interfaces of classes with different interfaces
  • Bridge Pattern:: Separate implementation and object interfaces
  • Composite: Simple and composite objects tree
  • Decorator: Dynamically add responsibilities to objects
  • Facade: Class that represents subclasses and subsystems
  • Flyweight: Minimize memory usage by sharing as much data as possible with similar objects
  • Proxy: Object that represents another object

Behavioral Patterns : These mainly deals with Class - Object communication. That means they are concerned with the communication between class and objects.
  • Chain of Responsibility: Pass requests between command and processing objects within a chain of objects
  • Command: Encapsulate a method call as an object containing all necessary information
  • Interpreter: Include language elements and evaluate sentences in a given language
  • Iterator: Give sequential access to elements in a collection
  • Mediator: Encapsulates and simplifies communication between objects
  • Memento: Undo modifications and restore an object to its initial state
  • Observer: Notify dependent objects of state changes
  • State: Change object behavior depending on its state
  • Strategy: Encapsulate algorithms within a class and make them interchangeable
  • Template Method: Define an algorithm skeleton and delegate algorithm steps to subclasses so that they may be overridden
  • Visitor: Add new operations to classes without modifying them


2. Ques: What is solid principle in software development?
Answer:
S.O.L.I.D is an acronym for the first five object-oriented design principles by Robert C. Martin, popularly known as Uncle Bob.
S.O.L.I.D stands for:
  • S – Single-responsiblity principle
  • O – Open-closed principle
  • L – Liskov substitution principle
  • I – Interface segregation principle
  • D – Dependency Inversion Principle

Single-responsibility Principle:
A class should have one and only one reason to change, meaning that a class should have only one job.

Open-closed Principle
Objects or entities should be open for extension, but closed for modification.

Liskov substitution principle
Object of a derived class should be able to replace an object of the base class without bringing any errors in the system or modifying the behavior of the base class.

Interface segregation principle
A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.

Dependency Inversion principle
Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.


3. Ques: What is Dependency Injection Pattern in C# ? What are the advantages of Dependency Injection pattern ?
Answer:
Dependency Injection (DI) is a software design pattern that allow us to develop loosely coupled code. DI also enables us to better manage future changes and other complexity in our software. Loose coupling offers us greater re usability, maintainability and test ability.

Advantages of Dependency Injection pattern
The main advantage of DI is, it makes our code more reusable, maintainable, testable and readable.

There are three basic type of Dependency Injection

Constructor Injection
  • Dependency Injection is done by class’s constructor when instantiating that class.
  • Injected component can be used anywhere within the class.
  • Should be used when the injected dependency is required for the class to function.
  • It addresses the most common scenario where a class requires one or more dependencies.
Advantages of Constructor Injection:

Construction Injection makes a strong dependency contract
Construction Injection supports testing, because dependencies can be passed in the constructor.
A dependency may be made immutable by making the dependency reference final by means that it prevents circular dependency.

Property injection
  • It also called Setter injection.
  • It used when a class has optional dependencies, or where the implementations may need to be swapped. Different logger implementations could be used this way.
  • May require checking for a provided implementation throughout the class.
  • Does not require adding or modifying constructors.
Method injection
  • Inject the dependency into a single method, for use by that method.
  • Could be useful where the whole class does not need the dependency, just the one method.
  • Generally uncommon, usually used for edge cases.

4. Ques: What is Pass By Reference? and  What is Pass By Reference Pass By Value?
Answer:
Pass By Reference:The calling functions passes the address of the variable instead of the actual values.Any 
changes made inside the actual method will be reflected in the calling function.

Pass By Value :Actual value is copied to another variable

5. Ques: What is enum ?
Answer:
An enum is a value type with a set of related named constants often referred to as an enumerator list. 
The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.
enumerations are value data type.Enums type can be integer (float, int, byte, double etc.). But if you used beside int it has to be cast.
ex:
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };



6. Ques: What is Singleton Design Patterns and How to implement in C#? 
Answer:
Singleton Design Pattern:-In this  a class has only one instance and provides a global point of access to it. 
A singleton is a class that only allows a single instance of itself to be created, and usually gives simple access to that instance. 
Most commonly, singletons don't allow any parameters to be specified when creating the instance, since a second request of an instance with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter then the factory pattern is more appropriate.) 
There are various ways to implement the Singleton Pattern in C#. The following are the common 

Key point  of a Singleton Pattern.  
  • A single constructor, that is private and parameter less.
  • The class is sealed.
  • A static variable that holds a reference to the single created instance, if any.
  • A public static means of getting the reference to the single created instance, creating one if necessary. 

7. Ques: How to use Nullable<> Types in .Net?  
Answer: 
A nullable Type is a data type is that contain the defined data type or the value of null. 
This nullable type concept is not compatible with "var".



8. Ques: What is the difference between a process and a thread?
Answer: 
Threads :
  • A thread is a subset of the process.
  • It is termed as a ‘lightweight process’, since it is similar to a real process but executes within the context of a process and shares the same resources allotted to the process by the kernel.
  • Threads share the address space of the process that created it; 
  • Threads have direct access to the data segment of its process.
  • Threads can directly communicate with other threads of its process.
  • New threads are easily created; 
  • Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; 
Process :
  • An executing instance of a program is called a process.
  • A process is always stored in the main memory also termed as the primary memory or random access memory.
  • processes have their own address space.
  • processes have their own copy of the data segment of the parent process.
  • processes must use interprocess communication to communicate with sibling processes.
  • new processes require duplication of the parent process.
  • changes to the parent process do not affect child processes.

9. Ques: What are the properties of Delegate in C# ?
Answer: 
Delegates are similar to C++ function pointers, but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
Methods don't need to match the delegate signature exactly. For more information, see Covariance and Contra variance.
C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be passed as parameters in place of a separately defined method.

No comments:

Post a Comment

Thank you for comment