All remaining Dotnet Developer Interview questions and answers which I was facing
Here are the answers for the remaining questions:
14. Multiple Return Types from a Method
Answer: You can return multiple values from a method using Tuple, out parameters, or a custom object.
Example using Tuple:
public (int, string) GetMultipleValues()
{
return (1, "Result");
}
var result = GetMultipleValues();
Console.WriteLine($"ID: {result.Item1}, Value: {result.Item2}");
---
15. Private Constructor
Answer: A private constructor is used to prevent the instantiation of a class from outside the class. It’s typically used in singleton patterns.
Example:
public class MyClass
{
private MyClass() { } // Private constructor
}
---
16. Method Hiding
Answer: Method hiding occurs when a method in a derived class has the same name and signature as one in its base class but is marked with the new keyword. This hides the base class implementation.
Example:
public class BaseClass
{
public void Display() { Console.WriteLine("Base Display"); }
}
public class DerivedClass : BaseClass
{
public new void Display() { Console.WriteLine("Derived Display"); }
}
---
17. Difference Between Interface and Abstraction
Answer:
Interface: Defines a contract that classes must follow, but does not provide any implementation. All methods are abstract.
Abstract Class: Can have both abstract methods (without implementation) and concrete methods (with implementation).
Example:
public interface IShape { void Draw(); }
public abstract class Shape { public abstract void Draw(); public void Fill() { } }
---
18. Difference Between HTTP GET and HTTP POST
Answer:
GET: Used to retrieve data from the server. Data is sent in the URL.
POST: Used to submit data to the server. Data is sent in the body of the request.
---
19. Web API Security
Answer: Security can be achieved using authentication mechanisms such as JWT tokens, OAuth, or API keys. HTTPS should be enforced, and sensitive data should be encrypted.
Example using JWT:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secret_key"))
};
});
---
20. Difference Between IEnumerable and IQueryable
Answer:
IEnumerable: Executes on the client side and iterates over collections in memory.
IQueryable: Executes on the server side and translates LINQ queries into SQL queries.
Example:
IEnumerable<Employee> employees = context.Employees.ToList();
IQueryable<Employee> queryableEmployees = context.Employees.Where(e => e.Salary > 50000);
---
21. SOLID Principles
Answer: SOLID stands for:
S: Single Responsibility Principle
O: Open/Closed Principle
L: Liskov Substitution Principle
I: Interface Segregation Principle
D: Dependency Inversion Principle
These principles help in writing clean, maintainable, and scalable code.
---
22. Method Access Restriction in Child Class
Answer: You can restrict method access by using the sealed or private keywords.
Example:
public class BaseClass
{
public void MethodA() { }
private void MethodB() { } // Private to restrict access
}
---
23. Method Overloading vs Method Overriding
Answer:
Method Overloading: Same method name, but different signatures (parameters or return types).
Method Overriding: Replaces a base class method in a derived class. Must have the same signature.
---
24. String Reverse Program Without Predefined Functions
string ReverseString(string input)
{
char[] charArray = new char[input.Length];
for (int i = 0, j = input.Length - 1; i <= j; i++, j--)
{
charArray[i] = input[j];
charArray[j] = input[i];
}
return new string(charArray);
}
---
25. LINQ Query for Joins
var result = from e in context.Employees
join d in context.Departments on e.DepartmentId equals d.Id
select new { e.Name, DepartmentName = d.Name };
---
26. Async Programming and Task Parallel Library (TPL)
Async Programming: Allows for non-blocking operations using async and await keywords.
TPL: Task Parallel Library is a set of APIs for parallel programming in .NET, used to improve performance by running tasks in parallel.
---
27. Normalization and ACID Properties
Normalization: The process of structuring a relational database to reduce redundancy and improve data integrity.
ACID:
A: Atomicity
C: Consistency
I: Isolation
D: Durability
---
28. Difference Between Dictionary and Hashtable
Answer:
Dictionary: Generic, type-safe, and faster. Dictionary<TKey, TValue>.
Hashtable: Non-generic and slower. It stores key-value pairs in object type.
---
29. Unit Testing in C#
Answer: Unit testing ensures individual units of code work as expected.
Example Using MSTest:
[TestMethod]
public void TestMethod()
{
var result = Add(5, 3);
Assert.AreEqual(8, result);
}
---
30. Dependency Injection in ASP.NET MVC
Answer: Dependency Injection can be achieved using Ninject or Unity frameworks.
Example:
IKernel kernel = new StandardKernel();
kernel.Bind<IMyService>().To<MyService>();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
---
31. Get Employee Names from JSON
Example:
var employees = JsonConvert.DeserializeObject<List<Employee>>(jsonData);
var empNames = employees.Select(e => e.Name).ToList();
---
32. Spread Operator in JavaScript
Answer: The spread operator (...) allows an iterable to expand in places where 0+ arguments are expected.
Example:
let arr = [1, 2, 3];
let newArr = [...arr, 4, 5]; // [1, 2, 3, 4, 5]
---
33. Top 3 Employee Salaries SQL Query
SELECT TOP 3 Salary FROM Employees ORDER BY Salary DESC;
---
34. C# Program Output (Static Fields)
static String location;
static DateTime time;
static void Main() {
Console.WriteLine(location == null ? "location is null" : location); // Output: location is null
Console.WriteLine(time == null ? "time is null" : time.ToString()); // Output: 1/1/0001 12:00:00 AM
}
---
35. C# Program Output (Record Type)
record Point(int X, int Y);
var original = new Point(1, 2);
var shifted = original with { X = original.X + 3, Y = original.Y - 1 };
Console.WriteLine(shifted); // Output: Point { X = 4, Y = 1 }
---
36. Tuples in C#
Answer: Tuples are a way to store multiple values in a single object.
Example:
var tuple = (1, "apple", true);
Console.WriteLine(tuple.Item1); // Output: 1
---
37. Anonymous Methods in C#
Answer: Anonymous methods are inline unnamed methods used in place of delegate instances.
Example:
Func<int, int> square = delegate (int x) { return x * x; };
Console.WriteLine(square(5)); // Output: 25
#dotnettechpro #csharp #dotnet #dotnetdeveloper
Comments
Post a Comment