Single Choice
Easy
QConsider the following program code:
class Out {
int a = 5; // Adding a variable to compare with x
int cal() {
int x = 10;
if (x > a) {
return --x; // Decrements x by 1 and returns 9
} else {
return ++x; // Increments x by 1 and returns 11
}
}
public static void main(String[] args) {
Out ob = new Out(); // Create an instance of the class
int x = ob.cal(); // Call the method cal() and store the result in x
System.out.println(x); // Print the value of x
}
}
The above program is an example of ..
ID: #22239
switch case in Java
116 views
Question Info
#22239Q ID
EasyDifficulty
switch case in JavaTopic
Your Answer
Choose the Best Option
Click any option to instantly check if you're correct.
Correct Answer
Explanation
Let's analyze the corrected Java code to determine its type:
Corrected Code:
class Out {
int a = 5; // Adding a variable to compare with x
int cal() {
int x = 10;
if (x > a) {
return --x; // Decrements x by 1 and returns 9
} else {
return ++x; // Increments x by 1 and returns 11
}
}
public static void main(String[] args) {
Out ob = new Out(); // Create an instance of the class
int x = ob.cal(); // Call the method cal() and store the result in x
System.out.println(x); // Print the value of x
}
}
Explanation:
The method cal() performs an operation based on the class-level variable a and modifies the value of x. It returns 9 in this case, because x is greater than a.
- The method is considered an impure method because it depends on and operates based on mutable state.
Answer:
The program demonstrates an example of an Impure Method.
Continue Practice
Share
Share This Question
Challenge a friend or share with your study group.
More from This Topic