Tech Point Fundamentals

Tuesday, October 13, 2020

Static Class Interview Questions C#

Interview Questions and Answers | Static Class, Constructor and Method in C#

Interview-Questions-And-Answers-CSharp

  A static class is the most frequent topic for the interview today. Today, we will discuss the most common interview questions and answers based on static class, static constructors and static methods.




Introduction

In C# classesconstructorsmethods, variables, propertiesevents, and operators can be defined as static using the static modifier keyword. But the interfacestructureindexersenum, and destructors or finalizers cannot be declared as static.


You can read more about the static class, constructor and methods here

01. Can a static class contains non-static members?

No, a static class cannot contain any non-static members except const and enum.

02. Can a static class inherit from another class?

No, a static class cannot inherit from other classes except the Object class.

03. Can a static class implement the interfaces?

No, static classes cannot implement the interface.

04. Is it true that a static class cannot be inherited by any class, why?

No, static classes cannot be inherited by any class since static classes are sealed implicitly by default.

Please read more about the sealed class here.

05. Can a static class contains instance constructors?

No, a static class cannot contain any instance constructors.



06. Can a static class be marked as sealed?

No, a static class cannot be marked as sealed explicitly because static classes are sealed by default.

Please read more about the sealed class here.

07. Can a static class contain enums?

Yes, a static class can contain enums. 

08. Can a static class contains any private constructor?

You cannot define any explicit private constructor in a static class. You can only define a static constructor without any access modifier because static constructors are private implicitly. 

09. A static class can be instantiated or not?

A static class cannot be instantiated, because it is abstract implicitly.

10. Can a non-static class contains static methods?

Yes, a non-static class can also contain static methods.



11. How a static method of a non-static class can be accessed?

A static method can only be accessed by the class name itself, without creating the object of the class.

12. Can a non-static class contain a static constructor?

Yes, a non-static class can also contain a static constructor.

13. Can a static class contains destructor?

No, a static class cannot contain any destructor.

14. Can you define an indexer in a static class?

No, an indexer cannot be defined in a static class, since the indexer cannot be static because this pointer cannot be used to reference the static members.

15. A static data member can be defined using the "var" keyword or not?

No, the contextual keyword "var" cannot be used to define any static members. You must have to specify a type of member explicitly after the static keyword.



16. Where do static classes and members are stored in the memory?

All the static classes and members are stored in a special memory area called High Frequency Heap.

17. If a static class cannot be instantiated then how it is initialized?

A static class is loaded by the CLR when the program that references the class is loaded. A static constructor is also called by the CLR itself.

18. Can a static method be overridden, why?

No, a static method cannot be overridden since it belongs to the class itself, not the instance of the class. 

19. Static methods overloading is allowed or not?

Yes, static methods can be overloaded but cannot be overridden.

20. Can you pass any ref or out parameter to a static method or not?

Yes, you can pass both ref and out type parameters in a static method.



21. Is the parameterized static constructor allowed?

No, a static constructor cannot contain any parameter.

22. Can a static constructor be private?

No, static constructors are private implicitly, so cannot be marked as private explicitly.

23. How a static constructor is called?

A static constructor is called by the CLR.

24. What is the default access level of a static constructor?

Static constructors are private implicitly.

25. Can a structure be defined as static?

No, a struct cannot be defined as static.



26. Can a structure contains static constructor and static methods?

Yes, a structure can contain static constructor and static methods.

27. Can an interface be defined as static in C#?

No, an interface cannot be static but with the introduction of C# 8, static methods are allowed.

Please read more about the static interface methods here.

28. A const field can be defined as static or not?

A const field cannot be declared static explicitly because a const field is static implicitly and it belongs to the type,  not to the instances of the type.

29. Can a readonly field be defined as static in a static class?

Yes, a readonly field can be defined as static. A field declared as static readonly may only be assigned as a part of its declaration or in a static constructor. 

30. Can a static method declare local static variables?

No, a static method cannot declare local static variables.



31. When the static constructor is called if you create a delegate to a static method?

The static constructor is called only when a static method assigned to an event or a delegate is invoked and not when it is assigned. 

