Lecture 3.5 Flashcards
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
b
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
c
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
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
c
True or False: Conditional statements are only used to handle situations where tasks are always performed, regardless of conditions.
a) True
b) False
b
What is the purpose of the if statement in Java?
To execute code based on whether a condition is true
How do you write a basic if statement in Java?
if (condition) { code }
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”);
if (x > 10) { System.out.println(“x is greater than 10”); }
What happens if the condition in an if statement evaluates to false?
The if statement will be skipped and no code will execute
In a flowchart, if the condition in the diamond shape evaluates to true, where does the arrow point?
To a process or task
True or False: The if statement in Java can only be used with boolean conditions.
True
In a flowchart, what shape is typically used to represent a condition or decision?
Diamond
What do the arrows leading out of the diamond shape in a flowchart represent?
Possible outcomes based on the condition
In a flowchart, if the condition in the diamond shape evaluates to true, where does the arrow point?
To a process or task
What is the purpose of using diamonds in a flowchart?
To represent decision points that direct the flow based on conditions
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.
False (Diamonds have multiple arrows leading out to represent different outcomes based on the condition, not just start and end points.)
In a flowchart, if a condition in the diamond shape evaluates to true, what is the next step?
Follow the true arrow to the next rectangle (task)