Blog for Junior Developers C#/.NET

Tuesday, June 25, 2024

Before the last article of our series, in which I try to prepare you for a job interview for the position of a junior .NET programmer. Today we will analyze the next 10 questions. I hope that you have already calmly analyzed the questions from the previous entries.

100-interview-questions-and-answers-for-junior-developers-csharp-dotnet-part-9-10.jpg

81) What are automated tests and what are their advantages?


Automation tests are pieces of code that test other code. We can also write such tests, among others, in C# using an appropriate framework. We can use e.g. NUnit or xUnit here.

Thanks to automatic testing, we can test the code more often and faster.

We catch more errors before implementing them into production.

They also make refactoring easier.

We have better code quality, it is easier to maintain this code, and good tests can also be documentation of our code.

[Test]
public void GetOutput_WhenInputIsDivisibleOnlyBy3_ShouldReturnFizz()
{
    var fizzbuzz = new FizzBuzz();
    var result = fizzbuzz.GetOutput(3);
    Assert.That(result, Is.EqualTo("Fizz"));
}


82) What are the types of automated tests?


First of all, we can divide automated tests into 3 types of tests.

These are unit tests. That is, testing code without external dependencies (such as files, databases or web services). Unit tests are small tests that test single units. Typically, most of these texts are written because they do not use external dependencies and are very fast.

In addition to unit tests, we also write integration tests. Here we test several interconnected units and methods with external dependencies. These tests are slower than unit tests because they use external dependencies. The third type of testing is end-to-end testing, i.e. testing the application through the user interface, e.g. Selenium. These E2E tests are the slowest.

According to the test pyramid, the application should have the most unit tests that are easy to write and fast. Fewer integration tests and the least end-to-end tests, i.e. the slowest tests.


83) What is mocking?


Mocking, i.e. imitating something or some behavior. They are simply artificial objects, dummies that imitate certain behaviors.

If a method has logic in it and uses external resources (such as a database, file, web services, etc.), then such a method would not be able to be tested in our unit tests. However, if we replace the external resource with an artificial object, i.e. a mock, which has no logic, then we can do it by all means. This means that we replace the actual implementation with a mock for unit testing purposes.

In C#, we have many different mocking frameworks to choose from, including Moq, NSubstitute, FakeItEasy, Rhino Mocks and many others. However, the most popular one is the Moq framework. It has all the functionality you need to mock objects and is easy to use.

var mockUserRepository = new Mock<IUsersRepository>();
mockUserRepository.Setup(x => x.Login("user", "password")).Returns(false);
var authentication = new Authentication(mockUserRepository.Object);


84) What is TDD?


TDD, i.e. Test Driven Development, i.e. test-driven software development. When writing software using TDD, we can say that our tests control the code that will be created. This is a technique that involves writing the test first and then the code.

By using TDD, we focus on better quality code. Thanks to the fact that the test was written first, we are sure that the code will be testable and, therefore, will follow good programming practices. Thanks to the refactoring required in each cycle, we will be able to improve it on an ongoing basis. Thanks to TDD, we also have a broader look at the code before moving on to the actual implementation. We focus on thoroughly understanding all requirements. Thanks to this, we are able to predict more test cases that need to be checked.

The TDD approach consists of three stages called Red-Green-Refactor.

We start implementations from the red phase, this is the phase in which we write the first test and its result should be red, i.e. the test should not pass. Additionally, often even after this phase, the code will not compile because we can use classes in the test that do not exist yet.

The second stage, green, is implementing the code for our test. Here it is important to note that we are implementing the simplest code that meets the requirements of our current test. After implementing the code, the test should be positive, i.e. green.

The final stage is refactoring the code, or tests, so that the code remains green.

Then these cycles are repeated to implement subsequent functions in our application.


85) What is REST API?


The abbreviation REST, i.e. Representational State Transfer, is a style of software architecture that involves exchanging data between systems, and was created while working on the HTTP protocol.

