.NET/C# Development
What is the result of the final conditional statement? int age = 20; return age < 20 ? "yes" : "no" 1. "no" 2. "yes" 3. An exception will occur 4. null
1. "No"
What default value will C# give to the first member of an enum? 1. 0 2. 128 3. 1 4. 1024
1. 0
What name do we give to the special methods used when creating an instance of a class? 1. Constructors 2. Newers 3. Initializers 4. Creators
1. Constructors
What are the three pillars of object oriented programming?
1. Encapsulation 2. Inheritance 3. Polymorphism (Arguably there are 4...include abstraction)
Read-only variables and constants have many similarities, but what is at least one way that they differ?
1. Read-only variables can support reference-type variables. Constants can hold only value-type variables. 2. Developers evaluate read-only variables at the runtime. They evaluate constants at the compile time.
What C# keyword allows direct access to base class members? 1. base 2. parent 3. super 4 this
1. base
Which of the following file extensions is common for a .NET assembly? 1. dll 2. asm 3. ocx 4. asmx
1. dll
Which of the following looping statements is guaranteed to execute at least once? 1. do while 2. for 3. foreach 4. while
1. do while
Which keyword allows a method to use a variable number of parameters? 1. params 2. ref 3. using 4. enum
1. params
Which of the following is not part of a method signature in C#? 1. The types of parameters 2. The return type 3. The number of parameters 4. The method name
2. The return type
How are delegates useful in C#? 1. We can use delegates to setup inheritance relationships. 2. We can use delegates to define variables that refer to methods. 3. We can use delegates to define value types. 4. We can use delegates to define abstract types.
2. We can use delegates to define variables that refer to methods.
What is the default access modifier for a class? 1. public 2. internal 3. protected 4. private
2. internal
A class member without an access modifier will have what level of protection? 1. protected 2. private 3. public 4. internal
2. private
What keyword will make classes inside a namespace available to use without using their full names? 1. requires 2. using 3. module 4. import
2. using
What is the name of the implicit variable containing the incoming value for a property's set accessor? 1. params 2. value 3. get 4. set
2. value
Which method of a string object will modify the underlying string? 1. ToUpper 2. Trim 3. No method will modify the string. 4. Split
3. No method will modify the string.
What is the default C# behavior for passing data to a method? 1. Pass by handle 2. Pass by reference 3. Pass by value 4. Pass by pointer
3. Pass by value
What does a variable of type int hold? 1. A pointer to an integer 2. An integer interface 3. The value of an integer 4. An operating system handle
3. The value of an integer
What is the purpose of the throw keyword? 1. To stop the current iteration of a loop and jump to the code following the loop. 2. To broadcast an event to all subscribers. 3. To raise an exception. 4. To stop the current iteration of a loop and start the next iteration.
3. To raise an exception.
If NameChanged is an event of type NameChangedDelegate, which of the following snippets is legal? 1. book.NameChanged = new NameChangedDelegate(OnNameChanged); 2. book.NameChanged = null 3. book.NameChanged += new NameChangedDelegate(OnNameChanged);
3. book.NameChanged += new NameChangedDelegate(OnNameChanged);
What statement will jump to the beginning of a loop and start the next iteration? 1. break 2. return 3. continue 4. throw
3. continue
What is the conventional file extension for C# source code files? 1. c# 2. csharp 3. cs 4. csp
3. cs
Which of the following keywords will pass a method parameter by reference? 1. in 2. struct 3. out 4. class
3. out
The name for a class member that is a method typically starts with a ... 1. noun 2. pronoun 3. verb 4. conjunction
3. verb
Which of the following is a legal expression for the case label of a switch statement? 1. x != 90 2. x < 90 && x > 90 3. !x.Equals(90) 4. "90"
4. "90"
What is the implicit base class for a class that does not specify a base class? 1. string 2. byte 3. interface 4. object
4. Object
Event arguments in .NET should derive from which framework class? 1. System.Delegate 2. System.Event 3. System.Object 4. System.EventArgs
4. System.EventArgs
What is a use of the void keyword? 1. You can use void to build a class 2. The member can only be a field 3. The member can only be a method 4. You can use void as the return type of a method with no return statement
4. You can use void as the return type of a method with no return statement
What is the difference between a class and an object, and how do these terms relate to each other?
A class is a comprehensive data type that is the primary building block, or template, of OOP. Class defines attributes and methods of objects, and contains an object's behavior and data. An object, on the other hand, represents an instance of class. As a basic unit of a system, objects have identity and behavior as well as attributes. The relationship is based on the fact that a class defines the states and properties that are common to a range of objects.
What is a delegate in .NET?
A delegate in .NET is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. In addition, we could use delegate to create custom event within a class.
Explain a delegate in .NET.
A delegate is a type that encapsulates a reference to a method. Delegate objects can then be passed to code which calls the method according to the method signature, which means the developer doesn't have to know at compile time which method is being invoked. A delegate can contain references to a single method or multiple methods. Delegates are similar to function pointers in C/C++ and have the advantage of being type-safe.
Describe the basic construction of a C# program. Write a simple program that outputs "Hello World" to the console.
A typical C# program consists of a namespace declaration, a class, methods, attributes, a main method, statements, expressions, and comments. using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { Console.WriteLine("Hello World"); Console.ReadKey(); } } }
What is the difference between an abstract class and an interface?
An abstract class is always used as a base class. It provides some abstract/virtual members that the inheriting entities must implement, as well as a partial implementation for a functionality. This class can also declare fields. Developers cannot create an object from this class. An interface, on the other hand, can declare properties, methods and events only (no access modifiers). The developer must implement all declared members. In short, an interface designates a contract/behavior that implementing classes should have.
Explain the differences between an Interface and an Abstract Class in .NET.
An interface merely declares a contract or a behavior that implementing classes should have. It may declare only properties, methods, and events with no access modifiers. All the declared members must be implemented. An abstract class provides a partial implementation for a functionality and some abstract/virtual members that must be implemented by the inheriting entities. It can declare fields too. Neither interfaces nor abstract classes can be instantiated.
Predict the output of the code below. delegate void Iterator(); static void Main() { List iterators = new List(); For (int i = 0; i < 15; i++) { iterators.Add(delegate { Console.WriteLine(i); }); } Foreach (var iterator in iterators) { iterator(); } }
At first glance, one would expect the program to output the numbers 0 to 15, fifteen times. Instead, the number 15 is printed fifteen times. Since the delegate is being added within the for loop, and because the delegate is only referencing the variable i instead of the value itself, the loop sets the value of the variable i to 15 before it is invoked within each delegate.
Explain the difference between the while and for loop. Provide a .NET syntax for both loops
Both loops are used when a unit of code needs to execute repeatedly. The difference is that the for loop is used when you know how many times you need to iterate through the code. On the other hand, the while loop is used when you need to repeat something until a given statement is true.
Explain the difference between boxing and unboxing. Provide an example
Boxing is the process of converting a value type to the type object, and unboxing is extracting the value type from the object. While the boxing is implicit, unboxing is explicit. Example (written in C#): int i = 13; object myObject = i; // boxing i = (int)myObject; // unboxing
Explain the difference between managed and unmanaged code.
Code written in C# or Visual Basic.NET will, when compiled, run only in the CLR, which provides functionalities such as garbage collection and memory management. The advantage of this is that managed code is platform-independent — because it runs in the CLR rather than the operating system of the machine accessing the application. Code written in other languages, such as C or C++, produces unmanaged code, meaning that developers can't rely on the CLR to provide this kind of portability. Managed and unmanaged code are interoperable. Examples of unmanaged code used in .NET include COM components, ActiveX interfaces and Win32 API functions.
How does C# handle encapsulation?
Encapsulation is a classic object-oriented design principle that reduces coupling between objects and encourages maintainable code. It involves enclosing objects within a logical package by limiting access to implementation details. In C#, this is accomplished through the access specifiers—public, private, protected, internal, and protected internal.
What is encapsulation?
Encapsulation is one of four basic features of OOP and refers to the inclusion within a program object of methods and data needed for the object to function. Encapsulation helps keep data from unwanted access through binding code and data in an object, which is the basic, single self-contained unit of a system. Another way of understanding encapsulation is to think of it as "hiding" the state of an object as private or protected. Under this principle of information hiding, the internal workings of an object are segregated from the rest of the application. This is useful because it makes it less likely that other objects can modify the state or behavior of the object in question.
Discuss what garbage collection is and how it works. Provide a code example of how you can enforce garbage collection in .NET
Garbage collection is a low-priority process that serves as an automatic memory manager which manages the allocation and release of memory for the applications. Each time a new object is created, the common language runtime allocates memory for that object from the managed Heap. As long as free memory space is available in the managed Heap, the runtime continues to allocate space for new objects. However, memory is not infinite, and once an application fills the Heap memory space, garbage collection comes into play to free some memory. When the garbage collector performs a collection, it checks for objects in the managed Heap that are no longer being used by the application and performs the necessary operations to reclaim the memory. Garbage collection will stop all running threads, it will find all objects in the Heap that are not being accessed by the main program and delete them. It will then reorganize all the objects left in the Heap to make space and adjust all the Pointers to these objects in both the Stack and the Heap. To enforce garbage collection in your code manually, you can run the following command (written in C#): System.GC.Collect();
What are some of the features of generics in C#?
Generics allow a developer to define a class or method that can work with virtually any data type by delaying specification of the programming elements' data types until they are needed. Generics come with a number of features. 1. They make C# code reusable, type safe, and performance optimized. 2. They allow the developer to create generic classes, methods, events, delegates, and interfaces. 3. They allow the developer to create generic collection classes in the System.Collections.Generic namespace. 4. They make it possible to get real-time information on the types used in a generic data type at runtime via reflection.
What do the following acronyms in .NET stand for: IL, CIL, MSIL, CLI and JIT?
IL, or Intermediate Language, is a CPU independent partially compiled code. IL code will be compiled to native machine code using current environmental properties by Just-In-Time compiler (JIT). JIT compiler translates the IL code to an assembly code and uses the CPU architecture of the target machine to execute a .NET application. In .NET, IL is called Common Intermediate Language (CIL), and in the early .NET days it was called Microsoft Intermediate Language (MSIL). CLI, or Common Language Infrastructure, is an open specification developed by Microsoft. It is a compiled code library used for deployment, versioning, and security. In .NET there are two CLI types: process assemblies (EXE) and library assemblies (DLL). CLI assemblies contain code in CIL, and as mentioned, during compilation of CLI programming languages, the source code is translated into CIL code rather than into platform or processor specific object code. To summarize: 1. When compiled, source code is first translated to IL (in .NET, that is CIL, and previously called MSIL). 2. CIL is then assembled into a bytecode and a CLI assembly is created. 3. Before code execution, CLI code is passed through the runtime's JIT compiler to generate native machine code. 4. The computer's processor executes the native machine code.
Value types are typically _______________?
Immutable
Explain deferred execution vs. immediate execution in LINQ. Provide examples.
In LINQ, deferred execution simply means that the query is not executed at the time it is specified. Specifically, this is accomplished by assigning the query to a variable. When this is done, the query definition is stored in the variable but the query is not executed until the query variable is iterated over. You can also force immediate execution of a query. This can be useful, for example, if the database is being updated frequently, and it is important in the logic of your program to ensure that the results you're accessing are those returned at the point in your code where the query was specified. Immediate execution is often forced using a method such as Average, Sum, Count, List, ToList, or ToArray. For example:
Explain the concept of inheritance and how it works to .NET.
In general OOP terms, inheritance means that a class can be based on another class, with the child class taking on the attributes of the parent class. For example, coders can create a class called Vehicle, and then child classes called Truck, Car, Motorcycle — all of which inherit the attributes of Vehicle. .NET supports single inheritance only, which means that a class can inherit only from one other class. Transitive nature of inheritance — for example, the Ford class inherits from Car, which inherits from Vehicle.
Explain the difference between a class and an object
In short, a class is the definition of an object, and an object is instance of a class. We can look at the class as a template of the object: it describes all the properties, methods, states and behaviors that the implementing object will have. As mentioned, an object is an instance of a class, and a class does not become an object until it is instantiated. There can be more instances of objects based on the one class, each with different properties.
Explain what inheritance is, and why it's important
Inheritance is one of the most important concepts in object-oriented programming, together with encapsulation and polymorphism. Inheritance allows developers to create new classes that reuse, extend, and modify the behavior defined in other classes. This enables code reuse and speeds up development. With inheritance, developers can write and debug one class only once, and then reuse that same code as the basis for the new classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. By default, all classes in .NET are inheritable.
When break is used inside two nested for loops, control comes out of which loop, the inner or the outer for loop? (I.e. does it break from all the present loops?)
It breaks from the inner loop only.
Why can't you specify access modifiers for items in an interface?
It is always public
How do you implement a generic action in WebAPI?
It's not possible, as the WebAPI runtime needs to know the method signatures in advance.
What is JSON data, and what is one way that .NET developers can work with JSON?
JSON (JavaScript Object Notation) provides developers with a way to organize and store data in a way that is easy to access and read. JSON is important for developers because it allows them to manipulate JSON feeds from other sites and to load them more quickly and easily than via SML/RSS feeds. Json.NET provides a way for .NET developers to define classes to parse objects and arrays from JSON text. You can also use Json.NET if you need to serialize value types into JSON text. Json.NET runs on .NET2, .NET3 and .NET4.
Explain what LINQ is
LINQ is an acronym for Language Integrated Query, and was introduced with Visual Studio 2008. LINQ is a set of features that extends query capabilities to the .NET language syntax by adding sets of new standard query operators that allow data manipulation, regardless of the data source. Supported data sources are: .NET Framework collections, SQL Server databases, ADO.NET Datasets, XML documents, and any collection of objects that support IEnumerable or the generic IEnumerable<T> interface, in both C# and Visual Basic. In short, LINQ bridges the gap between the world of objects and the world of data.
Define LINQ
LINQ stands for Language Integrated Query. This is a Microsoft programming model and methodology that offers developers a way to manipulate data using a succinct yet expressive syntax. It does so by instilling Microsoft .NET-based programming languages with the ability to make formal queries. It is part of C# and can be imported as a library in other languages.
Explain the difference between managed and unmanaged code
Managed code is a code created by the .NET compiler. It does not depend on the architecture of the target machine because it is executed by the CLR (Common Language Runtime), and not by the operating system itself. CLR and managed code offers developers few benefits, like garbage collection, type checking and exceptions handling. On the other hand, unmanaged code is directly compiled to native machine code and depends on the architecture of the target machine. It is executed directly by the operating system. In the unmanaged code, the developer has to make sure he is dealing with memory usage and allocation (especially because of memory leaks), type safety and exceptions manually. In .NET, Visual Basic and C# compiler creates managed code. To get unmanaged code, the application has to be written in C or C++.
What is Operator Overloading and how does it work?
Most of the built-in operators available in C# can be overloaded or redefined using the operator keyword. The sample code below depicts the syntax used to implement the addition operator (+) for a user-defined class. public static Rectangle operator+ (Rectangle b, Rectangle c) { Rectangle rectangle = new Rectangle(); rectangle.length = b.length + c.length; rectangle.breadth = b.breadth + c.breadth; rectangle.height = b.height + c.height; return rectangle; }
Explain nullable types in C#.
Nullable types are data types that, in addition to their normal values, also contain a defined data type for null. Nullable types exist to help integrate C#, which generally works with value types, and databases, which often use null values. You can declare a nullable type in C# using the following syntax:
What is OOP, and how does it relate to the .NET framework?
OOP stands for object-oriented programming. OOP languages such as Visual Basic.NET, C# and C++ are the core languages supported by .NET Framework. (There is also support for functional programming in the form of F#.) As a technique, OOP allows .NET developers to create classes containing methods, properties, fields, events and other logical modules. It also lets developers create modular programs, which they can assemble as applications. OOPs have four basic features: encapsulation, abstraction, polymorphism and inheritance.
Explain the difference between a stack and a queue.
Stacks and queues are two examples of collections. (Hash tables, bags, dictionaries and lists also fall into this category.) A stack keeps track of what is executing and contains stored value types to be accessed and processed as LIFO (Last-In-First-Out), with elements inserted and deleted from the top end. A queue, on the other hand, lists items on a FIFO (First-In-First-Out) basis in terms of both insertion and deletion, with items inserted from the rear end and deleting items from the front end of the queue.
Explain the role of structs in C#. Why would you choose to define a type as a struct instead of a class?
Structs are used to create structures, a value type that is used to represent a record. Structs play a similar role to classes, which are a reference type in C#. Since structs are value types, they are allocated and deallocated on the stack or inline in containing types. This generally makes structs cheaper to allocate or deallocate than classes, which are reference types. Reference types are allocated on the heap and garbage-collected, which tends to take up more resources. If the instances of a type are small and short-lived, it makes sense to define a value type using structs. The opposite is true when it comes to boxing and unboxing. A value type can get boxed when it is cast to a reference type or some other interface it implements. Because boxes are objects allocated to the heap and deallocated via garbage collection, too much boxing and unboxing of a value can negatively impact performance. Reference types like classes are preferred in these situations.
You would know that System.Object is the parent class of all .NET classes; In other words all types in .NET (whether implicit, explicit, or user-created) derive from the System.Object class. What are the various methods provided to System.Object's deriving classes/types?
System.Object provides the following important methods, among others: ToString—Returns a string that represents the current object both overrides of Equals(object), Equals(object, object) GetHashCode Finalize GetType ReferenceEquals MemberwiseClone Most of these methods provide the basic implementation required of any type that a developer will work with in the .NET stack.
What is the CLR?
The Common Language Runtime
What is the difference between dynamic type variables and object type variables in C#?
The difference between dynamic and object type variables has to do with when the type checking takes place during the application lifecycle. Dynamic type variables handle type checking at run time, while object type variables handle type checking during compile time.
What do the terms "boxing" and "unboxing" mean?
The idea is relatively simple: Boxing is a process that converts a value type to an object type — by "boxing" the variable inside a dedicated object or interface. Unboxing extracts this value and stores it in a value type. Boxing was essential in some old Collection types such as ArrayList, and can still be used for accurate conversion of types — for example, from a Double to an Int.
Explain the difference between the Stack and the Heap
The short answer would be: in the Stack are stored value types (types inherited from System.ValueType), and in the Heap are stored reference types (types inherited from System.Object). We can say the Stack is responsible for keeping track of what is actually executing and where each executing thread is (each thread has its own Stack). The Heap, on the other hand, is responsible for keeping track of the data, or more precise objects.
What are three of common acronyms used in .NET, and what do they stand for?
Three frequently used acronyms in .NET are IL, CIL and CLI: IL stands for Intermediate Language, which is an object-oriented programming language that is a partially compiled code that .NET developers will then compile to native machine code. CIL stands for Common Intermediate Language, formerly known as Microsoft Intermediate Language (MSIL). This is another programming language that .NET developers use, and it represents the lowest possible level for a language that humans can still read. CLI stands for Common Language Infrastructure. This is a compiled code library that Microsoft developed as an open specification. Developers use CLI for security, versioning and deployment purposes. Other acronyms commonly used in .NET include JIT (Just-in-Time) compiler, which uses the target machine's CPU architecture to perform a .NET operation; OOP (object-oriented programming); CLR (Common Language Runtime); and LINQ (Language Integrated Query).
What's the purpose of the finally keyword in C#?
To ensure a block of code executes even when there is an error condition.
When should you use .NET Web Forms over ASP.NET MVC?
Traditionally, the .NET framework has been based on web forms. This was essentially an effort to create web services using Microsoft's existing Visual tools without forcing developers to learn new scripting languages. Web Forms still allows developers to create quick and simple applications, and some legacy systems may still run as Web Forms. ASP.NET MVC is increasingly the standard for contemporary developers, however. MVC's most important feature is that it allows applications to be broken down into discrete models, views and controllers, making them much easier to test during development.
Name three ways to pass parameters to a method in C#
Value Parameters: Passing a parameter to a method by value creates a new storage location for the value parameter. Any changes to the value parameter by the method have no effect on the argument. Reference Parameters: Passing a parameter to a method by reference can be achieved by using the ref keyword. Instead of creating a new storage location for the parameter, the method accesses the memory location of the argument and passes it as a parameter. Changes made to the parameter will also affect the argument. Output Parameters: The out keyword allows a method to return two values from a function. It's similar to passing a reference parameter, except in this case data is being transferred out of the method.
What is .NET web service?
Web services are reusable components that allow developers to publish an application's function over the internet to make it accessible and directly interactable with other applications and objects online. Web services communicate by using standard web protocols and data formats — including HTTP, XML and SOAP — that allow them to connect across different platforms and programming languages. ASP.NET provides a simple way to develop web services. The .NET framework provides built-in classes for building and consuming web services.
Discuss the difference between constants and read-only variables
While constants and read-only variable share many similarities, there are some important differences: Constants are evaluated at compile time, while the read-only variables are evaluated at run time. Constants support only value-type variables, while read-only variables can hold reference-type variables. Constants should be used when the value is not changing during run time, and read-only variables are used mostly when their actual value is unknown before run time. Read-only variables can only be initialized at the time of declaration or in a constructor.
What type is an array?
reference type
Write a C# method to total all the even numbers in an array of ints.
static long TotalAllEvenInts(int[] intArray) { return (from i in intArray where i % 2 == 0 select (long)i).Sum(); }