Chapter 3 – Controlling Flow, Converting Types, and Handling Exceptions Flashcards
What happens when you divide an int variable by 0?
DivideByZeroException is thrown when dividing an integer or decimal by 0.
What happens when you divide a double variable by 0?
The double type contains a special value of infinity. Instances of floating-point numbers can have the special values of NaN (not a number), or in the case of dividing by 0, either
PositiveInfinity or NegativeInfinity.
What happens when you overflow an int variable, that is, set it to a value beyond its range?
It will loop unless you wrap the statement in a checked block, in which case,
OverflowException will be thrown.
What is the difference between x = y++; and x = ++y;?
In the statement x = y++;, the current value of y will be assigned to x and then y will
be incremented, and in the statement x = ++y;, the value of y will be incremented and then
the result will be assigned to x.
What is the difference between break, continue, and return when used inside a loop statement?
The break statement will end the whole loop and continue executing after the loop,
the continue statement will end the current iteration of the loop and continue executing at
the start of the loop block for the next iteration, and the return statement will end the current
method call and continue executing after the method call.
What are the three parts of a for statement and which of them are required?
The three parts of a for statement are the initializer, condition, and incrementer
expressions. All three parts are optional. If the condition is missing, then it will loop forever
unless you break out of the loop using break, return, or something else.
What is the difference between the = and == operators?
The = operator is the assignment operator for assigning values to variables, while the
== operator is the equality check operator that returns true or false.
Does the following statement compile? for ( ; ; ) ;
Yes. The for statement only requires the semicolons to separate the initializer expression, condition expression, and incrementer expressions. All three expressions are optional so
they can be missing. This for statement will execute the empty ; statement after the closing
brace, forever. It is an example of an infinite loop.
What does the underscore _ represent in a switch expression?
The underscore _ represents the default return value. It is called a discard because
it is used to represent a potential variable that is not needed. It can be used not just in switch
expressions but other scenarios where a placeholder variable must be declared but its value
is not needed, like parameters to lambda expressions and tuple deconstruction.
What interface must an object “implement” to be enumerated over using the foreach statement?
An object must “implement” the IEnumerable interface. It must have the correct methods with the correct signatures even if the object does not actually implement the interface.
What will happen if this code executes?
int max = 500;
for (byte i = 0; i < max; i++)
{
WriteLine(i);
}
The code will loop forever because the value of i can only be between 0 and 255. Once i gets incremented beyond 255, it loops back to 0 and therefore will always be less than max (500).
To prevent the infinite loop, you can add a checked statement around the code. This would cause an
exception to be thrown after 255 due to the overflow, as shown in the following output:
254
255
System.OverflowException says Arithmetic operation resulted in an overflow.
What are the values of x and y after the following statements execute?
x = 3;
y = 2 + ++x;
x is 4 and y is 6.
What are the values of x and y after the following statements execute?
x = 3 «_space;2;
y = 10»_space; 1;
x is 12 and y is 5.
What are the values of x and y after the following statements execute?
x = 10 & 8;
y = 10 | 7;
x is 8 and y is 15.