0% found this document useful (0 votes)
35 views22 pages

Abstract Class vs Interface in C#

Interfaces are better than abstract classes when defining an object's capabilities, as interfaces define a list of capabilities and data that are class-agnostic. Delegates can be chained together and are used for callbacks. C#'s asynchronous programming model is task-based. To determine if a class has a particular attribute, use the GetCustomAttribute method and pass in the attribute type.

Uploaded by

Rio Nguyen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views22 pages

Abstract Class vs Interface in C#

Interfaces are better than abstract classes when defining an object's capabilities, as interfaces define a list of capabilities and data that are class-agnostic. Delegates can be chained together and are used for callbacks. C#'s asynchronous programming model is task-based. To determine if a class has a particular attribute, use the GetCustomAttribute method and pass in the attribute type.

Uploaded by

Rio Nguyen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C#

Q1. In which of these situations are interfaces better than abstract classes?

  When you need to define an object type's characteristics, use an interface.


When you need to define an object type's capabilities, use an abstract class.

  Interfaces are a legacy of older versions of C#, and are interchangeable


with the newer abstract class feature.

  When you need a list of capabilities and data that are classes-agnostic,
use an interface. When you need a certain object type to share characteristics,
use an abstract class.

  You should use both an interface and an abstract class when defining any
complex object.

Q2. Which statement is true of delegates?

  Delegates are not supported in the current version of C#

  They cannot be used as callbacks.

  Only variables can be passed to delegates as parameters.

  They can be chained together.

Reference Official documentation: Delegates

Q3. Which choice best defines C#'s asynchronous programming model?

  reactive

  inherited callback

  task-based

  callback-based
Reference Official documentation: Task asynchronous programming model

Q4. How would you determine if a class has a particular attribute?

  .

var type = typeof(SomeType);


var attribute = [Link]<SomeAttribute>();

  .

var typeof(MyPresentationModel).Should().BeDecoratedWith<SomeAttribute>();

  .

[Link], typeof(SubControllerActionToViewDataAttribute)

  .

[Link](typeof(ExampleController),
typeof(SubControllerActionToViewDataAttribute))

Reference Official documentation: Attribute Class

Reference Official documentation: [Link] Method

Q5. What is the difference between the ref and out keywords?

  Variables passed to out specify that the parameter is an output parameter,


while ref specifies that a variable may be passed to a function without being
initialized.

  Variables passed to ref can be passed to a function without being


initialized, while out specifies that the value is a reference value that can be
changed inside the calling method.

  Variables passed to out can be passed to a function without being


initialized, while ref specifies that the value is a reference value that can be
changed inside the calling method.
  Variables passed to ref specify that the parameter is an output parameter,
while out specifies that a variable may be passed to a function without being
initialized.

Reference Official documentation: ref

Reference Official documentation: out parameter modifier

Q6. How could you retrieve information about a class, as well as create an
instance at runtime?

  reflection

  serialization

  abstraction

  dependency injection

Reference Official documentation: Reflection

Q7. What is this code an example of?

private static object objA;


private static object objB;

private static void performTaskA()


{
lock (objB)
{
[Link](1000);
lock (objA) { }
}
}

private static void PerformTaskB()


{
lock (objA)
{
lock (objB) { }
}
}

  a private class that uses multithreading

  multithread coding
  thread mismanagement

  a potential deadlock

Reference Official documentation: Deadlocks and race conditions

Q8. What is the difference between an anonymous type and a regular data
type?

  Anonymous types don't have type names

  Anonymous types can only be static

  Anonymous types can be used only in struts

  Anonymous types don't work with LINQ.

Reference Official documentation: Anonymous Types

Q9. When would you use a Dictionary rather that an Array type in your
application?

  when you need a jagged collection structure

  when you need to store values of the same type

  when you need to store key-value pairs rather than single values

  when you need an ordered, searchable list

Reference Official documentation: Dictionary<TKey,TValue> Class

Q10. What is the difference between [Link](b) and a == b?

  The .Equals method compares reference identities while the == compares


contents.

  The .Equals method compares primitive values while == compares all


values.
  The .Equals method compares contents while == compares references
reference identity.

  The .Equals method compares reference type while == compares primitive


value types.

Q11. Which choice best describes a deadlock situation?

  when you try to instantiate two objects at the same time in the same class
or struct

  when you are trying to execute an action after a user event is registered

  when simultaneous instructions are waiting on each other to finish before


executing

  when you try to execute a series of events simultaneously on multiple


threads

Reference Official documentation: Deadlocks and race conditions

Q12. How does the async keyword work?

  It allows access to asynchronous methods in the C# API

  It allows thread pooling and synchronous processes in static classes.

  It allows the await keyword to be used in a method

  It allows access to synchronous methods in the C# API

Reference Official documentation: async

Q13. What is an object in C#?

  a class or struct, including its variables and functions

  a primitive data type that can be created only at compile time


  a value type that can be used only with an abstract class

  an instance of a class or struct that includes fields, properties, and/or


methods

Reference Official documentation: Objects

Q14. Which code snippet declares an anonymous type named userData?

  var<<!---->T> userData = new <<!---->T> { name = "John", age = 32 };

  var userData = new { name = "John", age = 32 };

  AType userData = new AType { name = "John", age = 32 };

  Anonymous<T> userData = new Anonymous<T> { name = "John", age = 32 };

Reference Official documentation: Anonymous Types

Q15. What will be returned when this method is executed?

public void userInput(string charParamters) { }

  nothing

  a Boolean

  a string variable

  an integer

Reference Official documentation: void

Q16. In what order would the employee names in this example be printed to
the console?

string[] employees = { "Joe", "Bob", "Carol", "Alice", "Will" };

IEnumerable<string> employeeQuery = from person in employees


orderby person
select person;

foreach(string employee in employeeQuery)


{
[Link](employee);
}

  ascending

  unordered

  descending

  first in, first out

Reference dotnetpattern: LINQ OrderBy Operator

Q17. Lambda expressions are often used in tandem with which of the
following?

  Namespaces

  LINQ

  Type Aliasing

  Assemblies

Reference Official documentation: Language Integrated Query (LINQ) Overview

Q18. What is the correct formatting for single line and multiline comments?

  // - Single Line / - Multiline

  // Multiline /_ Single Line _/

  //* Multiline / Single Line

  // Single Line /_ Multiline _/

Reference w3schools: C# Comments

Q19. How do you make a method in an abstract class overridable?


  Make it public

  Make it static

  Make it private

  Make it virtual

Reference Official documentation: virtual

Reference Official documentation: abstract

Q20. How would you write code for an integer property called Age with a
getter and setter?

  public int Age { get - set }

  public int Age: get set;

  public int Age (get, set );

  public int Age { get; set; }

Reference Official documentation: Using Properties

Q21. What is an abstract class?

  a class that is denoted by the class keyword (can be seen and used by any
other class in the system--thus it is by default public)

  something denoted by the abstract keyword and used system wide; if you
want any program to create an object of a class you use the abstract class

  a class that is denoted by the virtual keyword

  a class that can be used only as base class

Reference Official documentation: Abstract and Sealed Classes and Class Members


Q22. When using a thread pool what happens to a given thread after it finishes
its task?

  The thread is destroyed and memory is freed up.

  The thread runs in loop until the next assignment.

  The thread goes inactive in the background and waits for garbage
collection.

  The thread returns to the pool for reuse.

Reference Official documentation: Thread pool characteristics

Q23. Which choice represents a class that inherits behavior from a base class?

  a second base class

  a revised class

  a derived class

  a parent class

Reference Official documentation: Inheritance

Q24. What does operator overloading allow you to do?

  hide built-in operatores when necessary

  add methods to be interpreted by the compiler at runtime

  define how enums and other primitive value types work within the rest of
the application

  define custom functionality for common operators like addition and


equality

Reference Official documentation: Operator overloading


Q25. What it the main purpose of LINQ?

  to delete duplicate data

  to bind namespaces and assemblies

  to query and transform data

  to connect assemblies

Reference Official documentation: Language Integrated Query (LINQ) Overview

Q26. What is the correct syntax for a new generic list of strings named
contacts?

  public List contacts = new List();

  public List(string names) contacts = new List(string names)();

  var contacts = new List();

  var contacts = new List(string);

Reference Official documentation: List Class

Q27. What is the difference between throw exceptions and throw clauses?

  Throw clauses fire only at runtime, while throw exceptions can fire at any
time.

  Throw exceptions overwrite the stack trace, while throw clauses retain the
stack information.

  Throw clauses overwrite the stack trace, while throw exceptions retain the
stack information.

  Throw exceptions fire only at runtime, while throw clauses can fire during
compile time.

Reference Official documentation: throw


Reference c-sharpcorner: Difference Between Throw Exception and Throw Clause

Q28. When an asynchronous method is executed, the code runs but nothing
happens other than a compiler warning. What is most likely causing the
method to not return anything?

  The return yield statement is missing at the end of the method.

  The method is missing an await keyword in its body.

  The wait keyword is missing from the end of the method.

  The yield keyword is missing from the method.

