Tech Point Fundamentals

Sunday, June 12, 2022

C# Interview Questions and Answers - Part 04

C# Interview Questions and Answers - Part 04

csharp-interview-questions-and-answers-part1

Are you preparing for the C# Interview? If yes, then you are at the right place. This is the C# Interview Questions and Answers article series. Here we will see the top 150+ C# Interview Questions with Answers. 

Please visit our YouTube Channel for Interviews and other videos by below link:


Please read the complete Design Patterns Interview Questions and Answers series here.



Introduction


This is the 4th part of this C# Interview Questions and Answers article series. Each part will contain 10 C# Interview Questions with Answers. Please read all the C# Interview Questions list here.

I will highly recommend you to please read the previous parts over here before continuing the current part:




C# Interview Questions and Answers - Part 04


Q031. What is the order of constructor execution in the case of inheritance?

This is one of the favorite questions of the interviewer. The answer to this question is very confusing as well. Because the answer to this question is different for different types (class and structure) and scenarios (standalone class and inheritance).

In class inheritance, the base class instance constructor is called first. But this is not true in the case of static constructors. The child class static constructor is called before any base class constructor. Please read the C# Constructor article here.




We know that if a class has both instance and static constructors, the static constructor will always be invoked first. But this is not true in the case of structure.  Please read more about the static class here and the static constructor video here. 

Please watch the Constructor Execution Sequence Order video here for a live demo.



Q032. What is the order of static constructor execution in the case of inheritance?

In class inheritance, the base class instance constructor is called first. But this is not true in the case of static constructors. The child class static constructor is called before any base class constructor. 

We know that if a class has both instance and static constructors, the static constructor will always be invoked first.




The structure contains a default parameterless instance constructor, but it cannot invoke the static constructor. For invoking the static constructor, there must be an explicit parameterized instance constructor in the structure.  

Please read more about structure in C# here for more details. Please watch the Constructor Execution Sequence Order video here for a live demo.



Q033. Can two classes having different accessibility be inherited?

The answer to this question is again both yes and no. If the derived class has less accessibility then there will not be an issue. But if the derived class has less accessibility, it will cause a compile-time error:

Compilation error (line 7, col 14): Inconsistent accessibility: 
base class 'BaseClass' is less accessible than class 'DerivedClass'



Q034. What is the static class? Can a static class implement interface, why?

A class declared with the static modifier is called a static class. A static modifier can only be used just before the class keyword and after the access modifier to make a class static. The concept of a static class was introduced in C# 2.




  1. In C# static class is a special class that has features of both abstract class and sealed class, since an abstract class can not be instantiated and a sealed class can not be inherited. 
  2. When a class is declared to be static, it is sealed and abstract by default. 
  3. A static class cannot implement an interface.
  4. All the members of a static class are static in nature. A static class can only contain static data members, static methods, and a static constructor, except const and enum. 
  5. Because a const field is static implicitly and enums are sealed implicitly by default. 
  6. Indexers are also not allowed in a static class since an indexer cannot be static because this pointer is not allowed for referencing static members.
  7. Explicit destructor is not allowed in the static class.
  8. Static class can contain only static constructors.

Please read the complete C# Static Class article here. Please also watch the C# Static Class video here.



Q035. What is the difference between a static class and an instance class having all members as static?

We can say that creating a static class is the same as creating a normal class with all static members and having a private constructor since a private constructor prevents the class from both being instantiated and inherited. 

If all the members of an instance class are static, we can access them by the class name, but a static class is not exactly the same as an instance class.

A static class always prevents us from using and defining non-static members, but in an instance class, any user can violate this rule unknowingly. 



If you define only a private constructor in any class, then it cannot be instantiated because it will be inaccessible due to its protection level. 

Static members can be accessed directly with the class name without any instance object. But if anyone creates any public constructor, then anyone can create the object of the class which has a private constructor. But a static class can never be instantiated.

The static class remains in memory for the lifetime of the Application Domain in which your program resides. Static classes and members are stored in a special memory area called High-Frequency Heap, unlike the instance classes.




Please read the complete static class article here for more details.



Q036. Can you inherit a class in a structure in C#?  Why?

No, a structure cannot inherit from either a class or structure because structures are value types in C#. But a structure can define constructors both static and instance constructors.

Both structure and class inheritance are not supported by structure because structures are sealed by default. 

But a structure can implement an interface. Please read more about structure here.



Q037. What is the difference between structure and class in C#?

  1. All the structs are value types while classes are reference types.
  2. Structs cannot have destructors, but classes can have destructors. 
  3. Structs cannot have an explicit default constructor while a class can have a default constructor.
  4. Structs do not support inheritance while classes support inheritance.
  5. Instance field declarations for a struct are not permitted to include variable initializers while a class can initialize the field members.
  6. In C# user can copy one structure object into another one using the assignment operator (=) but a class cannot be.
  7. A structure cannot have the abstract, virtual, or sealed members while a class can have.
  8. A structure cannot have a sealed method but a class can have sealed methods.
  9. A structure can only override the Object class virtual methods while a class can override any base class virtual method and abstract method.

Please read the complete C# Structure vs Class article here for more details. 



Q038. Can you define a destructor in a structure? Why?

No, the structure cannot define a destructor as it is a value type. In C# all struct types are implicitly inherited from System.ValueType which, in turn, inherits from the System.Object class.

In C# reference types are allocated on the heap and garbage-collected, whereas value types are allocated either on the stack or inline in containing types and deallocated when the stack unwinds or when their containing type gets deallocated.

But constructors are allowed in the structures both static and instance constructors. Please read more about the structure here.



Q039. Can a structure have protected members? Why?

No, the structure cannot have a protected member as it is a value type and it does not support inheritance. 

And a protected keyword implies that the member is accessible inside the class itself and in all classes that derive from that class. Please read more about the structure here.



Q040. What is the difference between static class and instance class?

  1. A static class cannot be instantiated while you are allowed to create objects of a non-static class by using a new keyword.
  2. All the data members of a static class can only be accessed by its class name, while the non-static data members of the non-static class cannot be directly accessed by its class name.
  3. A static class can only contain static members, while a non-static class may contain both static and non-static members.
  4. A static class can only contain a static constructor, while a non-static class contains both static and instance constructors.
  5. A static class cannot inherit from another class while a non-static class can inherit from another class.
  6. A static class cannot be inherited by any class while a non-static class can be inherited by other classes.
  7. A static class cannot implement any interface while a non-static class can implement the interface.
  8. A static class cannot contain indexers while a non-static class can contain indexers.

Please read the C# Static Class vs Instance Class article here


To Be Continued Part-05...


Recommended Articles






Thanks for visiting this page. Please follow and join us on LinkedIn, Facebook, Telegram, Quora, YouTube, Twitter, Pinterest, Tumbler, and VK for regular updates.


No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.