[go: up one dir, main page]

0% found this document useful (0 votes)
9 views9 pages

If-else_MCQs_Practise

The document contains multiple-choice questions focused on decision constructs in programming, specifically in Java. It covers topics such as if-else statements, switch-case structures, ternary operators, and the purpose of default cases. Additionally, it includes reasoning and assertion questions related to these constructs, as well as general MCQs about their usage and best practices.

Uploaded by

nowduri.sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

If-else_MCQs_Practise

The document contains multiple-choice questions focused on decision constructs in programming, specifically in Java. It covers topics such as if-else statements, switch-case structures, ternary operators, and the purpose of default cases. Additionally, it includes reasoning and assertion questions related to these constructs, as well as general MCQs about their usage and best practices.

Uploaded by

nowduri.sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Multiple Choice Questions on Decision Constructs

1. Which of the following is a decision-making construct in programming?


a) Loop
b) Array
c) If-else
d) Variable
Answer: c) If-else

2. What is the output of the following code?


int x = 10;
if (x > 5)
System.out.println("Hello");
else
System.out.println("World");
a) Hello
b) World
c) Hello World
d) No output
Answer: a) Hello

3. Which of the following statements correctly represents a switch case structure?


a) switch(expression) { case 1: statements; break; }
b) switch { case(expression): statements; break; }
c) switch { case 1: statements; break; }
d) case switch(expression) { 1: statements; break; }
Answer: a) switch(expression) { case 1: statements; break; }

4. What is the purpose of the default case in a switch statement?


a) To stop the execution of the program
b) To execute a statement when no case matches
c) To check all cases
d) To execute the first case always
Answer: b) To execute a statement when no case matches
5. What will be the output of the following Java code?
int a = 10, b = 20;
if (a > b)
System.out.println("A is greater");
else if (a < b)
System.out.println("B is greater");
else
System.out.println("Both are equal");
a) A is greater
b) B is greater
c) Both are equal
d) No output
Answer: b) B is greater

6. What will happen if the break statement is not used in a switch case?
a) Only the matching case will execute
b) The program will terminate
c) All the cases below the matching case will also execute
d) An error will occur
Answer: c) All the cases below the matching case will also execute

7. Which of the following is an example of a ternary operator?


a) if-else
b) switch-case
c) ?:
d) for loop
Answer: c) ?:
8. What is the output of the following code?
int num = 7;
String result = (num % 2 == 0) ? "Even" : "Odd";
System.out.println(result);
a) Even
b) Odd
c) Compilation error
d) Runtime error
Answer: b) Odd

9. Which of the following is NOT a valid relational operator in Java?


a) ==
b) !=
c) <=
d) =<
Answer: d) =<

10. What will be the output of the following code?


boolean flag = false;
if (!flag) // HINT ! (NOT) OPERATOR IS BEING USED
System.out.println("True");
else
System.out.println("False");
a) True
b) False
c) Compilation error
d) No output
Answer: a) True
Reasoning and Assertion Questions

1. Assertion (A): The if-else construct is used for decision-making in Java.


Reason (R): The if statement evaluates a condition and executes a block of code if the
condition is true.
a) Both A and R are true, and R explains A.
b) Both A and R are true, but R does not explain A.
c) A is true, but R is false.
d) A is false, but R is true.
Answer: a) Both A and R are true, and R explains A.

2. Assertion (A): The switch statement can be used with int, char, String datatypes in
Java.
Reason (R): The switch statement only works with numeric data types.
a) Both A and R are true, and R explains A.
b) Both A and R are true, but R does not explain A.
c) A is true, but R is false.
d) A is false, but R is true.
Answer: c) A is true, but R is false.

3. Assertion (A): A break statement is necessary to prevent fall-through in a switch case.


Reason (R): Without a break, all cases below the matching case will execute
sequentially.
a) Both A and R are true, and R explains A.
b) Both A and R are true, but R does not explain A.
c) A is true, but R is false.
d) A is false, but R is true.
Answer: a) Both A and R are true, and R explains A.
4. Assertion (A): The ternary operator is an alternative to an if-else statement.
Reason (R): The ternary operator uses ?: to return one of two values based on a
condition.
a) Both A and R are true, and R explains A.
b) Both A and R are true, but R does not explain A.
c) A is true, but R is false.
d) A is false, but R is true.
Answer: a) Both A and R are true, and R explains A.

5. Assertion (A): The default case in a switch statement is optional.


Reason (R): The default case executes when none of the other cases match the
expression.
a) Both A and R are true, and R explains A.
b) Both A and R are true, but R does not explain A.
c) A is true, but R is false.
d) A is false, but R is true.
Answer: a) Both A and R are true, and R explains A.

GENERAL MCQS
1). Which of the following cannot be handled using a switch case?
a) char
b) int
c) boolean
d) String
Answer: c) boolean

2. Why is switch-case preferred over if-else in some cases?


a) Faster execution
b) More readable
c) Less chance of errors
d) All of the above
Answer: d) All of the above
3. Which statement correctly converts the following if-else into a switch case?
if (num == 5)
System.out.println("Five");
else
System.out.println("Other");
a) switch (num) { case 5: System.out.println("Five"); break; default:
System.out.println("Other"); }
b) switch (num) { case 5: System.out.println("Five"); default:
System.out.println("Other"); }
c) switch (num) { case 5: System.out.println("Other"); break; default:
System.out.println("Five"); }
d) switch (num) { default: System.out.println("Five"); break; case 5:
System.out.println("Other"); }
Answer: a) switch (num)
{
case 5: System.out.println("Five");
break;
default: System.out.println("Other");
}

4. When should you avoid using switch cases?


a) When dealing with boolean expressions
b) When having multiple conditions
c) When working with complex conditions
d) All of the above
Answer: d) All of the above
CONVERSION FROM ONE DECISION CONSTRUCT TO ANOTHER

You might also like