C# Basic Test

.C# Basic Test


1. The fundamental building block in C# is a:

A. Class
B. Object
C. Type
D. Interface

Answer


2. Which of the following languages supports multiple inheritance of classes:

A. C++
B. C#
C. Perl
D. Java

Answer


3. What kind of a type system does C# have:

A. Unified
B. Decentralized
C. Stratified
D. Isolated

Answer


4. A _______ defines a blueprint for a value. A value is a storage location denoted by a _______ or a _______.


Answer


5. A type contains:

A. Function Members
B. No Members
C. Conditional Members
D. Data Members

Answer


6. Which of the following are categories of C# types:

A. Generic Type Parameters
B. Value Types
C. Reference Types
D. Pointer Types

Answer


7. Every C# application must have a single method specifying where the program execution should begin. The method must be called _____.

A. main
B. Main
C. Either of the above

Answer


8. Main method can return which of the following:

A. void
B. int
C. uint
D. long

Answer


9. Main method can accept which of theses as a parameter:

A. Array of char
B. Array of string
C. Array of int
D. No parameters

Answer


10. In C#, the args[0] parameter contains the name of the executable file (True or False)?


Answer


11. Microsoft's "All-In-One-Code Framework" project team created unified standards for coding C++, C#, and VB.NET. Which of the following represent True statements from their coding guidelines:

A. Do not use tabs in source files. All code should use spaces for indentation.
B. The length of code lines should not extend past column 90.
C. Pascal case (e.g. MyMethod) should be used when creating all identifiers, except for the following (True/False):

I. parameters
ii. local variables
iii. private fields
iv. structures
v. macros
vi. constants
vii. UI controls

Answer


12. Which of the following predefined value types are not primitives in the CLR:

A. float
B. ulong
C. sbyte
D. decimal

Answer


13. Which of the following are correct variable declarations C#:

A. decimal cost = 10.00;
B. decimal cost = 10.00D;
C. decimal cost = 10.00M;
D. float cost = 10.00;
E. float cost = 10.00F;
F. double cost = 10.00;

Answer


14. How many integers are instantiated in the following line of code:

int x = 45 * 3;

Answer


15. What data type does C# create for each of the following declarations:

A. var a = 1E1;
B. var b = 10;
C. var c = 10.0;
d. var d = 0xF0000000;

Answer


16. Why does the following statement create a compiler error:

float x = 3.5;

Answer


17. What data type is the "result" variable from the following statements:

float x = 3.5F;
int y = 2;
short z = 4;
var result = x * y / z;
Console.WriteLine("{0}", result.GetType());

Answer


18. What is printed for each of the statements below:

A. Console.WriteLine(1.0F / 0.0F);
B. Console.WriteLine(-1.0F / 0.0F);
C. Console.WriteLine(1.0F / -0.0F);
D. Console.WriteLine(-1.0F / -0.0F);
E. Console.WriteLine(0.0F / 0.0F);
F. Console.WriteLine(0.0F / 0.0F == float.NaN);
G. Console.WriteLine(float.IsNaN(0.0F / 0.0F));

Answer


19. Which of the following are correct variable declarations C#:

A. bool x1 = false;
B. Boolean y1 = true;
C. Boolean z1 = null;
D. bool? x2 = null;
E. bool? y2= True;
F. bool z2 = null;
G. bool x3 = FALSE;
H. bool? y3= true;
I. boolean z3 = true;

Answer


20. What is printed from the following lines of code:

byte x = Byte.MaxValue;
Console.WriteLine("{0}", x.ToString("X4"));

A. 7FFF
B. FFFF
C. 00FF
D. 007F

Answer


21. What is printed from the following lines of code:

short x = short.MaxValue;
Console.WriteLine("{0}", x.ToString("X4"));

A. 7FFF
B. FFFF
C. 00FF
D. 007F

Answer


22. What is printed from the following lines of code:

short x = short.MinValue;
Console.WriteLine("{0}", x.ToString("X4"));

A. 7FFF
B. FFFF
C. 8000
D. 008F

Answer


23. What is printed from the following line of code:

Console.WriteLine("{0:X}", (sbyte)~0xd);

A. F2
B. 0D
C. 8F
D. F8

Answer


24. What is printed from the following line of code:

Console.WriteLine("{0:X}", 0xd ^ 0xf);

A. d
B. 2
C. 7
D. f

Answer


25. The _______ keyword is used to ignore integral arithmetic overflow and results in truncation.


Answer


26. What is printed from the following line of code:

byte z = Byte.Parse("23");

A. Console.WriteLine("z is: {0}", z);
B. Console.WriteLine("z is: {0,-3}", z.ToString());
C. Console.WriteLine("z is: {0}", z.ToString("D3"));
D. Console.WriteLine("z is: {0}", z.ToString("X4"));
E. Console.WriteLine("z is: {0}", Convert.ToString(z, 2));
F. Console.WriteLine("z is: {0}", z & 0xF0);

Answer


27. If the default compile time integral overflow checking is used, what is printed from the following lines of code:

byte x = Byte.MinValue;
Console.WriteLine("x is: {0}", x);
x--;
Console.WriteLine("x is: {0}", x);

A. 0, then 255
B. -128, then -129
C -128, then -127
D. 0, then -1

Answer


28. Fix the code below so it does not result in a compile-time error:

short x = 1;
short y = 1;
short z = x + y;

Answer


29. What is printed from the following lines of code:

BitArray myBA = new BitArray(5);
myBA[1] = true;

foreach (Object obj in myBA)
 Console.Write("{0} ", obj);

A. False, True, False, False, False
B. True, False, False, False, False
C. True, null, null, null, null
D. null, True, null, null, null

Answer


30. Given the definition of variable x below, which "if" statements have correct C# syntax:

byte x = 0x01;

A. if ((x != null) & (x < 3))
B. if ((x != null) && (x < 3))
C. if (x != null) && (x < 3)
D. if x != null && x < 3

Answer


31. What is the result of the following code in C#:

int x = -3;
sbyte y = -3;
Console.WriteLine((x = y) ? "Equals" : "Not Equal");

A. Compiler Error
B. Run-time Error
C. Prints: Equals
D. Prints: Not Equal

Answer


32. What is the size in bits for the following data types:

A. bool
B. short
C. float
D. decimal
E. double
F. long

Answer


33. What data types are printed for the following literals:

A. Console.WriteLine(1.GetType());
B. Console.WriteLine(1.0.GetType());
C. Console.WriteLine(1M.GetType());
D. Console.WriteLine(1D.GetType());
E. Console.WriteLine(1F.GetType());
F. Console.WriteLine(1U.GetType());
G. Console.WriteLine(1UL.GetType());
H. Console.WriteLine(1L.GetType());
I. Console.WriteLine(true.GetType());
J. Console.WriteLine(1E03.GetType());

Answer


34. Which of the following data declarations have valid C# syntax:

A. long x1 = 1;
B. double x2 = 1;
C. double x3 = 1.0;
D. float x4 = 1.0;
E. decimal x5 = 1.0;

Answer


35. The arithmetic operators (+, -, *, /) are valid for all numeric data types except: _______ and _______.

Answer


36. Which of the following are value types and which are reference types:

A. struct
B. enum
C. string
D. object
E. array of integers

Answer


37. You can assign values of any type to a variable of type object (True or False).

Answer


38. The following code is an example of ________ and ___________.

object x = 1;
int y = (int) x;
Answer


39. Where does an integer reside when it is declared as a:

A. local variable
B. field with an object
C. element of an array

Answer


40. Code two "if" statements to test if a file called "test.txt" in the Windows temp directory of a Windows 7 server called Kevin-PC. Use backslashes in the path. Code the statement using verbatim string literals and then code the statement again without the use of verbatim string literals.
Answer


41. Which of the following statements will print the number 0?

A. Console.WriteLine(0x30);
B. Console.WriteLine("{C}", 0x30);
C. Console.WriteLine("{U}", 0x30);
D. Console.WriteLine('\u0030');
E. Console.WriteLine('\u30');

Answer


42. Which of the following statements are true about arrays?

A. Arrays are always stored in contiguous blocks of memory.
B. Once an array is created, its length can not be changed.
C. Array elements are automatically initialized.
D. An array is always a reference type regardless of type of its elements.
E. An array must contain all the same type of elements.

Answer


43. How should data members and functions members be designated if you wish to operate on the type itself, instead of operating on an instance of the type?
Answer


