Search This Blog

Sunday, May 16, 2021

Oops Interview Question - 3

 52. What is the difference between a Hash Table and a Dictionary?

HashtableDictionary
A Hashtable is a non-generic collection.A Dictionary is a generic collection.
Hashtable is defined under System.Collections namespace.Dictionary is defined under System.Collections.Generic namespace.
In Hashtable, you can store key/value pairs of the same type or of the different type.In Dictionary, you can store key/value pairs of same type.
In Hashtable, there is no need to specify the type of the key and value.In Dictionary, you must specify the type of key and value.
The data retrieval is slower than Dictionary due to boxing/ unboxing.The data retrieval is faster than Hashtable due to no boxing/ unboxing.
In Hashtable, if you try to access a key that doesn’t present in the given Hashtable, then it will give null values.In Dictionary, if you try to access a key that doesn’t present in the given Dictionary, then it will give error.
It is thread safe.It is also thread safe but only for public static members.
It doesn’t maintain the order of stored values.It always maintain the order of stored values.


Example Hash Table

using System;
using System.Collections;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Create a hashtable
        // Using Hashtable class
        Hashtable my_hashtable = new Hashtable();
  
        // Adding key/value pair in the hashtable
        // Using Add() method
        my_hashtable.Add("A1", "Welcome");
        my_hashtable.Add("A2", "to");
        my_hashtable.Add("A3", "GeeksforGeeks");
  
        foreach(DictionaryEntry element in my_hashtable)
        {
            Console.WriteLine("Key:- {0} and Value:- {1} ",
                               element.Key, element.Value);
        }
    }
}

Output:

Key:- A3 and Value:- GeeksforGeeks 
Key:- A2 and Value:- to 
Key:- A1 and Value:- Welcome 

Example Dictionary

using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating a dictionary
        // using Dictionary<TKey, TValue> class
        Dictionary<string, string> My_dict = 
                    new Dictionary<string, string>();
  
        // Adding key/value pairs in the Dictionary
        // Using Add() method
        My_dict.Add("a.01", "C");
        My_dict.Add("a.02", "C++");
        My_dict.Add("a.03", "C#");
  
        foreach(KeyValuePair<string, string> element in My_dict)
        {
            Console.WriteLine("Key:- {0} and Value:- {1}"
                              element.Key, element.Value);
        }
    }
}

Output:

Key:- a.01 and Value:- C
Key:- a.02 and Value:- C++
Key:- a.03 and Value:- C#

53. What are the state management techniques used in .Net?

HTTP Protocol is a Stateless protocol HTTP/HTTPs doesn't remember what website or URL we visited, or in other words we can say it doesn't hold the state of a previous website that we visited before closing our browser, that is called stateless. So how to maintain the state for request. This is called state management.

State Management Types

  • Client-side Management
  • Server-side Management

Client-side Techniques

Client-Side State Management, the state related information will directly get stored on the client-side. That specific information will travel back and communicate with every request generated by the user then afterwards provides responses after server-side communication.

  • View
  • Hidden
  • Cookies
  • Control State
  • Query Strings

Server-side Techniques

In Server-Side State Management all the information is stored in the user memory. Due to this functionality there is more secure domains at the server side in comparison to Client-Side State Management.

  • Session State
  • Application State

View State

  • It is page-level State Management
  • Used for holding data temporarily
  • Can store any type of data
  • Property dependent

Hidden Fields

  • Contains a small amount of memory
  • Direct functionality access

Cookies

  • Store information temporarily
  • It's just a simple small sized text file
  • Can be changed depending on requirements
  • User Preferred
  • Requires only a few bytes or KBs of space for creating cookies

Query String/Parameter

  • It is generally used for holding values
  • Works temporarily
  • Switches info from one to another page
  • Increase performance
  • Uses real and virtual path values for URL routing

Session State

The Session basically stores the values as a dictionary collection in key/value pairs. It completely utilizes server resources to store the data. It is a secure way of storing data, since the data will never be passed to the client.

