1. Given an array of integers write a function in C# to sum all the numbers that are even (any developer should be able to quickly write this out on a white board or paper, no need for tooling).

Ans:

public int sum(int[] list)

{

int sum = 0;

for(int i=0;i<list.Length;i++)

{

if(list[i]%2==0)

{

sum = sum + list[i];

}

}

return sum;

}

  1. What is a REST service and how would you build one in .Net?

Ans:

REST stands for Representational State Transfer. It is an architectural concept for building inter-operable light weight web services which helps to access and manipulate the web resources identified though URI (Uniform Resource Identifier). It was introduced by Roy Fielding. Like WWW (World Wide Web, REST identifies all resources on server through a unique URI. For example: http://yoursite.com/employee/1 to access the employee information with id 1. The REST web service accepts a requests for a resource and returns response in almost every data interchange formats like XML, JSON, HTML, etc.

Creating WebApi project in Visual Studio 2015

  1. Open Visual Studio 2015. Click New > Project
  2. Select Web templates and ASP.Net Web Application.
  3. Select Empty template and check Web API for “Add folders and core reference for”. Refer below image.

Click OK.

This will create a new empty WebApi project with all required Nuget packages, default routing setup required to create your first web API.

  1. Add a Web API controller by right clicking Controllers folder and Add > Controller. Select WebAPI 2.0 controller with read/write actions.

I have only added a simple Get() and Get(id) action methods to return hardcoded employee information for simplicity.

Employee API Controller

public class EmployeeController : ApiController
{
// GET api/
public IEnumerable Get()
{
return new[] { new Employee { Id = 1, Name = “Tom”, Gender = “Male” }, new Employee { Id = 2, Name = “John”, Gender = “Male” } };
}

// GET api//5
public Employee Get(int id)
{
return new Employee() { Id = 1, Name = “Tom”, Gender = “Male” };
}

// POST api/
public void Post([FromBody]string value)
{
//post code
}

// PUT api//5
public void Put(int id, [FromBody]string value)
{
//put code
}

// DELETE api//5
public void Delete(int id)
{
//delete code
}
}
//Employee model

public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}

  1. Press F5 to execute the application.

Visiting http://localhost:56323/api/employee will list all the employees.

Visiting http://localhost:56323/api/employee/1 will list employee information for employee id 1.

Note– replace the port # with your project’s port number.

  1. How do you typically work with a database in .Net?

Ans:

           Normally we create database in SQL server 2012 and use entity framework to access it. And update web.config with connection string to get access to database.

  1. What is dependency injection and what frameworks have you used?

Ans: Dependency Injection (DI) is a design pattern that demonstrates how to create loosely coupled classes. DI provides implementation of Inversion of Control. There are different frameworks(containers) we used to achieve Dependency injection. I used MEF, Ninject, StructureMap frameworks to achieve DI.

  1. What is an anti-forgery token and why would you use it?

Ans: A great feature in ASP.NET MVC is the AntiForgeryToken. This Generates a hidden form field (anti-forgery token) that is validated when the form is submitted. The anti-forgery token can be used to help protect your application against cross-site request forgery. To use this feature, all you need to do is add the following HTML helper to your form so it is submitted as part of the form post:@Html.AntiForgeryToken() 

  1. How is programming with interfaces beneficial/detrimental to testing?

Ans:  using interfaces we are decoupling classes and due to decouple we can independently test each component.

  1. What is an Angular component? Why would you use one?

Ans: Component is combination of template and class. Template contains view layout, html and includes bindings and directives whereas class supports view, created using typescript.

Use component have some advantages

  1. Components only control their own View and Data
  2. Components have a well-defined public API – Inputs and Outputs
  3. Components have a well-defined lifecycle 
  1. What does the keyword `this` in JavaScript mean? If I opened up chrome dev tools and typed `this` what would I get in return?

Ans:  ‘this’ is the current execution context of a function. In chrome it is showing window object which is global object for current instance

 

  1. What is the difference between an inner and outer join?

Ans:  An inner join finds and returns matching data from tables, while an outer join finds and returns matching data and some dissimilar data from tables.

  1. What is a foreign key? What problems might you run into when deleting data with a foreign key in a related table?  How do you overcome this?

Ans: foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table or the same table. When deleting data with a foreign key getting error of referential integrity constraints to solve this error add constrain of on cascade delete while creating foreign key.

11. Get array of only once appearance of element
int[] xs = { 1, 2, 3, 3, 4 };

Ans1: var reslt =
from t in xs
group xs by t into grp
where grp.Count() == 1
select grp.Key;

Ans2: var a= xs.GroupBy(g => g).Where(g => g.Count() == 1).Select(g => g.First());

12. Parse below json string
string json =@”{‘Emp1’: ‘10000’,’Emp2′: ‘20000’,}”;

Answer :

Salary sal = JsonConvert.DeserializeObject(json);

string EmployeeName = sal.a;
int Salary= sal.b;

 

Advertisements