Lecture 3.5 Flashcards

1
Q

What is the primary purpose of a conditional statement in coding?
a) To perform repetitive tasks
b) To code for cases that depend on certain conditions
c) To define variables
d) To create loops

A

b

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

Conditional statements are used to decide whether to perform a task based on:

a) The length of the code
b) The type of programming language
c) The conditions specified
d) The number of lines in the program

A

c

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

In which of the following scenarios would you use a conditional statement?

a) When you need to execute a block of code only if a specific condition is met
b) When you want to initialize a variable
c) When you are importing libraries
d) When you are defining a function

A

a

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

Which of the following is a common structure of a conditional statement?

a) for loop
b) while loop
c) if-else statement
d) switch-case statement

A

c

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

True or False: Conditional statements are only used to handle situations where tasks are always performed, regardless of conditions.

a) True
b) False

A

b

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

What is the purpose of the if statement in Java?

A

To execute code based on whether a condition is true

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

How do you write a basic if statement in Java?

A

if (condition) { code }

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

Which of the following is a correct use of an if statement in Java?
if (x > 10) System.out.println(“x is greater than 10”);
b) if x > 10 { System.out.println(“x is greater than 10”); }
c) if (x > 10) { System.out.println(“x is greater than 10”); }
d) if (x > 10) { System.out.println(“x is greater than 10”);

A

if (x > 10) { System.out.println(“x is greater than 10”); }

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

What happens if the condition in an if statement evaluates to false?

A

The if statement will be skipped and no code will execute

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

In a flowchart, if the condition in the diamond shape evaluates to true, where does the arrow point?

A

To a process or task

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

True or False: The if statement in Java can only be used with boolean conditions.

A

True

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

In a flowchart, what shape is typically used to represent a condition or decision?

A

Diamond

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

What do the arrows leading out of the diamond shape in a flowchart represent?

A

Possible outcomes based on the condition

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

In a flowchart, if the condition in the diamond shape evaluates to true, where does the arrow point?

A

To a process or task

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

What is the purpose of using diamonds in a flowchart?

A

To represent decision points that direct the flow based on conditions

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

True or False: A flowchart’s diamond shape can have multiple arrows leading out, but they are only used to show the start and end of the flowchart.

A

False (Diamonds have multiple arrows leading out to represent different outcomes based on the condition, not just start and end points.)

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

In a flowchart, if a condition in the diamond shape evaluates to true, what is the next step?

A

Follow the true arrow to the next rectangle (task)

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

What does the rectangle in a flowchart typically represent?

A

A process or task

19
Q

If a condition in a flowchart evaluates to false, what typically happens next?

A

The false arrow directs the flow to an alternative task or path

20
Q

Why might a flowchart have both true and false arrows coming out of a diamond shape?

A

To show the flow based on whether the condition is met or not

21
Q

True or False: In a flowchart, if a condition is evaluated as true, the tasks associated with the false path will be skipped.

A

True

22
Q

After handling both the true and false branches in a flowchart, what should be done next?

A

Merge the true and false branches back into a single path

23
Q

What common mistake should be avoided when dealing with branches in a flowchart?

A

Leaving a branch without a successor

24
Q

In a flowchart, what does merging the true and false branches into one arrow signify?

A

That both branches lead to the same next step or task

25
Q

Why is it important to merge branches back together in a flowchart?

A

To ensure that all paths eventually lead to a unified process

26
Q

What type of value must the condition inside an if statement evaluate to?

A

A boolean (true or false)

27
Q

What are logical expressions used for in programming?

A

To determine the Boolean result of conditions

28
Q

Which of the following is a common comparison operator in programming?

A

!=

29
Q

What does the comparison operator != check for?

A

Not equal to

30
Q

Which logical operator is used to combine two conditions, where both must be true for the overall expression to be true?

A

&& (AND)

31
Q

True or False: The comparison operator >= checks if a value is greater than or equal to another value.

A

True

32
Q

What happens if the condition inside the parentheses of an if statement evaluates to true?

A

The statements inside the if block are executed

33
Q

If the condition in an if statement evaluates to false, what is the result?

A

he statements inside the if block are skipped

34
Q

How can you ensure that multiple statements are executed within an if block?

A

Use curly braces {} to group the statements together

35
Q

What is the role of curly braces {} in an if statement when multiple statements need to be executed?

A

They define the start and end of the if block for grouping statements

36
Q

True or False: In Java, if the condition in an if statement is false, none of the statements inside the if block will be executed.

A

True

37
Q

What is the purpose of using curly braces {} in an if statement in Java?

A

To group multiple statements together that should be executed if the condition is true

38
Q

How should you format multiple statements within an if block in Java?

A

Use curly braces to group the statements and place each statement on a new line

39
Q

What does the closing brace } signify in an if statement?

A

The end of the entire if statement block

40
Q

True or False: In Java, if you have only one statement to execute after an if condition, you can omit the curly braces {}

A

True

41
Q

What is the purpose of the else clause in an if statement?

A

To handle alternative tasks when the if condition evaluates to false

42
Q

What is the correct syntax to use an else clause following an if statement in Java?

A

if (condition) { statements } else { alternative statements }

43
Q

True or False: In Java, it is necessary to use curly braces {} around the statements inside an else block if there are multiple statements to execute.

A

True

44
Q
A