REST uses, among others, a uniform interface (we only use HTTP protocol methods: GET, POST, PUT, DELETE), a client-server pattern, each interaction with the server must be stateless, all resources are available using uniform resource identifiers (URIs). These principles make REST applications simple and fast.

[HttpPost]
public IActionResult UpdateStudent()
{
    return Ok();
}


86) What is AJAX?


AJAX, i.e. Asynchronous JavaScript and XML, i.e. asynchronous javascript and xml. Thanks to AJAX, the application user can send an asynchronous request to the server without reloading the entire document. So, for example, in ASP.NET MVC we can trigger some action in the controller without reloading the entire page, without refreshing the page.

$.ajax({
    type: "GET",
    url: '/Home/Action',
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    success: function (data) {
    },
    error: function () {
    }
});


87) What is CORS?


CORS, i.e. Cross-Origin Resource Sharing, is a mechanism that allows sharing resources between servers located in different domains. This mechanism is extremely useful for SPA applications where the backend and frontend are located on separate domains, or, for example, we use other additional services, e.g. payments, which are also on different domains.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseCors(
        options => options.WithOrigins("http://example.com").AllowAnyMethod()
    );
}


88) What is ViewData, ViewBag and TempData and what are the differences?


Thanks to ViewData, ViewBag and TempData, we can pass data from the controller to the view in ASP.NET.

ViewData is a dictionary whose values ​​we refer to by providing the appropriate key. We pass any type of object as the dictionary value. If we read a value from this dictionary, we also receive an object type, so ViewData usually requires us to cast it to another complex type. Its value is only stored within a single request. If redirection occurs, the value will be deleted.

ViewBag is such an improved version of ViewData. It is a wrapper on top of ViewData, which makes it a bit slower. It is a dynamic type, so we do not have to cast to the expected type every time. Like ViewData, the value is only stored within a single request. If we want to assign an object to ViewBag, just enter any name after a dot and assign it, just like we assign values ​​to object properties. We can also read the value in the view in the same way.

When it comes to TempData, we assign values ​​similarly to ViewData, because it is also a dictionary type. The key difference between TempData and ViewData is that redirection does not delete the TempData value. Data can be transferred between requests. This data is saved in the session.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        ViewData["Age"] = 32;
        ViewBag.Year = 2020;
        TempData["Number"] = 1;
        return View();
    }
}


89) What is Razor?


Razor is an HTML rendering engine that allows us to write views in ASP.NET MVC. It combines HTML and C#. Views in an ASP.NET MVC application are written in razor and have a cshtml extension. Cs from csharp + HTML, which gives us cshtml. Razor's syntax is very intuitive, we simply write code in HTML, and if we want to switch to C#, we use the @ sign.

@using App
@model Student

@{
    ViewBag.Title = "Student";
}

<body>
    <div>
        @foreach (var item in Model.Address)
        {
            <div>@item.City</div>
        }
    </div>
</body>


90) What is SignalR and how does it work?


SignalR is an open source library for ASP.NET developers that allows us to send server-side data to clients in real time. This means that the server can send some asynchronous notifications to application clients. The SingalR library is primarily suitable for applications that require frequent updates from the server, such as games, chats, e-mails, alerts, monitoring applications, navigation, auctions, etc.


TERMINATION


Those are all the questions for today. As usual, I invite you to read more in the next material. This will be the last article in this series. If you like this material, be sure to join my community - free registration, where you will also have access to additional materials.

That's all for today, see you in the next article.

Author of the article:
Kazimierz Szpin

KAZIMIERZ SZPIN
Software Developer C#/.NET, Freelancer. Specializes in ASP.NET Core, ASP.NET MVC, ASP.NET Web API, Blazor, WPF and Windows Forms.
Author of the blog CodeWithKazik.com

Previous article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 8/10)
Next article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 10/10)
Dodaj komentarz
© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik