Chapter 5 PDF Flashcards

1
Q

Describe sequence in the flow of control

A

Execute instructions in order

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

Describe method calls in the flow of control

A

Transfer control to method, execute instructions in method, then return with or without a value

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

Describe selection in the flow of control

A

Execute different instructions depending on the data

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

Describe looping in the flow of control

A

Repeat a set of instructions for different data

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

Describe equality operators

A

They’re used to determine if the values of two expressions are equal or not equal

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

What is special about equality operators

A

Must be used with a primitive data type and object references, not to compare object data

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

What is a common error made with equality operators

A

Equality operators (==) and easily confused with the assignment operator (=)

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

What is relational operators used for

A

Relational Operators are used to compare the values of two expressions

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

What is the result for relational operators

A

The result for relational operators is either true or false

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

What is ! used for in programming

A

! is the NOT operator

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

What is the ! (NOT OPERATOR) used for

A

The NOT operator is used to evaluate to the inverse of the value of its operand. (EX: If the operand is true the result will be the opposite)

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

What is && used for in programming

A

&& is the AND operator

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

What is the && (AND OPERATOR) used for

A

The AND operator takes two boolean expressions as operands, if both operands are true then the result is true. Otherwise the result is false.

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

What is || used for in programming

A

|| is the OR operator

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

What is the OR operator used for

A

The OR operator takes two boolean expressions as operands. If both operands are false, the result will be false and otherwise it’ll be true.

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

In any logical operator, how are the operands evaluated?

A

Operands are evaluated left to right for any logical operator

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

What occurs if the result of the logical operator can be determined after evaluating the first operand

A

Only the first operand will be evaluated and the second one will be ignored

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

What is the result if the first operand of an OR statement is true

A

The result of the statement will be TRUE

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

What is the result if the first operand of an AND statement is false

A

The result of the statement will be FALSE

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

What is a common error when testing three ints

A

Each operand of a logical operator must be a boolean expression or else you will have a compiler error

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

What are DeMorgans Laws

A
  1. NOT( A AND B ) = ( NOT A ) OR ( NOT B )

2. NOT( A OR B ) = ( NOT A ) AND ( NOT B )

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

How would you find an equivalent expression using DeMorgans Laws

A
  1. Change AND to OR
  2. Change OR to AND
  3. NEGATE EACH OPERAND EXPRESSION
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is an if statement used for

A

If statements are used when the program should perform an operation for one set or data but do nothing for all other data

24
Q

What is special about curly brackets if the true block of an if statement only contains one statement

A

Curly braces are optional if the true block contains only one statement

25
Q

Describe a simple IF Flow of Control

A

If the condition evaluates to true, the true block is executed.
If the condition evaluates to false, the true block is skipped

26
Q

What is a a common error with if statements

A

Do not put a semicolon after the condition in an if statement. Doing so would indicate that the true block is empty and will cause a logic error

27
Q

When is the if/else statement used

A

Used when Data falls into two mutually exclusive categories and the program should perform different operations for each set of data

28
Q

What is special about curly brackets if the true block of an if/else statement only contains one statement

A

Curly braces are optional if the true block contains only one statement

29
Q

Describe an id/else flow of control

A

If the condition is true, then the true block is executed.

If the condition is false then the false block is executed

30
Q

What is scope

A

The region within a program where an identifier can be referenced.

31
Q

Where does the scope of a variable end and begin

A

The scope of a variable extends from the point of declaration to the end of the block in which it is declared

32
Q

What are some examples of blocks

A

Classes
Methods
If statements true and false blocks

33
Q

When is an if/else if statement used

A

Used when data falls into multiple, mutually exclusive categories and the program should perform different operations for each set of data

34
Q

Describe the if/else if Flow of Control

A
  • If the first condition is true, the first true block is executed. The rest of the if/else statement is skipped.
  • If the n^th condition is true then the n^th true block is executed. The rest of the if/else statement is skipped.
  • If no condition is true, the false block is executed.
35
Q

What are nested if statements

A

If statements can be written as part of the true or false block of another if statement.

36
Q

What is needed if you nest an if statement

A

If you nest an IF statement more information is required beyond the results of the first if condition

37
Q

What occurs if you don’t assign an else clause in a nested if statement

A

The compiler will match any else clause with the most previous if statements that doesn’t already have an else clause

38
Q

What can curly brackets be used for in a nested if/else statements

A

You can use curly brackets to force a desired if/else pairing

39
Q

What is a “Dangling Else”

A

A dangling else occurs when an else clause cannot be paid with an if condition

40
Q

What can occur with floating point representation

A

Minor rounding errors can occur in calculations, causing the equality operator not to consider the values equal.

41
Q

How do we solve the equality operator not being able to consider values equal (in relation to rounding errors)

A

Consider a small threshold value. If the absolute value of the difference between the two values is less than the threshold value, then the two floating point numbers will be considered equal.

42
Q

What does the equality operator compare

A

The equality operator compares object references. ie: compares that they point to the same object… doesn’t compare the data

43
Q

How would you compare object data instead of comparing the objects themselves

A

You would use the equals method

44
Q

What is the common error when using equality operators

A

You do not use equality operators to compare object data, instead use the equals method

45
Q

What are strings considered

A

Strings are objects (data)

46
Q

How would you compare two strings

A

Use the equals method since Strings are objects (data)

47
Q

What does a conditional operator do

A

A conditional operator contributes one of two values to an expression based on the value of the condition

48
Q

What are some uses of conditional operators

A

Handling invalid input

Outputting similar messages

49
Q

When is a switch statement used

A

In some selection operators, the switch statement can be used instead of an if/else/if statement

50
Q

What are the requirements for using a switch statement

A

Can be used when we are comparing the value of an expression to constant intergers (byte, short, int), characters (char) or Strings.

51
Q

How is the switch operation performed

A

The expression is evaluated, then its value is compared to the case constants in order. When a match is found, the statements following that case constant are executed in sequence until either a break statement or the end of a switch block is reached.

52
Q

What are some optional aspects when using a switch statement

A

The break statement is optional
The default label and its statements are also optional
The statements under the case constant are also optional

53
Q

Why is the break statement optional when using a switch statement

A

The break statement is optional when using a switch statement because the job of the break is to terminate execution of the switch statement

54
Q

Why is the default label and its statements optional when using the switch statement

A

The default label is optional because they are executed only when the value of the expression does not match any of the case constants

55
Q

Why are the statements under the case constant optional when using a switch statement

A

The statements under the case constant are also optional so multiple case constants can be written in sequence if identical operations will be performed for those values

56
Q

What are common errors with break statements

A

(Remember that once a match is found, all succeeding statements are executed until a break statement is encountered).
Be careful to code a break statement when you have finished the processing of a value, otherwise you may execute unintended statements