class 8 chapter 6
JAVA – CONTROL STATEMENTS
Complete Questions and Answers
A. Choose the Correct Answer (MCQ)
1. In Java, which keyword is used to start a conditional block that executes only if a specific condition is true?
a. else
b. if
c. switch
d. loop
Answer: b. if
2. What is the correct way to write an if condition in Java where x is greater than y?
a. if (x > y)
b. If (x > y)
c. if { x > y }
d. If [ x > y ]
Answer: a. if (x > y)
3. Which of the following is NOT a type of control statement?
a. Decision Control Statement
b. Looping Control Statement
c. Input/Output Control Statement
d. Jump Statement
Answer: c. Input/Output Control Statement
4. Which of these provides an alternative path of execution when an if condition is false?
a. break
b. else
c. case
d. default
Answer: b. else
5. The if-else-if ladder is useful in handling how many conditions?
a. One
b. Two
c. Multiple
d. No such ladder
Answer: c. Multiple
6. What is the primary purpose of decision control statements in a program?
a. To jump from one part of the program to another
b. To make choices and execute specific code based on conditions
c. To display output on the screen
d. To repeat a block of code multiple times
Answer: b. To make choices and execute specific code based on conditions
B. Fill in the Blanks
An expression that evaluates to either true or false is called a Boolean expression.
If an
ifcondition is false, and there is no else block, the program simply skips the if block.The
else ifpart of a ladder is checked only if the precedingiforelse ifconditions were false.In Java, code blocks are enclosed within curly braces.
In a flowchart, the
ifis shown as a decision box.
C. State Whether the Statements are True or False
1. The else block in an if...else statement is always mandatory.
Answer: False
2. The else if keyword in Java is used to define an alternative block of code if the initial if condition is false.
Answer: True
3. If the condition in an if statement is true, the code inside its block is skipped.
Answer: False
4. An if...else statement ensures that exactly one of its two blocks will be executed.
Answer: True
5. The System.out.println() command is used to get input from the user.
Answer: False
D. Answer the Following Questions
1. What is the difference between an if statement and an if-else statement?
Answer: An if statement executes a block of code only when the specified condition is true. If the condition is false, nothing happens. An if-else statement provides an alternative block of code that executes when the condition is false.
2. What is the purpose of control statements in programming?
Answer: Control statements determine the flow of execution in a program based on specific conditions. They allow the program to make decisions, repeat actions and execute different blocks of code.
3. Write a simple Java if statement to check if a variable age is greater than 60 and print "Senior Citizen".
Answer:
if (age > 60) {
System.out.println("Senior Citizen");
}4. Write a simple Java if-else statement to check if a char variable gender is 'M' and print "Male", otherwise print "Female".
Answer:
if (gender == 'M') {
System.out.println("Male");
} else {
System.out.println("Female");
}5. Write the syntax for if-else if-else statement.
Answer:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if all conditions are false
}Note: All answers are arranged section-wise for easy reading, classroom teaching and examination preparation.
Comments
Post a Comment