44. Will the address of the character 'h' have the same address in loop 2 as it does in loop 1?
unsafe
{
string a = "the";
char[] b = a.ToCharArray();

fixed (char* charPtr = &b[0])
{
// loop 1
int i = 0;
char* charPtr2 = charPtr;
foreach (char myChar in b)
{
Console.WriteLine("Address of: {0} is: {1}", b[i++],(int)charPtr2++);
}

// loop 2
a += " end";
b = a.ToCharArray();
i = 0;
charPtr2 = charPtr;
foreach (char myChar in b)
{
Console.WriteLine("Address of: {0} is: {1}", b[i++], (int)charPtr2++);
}
}
Answer


45. How many bytes are used fore each char data type and how are char literals delimited?
Answer


46. Given the following string declarations, what will be printed:

string a1 = "alligator";
string a2 = "alligator";
string a3 = "Alligator";
string a4 ="xxx";
string a5;
string z1 = "zebra";

A. Console.WriteLine(a1 == a2);
B. Console.WriteLine(a1 == a3);
C. Console.WriteLine(a1 D. Console.WriteLine(a1.ToLower().Equals(a3.ToLower()));
E. Console.WriteLine(ReferenceEquals(a1, a2));
F. a4 = "alligator"; Console.WriteLine(ReferenceEquals(a1, a4));
G. a5 = "alli"; a5 += "gator"; Console.WriteLine(ReferenceEquals(a1, "a5"));
H. Console.WriteLine(a1.CompareTo(a2));
I. Console.WriteLine(a1.CompareTo(a3));
J. Console.WriteLine(z1.CompareTo(a3));
K. Console.WriteLine(String.Compare(a1, a3, true));
L. Console.WriteLine(String.Compare(a1, a3));
M. Console.WriteLine(String.CompareOrdinal(a1, "crocodile"));

Answer


47. What are the two types of multidimensional arrays in C# and how do they differ?
Answer


48. Write the code to declare and print the contents of a 2 dimensional array with 4 elements in each dimension.
Answer


49. Write the code to declare and print an array containing 4 arrays. The first inner array should have 1 element, the second 2 elements, the third 3 elements, and the forth should have 4 elements.
Answer


50. Which of the following are valid array definitions?

A. int[,] myArray1 = new int[4, 4];
B.int[][] myArray2 = new int[4][4];
C. int[,] myArray3 = new int[,];
D. int[,] myArray3a = new int[,]
{
  {1,2,3},
  {4,5}
};
E. int[][] myArray4 = new int[][]
{
  new int[] {1,2,3},
  new int[] {4,5}
};
F. var myArray5 = new int[][]
{
  new int[] {1,2,3},
  new int[] {4,5}
};
G. var myArray6 = new[] { 1, 2, 3 };
H.var myArray7 = new var[][]
{
  new int[] {1,2,3},
  new int[] {4,5}
};

Answer


51. What is printed from the following block of code:

var x = new[] { 1, 100000000000 };
Console.WriteLine(x[0].GetType());
Answer


52. What is printed by each line of code:

A. Console.WriteLine(default(int));
B. Console.WriteLine(default(bool));
C. Console.WriteLine(default(string) == null);
D. Console.WriteLine(default(char) == null);
E. Console.WriteLine(default(char) == '\0');

Answer


53. Where are Static Fields and Constants stored? How long do they live?
Answer


54. A constant is evaluated at ________, while a readonly variable is evaluated at ________.
Answer


55. Both constants and readonly variables can be initialized when they are declared. But a readonly variable can also be initialized in the __________.
Answer


56. C# enforces a definite assignment policy. What does this mean?
Answer


57. By default, arguments are passed by:

A. reference
B. value
C. either reference or value depending upon the data type

Answer


58. What is printed from the following code:

object o1 = 9;
Foo(o1);
Console.WriteLine(o1);

static void Foo(object myObject)
{
  myObject = (int) myObject + 6;
}

Answer


59. What are the two parameter modifiers and how do they work?
Answer


60. Where do parameter modifiers need to be specified (hint: 2 places)?
Answer


61. How is the "params" parameter modified used?
Answer


62. Write a method and method call that accepts a variable number of integers and returns their sum.
Answer


63. Indicate whether the following statements are true or false:

A. Mandatory parameters must be listed before optional parameters.
B. Positional parameters must be listed before named parameters.
C. Optional parameters can be marked as either "ref" or "out".

Answer


64. Write a method that has one required and one optional parameter and returns their product. Also code calls to the method with 1 and 2 parameters.
Answer


65. Write a method with 3 optional parameters. Code three calls to the method:

1. Call using first two parameters by position.
2. Call using first two parameters by name.
3. Call using only third parameter by name.

Answer


66. Name two operators that are left-associative and give examples of each.
Answer


67. Name four operators that are right-associative.
Answer


68. Name three operators which use parentheses and give examples.
Answer


69. Name three unsafe operators.
Answer


70. Given the following declarations, what will be printed by each statement. If the statement doesn't print anything, specify why (compiler error, runtime error).

const int c = 0;
int x = 0;
int y = 1;
int z;

A. Console.WriteLine(z);
B. Console.WriteLine(x / y);
C. Console.WriteLine(y/x);
D. Console.WriteLine(x/c);
E. Console.WriteLine(8 % (4 % 2));
F. Console.WriteLine(8 / (2-2+1-1));

Answer


71. Given the following code, identify which statements will cause compiler errors and specify why.

int x = 1;
{
  int x = 2;
  int y = 3;
}
{
  int y = 4;
  int z = 5;
}
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);

Answer


72. What is printed by the following code:

foreach (char c in "Visual+Studio 2012")
{
  if (c.Equals('s')) continue;
  if (c.Equals('\u0020')) break;
  if (Char.IsSymbol(c)) goto skip1;
  Console.Write(c);
}
skip1:
  Console.WriteLine("X");

Answer


73. If your project contains two types with the same name from different assemblies, how can you resolve the ambiguity in your code when referencing the type? For example, your project references both one.dll and two.dll. Both dll's have defined a class called MyClass. In your code, how do you reference the MyClass defined in two.dll?
Answer


74. What token is commonly used in auto-generated code to avoid name conflicts between namespaces?
Answer


75. What is the difference between the using directive and the using statement?
Answer


Answers


1. Answer is C.
A Type is the fundamental building block in C#. It is an encapsulated unit of data and functions.

"The fundamental building block in C# is an encapsulated unit of data and functions called a type. C# has a unified type system, where all types ultimately share a common base type. This means that all types, whether they represent business objects or are primitive types such as numbers, share the basic set of functionality. ... In a traditional object-oriented paradigm, the only kind of type is a class. In C#, there are several other kinds of types, one of which is an interface. An interface is like a class, except it only describes members." C# 5.0 in a Nutshell: The Definitive Reference By Joseph Albahari, Ben Albahari, O'Reilly Media Inc.

Back


2. Answer is A and C.
C++ and Perl support multiple inheritance of classes. C# and Java do NOT support multiple inheritance of classes, but instead use interfaces to perform a similar function.

Back


3. Answer is A.
C# has a unified type system. This means all types share the same basic set of functionality.

Back


4. Answer is:
A type defines a blueprint for a value. A value is a storage location denoted by a variable or a constant .

Back


5. Answer is: A and D.
A type consists of Function Members (e.g. methods, constructors) and Data Members (e.g. fields).

Back


6. Answer is: A, B, C, D.
The categories for C# types include: Generic Type Parameters, Value Types, Reference Types, and Pointer Types.

Back


7. Answer is: B.
In C#, Main must be capitalized. (Java uses lowercase main).

Back


8. Answer is: A,B.
The main method can return void or int.

Back


9. Answer is: B,D.
The main method can only accept an array of string as parameters, or can have no parameters.

Back


10. Answer is: False
In C#, the name of the executable file is not contained in the parameters (as it is in C and C++).

Back


11. Answers are:
A. True. Do not use tabs in source files as they are rendered differently by various text editors. All code should use spaces for indentation. Visual studio (and most IDEs) can be configured to insert spaces for tabs.

B. True. Having overly long lines inhibits the readability of code. Break the code line when the line length is greater than column 78 for readability. If column 78 looks too narrow, use column 86 or 90.

C.
I. True. parameters use camelCase (e.g. myParameter)
ii. True. local variables use camelCase
iii. True. private fields use camelCase or _camelCase
iv. True. structures use all upper case with underscore between words
v. True. macros use all upper case with underscore between words
vi. True. constants use all upper case with underscore between words (e.g. MY_CONSTANT)
vii. True. UI controls should use prefixes in camelCase to identify the type of control (e.g. btnFileSave).
Also, do not use Hungarian notation (i.e., do not encode the type of a variable in its name) in .NET.

Back


12. Answer is: D
Decimal is a predefined data type that is not a primitive in the CLR. Decimal uses base 10, while the other floating point numbers use base 2 ... which makes Decimal more accurate for financial calculations. A Decimal calculation is about 10 times slower than an equivalent double calculation.

Back


13. Answer is: C, E, F.
C. decimal cost = 10.00M;
E. float cost = 10.00F;
F. double cost = 10.00;


Back


14. Answer is: 3.
3 integers are instantiated because predefined types can be instantiated simply by using a literal.

Back


15. Answer is:
A. var a = 1E1; (double)
B. var b = 10; (integer)
C. var c = 10.0; (double)
D. var d = 0xF0000000; (uint)


Back


16. Answer is: Compiler Error
Can't implicitly convert double to float

Back


17. Answer is: float (a.k.a single)
The result datatype is a float because no double data types were used in the calculation.

Back


18. Answers are:
A. Console.WriteLine(1.0F / 0.0F); // Prints: Infinity
B. Console.WriteLine(-1.0F / 0.0F); // Prints: -Infinity
C. Console.WriteLine(1.0F / -0.0F); // Prints: -Infinity
D. Console.WriteLine(-1.0F / -0.0F); // Prints: Infinity
E. Console.WriteLine(0.0F / 0.0F); // Prints: NaN
F. Console.WriteLine(0.0F / 0.0F == float.NaN); // Prints: False
G. Console.WriteLine(float.IsNaN(0.0F / 0.0F)); // Prints: True

Back


19. Answer is: A, B, D, H are correct variable declarations.
These are the INCORRECT variable declarations:
C. Boolean z1 = null; // INCORRECT: Cannot convert null to bool
E. bool? y2= True; // INCORRECT: True does not exist in current context
F. bool z3 = null; // INCORRECT: Cannot convert null to bool
G. bool x3 = FALSE; // INCORRECT: FALSE does not exist in current context
I. boolean z3 = true; // INCORRECT: type or namespace 'boolean' could not be found

Back


20. Answer is: C. 00FF

Back


21. Answer is: A. 7FFF

Back


22. Answer is: C. 8000

Back


23. Answer is: A. F2

Back


24. Answer is: B. 2

Back


25. Answer is: unchecked

Back


26. Answer is:
A. 23
B. 23
C. 023
D. 0017
E. 10111
F. 16

Back


27. Answer is:
A. 0, then 255

Back


28. Answer is:

short x = 1;
short y = 1;
short z = (short) (x + y);

Back


29. Answer is: A. False, True, False, False, False

Back


30. Answers are:
A. if ((x != null) & (x < 3)) // Correct
B. if ((x != null) && (x < 3)) // Correct
C. if (x != null) && (x < 3) // INCORRECT
D. if x != null && x < 3 // INCORRECT

Back


31. Answer is: A. Compiler Error
Cannot implicitly convert type 'int' to 'bool'. (Changing (x = y) to (x == y) will result in in printing Equals.

Back


32. Answers are:
A. bool = 8 bits
B. short = 16 bits
C. float = 32 bits
D. decimal = 128 bits
E. double = 64 bits
F. long = 64 bits

Back


33. Answers are:
A. Console.WriteLine(1.GetType()); // Int32
B. Console.WriteLine(1.0.GetType()); // Double
C. Console.WriteLine(1M.GetType()); // Decimal
D. Console.WriteLine(1D.GetType()); // Double
E. Console.WriteLine(1F.GetType()); // Single (float)
F. Console.WriteLine(1U.GetType()); // UInt32
G. Console.WriteLine(1UL.GetType()); // UInt64
H. Console.WriteLine(1L.GetType()); // Int64
I. Console.WriteLine(true.GetType()); // Boolean
J. Console.WriteLine(1E03.GetType()); // Double

Back


34. Answer is: A, B, C
A. long x1 = 1; // Valid
B. double x2 = 1; // Valid
C. double x3 = 1.0; //Valid
D. float x4 = 1.0; // Compiler Error, need F suffix
E. decimal x5 = 1.0; // Compiler Error, need M suffix

Back


35. Answer is: 8-bit integral types and 16-bit integral types (byte, sbyte, short, ushort).

Back


36. Answers are:
A. struct // Value Type
B. enum // Value Type
C. string // Reference Type
D. object // Reference Type
E. array of integers // Reference Type


Back


37. Answer is: True

Back


38. Answer is: Boxing and Unboxing

Back


39. Answers are:
A. local variable // stack
B. field with an object // heap
C. element of an array // heap

Back


40. Answers are:
A. With verbatim string literal:
if (File.Exists(@"\\Kevin-PC\admin$\temp\test.txt"))
B. Without verbatim string literal:
if (File.Exists("\\\\Kevin-PC\\admin$\\temp\\test.txt"))

Back


41. Answers are:
A. Console.WriteLine(0x30); // No, prints: 48
B. Console.WriteLine("{C}", 0x30); // Compiler Error
C. Console.WriteLine("{U}", 0x30); // Compiler Error
D. Console.WriteLine('\u0030'); // Yes, prints 0
E. Console.WriteLine('\u30'); // Compiler Error

Back


42. Answers are:
A. True - Arrays are always stored in contiguous blocks of memory.
B. True - Once an array is created, its length can not be changed.
C. True - Array elements are automatically initialized.
D. True - An array is always a reference type regardless of type of its elements.
E. True - An array must contain all the same type of elements.

Back


43. Answer is: static

Back


44. Answer is: Yes. By default strings are immutable, but they can be made mutable by pinning their memory address with the fix statement.

Back


45. Answer is: The char data type uses 2 bytes of storage and is delimited by single quotes.

Back


46. Answers are:
A. Console.WriteLine(a1 == a2); // Prints: True
B. Console.WriteLine(a1 == a3); // Prints: False
C. Console.WriteLine(a1 D. Console.WriteLine(a1.ToLower().Equals(a3.ToLower())); // Prints: True
E. Console.WriteLine(ReferenceEquals(a1, a2)); // Prints: True (String Interning)
F. a4 = "alligator"; Console.WriteLine(ReferenceEquals(a1, a4)); // Prints: True (String Interning)
G. a5 = "alli"; a5 += "gator"; Console.WriteLine(ReferenceEquals(a1, "a5")); // Prints: False
H. Console.WriteLine(a1.CompareTo(a2)); // Prints: 0 (a1 equals a2)
I. Console.WriteLine(a1.CompareTo(a3)); // Prints: -1 (a1 is less than a3)
J. Console.WriteLine(z1.CompareTo(a3)); // Prints: 1 (z1 is greater than a3)
K. Console.WriteLine(String.Compare(a1, a3, true)); // Prints: 0
L. Console.WriteLine(String.Compare(a1, a3)); // Prints: -1 (a1 is less than a3)
M. Console.WriteLine(String.CompareOrdinal(a1, "crocodile")); // Prints: -2 (a1 is 2 less than "crocodile")

Back


47. Answer is: Rectangular and jagged arrays. Rectangular arrays have the same number of elements in each dimension, while that is not required for jagged arrays. A rectangular array is an n-dimensional array, while a jagged array is an array of arrays.

Back


48. Answer is:
int[,] myArray = new int [4,4]; // Rectangular array

for (int i = 0; i < myArray.GetLength(0); i++)
  for (int j = 0; j < myArray.GetLength(1); j++)
   Console.Write(myArray[i,j]); //Prints: 0000000000000000 (16 zeros)
Console.WriteLine();

Back


49. Answer is:
int[][] myArray2 = new int[4][]; // Jagged array
for (int i = 0; i < myArray2.Length; i++)
  myArray2[i] = new int[i+1];

for (int i = 0; i < myArray2.Length; i++)
  for (int j = 0; j < myArray2[i].Length; j++)
   Console.Write(myArray2[i][j]); //Prints: 0000000000 (10 zeros)
Console.WriteLine();

Back


50. Answers are:
A. int[,] myArray1 = new int[4, 4]; // Valid
B.int[][] myArray2 = new int[4][4]; // Invalid (Can not specify size of inner dimension on jagged array)
C. int[,] myArray3 = new int[,]; // Invalid (Must specify all size of all dimensions on rectangular array)
D. int[,] myArray3a = new int[,] // Invalid (Inner dimension on rectangular array is not variable)
{
  {1,2,3},
  {4,5}
};
E. int[][] myArray4 = new int[][] // Valid
{
  new int[] {1,2,3},
  new int[] {4,5}
};
F. var myArray5 = new int[][] // Valid
{
  new int[] {1,2,3},
  new int[] {4,5}
};
G. var myArray6 = new[] { 1, 2, 3 }; // Valid
H.var myArray7 = new var[][] // InValid
{
  new int[] {1,2,3},
  new int[] {4,5}
};

Back


51. Answer is:
Console.WriteLine(x[0].GetType()); // Prints: Int64 (All array elements must be of same type, so "long" is used because of second element)

Back


52. Answers are:
A. Console.WriteLine(default(int)); // Prints: 0
B. Console.WriteLine(default(bool)); // Prints: False
C. Console.WriteLine(default(string) == null); // Prints: True
D. Console.WriteLine(default(char) == null); // Prints: False
E. Console.WriteLine(default(char) == '\0'); // Prints: True (null character not same as null reference)

Back


53. Answer is:
Static Fields and Constants are stored on the heap. They live for the entire life of the application. However, they are not involved in garbage collection.

Back


54. Answer is: A constant is evaluated at compile time, while a readonly variable is evaluated at runtime.

Back


55. Answer is: Both constants and readonly variables can be initialized when they are declared. But a readonly variable can also be initialized in the constructor(s).

Back


56. Answer is: The Definite Assignment Policy in C# means that, outside of the unsafe context, it is impossible to access uninitialized memory.

Back


57. Answer is: B.
By default, arguments in C# are passed by value.

Back


58. Answer is: The number 9 is printed.
The argument is passed by value so the original version is not updated. Note: a parameter can be passed by reference or by value, regardless of whether the parameter type is a reference type or a value type.

Back


59. Answers are:

1. ref - is an input/output parameter. Variable must be assigned a value going into the method.
2. out - is an output parameter. Variable must be assigned a value going out of the method.

Back


60. Answer is:

Parameter modifiers must be specified in two places: 1. inside the methods parameter list and 2. inside the method call.

Back


61. Answer is:

The params parameter modifier is used to accept an array of parameters. It must always be the last parameter defined in the method. Only one params parameter modifier can be used in the parameter list. Only one array can be used with the params modifier.

Back


62. Answer is:

static long Sum(params int[] theParms)
{
  long theSum = 0;
  for (int i = 0; i < theParms.Length; i++)
   theSum += theParms[i];
  return theSum;
}

int[] intArray = {1, 2, 3, 4};
Console.WriteLine("Sum is {0}", Sum(intArray))

Back


63. Answers are:
A. Mandatory parameters must be listed before optional parameters. (TRUE).
B. Positional parameters must be listed before named parameters. (TRUE).
C. Optional parameters can be marked as either "ref" or "out". (FALSE - Optional parameters can not be either "ref" or "out" parameters.)

Back


64. Answer is:

static long Multi(int firstParm, int secondParm = 3)
{
  return (firstParm * secondParm);
}

long x = Multi(5, 5);
Console.WriteLine(x);
long y = Multi(5);
Console.WriteLine(y);


Back


65. Answer is:
static void myMethod (char x='X', char y='Y', char z = 'Z')
{
  Console.WriteLine("x is {0} and y is {1} and z is {2}", x, y, z);
}

myMethod('A', 'B'); //Prints: x is A and y is B and z is Z
myMethod(y: 'A', x: 'B'); //Prints: x is B and y is A and z is Z
myMethod(z: 'A'); //Prints: x is X and y is Y and z is A

Back


66. Answers are: Division and Modulus operators are left-associative.
Console.WriteLine(16 / 4 / 2); // Prints: 2 (Left-Associative)
Console.WriteLine(16 / (4 / 2)); // Prints: 8
Console.WriteLine(16 % 3 % 2); // Prints: 1
Console.WriteLine(16 % (3 % 2)); // Prints: 0

Back


67. Answer is: The following operators are right-associative:
1. Assignment Operator (=)
2. Lambda Operator (=>)
3. Null Coalescing Operator (??)
4. Conditional Operator (?:)

Back


68. Answer is: 68. Three operators which use parentheses are:
1. Cast, example: (int) x
2. Grouping, example: if (x==a)
3. Method Call, example: myMethod()

Back


69. Answer is: Three unsafe operators are:
1. * is "Value at address"
2. & is "Address of value"
3. -> is "Pointer to struct"

Back


70. Answers are:
A. Console.WriteLine(z); // Compiler Error: Use of unassigned variable
B. Console.WriteLine(x / y); // Prints: 0
C. Console.WriteLine(y/x); // Runtime Error: Division by zero
D. Console.WriteLine(x/c); // Compiler Error: Division by constant zero
E. Console.WriteLine(8 % (4 % 2)); // Compiler Error: Division by constant zero
F. Console.WriteLine(8 / (2-2+1-1)); // Compiler Error: Division by constant zero

Back


71. Answer is:
int x = 1;
{
  int x = 2; // Compiler Error: variable already defined
  int y = 3;
}
{
  int y = 4;
  int z = 5;
}
Console.WriteLine(x);
Console.WriteLine(y); // Compiler Error: variable does not exist
Console.WriteLine(z); // Compiler Error: variable does not exist

Back


72. Answer is: ViualX

Back


73. Answer is: Use an external alias to resolve the ambiguity.

Compile with references to the dll's:   csc /r:A1=one.dll /r:A2=two.dll myApplication.cs

Define the external references as the very top of your code:

extern alias A1;
extern alias A2;

Then use the alias when referencing the class:

A1.MyClass // MyClass from one.dll
A2.MyClass // MyClass from two.dll

Back


74. Answer is: What ::token is commonly used in auto-generated code to avoid name conflicts between namespaces. It can be referenced from the root of all names as designated by the global keyword.

global::TheNameSpace.TheClass();


Back


75. Answer is: The using directive is used to import namespaces, or to create an alias for a namespace or a type:

using System; // Import namespace

or

using ST = System.Text.StringBuilder; // using alias directive
class Program { ST p; }

while the using statement ensures the correct use of objects which support the IDisposable interface. When you use an object that supports the IDisposable interface, it should be declared and instantiated inside a using statement to ensure the object is correctly disposed of in a timely manner. The using statement makes sure the object is disposed of as soon as the object goes out of scope instead of having to wait for garbage collection. The using statement is equivalent to putting the object in a try/finally block with Dispose() being called in the finally block.

Back

C# Basics CrossWord Puzzle

.C# Basics

Across
2. Structure used to represent an instance in time.
4. Used to declare a member that belongs to the type itself instead of an instance of the type.
6. Number of bytes used by the int data type.
8. The fundamental building block of C#.
10. Keyword used to indicate a class can not be inherited from.
11. Area of memory where a value type stores its contents.
15. The default way arguments are passed to parameters.
17. The type of a class.
18. Keyword used to invoke a context that allows for the use of pointers.
19. Default type for a real number literal that does not have a suffix.
21. Predefined type that should be used with financial calculations.
22. Keyword used to ignore integral arithmetic overflow.
23. Keyword used to define an input/output parameter.
24. A private field which contains accessor methods to read and/or write its value.
27. Number of bits used by the bool data type.
28. Keyword used to mark code to be skipped during the compile-time type checking.
30. The type of a struct.
31. Name for the area of memory where constants and static fields are stored.
Down
1. Naming convention used for naming classes, structs, and methods.
3. Used to declare a member whose implementation is missing or incomplete.
5. Process of converting a value type to a reference type.
7. Type of array that does not contain the same number of elements in each dimension.
9. Keyword used to define an array of parameters.
12. Name for a set of keywords used to specify the accessibility level of types and type members.
13. The base type for all other types.
14. The process which reclaims memory used by objects that are no longer referenced.
16. Used to declare a method which can be overridden in a derived class.
20. Type used to create a group of named constants associated with an underlying integral type.
25. Type of array that contains the same number of elements in each dimension.
26. Number of bytes used by the char data type.
29. Naming convention used for naming parameters and variables.




Answers

Across
2. Structure used to represent an instance in time. (datetime)
4. Used to declare a member that belongs to the type itself instead of an instance of the type. (static)
6. Number of bytes used by the int data type. (four)
8. The fundamental building block of C#. (type)
10. Keyword used to indicate a class can not be inherited from. (sealed)
11. Area of memory where a value type stores its contents. (stack)
15. The default way arguments are passed to parameters. (byvalue)
17. The type of a class. (reference)
18. Keyword used to invoke a context that allows for the use of pointers. (unsafe)
19. Default type for a real number literal that does not have a suffix. (double)
21. Predefined type that should be used with financial calculations. (decimal)
22. Keyword used to ignore integral arithmetic overflow. (unchecked)
23. Keyword used to define an input/output parameter. (ref)
24. A private field which contains accessor methods to read and/or write its value. (property)
27. Number of bits used by the bool data type. (eight)
28. Keyword used to mark code to be skipped during the compile-time type checking. (dynamic)
30. The type of a struct. (value)
31. Name for the area of memory where constants and static fields are stored. (heap)
Down
1. Naming convention used for naming classes, structs, and methods. (pascalcase)
3. Used to declare a member whose implementation is missing or incomplete. (abstract)
5. Process of converting a value type to a reference type. (boxing)
7. Type of array that does not contain the same number of elements in each dimension. (jagged)
9. Keyword used to define an array of parameters. (params)
12. Name for a set of keywords used to specify the accessibility level of types and type members. (accessmodifiers)
13. The base type for all other types. (object)
14. The process which reclaims memory used by objects that are no longer referenced. (garbagecollection)
16. Used to declare a method which can be overridden in a derived class. (virtual)
20. Type used to create a group of named constants associated with an underlying integral type. (enum)
25. Type of array that contains the same number of elements in each dimension. (rectangular)
26. Number of bytes used by the char data type. (two)
29. Naming convention used for naming parameters and variables. (camelcase)