Some dotnet Questions

1. Which of the following statements about loading related objects in Entity Framework are true?
a. – For POCO classes, it is possible to explicitly load related entities using Load() method
b. – Eager loading makes it possible to retrieve entity object(s) with related data in single database trip
c. – Lazy loading is enabled by default in Entity Framework
d. – Lazy loading performs database select behind the scenes, when entity’s navigation property is accessed.
2. Which of the following statements about ASP.NET MVC HTML helpers are true?
a. – ASP.NET MVC framework has some set of useful, built in HTML helpers
b. – To Write your own html helper, you need to write an extension method of HtmlHelper class
c. – System.Web.Mvc has to be referred explicitly in a view in order to use built in html helpers there
d. – HTML Helpers are simple functions where you can specify the HTML markup for some reusable component.
3. Which pattern is used, when subclassing is not possible or impractical, to add functionality at runtime?
a. – Decorator,
b. – Adapter,
c. – Composite,
d. – Proxy
4. Which of the following statements about C# interfaces are true?
a. – Interfaces can be inherited by other interfaces
b. – Classes can implement 2 interfaces which have the same method signature
c. – interfaces can contain only methods as members
d. – Interface members can have access modifiers
e. – Interfaces can be instantiated directly
5. What can be used to identify a postback?
a. – HasPostedBack()
b. – IsPostBack()
c. – ViewState
d. – Session
6. Given the following code:
class ​Sample {
  event SampleEventHandlerType SampleEvent;
}
a. – SampleEventHandlerType must be derived from System.EventHandler
b. – SampleEventHandlerType must take two parameters: Object and classderived from System.EventArgs
c. – SampleEventHandlerType must not be static.
7. Which common design pattern is shown below?
public class A
{
private A instance;
private A() {}
  public static A Instance
     {
     get{
        if(instance == null)
            instance = new A();
         return instance;
       }
    }
}

a. – Abstract Factory

b. – Factory,
c. – Singleton
d. – Builder
8. Which object can help you maintain data across users?
a. – Application Object,
b. – Response Object,
c. – Session Object,
d. – Server Object
9. You can prevent a class from being inherited by another class by using which keyword?
a. – sealed,
b. – Const,
c. – Final,
d. – Static
10. Which of the following scenarios force to select a strategy pattern?
a. – A client needs to choose from multiple algorithms
b. – A client needs to use a family of related objects
c. – Multiple classes are the same but differ only in their behaviors
d. – A change to an object requires changing other objects
11. Which of the following statements are true about exception handling in C#?
a. – catch blocks should be placed from the most specific to most general one
b. – correct blocks order is try/catch/finally
c. – Writing single catch(Exception ex) block should be avoided
d. – Code in finally block is only executed when exception does not occur
e. – throw ex; opposed to throw; placed in catch block makes sure stack trace is preserved
12. In the entity framework which is the primary object that you use to query and modify data?
a. – SessionContext,
b. – EntityObject,
c. – DataContext,
d. – DbContext,
13. Which of the following are a type of entity?
a. – POCO
b. – DBSet
c. – EntityObject
d. – POCO Proxy
14. Which of the following statements about ASP.NET MVC action filters are true?
a. -ASP.NET MVC currently supports 4 types of action  filters, but that number can be increased by adding new ones by a developer
b. – It is a mechanism of filtering and limiting requests to given routes
c. – There is a certain order in which action filters are executed
d. – It is a mechanism of adding extra, reusable logic like authorization to action method by decorating it via attribute.
15. Which .NET Collection class allows elements to be accessed using a unique key?

a. – Hashtable,

b. – Dictionary,
c. – List,
d. – ListDictionary,
e. – Stack
16. What is the correct header of string Extension method called WordCount, returning int?
a. – static int WordCount(this string str)
b. – public static int WordCount(string str)
c. – public int WordCount(this string str)
d. – public static int WordCount(this string str)
e. – public int WordCount(string str)
17. What are the advantages of System.Collections.IDictionaryEnumerator over System.Collections.IEnuerator
a. – It provides additional methods to distinguish a key from a specific value,
b. – It is optimized for the dictionary structure,
c. – It has additional properties for direct access to the key and the value
18. What is a delegate?
a. – A strongly typed function pointer;
b. – A light weight thread or process that can call a single method,
c. – An inter-process message channel,
d. – A reference to an object in a different process.
19. Which of the following statements about the Entity Framework are true?
a. – It Provides query syntax for querying the model.
b. – It automatically generates the classes from the model and updates these classes dynamically when the model is changed.
c. – It does not provide any mechanisms to track changes to the model’s objects.
d. – It takes care of database connectivity
20. Choose types are guarantee atomic reads and writes.
a. – float,
b. – long,
c. – double,
d. – string,
e. – int
21. Assuming that only default MVC route is applied, typing ‘/posts/list” URL will result in calling following method on server side.
a. – List() in Posts class,
b. – List() in PostsController class,
c. – PostsList() in Controller class,
d. – This URL is invalid
22. Which of the following jobs are performed by Garbage Collector?
a. – Closing Unclosed files,
b. – Freeing memory on the stack
c. – Closing unclosed database collections,
d. – Freeing memory occupied by unreferenced objects,
e. – Avoiding memory leaks.
23. Which of the following ways of overloading a method in C# are correct?
a. – Different number of Parameters,
b. – Different Parameter data types,
c. – Different return type,
d. – Different order of parameters.
24. Which ServiceController method can be used to send a command to the service?
a. – SendCommand(),
b. – Begin(),
c. – Start(),
d. – ExecuteCommand()

Installing GCC 7.2.0 on Windows 10

Follow the below Steps:

1. Install Cygwin (https://cygwin.com/install.html), which gives you a unix like environment on windows. Don’t select any packages while installing it. Don’t delete the setup file, which will be required to install/uninstall packages. It get’s installed in C:\Cygwin or C:\Cygwin64. Copy the installer into this folder.

2. Install a set of Cygwin packages required for building GCC.

C:\cygwin64>setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel

3. Install the latest GCC by downloading it and building from the source code. (At the time of writing this 7.2.0 was the latest verion.

$ wget http://infinity.kmeacollege.ac.in/gnu/gcc/gcc-7.2.0/gcc-7.2.0.tar.gz
$ tar xf gcc-7.2.0.tar.gz

Then

$ mkdir build-gcc
$ cd build-gcc
$ ../gcc-7.2.0/configure –program-suffix=-7.2.0 –enable-languages=c,c++ –disable-bootstrap –disable-shared

Then

$ make -j4

Finally install it

$ make install
$ cd ..

4. Test the GCC compiler in C++14 mode using “-std=c++14” option. Create a Test.cpp.

$ g++ –version
$ g++ -std=c++1y test.cpp

For More Details you can refer to : http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/

 

ASP.NET MVC Filters

Here is a short notes I made for my reference, which I guess somebody who is getting started might find it useful.

– helps to perform logic before an action method is called (pre-action) or after an action method runs (post-action).

Filter Types :

1. Authorization Filters :
– Implements IAuthorizationFilter
– makes security decisions about whether to execute an action method, such as   performing authentication or validating properties of the request.
– runs before any other filter
– Ex : AuthorizeAttribute, RequireHttpsAttribute

2. Action Filters
– Implements IActionFilter
– runs at Pre-Action and Post-Action
– can perform additional processing such as providing extra data to the action method, inspecting the return value, or canceling execution of action method.

3. Result Filters
– Implement IResultFilter
– can perform additional processing such as modifying the HTTP Response
– ex. OutputCacheAttribute

4. Exception Filters
– Implements IExceptionFilter
– executes if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline.
– can be used for tasks such as logging or displaying an error page.
– ex. HandleErrorAttribute

Controller class implements each of the filter interfaces. We can implement any of the filters for a specific controller by overriding the controllers On<Filter> method.
Ex: OnAuthorization, OnException, OnActionExecuting, OnActionExecuted, OnResultExecuting, OnResultExecuted

Filters Provided in ASP.NET MVC – are implemented as attributes

1. AuthorizationAttribute – restricts access by authentication and optionally authorization
2. HandleErrorAttribute – Specifies how to handle an exception that is thrown by an action method
3. OutputCacheAttribute – provides output caching
4. RequireHttpsAttribute – Forces unsecured HTTP requests to be resent over HTTPS.

Filter Order – Filters are run in the following order
1. Authorization Filter
2. Action Filter
3. Response Filter
4. Exception Filter

Action-Filter

Bundling and Minification

Bundling is the process which makes it easy to combine or bundle multiple files into a single file. Fewer files means fewer HTTP requests that can improve first page load performance.

Minification is the process of removing all unnecessary characters (whitespace, newline, comments, block delimiters which are used to add readability to the code but not required for execution) or shortening variable names to one character (it can be a form of obfuscation) from source code without changing its functionality. It reduces the network traffic and improves request load time. It is different than compression, in that the minified source can be interpreted immediately without need for uncompression.

Both apply well to CSS, Javascript and other bundles.

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web.config file.

<system.web>
<compilation debug=”true” />
<!– Lines removed for clarity. –>
</system.web>

To enable bundling and minification, set the debug value to “false”. You can override the Web.config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.config file.

public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle(“~/bundles/jquery”).Include(
“~/Scripts/jquery-{version}.js”));

// Code removed for clarity.
BundleTable.EnableOptimizations = true;
}

 

Webpage Performance Optimization

  • Compress HTTP but never images.
  • Provide cachable content with future expiry date.
  • Use conditional requests, GET if modified since.
  • Minify JavaScript; can reduce by 25%.
  • Don’t scale images, resample them.
  • Use image sprites.
  • Avoid inline JavaScript.
  • Linking JavaScript in head prevents HTML render, or use defer=”defer”
  • Avoid embedded styles.
  • Only send styles actually required by the page – server side plug-ins can automate this.
  • Link CSS at top in head.
  • Avoid using @import for hierarchical styles, place them in HTML head.
  • Minimize symbol resolution, cache in method body. Functions also; create function pointers in local method.
  • Use JSON native methods if available.
  • Remove duplicate script file declarations.
  • Minimize DOM interactions, cache references.
  • Built-in DOM methods are always faster.
  • Use selector APIs.
  • Complex element selectors are slow; favor class selectors and child instead of descendant.
  • Apply visual changes in batches.

Same-origin policy and ways to bypass it

Same-origin policy disallows running JavaScript to read media DOM elements or XHR data fetched from outside the page’s origin. The aggregation of the site’s scheme, port number and host name identifies as its origin.Certain “cross-domain” requests, notably Ajax requests, however, are forbidden by default by the same-origin security policy.

Solutions

1. JSONP (JSON with Padding) is used to request data from a server residing in a different domain than the client. But can cause cross site scripting (XSS) issues when external site is compromised. And due to inherent insecurities, JSONP is being replaced by CORS .

2. CORS (Cross-Origin Resource Sharing) is a mechanism that allows restricted resources (eg. fonts) on a web page to be requested from another domain outside the domain from which the first resource was served. For CORS to work, the REST API server must support CORS by setting the “Access-Control-Allow-Origin” things.

Refer for more details : StackOverFlow