Q29. What are C# events?

  system actions that communicate directly with the compiler at runtime

  actions that execute when the code compiles, generating logs and test
output

  actions that generate notifications, which are sent to their registered


listeners

  user-only methods that send data to the application's back end

Reference Official documentation: Introduction to events

Q30. What kind of values can arrays store?

  unordered collections of numerc values

  key-value pairs of any C# supported type

  class and struct instances

  multiple variables, or collections, of the same type

Reference Official documentation: Arrays


Q31. Given this enumeration, how would you access the integer-type value of
'[Link]'?

enum AppState { OffLine, Loading, Ready }

  string currentState = (string)[Link];

  string currentState = [Link];

  int currentState = [Link];

  int currentState = (int)[Link];

Reference Official documentation: Enumeration types

Q32. What character would you use to start a regular expression pattern at a
word boundary?

  d

  \a

  \b

  \w

Reference regular-expressions: Word Boundaries

Reference Official documentation: Regular Expression Language - Quick Reference

Q33. To conform to the following interface, which of its members need to be


implemented?

public interface INameble


{
string FirstName { get; set; }
string LastName { get; }
}

  Both the FirstName and LastName properties need to be implemented.

  Neither, they are both optional.


  Only the LastName property needs to be implemented.

  Only the FirstName property needs to be implemented.

Reference Official documentation: interface

Q34. You're dealing with multiple assemblies in your program, but are worried
about memory allocation. At what point in the program life cycle are
assemblies loaded into memory?

  at runtime

  at compile time

  only when required

  only when programmatically loaded

Q35. What is most accurate description of a regular expression?

  A regular expression is a C# tool used to parse HTML

  A regular expression is a special text string for describing a search patters.

  A regular expression allows a variable to be passed by reference.

  A regular expression allows a class to conform to the Equatable protocol.

Reference Official documentation: Regular Expression Language - Quick Reference

Reference Official documentation: .NET regular expressions

Q36. Why would you use a class field in C#

  To define behaviours of the class

  To hold information and data contained in the class object

  To communicate between classes and object


  To store the class definition value

Reference Official documentation: Introduction to classes

Q37. When would you use generics in your code?

  to increase code performance

  all of these answers

  when code reuse is a priority

  when type safety is important

Reference Official documentation: Generic classes and methods

Q38. What prints to the console when this code is executed?

public delegate void AuthCallback(bool validUser);


public static AuthCallback loginCallback = Login;
public static void Login()
{
[Link]("Valid user!");
}

public static void Main(string[] args)


{
loginCallback(true);
}

  Login successful...

  Valid user!

  an error, because the method signature of Login doesn't match the
delegate

  Login successful... Valid user!

Reference Official documentation: Introduction to Delegates

Reference Official documentation: Introduction to Events


Q39. How would you declare a sealed class named User?

  public class User

  abstract User {}

  sealed class User

  private sealed class User

Reference Official documentation: Abstract and Sealed Classes and Class Members

Q40. What is the correct syntax for a new generic list of strings named
contacts? (similar to Q26)

  var contacts = new List<string>();

  var contacts = new List(string);

  public List<string names> contacts = new List<string names>();

  public List(string names) contacts = new List(string names);

Reference Official documentation: List

Q41. What is the difference between non-static and static classes?

  non-static classes need to be initialized before use, while static classes do


not

  non-static classes are accessible only from an interface while static classes
are accessible from anywhere

  non-static classes need to initialize all class members at runtime, while


static classes do not

  non-static classes do not need to be initialized while static classes do

Reference stackoverflow
Reference Official documentation: Static Constructors

Q42. Which characteristic prevents this code from compiling?

public int age="28"

  type safety

  single inheritance

  dependency injection

  multiple inheritance

Reference c-sharpcorner: Type Safety in .NET

Q43. How would you serialize this class?

public class User {}

  Mark the User class with the DeserializableAttribute.

  Declare the class as public serializable class User {}.

  Mark the User class with the SerializableAttribute attribute.

  Declare the class as private serializable class User {}.

Reference Official documentation: SerializableAttribute Class

Q44. How would you write a delegate named ResultCallback with an int
parameter named responseCode

  public delegate ResultCallback(int responseCode)

  public delegate void ResultCallback<(int) responseCode>;

  public void delegate ResultCallback;

  public delegate void ResultCallback(int responseCode);

Reference Official documentation: Delegates


Q45. What is the difference between a static and non-static method?

  non-static methods always need to have a void return type

  non-static methods do not have access to static member variables

  static methods do not have to instantiate an instance of the class to call


the method

  static methods always have to be public

