Blog for Junior Developers C#/.NET

Tuesday, June 18, 2024

In the previous article, we analyzed the first 10 interview questions and answers for the position of junior .NET developer. However, in this article we will continue this series and move on to the next questions with answers.

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

11) What is a constructor?


A constructor is a special method with the same name as the class. Does not include return type in signature. It is called when the object is created. There may be many constructors if they have different signatures, or there may be none, in which case an empty constructor is launched by default.

public class Student
{
    public Student()
    {

    }

    public Student(int age)
    {

    }
}


12) What is GUID?


GUID is an abbreviation of Global Unique Identifier. It is generated, among other things, based on the generation time and pseudorandom numbers. It consists of 32 characters and the probability of generating two identical keys is minimal.

public class Program
{
    static void Main(string[] args)
    {
        var guid = Guid.NewGuid();
        Console.WriteLine(guid);
    }
}

//Wynik: 649eeac0-6f1e-4f46-bcd3-60d0afd5d7a1


13) What is the difference between unsigned int and signed int?


Unsigned is an unsigned int type and denotes a type that only stores positive numbers. Signed int, on the other hand, means the int type, which can accept both positive and negative numbers. Unsigned and signed can also be used for other types, e.g. byte, short or long. The int type has a size of 32 bits. For unsigned int, you can use any integer from 0 to over 4 billion. However, in the case of signed int it will be an integer from over -2 billion to over 2 billion. So the size is the same, just in different ranges.

int signed = -1;
uint unsigned = 1;


14) What is refactoring?


Refactoring, i.e. changing the code structure without changing the functionality. We perform refactoring to increase the quality of the code without creating new functionality. Pre-written unit tests help in carrying out correct refactoring, thanks to which we protect ourselves against possible regression.


15) What is var?


Var is the so-called implicit type and C# developers have access to it since version 3.0. We can only use an implicit type in cases where the compiler can guess what type will be created at compile time. Or it can figure it out implicitly thanks to initialization. So we actually know at compilation time what type var will be converted to. We use it because it makes our code more readable and is very useful, among others, in LINQ queries, because we do not have to write long variable names. In fact, for example, writing var a = 1 is identical to writing int a = 1. Based on the initialization in both cases, the compiler knows that we are talking about the int type. It is worth emphasizing that you cannot use the implicit type var just for declaration without initialization. The same for properties, fields and methods.

int a = 1;
var b = 1;


16) Explain the keyword static?


The static keyword can be used to mark a class, method, field or property and marks a given component as static. To call a static method, we do not create a new instance of the object, but refer to it by the class name and then the member name. So it can be a field, a property or a method. In this case, memory is reserved the first time this method is used. If we mark a class with the static keyword, it is static and our class can only contain static members. However, we can create a non-static class and within it only some members can be static. In this case, we can refer to their components in 2 different ways. To static components – through the class name and component name. However, we will have access to non-static components after creating a new instance of this class, i.e. after creating a new object.

public static class Math
{
    public static int Number1 { get; set; }

    public static int GetResult()
    {
        return 0;
    }
}


17) What is the this keyword used for?


The word this in C# has several uses. First, we can use it inside a class and then this refers to the current instance of the class. We can also use this word when creating extension methods and then the first parameter must be marked with this word and denotes the class we are extending.

public class Student
{
    private string name;

    public Student(string name)
    {
        this.name = name;
    }
}

public static class StringExtensions     
{
    public static string NewMethod(this string model)
    {
        return model;
    }
}


18) What is the difference between an array and a list?


First of all, array size is rigid and list size is dynamic. In the case of a list, you can freely add and remove elements from the list, but in the case of an array, this is not possible.

var list = new List<int>();
var table = new int[5];


19) What is the difference between properties and fields?


First of all, encapsulation. As for the field, it is an ordinary variable, while the property also has a getter and setter, thanks to which it is possible to introduce additional validation and encapsulation. Here you have more control over the data and can set the appropriate access modifier for both writing and reading. That is, you can, for example, allow changes to properties only inside the class, and reading can be public. Additionally, it may often be useful to introduce additional validation of data correctness in a set. It is usually good practice to create public properties instead of fields. In turn, we usually create private fields.

private int _field;
public int Properties { get; private set; }


20) What is the name of the base class for all exceptions?


The base class for all exceptions is the Exception class. If you want to create your own exception, your class should inherit from the Exception class.

public class MyNotFoundException : Exception
{
    public MyNotFoundException(string url) : base($"Invalid page: {url}.")
    {
    }
}


TERMINATION


That's all the questions in this article. We will analyze the next ones in the next entry. 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 1/10)
Next article - 100 Interview Questions (and Answers!) for Junior Developers C#/.NET (Part 3/10)
Dodaj komentarz

Search engine

© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik