Fremus.co.za

Demistifying Life and Web Development

My Quizzes, 70-536

I thought I would create a page relating to TS: Microsoft .NET Framework – Application Development Foundation. I want to try and understand as much as I can, not just pass the exam, so I want to create my own basic questions around the content.

1. What is the difference between a Value Type and a Reference type?

The key difference between a value type and a reference type is the way in which their values are stored in memory.

2. What are value types?

Value types are stored on the Stack. Value types are all the intrinsic (built-in) types that C# uses as well as enumerations (enums) and structs.

3. What is the stack?

The stack is a section of memory that stores values for the currently executing program. You can think of a stack of plates as all the variables within a program or application. Once all the plates have been washed there is room for more plates. The variables are stored in a Last In First Out fashion. Memory reserved on the memory stack for a method is automatically reserved and reused as the method executes.

4. What are reference types?

Reference types sit on the stack but they hold the address of an object on the heap. Reference types store the address of their data on the stack, with the actual data being stored on the heap.

5. What is the heap?

The heap is an area of memory that stores the actual data of a reference type. The lifetime of the data on the heap varies and might exist for the entire lifetime of an application. The memory is managed by the Garbage Collector. The garbage collector periodically releases memory when objects are no longer being referenced.

6. List the intrinsic types in C#, specifying whether they are CLS compliant or not, what their equivalent CLR Type is. Also give a description of each type.

C# Type CLS Compliant CLR Type Description
byte Yes System.Byte 8 bit unsigned integer
sbyte No System.SByte 8 bit signed integer
short Yes System.Int16 16 bit signed integer
ushort No System.UInt16 16 bit unsigned integer
int Yes System.Int32 32 bit signed integer
uint No System.UInt32 32 bit unsigned integer
long Yes System.Int64 64 bit signed integer
ulong No System.UInt64 64 bit unsigned integer
float Yes System.Single 32 bit; IEEE conforming floating-point value
double Yes System.Double 64 bit; IEEE conforming float-point value
decimal Yes System.Decimal 128 bit; IEEE conforming float-point value
bool Yes System.Boolean 1 bit representing true or false
char Yes System.Char 16 bit unicode character

7. Why can a value be declared but not be used without initializing it?

A value type cannot contain a null reference. Remember that a value type is stored on the stack, so if you declare the value type without initializing it and you then try to use it, it won’t work because there is no value for it in memory. You will get a compiler error if you try this:

void MyFunction()
{
int i;
return i;
}

8. How do you overcome the problem described in question 7?

There are two ways in which you can overcome it:

  • Initialise the variable
    void MyFunction()
    {
    int i = 10;
    return i;
    }
  • Use the new keywordvoid MyFunction()

    {

    int i = new int();

    return i;

    }

You can also use a Nullable type.

9. What happens when you assign one value type to another?

In the following code a is copied to b. So the value type is simply copied.

int a = 10;
int b = a;
a = 20;
//b will still be 10

10. Why would you pass a value type by reference?

In the following code example any change to the variable i will not have any effect on its initial value.

public int SquareRoot(int i){x *= x;};
public static void Main(string[] args)
{
 int i = 25;
 SquareRoot(i);
}

11. How would you pass a value type by reference?

By using the ref or out keyword.

public int SquareRoot(ref int i){x *= x;};
public static void Main(string[] args)
{
 int i = 5;
 SquareRoot(i);
 Console.WriteLine(i); //i will be 25
}

12. How do you mark functions explicitly as being CLS Compliant or not?

With the CLSCompliant attribute:

[CLSCompliant(false)]
public ushort foo(){...}

13. What are the types of casting you can do in the CLR/C#? What is the difference?

Implicit and explicit casting transforms a value type from one type to another. Implicit casting implies that there will be no loss of data when the cast is done, whereas explicit casting transforms a type into another type with the possibility of data being lost as a result of the casting. Implicit type casting can occur when you copy a 16-bit integer into a 32-bit double:

Int32 iNumber = 185;
Double dNumber = 30.5d;

dNumber = iNumber; //Implicit cast, dNumber is now 185.0d

Explicit conversion occurs when the type you are casting to will cause the type you are casting from to lose data. In other words if I casted a 32-bit integer to an 8-bit byte then there will be data loss:

int fourBytes = 0x000000FE;
byte lowestByte = (byte)fourBytes;

14. What are some of the basic differences between classes and structs? Where would a struct be more useful?

Structs are light weight alternatives to classes. Structs are also value types, classes are reference types. Structs support access modifiers, operators, nested types, fields, properties, methods, constructors, indexers. Structs may declare a default constructor or a custom constructor. Structs cannot inherit be inherited from by another class, and cannot derive from anything other than System.Valuetype. Structs can implement interfaces and do not have to be instantiated with the new keyword.

Structs take up fewer resources than classes and should be considered when you have a small but frequently used class.

15. What are enums and what are their uses?

Enums are a set of related constants and make code easier to read

16. What are nullable types and why are they used?

Nullable types are needed because its not always easy to determine if a value type has been assigned a null value, and unlike reference types a primitive value-type cannot be assigned a null value.

17. Show two ways in which nullable types can be declared and used?

System.Nullable myValue;

T? myValue2;

Nullable i = null //not possible if you dont use Nullable

int? j = 10;
double? d1 = 3.14;
bool? flag = null;
char? letter = 'a';
int?[] arr = new int?[10];

17. How can you check if a nullable type has a value?

By using two public read-only property. You first check if a type has a value with HasValue and you then get the value through the Value property.

int? x = 10;

if(x.HasValue)
  Console.WriteLine(x.Value);
else
  Console.WriteLine("There is no value");

18. What does the GetValueOrDefault(T DefaultValue) method do?

It assigns a value to the nullable type if the type is null, e.g.

int? myFirstInt = null;
int? mySecondInt = 80;

mySecondInt = myFirstInt.GetValueOrDefault(86); //my secondInt equals 86

19. What does boxing and unboxing mean?

Boxing refers to a value type being cast to as a reference type and unboxing means a reference type is cast back to a value type. E.g.

int i = 20;
object o = (object)i; //boxing

int j = (int) o; //unboxing

20. What happens to a nullable value type?

The value type is boxed, and only the value is boxed. If the nullable type’s HasValue is false then the type is boxed as a nullable type and when it is unboxed the value type gets assigned correctly.

int? nullableNumber = null;
int number = nullableNumber.GetValueOrDefault();
nullableNumber = 146;
number = nullableNumber.GetValueOrDefault();

21. How would you specify your own default value?

You can specify a default value by either passing it to the GetValueOrDefault method or you can use the ?? operator. So that would mean:

int? nullableNumber = null;
int number = nullableNumber.GetValueOrDefault(-1); // number = -1
nullableNumber = 146;
number = nullableNumber.GetValueOrDefault(-1); // number = 146

You could write the above as:

int? nullableNumber = null;
int number = nullableNumber.GetValueOrDefault ?? -1; // number = -1
nullableNumber = 146;
number = nullableNumber.GetValueOrDefault ?? - 1;

22. What are reference types? What are classified as reference types in C#?

Reference types are the objects within C#. Other reference types include classes (objects), interfaces, delegates and arrays. A reference type will be stored on the heap during program execution and the reference type will not store the actual value of the object, but rather it will have reference points to the data that defines the reference type. The references that reside on the heap are “garbage collected”, in other words the Garbage Collector, will clear the data when the object is no longer in use.

23 What is so special about the Object type in C#?

The Object type is the top-most object in the C# language and all types, whether directly or indirectly, or whether they are value types or reference types are derived from the Object type.

24. What happens when you assign one reference type to another?

When you assign one reference type to another you are not copying the one object to the other, you are simply pointing the new object to the reference of the existing object. So if you make any changes to the new object, that change will be reflected in both instances.