Reference Official documentation: Static Members

Q46. What is the correct way to write an event named apiResult based on a
delegate named ResultCallback?

  public void event ResultCallback apiResult;

  public event ResultCallback(() -> apiResult);

  public event void ResultCallback

  public event ResultCallback apiResult;

Reference Official documentation: Introduction to events

Q47. When will the code inside finally block be executed in a try-catch
statement?

  if there is an error, it won't execute at all

  between the try and catch blocks

  after the try and catch blocks

  when the finally block overrides the catch block and executes in its place

Reference Official documentation: try-catch

Q48. What method correctly extends the string class?


  public static string IsvalidName(this string i, string value) {}

  public static void IsvalidName(this string i, string value) {}

  public string IsvalidName(this string i, string value) {}

  public void IsvalidName(this string i, string value) {}

Q49. How are C# classses limited?

  They do not support multiple inheritance.

  They support multiple inheritance.

  They can have only a set number of properties.

  They can have only a set number of methods.

Reference Official documentation: Class inheritance

Q50. What function do namespaces perform?

  Namespaces calculate code coverage at runtime.

  Namespaces compile application code together at compile time.

  Namespaces group code together into a single repository.

  Namespaces separate code into groupings, control access, and void


naming collisions.

Reference Official documentation: namespace

Q51. What is the correct way to write a public property with a private backing
field?

 []

private int _password;


pubic int Password = { get; set; }
 []

private int _password;


public int Password = _password;

 []

private int _password;


public int Password
{
get -> _password;
set-> _password = value;
}

 [x]

private int _password;


public int Password
{
get { return _password; }
set { _password = value; }
}

Reference Official documentation: Using Properties

Q52. What is a thread pool?

  a collection of synchronous methods created during initialization that


cannot be reused

  a collection of threads created during initialization that can be reused

  a collection of threads only recognized at compile time that can be reused

  a collection of asynchronous methods created at compile time that cannot


be reused

Reference Official documentation: ThreadPool Class

Q53. What is the most accurate description of a regular expression?

  A regular expressions allows a variable to be passed by reference

  A regular expression allows a class to conform to the Equatable protocol

  A regular expression is a C# tool used to parse HTML


  A regular expression is a special text string for describing a search pattern

Reference Official documentation: Regular Expression Language - Quick Reference

Q54. When an object in C# is serialized, what is it converted to?

  XML

  JSON

  byte stream

  value stream

Reference: Official documentation: Serialization

Q55. What is a delegate

  a variable that holds a reference to a value type and its content

  a specific value type that can be used only in callback methods

  a type that holds a reference to a method with a particular parameter list


and return type

  a custom variable type that can be used in abstract classes

Reference: Official documentation: Delegates

Q56. What are the four keywords associated with exception handling in C#?

  try, catch, valid, invalid

  try, valid, finally, throw

  try, catch, finally, throw

  finally, throw, valid, invalid


Reference: Tutorial Point

Q57. What is the main difference between the is and as operators?

  The is operator checks instance types, while the as operator checks the
inherited type.

  The is operator checks primitive data types, while the as operator checks
the object type.

  The as operator checks object type, while the is operator attempts to cast
an object to a specific type.

  The is operator checks object type, while the as operator attempts to cast
an object to a specific type.

Reference: Pluralsight guide

Q58. What is the difference between finally and finalize blocks?

  The finally block is called during the execution of a try and catch block,
while the finalize method is called after garbage collection.

  The finally block is called after the execution of a try and catch block,
while the finalize method is called just before garbage collection.

  The finalize block is called before the execution of a try and catch block,
while the finally method is called just before garbage collection.

  The finalize block is called during the execution of a try and catch block,
while the finally method is called after garbage collection.

Reference: C-sharpcorner

Q59. Your application has a value type called username that needs to be able to
accept null values, but this is generating compile-time errors. How would you
fix this in code?

  Null username = null;


  string? username = null;

  Type? username = null;

  Optional username = null;

Q60. Which code snippet correctly declares a custom exception named


InvalidResponse?

  struct InvalidResponse: Exception {}

  class InvalidResponse: Exception {}

  public Exception InvalidResponse = new Exception ();

  public Exception InvalidResponse () -> Exception;

Reference: Official documentation: Exceptions

Q61. How would you write an enum variable called AppState with values for
Offline, Loading, and Ready?

  enum AppState = [Offline, Loading, Ready]

  enum AppState {"Offline", "Loading", "Ready"}

  enum AppState = {Offline, Loading, Ready}

  enum AppState {Offline, Loading, Ready}

Reference: Official documentation: Enum

You might also like