C# .NET quiz questions

C# .NET interview questions

  • 1.

    You are using a multicast delegate with multiple subscribers. You want to make sure that all subscribers are notifed, even if an exception is thrown. What do you do?

    1. Manually raise the events by using GetInvocationList.

    2. Wrap the raising of the event in a try/catch.

    3. Nothing. This is the default behavior.

    4. Let subscribers return true or false instead of throwing an exception.

    Answer
  • 2.

    You have declared an event on your class, and you want outside users of your class to raise this event. What do you do?

    1. Make the event public.

    2. Add a public method to your class that raises the event.

    3. Use a public delegate instead of an event.

    4. Use a custom event accessor to give access to outside users.

    Answer
  • 3.

    You have a private method in your class and you want to make invocation of the method possible by certain callers. What do you do?

    1. Make the method public.

    2. Use an event so outside users can be notifed when the method is executed.

    3. Use a method that returns a delegate to authorized callers.

    4. Declare the private method as a lambda.

    Answer
  • 4.

    You can customize events by adding a custom event accessor and by directly using the underlying delegate type.
     

    1. True

    2. False

    Answer
  • 5.

    Events can be raised only from the declaring class. Users of events can only remove and add methods the invocation list.
     

    1. True

    2. False

    Answer
  • 6.

    Events are a layer of syntactic sugar on top of delegates to easily implement the publish-subscribe pattern.
     

    1. True

    2. False

    Answer
  • 7.

    Lambda expressions, also known as anonymous methods, use the = operator and form a compact way of creating inline methods.

    1. True

    2. False

    Answer
  • 8.

    Delegates can be instantiated, passed around, and invoked.

    1. True

    2. False

    Answer
  • 9.

    Delegates are a type that defnes a method signature and can contain a reference to a method.

    1. True

    2. False

    Answer
  • 10.

    You are implementing a state machine in a multithreaded class. You need to check what the current state is and change it to the new one on each step. Which method do you use?

    1. Volatile.Write(ref currentState)

    2. Interlocked.CompareExchange(ref currentState, ref newState, expectedState)

    3. Interlocked.Exchange(ref currentState, newState)

    4. Interlocked.Decrement(ref newState)

    Answer
  • 11.

    You need to implement cancellation for a long running task. Which object do you pass to the task?

    1. CancellationTokenSource

    2. CancellationToken

    3. Boolean isCancelled variable

    4. Volatile

    Answer
  • 12.

    You can cancel tasks by using the CancellationTokenSource class with a CancellationToken.

    1. True

    2. False

    Answer
  • 13.

    You can use the Interlocked class to execute simple atomic operations.

    1. True

    2. False

    Answer
  • 14.

    Use the lock statement on a private object to synchronize access to a piece of code.

    1. True

    2. False

    Answer
  • 15.

    When accessing shared data in a multithreaded environment, you do not need to synchronize access to avoid errors or corrupted data.
     

    1. True

    2. False

    Answer
  • 16.

    You are working on an ASP.NET application that retrieves some data from another web server and then writes the response to the database. Should you use async/await?

    1. No, both operations depend on external factors. You need to wait before they are fnished.
       

    2. No, in a server application you don’t have to use async/await. It’s only for responsiveness on the client.

    3. Yes, this will free your thread to serve other requests while waiting for the I/O to
      complete.

    4. Yes, this put your thread to sleep while waiting for I/O so that it doesn’t use any
      CPU

    Answer
  • 17.

    You are creating a complex query that doesn’t require any particular order and you want to run it in parallel. Which method should you use?
     

    1. AsParallel

    2. AsSequential

    3. AsOrdered

    4. WithDegreeOfParallelism

    Answer
  • 18.

    You have a lot of items that need to be processed. For each item, you need to perform a complex calculation. Which technique should you use?
     

    1. You create a Task for each item and then wait until all tasks are fnished.

    2. You use Parallel. For to process all items concurrently

    3. You use async/await to process all items concurrently.

    4. You add all items to a BlockingCollection and process them on a thread created by
      the Thread class.

    Answer
  • 19.

    Concurrent collections can be used to safely work with data in a multithreaded (concurrent access) environment.

    1. True

    2. False

    Answer
  • 20.

    The new async and await operators can be used to write asynchronous code more easily.

    1. True

    2. False

    Answer

© 2017 QuizBucket.org