Expressions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What kinds of expressions are there in Swift?

A

In Swift, there are four kinds of expressions: prefix expressions, binary expressions, primary expressions, and postfix expressions.

Prefix and binary expressions let you apply operators to smaller expressions.

Primary expressions are conceptually the simplest kind of expression, and they provide a way to access values.

Postfix expressions, like prefix and binary expressions, let you build up more complex expressions using postfixes such as function calls and member access.

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

What does evaluating an expression do?

A

Evaluating an expression returns a value, causes a side effect, or both.

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

Name the prefix operators defined by the Swift standard library.

A
\++ Increment
-- Decrement
! Logical NOT
~ Bitwise NOT
\+ Unary plus
- Unary minus
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What other prefix operator is defined by the Swift language?

A

In addition to the standard library operators listed above, you use & immediately before the name of a variable that’s being passed as an in-out argument to a function call expression.

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

What exponentiative binary operators does the Swift standard library provide?

A

&laquo_space;Bitwise left shift
» Bitwise right shift

(No associativity, precedence level 160)

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

What multiplicative binary operators does the Swift standard library provide?

A
* Multiply
/ Divide
% Remainder
&* Multiply, ignoring overflow
&/ Divide, ignoring overflow
&% Remainder, ignoring overflow
& Bitwise AND

(Left associative, precedence level 150)

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

What additive binary operators does the Swift standard library provide?

A
\+ Add
- Subtract
&+ Add with overflow
&- Subtract with overflow
| Bitwise OR
^ Bitwise XOR

(Left associative, precedence level 140)

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

What range binary operators does the Swift standard library provide?

A

.. Half-closed range
… Closed range

(No associativity, precedence level 135)

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

What cast binary operators does the Swift standard library provide?

A

is Type check
as Type cast

(No associativity, precedence level 132)

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

What comparative binary operators does the Swift standard library provide?

A
< Less than
 Greater than
>= Greater than or equal
== Equal
!= Not equal
=== Identical
!== Not identical
~= Pattern match

(No associativity, precedence level 130)

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

What conjunctive binary operator does the Swift standard library provide?

A

&& Logical AND

Left associative, precedence level 120

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

What disjunctive binary operator does the Swift standard library provide?

A

|| Logical OR

Left associative, precedence level 110

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

What assignment binary operators does the Swift standard library provide?

A
= Assign
*= Multiply and assign
/= Divide and assign
%= Remainder and assign
\+= Add and assign
-= Subtract and assign
<>= Right bit shift and assign
&amp;= Bitwise AND and assign
^= Bitwise XOR and assign
|= Bitwise OR and assign
&amp;&amp;= Logical AND and assign
||= Logical OR and assign

(Right associative, precedence level 90)

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

What ternary operator does the Swift standard library provide?

A

?: Ternary conditional

Right associative, precedence level 100

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

How are binary operator precedence rules applied?

A

At parse time, an expression made up of binary operators is represented as a flat list. This list is transformed into a tree by applying operator precedence

For example, the expression 2 + 3 * 5 is initially understood as a flat list of five items, 2, +, 3, *, and 5.

This process transforms it into the tree (2 + (3 * 5)).

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

Which binary operator is this?

~=

A

Pattern match

Category: Comparative
No associativity
Precedence level 130

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

Which binary operator is this?

||

A

Logical OR

Category: Disjunctive
Left associative
Precedence level 110

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

Which binary operator is this?

*=

A

Multiply and assign

Category: Assignment
Right associative
Precedence level 90

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

Which binary operator is this?

&-

A

Subtract with overflow

Category: Additive
Left associative
Precedence level 140

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

Which binary operator is this?

> =

A

Greater than or equal

Category: Comparative
No associativity
Precedence level 130

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

Which binary operator is this?

&=

A

Bitwise AND and assign

Category: Assignment
Right associative
Precedence level 90

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

Which binary operator is this?

/=

A

Divide and assign

Category: Assignment
Right associative
Precedence level 90

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

Which binary operator is this?

^=

A

Bitwise XOR and assign

Category: Assignment
Right associative
Precedence level 90

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

Which binary operator is this?

-=

A

Subtract and assign

Category: Assignment
Right associative
Precedence level 90

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

Which binary operator is this?

==

A

Equal

Category: Comparative
No associativity
Precedence level 130

26
Q

Which binary operator is this?

&*

A

Multiply, ignoring overflow

Category: Multiplicative
Left associative
Precedence level 150

27
Q

Which binary operator is this?

!==

A

Not identical

Category: Comparative
No associativity
Precedence level 130

28
Q

Which binary operator is this?

is

A

Type check

Category: Cast
No associativity
Precedence level 132

29
Q

Which binary operator is this?

> >

A

Bitwise right shift

Category: Exponentiative
No associativity
Precedence level 160

30
Q

Which binary operator is this?

..

A

Half-closed range

Category: Range
No associativity
Precedence level 135

31
Q

Which binary operator is this?

%

A

Remainder

Category: Multiplicative
Left associative
Precedence level 150

32
Q

Which binary operator is this?

!=

A

Not Equal

Category: Comparative
No associativity
Precedence level 130

33
Q

Which binary operator is this?

*

A

Multiply

Category: Multiplicative
Left associative
Precedence level 150

34
Q

Which binary operator is this?

> > =

A

Right bit shift and assign

Category: Assignment
Right associative
Precedence level 90

35
Q

Which binary operator is this?

«=

A

Left bit shift and assign

Category: Assignment
Right associative
Precedence level 90

36
Q

Which binary operator is this?

-

A

Subtract

Category: Additive
Left associative
Precedence level 140

37
Q

Which binary operator is this?

/

A

Divide

Category: Multiplicative
Left associative
Precedence level 150

38
Q

Which binary operator is this?

A

Less than

Category: Comparative
No associativity
Precedence level 130

39
Q

Which binary operator is this?

+

A

Add

Category: Additive
Left associative
Precedence level 140

40
Q

Which binary operator is this?

===

A

Identical

Category: Comparative
No associativity
Precedence level 130

41
Q

Which binary operator is this?

A

Bitwise XOR

Category: Additive
Left associative
Precedence level 140

42
Q

Which binary operator is this?

<=

A

Less than or equal

Category: Comparative
No associativity
Precedence level 130

43
Q

Which binary operator is this?

&&=

A

Logical AND and assign

Category: Assignment
Right associative
Precedence level 90

44
Q

Which binary operator is this?

&+

A

Add with overflow

Category: Additive
Left associative
Precedence level 140

45
Q

Which binary operator is this?

%=

A

Remainder and assign

Category: Assignment
Right associative
Precedence level 90

46
Q

Which binary operator is this?

|=

A

Bitwise OR and assign

Category: Assignment
Right associative
Precedence level 90

47
Q

Which binary operator is this?

&/

A

Divide, ignoring overflow

Category: Multiplicative
Left associative
Precedence level 150

48
Q

Which binary operator is this?

&

A

Bitwise AND

Category: Multiplicative
Left associative
Precedence level 150

49
Q

Which binary operator is this?

A

Closed range

Category: Range
No associativity
Precedence level 135

50
Q

Which binary operator is this?

<

A

Bitwise left shift

Category: Exponentiative
No associativity
Precedence level 160

51
Q

Which binary operator is this?

>

A

Greater than

Category: Comparative
No associativity
Precedence level 130

52
Q

Which binary operator is this?

|

A

Bitwise OR

Category: Additive
Left associative
Precedence level 140

53
Q

Which binary operator is this?

&%

A

Remainder, ignoring overflow

Category: Multiplicative
Left associative
Precedence level 150

54
Q

Which binary operator is this?

=

A

Assign

Category: Assignment
Right associative
Precedence level 90

55
Q

Which binary operator is this?

&&

A

Logical AND

Category: Conjunctive
Left associative
Precedence level 120

56
Q

Which binary operator is this?

as

A

Type cast

Category: Cast
No associativity
Precedence level 132

57
Q

Which binary operator is this?

||=

A

Logical OR and assign

Category: Assignment
Right associative
Precedence level 90

58
Q

Which binary operator is this?

+=

A

Add and assign

Category: Assignment
Right associative
Precedence level 90

59
Q

What are the rules for the assignment operator?

=

A

The assigment operator sets a new value for a given expression. It has the following form:

expression = value

The value of the expression is set to the value obtained by evaluating the value. If the expression is a tuple, the value must be a tuple with the same number of elements. (Nested tuples are allowed.) Assignment is performed from each part of the value to the corresponding part of the expression. For example:

(a, _, (b, c)) = ("test", 9.45, (12, 3))
//a is "test", b is 12, c is 3, and 9.45 is ignored

The assignment operator does not return any value.

60
Q

What are the rules for the ternary operator?

? :

A

The ternary conditional operator evaluates to one of two given values based on the value of a condition. It has the following form:

condition ? a : b

If the condition evaluates to true, the conditional operator evaluates the first expression and returns its value. Otherwise, it evaluates the second expression and returns its value. The unused expression is not evaluated.

61
Q

What are the rules for the typecasting operator?

as

A

If conversion to the specified type is guaranteed to succeed, the value of the expression is returned as an instance of the specified type.

If conversion to the specified type is guaranteed to fail, a compile-time error is raised.

Otherwise, if it’s not known at compile time whether the conversion will succeed, the type of the cast expresion is an optional of the specified type. At runtime, if the cast succeeds, the value of expression is wrapped in an optional and returned; otherwise, the value returned is nil.

62
Q

What are the rules for the type-testing operator?

is

A

The is operator checks at runtime to see whether the expression is of the specified type. If so, it returns true; otherwise, it returns false.

The check must not be known to be true or false at compile time.