Chapter 3 – Controlling Flow, Converting Types, and Handling Exceptions Flashcards

1
Q

What happens when you divide an int variable by 0?

A

DivideByZeroException is thrown when dividing an integer or decimal by 0.

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

What happens when you divide a double variable by 0?

A

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.

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

What happens when you overflow an int variable, that is, set it to a value beyond its range?

A

It will loop unless you wrap the statement in a checked block, in which case,
OverflowException will be thrown.

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

What is the difference between x = y++; and x = ++y;?

A

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.

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

What is the difference between break, continue, and return when used inside a loop statement?

A

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.

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

What are the three parts of a for statement and which of them are required?

A

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.

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

What is the difference between the = and == operators?

A

The = operator is the assignment operator for assigning values to variables, while the
== operator is the equality check operator that returns true or false.

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

Does the following statement compile? for ( ; ; ) ;

A

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.

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

What does the underscore _ represent in a switch expression?

A

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.

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

What interface must an object “implement” to be enumerated over using the foreach statement?

A

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.

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

What will happen if this code executes?
int max = 500;
for (byte i = 0; i < max; i++)
{
WriteLine(i);
}

A

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.

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

What are the values of x and y after the following statements execute?
x = 3;
y = 2 + ++x;

A

x is 4 and y is 6.

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

What are the values of x and y after the following statements execute?
x = 3 &laquo_space;2;
y = 10&raquo_space; 1;

A

x is 12 and y is 5.

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

What are the values of x and y after the following statements execute?
x = 10 & 8;
y = 10 | 7;

A

x is 8 and y is 15.

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