For each and every user, a separate Session is created, and each and every Session has its Unique ID. This ID is being stored in the client's machine using cookies. If there are multiple users who are accessing a web application, then for each user a separate Session is created. If a single user logs in and logs out the Session is killed, then if the same user again logs into the application, then a new Session ID is being created for the same user.

The Session has a default timeout value (20 minutes). We can also set the timeout value for a session in the web.config file.


54. What is Data Abstraction?

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces

The abstract keyword is used for classes and methods


55. What is Abstract Class?

  • Abstract class is a restricted class that cannot be used to create objects (cannot be instantiated).
  • To access it, it must be inherited from another class).
  • This class must contain at least one abstract method.
  • The Abstract classes are typically used to define a base class in the class hierarchy.
  • Abstract classes are either partially implemented or not implemented at all.
  • You can take advantage of abstract classes to design components and specify some level of common functionality that must be implemented by derived classes.
  • An abstract class can have both abstract and regular methods.

Example

abstract class Animal 

{

  public abstract void animalSound();

  public void sleep() 

  {

    Console.WriteLine("Zzz");

  }

}


Animal myObj = new Animal(); // Will generate an error (Cannot create an instance of the abstract class or interface 'Animal')


56. What is Abstract Method?

  • Abstract method can only be used in an abstract class, and it does not have a body.
  • The body is provided by the derived class (inherited from).


57. What is Interface?

  • An interface is a completely "abstract class", which can only contain abstract methods and properties (with empty bodies)
  • By default, members of an interface are abstract and public.
  • Interfaces can contain properties and methods, but not fields.
  • Interface methods do not have a body - the body is provided by the "implement" class
  • On implementation of an interface, you must override all of its methods
  • An interface cannot contain a constructor (as it cannot be used to create objects)
  • An interface is a group of related methods with empty bodies. 
  • C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. 

Example

interface Animal 

{

  void animalSound(); // interface method (does not have a body)

  void run(); // interface method (does not have a body)

}


58. What is the difference between Abstract class and Interface?

Abstract ClassInterface
It contains both declaration and definition part.It contains only a declaration part.
Multiple inheritance is not achieved by abstract class.Multiple inheritance is achieved by interface.
It contain constructor.It does not contain constructor.
It can contain static members.It does not contain static members.
Abstract class members can have access modifiers.Interface members can’t have access modifiers.
Abstract class members can be private by default which can be change.Interface members can be public by default which can not be change.
Abstract class can inherit from another abstract class or another interface.Interface can inherit from another interface only and cannot inherit from an abstract class.
A class can only use one abstract class.A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class.If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc.Interface can only contain methods .
It can be fully, partially or not implemented.It should be fully implemented.
How to create Abstract Classes and Interfaces?

public abstract class Customer  

{

}

public interface ICustomer  

{  

}


How to create Abstract Classes and Interfaces along with fields? 

public abstract class Customer  

{  

    int i1;  

}  

public interface ICustomer  

{  

    int i2;//Not possible  


How to create Abstract Classes and Interfaces along with members? 

public abstract class Customer  

{  

    public abstract void Print1();  

}  

public interface ICustomer  

{  

    void Print2();  

}


How ismember implementation possible in Abstract Classes and Interfaces? 

public abstract class Customer  

{  

    public void Print1()  

    {  

    }  

}  

public interface ICustomer  

{  

    void Print2()//Not possible  

    {  

    }  

}  


How to implement abstract or interface members in inherited class? 

public abstract class Customer  

{  

    public abstract void Print1();  

}  

public interface ICustomer  

{  

    void Print2();  

}  

public class MainClass : Customer, ICustomer  

{  

    public override void Print1()  

    {  

    }  

    public void Print2()  

    {  

    }  


How to implement inherit from another Abstract Class or Interface to Abstract Class or Interface? 

public abstract class Customer1  

{  

}  

public abstract class Customer2 : Customer1  

{  

}  

public abstract class Customer3 : Customer1, ICustomer1  

{  

}  

public interface ICustomer1  

{  

}  

public interface ICustomer2 : ICustomer1  

{  

}  

public interface ICustomer3 : Customer1 //Not possible  

{  










No comments:

Post a Comment


This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.