In the following example you create a StringBuilder class, create a second StringBuilder class and assign the first StringBuilder class to the second one. You then add a value to the second StringBuilder, and then you print the value of the first StringBuilder.

        static void Main(string[] args)
        {
            StringBuilder builder1 = new StringBuilder();
            StringBuilder builder2 = builder1;

            builder2.Append("Hello");

            Console.WriteLine(builder1.ToString());

            Program p = new Program();
            p.DoStuff(builder1);
            Console.WriteLine(builder1.ToString());
            Console.WriteLine(builder2.ToString());
        }
        public StringBuilder DoStuff(StringBuilder b)
        {
            b.AppendLine("World");
            return b;
        }

The output from this would be:

Hello

HelloWorld

HelloWorld

Note that there is no ref keyword in the DoStuff method.

25. What are the elements that make up a class declaration?

The basic class declaration looks like this:

[attributes] [modifiers] class identifier [:base-type]
{
body[;]
}
  • Attributes are optional and are used to add extra declarative information
  • The modifier is optional. The allowed modifiers are static, sealed, abstract, internal, unsafe and public. If no modifier is specified a default modifier is used, internal
  • The keyword class must be followed by an identifier that names the class
  • The base-type may define any class other than System.Array, System.Delegate, System.multicastDelegate, System.Enum or System.ValueType. If a base class is not supplied then the class will inherit from System.Object
  • The base-type may also specify interfaces implemented by this class. The interfaces must be listed after the base class name and must be separated by commas
  • The body contains the member declarations

26. What are the access modifiers? What do they mean?

  • abstract – the class is created solely for the purpose of inheritance. You cannot create an instance of an abstract class
  • sealed – the class cannot be inherited from
  • static – the class can only contain static members
  • unsafe – allows for unsafe constructs such as pointers
  • public – any item in the current assembly or any assembly that
  • The access levels protected and private are only allowed on nested classes

27. What are nested classes and why would you want to use them?

A nested class is one that is created inside another class. One reason might be that an object can have a special relationship with another object, and another might be to hide the class within another class so that it cannot be accessed or used outside of that class.

28. How would you relate polymorphism?

If certain objects make up the whole of another object or is composed of several other objects you can describe this as composition and aggregation, which makes a whole-part relationship that can be used to decompose an object into more manageable entities. Any object that can exist and be used independently uses aggregation, whereas an object that has no meaning outside the relationship between two objects uses composition. A car for instance needs a battery and an engine, but both these objects are unrelated to the car resulting in a composition. A car can however exist without a door, which is an aggregate relationship. In the following we use the car example:

public class Car
    {
        //aggregation uses classes declared outside of this class
        protected Door FrontRight;
        protected Door FrontLeft;
        protected Door RearRight;
        protected Door RearLeft;

        protected class Engine
        {
            public int horsePower;
        }
        public class Battery
        {
            public int voltage;
        }
        protected Engine theEngine;
        protected Battery theBattery;
        public Car()
        {
            theBattery = new Battery();
            theEngine = new Engine();
        }
    }
    public class Door
    {
        public int position;
    }

29. What are the differences between declaring nested and non-nested classes?

A nested class is declared in the same manner as a normal class declaration, with the difference being that the nested class gets access to all the available access modifiers. The this keyword applies to the inner class only and if you need to use the this keyword you need to pass a constructor of the outer class to the inner class. static member of the outer class are available to the inner class irrespective of their access level.

30. What are the access modifiers that are applicable to a nested class?

  • abstract – the class is created solely for inheritance purposes. You cannot create an instance of a class
  • sealed – the class cannot be inherited from
  • static – the class can only contain static members
  • unsafe – allows for unsafe constructs such as pointers
  • public – any item in the current assembly or any assembly that references it can access this class
  • protected – access is limited to the containing class or derived classes only
  • internal – any item in the current assembly can access this class
  • protected internal – access is limited to the current assembly or types derived from the containing class
  • private – access is limited to the containing class
  • Share/Bookmark

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment
Get Adobe Flash playerPlugin by wpburn.com wordpress themes