Check the code. Would it compile? If not, make logical change (smallest one poss
ID: 3604896 • Letter: C
Question
Check the code. Would it compile?
If not, make logical change (smallest one possible) such that it would work. Explain what was the problem.
public static int getStrippedNumber(int num) {
int numberOfDecSets;
if ((numberOfDecSets = num % 10) == 0) {
return num - numberOfDecSets;
} else {
num= num - (numberOfDecSets + 1 );
}
}
What would be printed (before and after changes) if the method would be invoked from main as
System.out.println(getStrippedNumber(100)); // prints
System.out.println(getStrippedNumber(55)); // prints
System.out.println(getStrippedNumber(152)); // prints
Explanation / Answer
public static int getStrippedNumber(int num) {
int numberOfDecSets;
if ((numberOfDecSets = num % 10) == 0) {
return num - numberOfDecSets;
} else {
return num - (numberOfDecSets + 1 ); //we should return value to the called function.
}
}
System.out.println(getStrippedNumber(100)); // prints 100
System.out.println(getStrippedNumber(55)); // prints 49
System.out.println(getStrippedNumber(152)); // prints 149
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.