Chapter 3: Controlling Flow, Converting Types, and Handling Exceptions [Flashcarder]
What topics are covered in this, 3rd, chapter?
The chapter covers writing code that performs operations on variables,
making decisions,
pattern matching,
repeating statements or blocks,
working with arrays,
type casting and converting,
exception handling, and checking for overflow.
What are binary operators, and how do they work?
Binary operators work on two operands and return a new value that is the result of the operation.
For example, x + y
and x * y
are binary operators.
What are unary operators, and how do they work?
Unary operators work on a single operand and can be applied before or after the operand.
What are examples of unary operators in C#?
Examples of unary operators include the increment operators (++x, x++), the typeof operator to get a type, the nameof operator to get a variable name, and the sizeof operator to get the size of a type.
What is a ternary operator in C#?
A ternary operator works with three operands.
In C#, the conditional operator ?: is an example of a ternary operator.
It evaluates a Boolean expression and returns one of two values depending on whether the expression is true or false.
Why might experienced C# developers prefer using ternary operators over if statements?
Experienced C# developers might prefer ternary operators because they are concise and can result in cleaner code once you are familiar with reading them.
What is the difference between the postfix ++ operator and the prefix ++ operator in C#?
The postfix ++ operator increments the value after it is used in an assignment.
The prefix ++ operator increments the value before it is used.
In the code snippet int a = 3; int b = a++;, what are the values of a and b after execution?
a will have a value of 4, and
b will have a value of 3.
In the code snippet int c = 3; int d = ++c;, what are the values of c and d after execution?
Both c and d will have a value of 4.
What is the recommended practice for using the increment (++) and decrement (- -) operators in C# to avoid confusion?
The recommended practice is to avoid combining the use of increment (++) and decrement (- -) operators with the assignment operator (=).
Instead, perform these operations as separate statements to enhance code clarity and maintainability.
What do binary arithmetic operators do in C# programming?
Binary arithmetic operators perform arithmetic operations on two numbers.
Common binary arithmetic operators includeL
addition (+),
subtraction (-),
multiplication (*),
division (/), and
modulus (%).
How does C# calculate the modulus (%) operation and what is the result for e % f when e is 11 and f is 3?
The modulus operation calculates the remainder after division of one number by another.
For e % f with e as 11 and f as 3, the result is 2, representing the remainder when 11 is divided by 3.
What difference does it make to the division operation in C# when the first operand is a floating-point number like g = 11.0?
When the first operand is a floating-point number, the division operation returns a floating-point result.
For instance, dividing g = 11.0 by f = 3 yields approximately 3.6666666666666665, reflecting the actual quotient without truncating to an integer.
What does the assignment operator += do in C#?
The += operator adds the right operand to the left operand and assigns the result to the left operand.
For example, p += 3 is equivalent to p = p + 3.
Explain the function of the -= operator in C#.
The -= operator subtracts the right operand from the left operand and assigns the result back to the left operand.
For instance, p -= 3 means p = p - 3.
Describe the use of *= in C#.
The *= operator multiplies the left operand by the right operand and assigns the result to the left operand.
p *= 3 translates to p = p * 3.
How does the /= operator work in C#?
The /= operator divides the left operand by the right operand and assigns the result to the left operand.
p /= 3 is equivalent to p = p / 3.
What is the purpose of the null-coalescing operator ?? in C#?
The ?? operator returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
For example, int maxLength = authorName?.Length ?? 30
assigns maxLength the value of authorName.Length if authorName is not null, or 30 if authorName is null.
How does the null-coalescing assignment operator ??= function in C#?
The ??= operator assigns the right-hand value to the left-hand operand only if the left-hand operand is null.
For example, authorName ??= "unknown"
assigns “unknown” to authorName if authorName is currently null.
What is the result of the AND logical operator when both operands are true?
True
What is the result of the AND logical operator when one operand is true and the other is false?
False
What is the result of the OR logical operator when both operands are true?
True
What is the result of the OR logical operator when one operand is true and the other is false?
True
What is the result of the OR logical operator when both operands are false?
False