Type System Flashcards

1
Q

What is a type?

A

Type defines a blueprint for a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a predefined type?

What are some predefined types?

A

Predefined is a type that is specially supported by the compiler. In C#, predefined types are recognized with a C# keyword.
Examples of predefined types are: int, string, bool
https://stackoverflow.com/questions/13464641/c-sharp-predefined-types-vs-primitive-types/46866599#46866599

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is data created?

A

Data is created by instantiating a type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is a predefined type instantiated?

A

Simply by using a literal, such as 12 or “Hello, world”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is a custom type instantiated?

A

Using “new” operator.

MyType instanceOfType = new MyType();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What comprises value types?

A

Most built-in types (all numeric types, char and bool types) as well as custom struct and enum types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What comprises the reference types?

A

Class, array, delegate and interface types. String type is a reference type as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does a reference-type variable contain?

A

A reference-type variable contains a reference to an object that contains the value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between reference and value types in regards to them containing null value?

A

Reference types can be assigned null value which means that they point to no object. Value types cannot ordinarily have a null value. (Exception: nullable value types)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Are all types from System namespace predefined in C#?

A

No. System namespace contains many types that are not predefined by C#. For example DateTime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is System.DateTime type predefined in C#?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List 4 type categories in C#.

A
  1. Value types.
  2. Reference types.
  3. Generic type parameters.
  4. Pointer types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is anonymous type created?

A

To create an anonymous type use “new” operator with the object initializer.

var v = new { Amount = 108, Message = “Hello” };

In this case “Amount” and “Message” are user defined object properties and their type will be derived by the compiler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Are methods and events allowed in anonymous types?

A

No. Only properties are allowed to be defined in the anonymous type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is accessibility level of properties in an anonymous type?

A

Properties of an anonymous type are read-only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can a static class contain instance variables? Here is an example:

static class MyClass
{
    int myIntVar = 3;
}
A
No. 
All data and function members of a static class must be static.
17
Q

How many bytes will the following Struct occupy in memory?

struct MyStruct{
int x;
int y;
}

A

8 bytes. 4 bytes for each integer and no overhead. Value type instances occupy precisely the memory required for their fields.

18
Q

What information is stored in the overhead of the reference type instance?

A
  1. The key to object’s type.
  2. Object’s lock state.
  3. Whether the object has been flagged by garbage collector.
19
Q

What type does a compiler infer a variable to be if it is a number with a decimal point?

A

The number with a decimal point is always inferred to be a double type by a compiler.

20
Q

What is the largest number that int can hold in C#?

A

2^31 - 1. int has 4 bytes to store its value but 1 bit is reserved for a sign value of a number.

21
Q

Which real number type is typically used for financial calculations where base-10 accurate arithmetic is required?

A

decimal

22
Q

How is hexadecimal value denoted in C#?

A

Hexadecimal value is denoted with “0x” prefix. For example: long x = 0x7F;