32. What will happen if a static constructor throws an exception?

If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running. 

Most commonly, a TypeInitializationException exception is thrown when a static constructor is unable to instantiate a type or for an unhandled exception occurring within a static constructor. 

33. What is the difference between static, abstract, and sealed class in .Net?

In C# static class is a special class that has features of both abstract class and sealed class. When a class is declared to be static, it is sealed and abstract by default. 

An abstract class cannot be instantiated but can inherit from other classes or interfaces. An abstract class can be used only as a base class. And an abstract class cannot be marked as sealed or static.

A sealed class cannot be inherited, but it can inherit from other classes. A sealed class cannot be marked as abstract or static.

34. What is the difference between a static class and a non-static class having all static members with a private constructor?

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. But 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.



35. 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 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.

36. What is the difference between static class and singleton?

Singleton is a design pattern that makes sure that the application creates only a single instance of the class at any time. A static method is used for singleton implementation. 

On the other hand for a static class, a single instance remains in memory for the lifetime of the Application Domain in which your program resides and it is accessed globally throughout the application. 

Below are some main differences between static class and singleton:

  1. Singleton is a pattern not a keyword like static. So for creating a static class static keyword is sufficient while in the case of singleton there is a need to write the logic for the singleton.
  2. Singleton class must have a private default instance constructor while a static class cannot contain any instance constructor.
  3. A static class is neither instantiated nor extended while a singleton class can be.
  4. A static class is sealed implicitly but the singleton class must be decorated as sealed explicitly.
  5.  It is possible for a singleton to implement the interface or inherit from another class but the static class neither implements the interface nor extends from any other class.
  6. We cannot implement the dependency injection with static class but DI is possible with the singleton class because it can be interface driven.
  7. The scope of the static class is at the app domain level because it is managed by the CLR while the scope of the singleton object is across the application lifecycle.
  8. A static class cannot have any destructor but a singleton class can define a destructor.
  9. The singleton class instance can be passed as a parameter to another method while a static class cannot be because it contains only static members.

Please read more about the singleton pattern here and the static class here for more details.

37. What is the use of static class?

    1. Static classes and methods are used for creating Extension Methods.
    2. The main advantage of a static class is that it guarantees that only one global instance will be created by the CLR and no instantiation is allowed.
    3. A static class can be used to implement helper or utility classes as static classes don’t need to be instantiated or inherited and generally contain a collection of some reusable methods and properties.

38. Give me some real examples of static class and static methods available in the .Net?

    1. System.Math is a static class  
    2. System.IO.File is a static class
    3. System.Environment is static class class
    4. System.String is a sealed class which contains static methods (like Format, Compare, Concat, Equals, Join )
    5. System.TimeZone is an abstract class that contains CurrentTimeZone and IsDaylightSavingTime static method.
    6. System.DateTime is a structure that contains a lot of static methods and operators (like Now, UtcNow, Today, Parse, Equals)
    7. System.Int16, System.Int32 and System.Int64 are structures that contain Parse, TryParse static methods.
    8. System.Double and System.Single are structures that contains a lot of static methods and operators (like Parse, TryParse, IsInfinity, IsNaN, IsNegativeInfinity, IsPositiveInfinity, operator ==, !=, <,>,<=,>=)
    9. System.Boolean is a structure that contains Parse and TryParse static methods.

39. What are the disadvantages of static class?

    1. Since in OOPS, object behaviors are usually associated with object state i.e. the behavior should belong to the object. But by using a static function you are implying that the behavior shouldn't belong to any particular object.
    2. Polymorphic and interface-driven designs are also hindered by overusing static classes and methods since a static method cannot be overridden in the derived class and a static class cannot be attached to an interface. 
    3. One of the most common pillars of OOPS i.e. inheritance is also not supported by the static class. Since a static class can neither inherit from other classes nor be inherited by other classes.
    4. Also, a static class cannot contain any indexer.
    5. structure can contain static members but cannot be static itself.
    6. No user or program control over static constructors.

40.  The Main() method can be non-static in C #?

No, the static Main() method cannot be non-static. The static main method is used as the entry point for the Console Application and Windows Application.




No comments:

Post a Comment

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