🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C# Workshop - Week 3 Review Questions

Started by
10 comments, last by JWalsh 17 years ago
Quote: Original post by NickGravelyn
Quote: Original post by shawnre
2. What are the conditional operator in C#?

And(&), Or(|), Conditional (?:)


Just thought I'd point out that the 'and' and 'or' operators you are looking for are actually && and || as in

if (a && b) // if a and b
if (a || b) // if a or b

Using the single & and | are actually bitwise operators.


Almost. [grin]
Check this thread.

&& and || are conditional and/or operators.
& and | are logical and/or operators on bools. On integral types, they are bitwise as you said.
Advertisement
.NET Book Zero Chapters 7, 8, 9, and 10 Review Questions

Greetings everyone. Please look over the following answers and make sure it matches with yours. If it doesn't and you're confused about why, please post your questions here.
  1. What are the primary operators in C#?

  2. Parentheses, The period, the Method call, the indexer, the post increment, post decrement, new, typeof, sizeof, checked, and unchecked.

  3. What are the conditional operator in C#?
    && || ?:

  4. Which two categories of operators have right to left associativity?
    Ternary (conditional) and the different assignment operators

  5. What is operator precedence?
    When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated.

  6. What determines which operators get evaluated first if two or more have the same precedence?
    The associativity.

  7. What operator can you use to change the precedence of other operators?
    The Parenthesis operator

  8. True or False, assigning a value to a variable returns a value?
    True

  9. How many arguments does a unary operator have?
    1

  10. An expression must always be placed before or after the unary operator symbol?
    After

  11. What is the result of the - operator when applied to a number?
    The negative of the number.

  12. Can the ! operator be applied to any other type besides Boolean value?
    No.

  13. What is the result of the ! operator when applied to a Boolean value?
    The Negative Boolean expression.

  14. Which types can use the ~ unary operator, and what does it do?
    int, uint, long, and ulong. It switches all 0 bits to 1's and all 1's to 0's.

  15. The + operator is defined for which data type in addition to all numerical types?
    Strings

  16. What do the << and >> operators do?
    They shift the value of a variable to the left or right by a specified number of bits?

  17. When a signed type is shifted to the right, is the most significant bit set to the value of the sign bit, or is it set to 0?
    It is set to the sign bit

  18. The relational operators <, >, <=, and >=, as well as the equality operators == and != all return a value of what type?
    Boolean

  19. True for false, the equality operators == and != CANNOT be used in C# to compare strings.
    False. C# Allows the comparison of strings with the equality operators.

  20. What are the Logical operators in C#?
    & ^ |

  21. What happens when the logical operators are applied to integral values?
    They function as bitwise operators

  22. What happens with the logical operators are applied to Boolean values?
    They function as Boolean operators. & returns true only if both operands are true, ^ returns true only if exactly 1 operand is true, and the | operand returns true if either operand are true.

  23. What are the only data types which can be combined with the conditional operators || and &&?
    Boolean

  24. What is the difference between the logical operators and the conditional operators when working with Boolean types?
    With logical operators both operands are evaluated. With conditional operands, only the left most operand is guaranteed to be evaluated. The right operand is only evaluated if the left operand is not enough to determine the result of the comparison.

  25. In C# is it possible to mix integer and Boolean types with the logical operators?
    No. Both operands must be either Boolean or integer.

  26. What is the only operator with 3 operands? What does it do?
    The conditional operator, sometimes called the ternary operator is the only one with 3 operands. It checks the value of the first operand (which must be Boolean). If the value is true the second operand is executed, otherwise the third operand is executed.

  27. What does compound assignment mean in C#, and what are the compound assignment operators?
    Compound assignment means that the operator performs its designated operations on the left and right operands, and assigns the value back to the left operand.
    *= /= %= += -= <<= >>= &= ^= |= ??

  28. The keyword 'if' must be followed by parenthesis with what kind of expression inside?
    Boolean

  29. If more than one statement needs to be executed as a result of an if-statement, how can those statements be grouped?
    They can be grouped in a block using curly braces.

  30. When is an 'else' statement evaluated?
    Only when all previous if statements resulted in a false value.

  31. Can if and else statements be nested inside of each other?
    Yes.

  32. When you declare variables within a block, when are they no longer visible?
    When execution exits from the block.

  33. It is possible to declare a variable within a block with the same name as a variable in a higher scoped block?
    No.

  34. It is possible to declare a variable within a block with the same name as a variable in a similar (adjacently, or sibling) scoped block?
    Yes.

  35. When using C# Switch statements, what must be true for execution to "fall through" one case into the next case.
    The case that is being fallen through must be empty of any statements.

  36. If a goto statement is provided at the end of switch case, is a break statement still necessary?
    No.

  37. What are the possible types allowed in the expression at the top of a switch statement?
    integer types, characters, enumerations, and strings.

  38. Is it possible to put string expressions in a switch-state comparison?
    Yes.

  39. What internal technique makes it efficient for string comparisons in C#?
    String Interning

  40. What does a while-statement do?
    It allows execution to repeat a group of statements as long as a conditional statement at the top of the block remains true.

  41. What's the difference between a while-statement and a do-while-statement?
    With a while statement, the contents of the statement may never be performed. With a do-while the contained statement(s) are performed at least once.

  42. What is true of the evaluated expression in a while or do-while statement? That is, what type must it be?
    It must be Boolean.

  43. Explain the three parts involved in the first line of a for-statement?
    The first part is the initializer, and is called once - before anything is executed within the for-loop
    The second part is a conditional, which must be a Boolean expression and its evaluated before each iteration. if it is false, the contained expression is not executed, and execution moves on to the next line after the for-statement.
    The third part is an expression which is executed AFTER each iteration.

  44. What does a continue and break statement do within all iterative statements (for, while, do-while, and foreach)?
    The break statement stops the current iteration and progresses to the next line after the end of the iterative statement.
    The continue statements stops execution, performs any comparisons required, and then continues with the next iteration if appropriate.

  45. If a variable is created within the initializer section of a for-loop, can it be used outside the for-loop after the loop is terminated?
    No.

  46. In a foreach block, what is true of the iteration variable? As a result, is it possible to initialize the values in an array by using a foreach statement?
    The iteration variable is Read Only. No, it's not possible to initialize an array within a foreach, because the array is read-only.

  47. What interface must be implemented in order for a type to work with the foreach statement?
    The IEnumerable interface.

  48. How do you define a label for a goto statement?
    The name of the label, followed by a colon :

  49. Is it possible to goto a label which is not in either the current block or a parent block?
    No.

  50. Are stacks shared between threads in an application, or does each thread have its own stack?
    Every thread has its own stack

  51. What types of data are stored on a stack?
    return addresses during function calls, arguments passed into function calls, and locally defined variables.

  52. When is memory allocated on the stack for local variables in a method? When is the memory freed?
    The memory is allocated as soon as the method execution begins. The memory is freed as soon as the method terminates.

  53. Are heaps shared between threads in an application, or does each thread have its own stack?
    Heaps are shared between threads.

  54. Where is a local string reference stored? Where is the actual string data stored?
    The local string referenced is stored on the stack. The string data is stored on the heap.

  55. Is the amount of space required on the stack for a string consistent or dynamic?
    It is consistent

  56. Can the address (the value in a reference) be directly manipulated or used in an arithmetic expression?
    No.

  57. What happens to a memory block on the heap which no longer has any references to it?
    It is marked for garbage collection.

  58. When you declare variables of all the simple numeric types, is the variable stored on the stack, or the heap?
    The Stack

  59. When you create an object, is the reference to the object stored on the stack or the heap? How about the data for the object itself?
    The reference is stored on the stack, the object data itself is stored on the heap.

  60. What is a string with no characters referred to as?
    An empty string

  61. What keyword is used to tell a reference variable there is no memory allocated for it on the heap.
    null

  62. Can null be used in comparisons to determine if a reference variable has memory allocated for it?
    Yes.

  63. Is it possible to set a value-type to null, why or why not?
    No. Because value types are created on the stack, and there is ALWAYS space allocated for them.

  64. What is an Array?
    An ordered collection of objects of the same type.

  65. What do you call each item in an array
    An array element

  66. What is the acceptable range of indices into an array
    From 0 to n-1, where n is the number of elements in the array.

  67. Where is the initial square bracket (not the one when allocating) placed in an array declaration? Can it have any numbers in it?
    After the type, before the variable name. No, it cannot have numbers in it.

  68. What operator do you use to allocate memory for an array?
    The new operator.

  69. What number do you put in the brackets present when allocating memory for an array?
    Whatever number indicates the number of elements you want in the array.

  70. Are array elements allocated on the stack or the heap?
    Heap.

  71. What happens at run-time if you just a negative index, or an index that is larger than the maximum allowed index?
    It throws a IndexOutOfRangeException exception.

  72. What is the implicit base class for all arrays?
    System.Array

  73. What does Petzold consider to be the most important property of an array?
    Length

  74. Is it possible to allocate memory for the array when declaring it?
    Yes

  75. Is it possible to initialize the values in an array as part of the declaration?
    Yes

  76. If you initialize the values in an array as part of the declaration are you required to put the number of elements you're allocating?
    No

  77. If you initialize the values in an array as part of the declaration are you required to use the new operator?
    No

  78. If you initialize an array of reference types, what is the initial value in each element?
    null

  79. How many types of multi-dimensional arrays does C# allow?
    2. Mutli-Dimensional, and Jagged.

  80. For multi-dimensional arrays, how do you reference a specific element?
    by separating the dimensional elements with comma's within the brackets

  81. For a multi-dimensional array, what value is returned from the Length property?
    The product of the lengths of each dimension. So if you've got an [n,m] array, it will be n X m.

  82. What does the Rank property of an array tell you?
    It tells you the number of dimensions in the array.

  83. What is the functional difference between multi-dimensional array, and jagged arrays?
    With multi-dimensional arrays, each element within a dimension is required to have the same number of sub-elements.
    With jagged arrays, each element within a dimension can have a different number of sub-elements.

  84. True or False, because jagged arrays are essentially arrays of arrays, they require multiple 'new' invocations, one for the main variable, and one for each element in each sub-dimension?
    True

  85. True or false, You can initialize the values of an entire jagged array during declaration by putting 'new' and nested brackets in the correct locations?
    True.
Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement