In this article, we’ll go through questions commonly asked in an API interview and provide sample answers to those questions
Click on the question that you would like to read.
What is object-oriented programming (OOP)?
OOP stands for Object-Oriented Programming. Object-oriented programming is about creating objects that contain both data and functions.
OOP is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including C#, JavaScript, C++, Java, and Python.
What are the main principles of OOP?
There are 4 major principles of OOP:
Encapsulation
Encapsulation, in OOP methodology, prevents access to implementation details. Encapsulation is implemented by using access specifiers
Abstraction
Abstraction is a process of hiding the implementation details and displaying the essential features It means that only the required information is visible to the user and the rest of the information is hidden.
Abstraction can be implemented using abstract classes in C#. Abstract classes are base classes with partial implementation.
Polymorphism
This means that an object can have multiple functionalities. Polymorphism allows the same method to execute different behaviors in two ways: method overriding and method overloading.
We can write a code that works on the superclass, and it will work with any subclass type as well.
There are two types of polymorphism: static and dynamic.
Static polymorphism is achieved using method overloading (Early Binding). There can be multiple definitions for the same function name. All of these definitions may differ by the number of arguments they contain or the type of these arguments
Dynamic polymorphism using method overriding (Late Binding). It is implemented using abstract classes and virtual functions.
Inheritance
Inheritance allows a class to inherit the functionality of another class. The original class is known as the base class and the class which inherits the functionality of the base class is the derived class. In derived classes we can reuse the code of existing super classes
What are the Data Types in C#?
There are 3 types un C#:
Value Data Types –Value type variables can be assigned a value directly. They are derived from the class System.ValueType. These are integer and floating point based. Some examples of value data types are int, char, float, double, bool, etc.
Reference Data Types – These data types contain a reference to the variables and not the actual data. They refer to a memory location. They are derived from the class System.Object. Some build in reference types are object, arrays, dynamic and string.
Pointer Data Types – This store the memory address of another data type and the data can be accessed using pointers.
What is an Static Constructor?
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.
A static constructor doesn’t take access modifiers or have parameters.
A class or struct can only have one static constructor.
Static constructors cannot be inherited or overloaded.
A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically.
The user has no control on when the static constructor is executed in the program.
Where are stored variables in the memory?
While value types are stored generally in the stack, reference types are stored in the managed heap.
What is the difference between an Interface and a Class?
A Class has both definition and an implementation whereas Interface only has a definition.
A Class can be instantiated but an Interface cannot be instantiated You can create an instance of an Object that implements the Interface.
A Class is a full body entity with members, methods along with there definition and implementation. An Interface is just a set of definition that you must implement in your Class inheriting that Interface.
What is the difference between an interface and an abstract class?
A class can implement any number of interfaces but a subclass can at most use only one abstract class.
An abstract class can have non-abstract methods (concrete methods) while in case of interface, all the methods have to be abstract.
An abstract class can declare or use any variables while an interface is not allowed to do so.
In an interface all methods definitions are public.
In an abstract class, we need to use abstract keywords to declare abstract methods, while in an interface we don’t need to use that.
An abstract class use constructor while in an interface we don’t have any type of constructor
What is the difference between int[] and List< int >?
The difference is that int[] is an static data structure that store on the stack a fixed number of integer values, while List<int> is a dynamic data structure that store a random collection of objects.
What is a Hashtable?
Represents a collection of key/value pairs that are organized based on the hash code of the key.
Hashtable is a data structure implemented in the .NET Framework in two ways, simple Hashtable in the namespace System.Collections and as generic data structure Dictionary in System.Collections.Generic namespace, it is recommended to use Dictionary instead of Hashtable, the working principle of Hashtable and Dictionary is that construct a hash that is index into an array usually using polynomials.
What is a Dictionary in C#?
Represents a collection of keys and values. The Dictionary<TKey,TValue> generic class provides a mapping from a set of keys to a set of values.
A Dictionary is faster than a Hashtable as it eliminates the boxing and un-boxing overheads.
Sample:
Dictionary<string, string> openWith = new Dictionary<string, string>();
openWith.Add(“txt”, “notepad.exe”);
openWith.Add(“bmp”, “paint.exe”);
openWith.Add(“dib”, “paint.exe”);
What are the differences between Dictionary and Hashtable?
Hashtable | Dictionary |
---|---|
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. |
What data structure is used in the implementation of dictionary?
Dictionary implements a Hashtable data structure.
What is string and stringbuilder?
A String object is immutable (read-only) and a sequential collection of System.Char objects that represent a string. The maximum size of a String object in memory is 2 GB (about 1 billion characters).
A StringBuilder (System.Text.StringBuilder) represents a mutable string of characters. This class cannot be inherited. The default capacity of this object is 16 characters, and its maximum capacity is more than 2 billion characters.
Do not use a string when you don’t know the number of concatenations or if the number of concatenations are large.
Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you’re using a loop to concatenate a random number of strings of user input. Or in the case of a big loop as in the following.
What is ref and out?
Ref and out keywords in C# are used to pass arguments within a method or function. Both indicate that an argument/parameter is passed by reference.
Ref | Out |
The parameter or argument must be initialized first before it is passed to ref. | It is not required to initialize a parameter or argument before it is passed to an out. |
It is not required to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method. | A called method is required to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method. |
It is not required to initialize a parameter value before using it in a calling method. | A parameter value must be initialized within the calling method before its use. |
When we use REF, data can be passed bi-directionally. | When we use OUT data is passed only in a unidirectional way (from the called method to the caller method). |
What is Boxing and Unboxing in C#?
Boxing The process of converting from a value type to a reference type.
// Boxing
int num = 123;
Object obj = num;
Unboxing is the process of converting from a reference type to a value type.
// Unboxing
Object obj = 123;
int num = (int)obj;
What are Mutable an Immutable objects in C#?
The mutable types are those whose data members can be changed after the instance is created. When we change the value of mutable objects, value is changed in same memory.
Immutable types are those whose data members can not be changed after the instance is created. When we change the value of immutable objects a new memory is created and the modified value is stored in new memory.
String is a mutable or immutable type?
String is an immutable type, which means we are creating new memory every time instead of working on existing memory.
What is a Nullable Type?
The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with value type not with reference type.
The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.
Sintax:
Nullable<data_type> variable_name = null;
Or
datatype? variable_name = null;
Define the different types of Polimorphism
The Ad-Hoc polymorphism is called as overloading. This allows function with same name to act in different manner for different types.
The Inclusion polymorphism is called as subtyping. This allows to point derived classes using base class pointers and references
The Coercion polymorphism is called as casting. This type of polymorphism occurs when an object or the primitive is cast into some other type.
The Parametric polymorphism is called as Early Binding. This type of polymorphism allows to use same piece of code for different types.
Have you worked with Static? What is the main principle?
What is system control versions?
Public interface DoSomething – can you provide examples?
Why do you use ref?
What is thread safe meaning?
What is obstruction C#? What are the main principles? Can you give a time you used obstruction?
What is concept of Laziness? How do you use it? Can you provide a code example?
What is concept of axis?
Do you use Public or Private method? Can you use both?
Example of a task they make be asked to complete:
Provide us with a solution to the task below.
The Task is to write a file manager with the following functions (only for .txt format):
1. Folder CRUD.
2. .txt file CRUD.
3. Folder moving (copy, paste, move, remove folder).
4. .txt file moving (copy, paste, move, remove folder).
5. Option to go from one folder to the other. Once you enter the folder – everything that is inside it should be shown.
6. Option to create breadcrumbs.
Expected deliverable: source code .Net solution uploaded anywhere with an option to download so we can take it and run on our side.