Asked by anonymous
Consider the following program segment. Without using Dr.Java, What this program prints? (my class uses Dr.Java)
The answer that this program prints which i got was: 22
Is my answer correct?
public class sample {
public static void main(String[] args){
Sample x = new Sample();
int i = 10;
int b = x.g(i);
System.out.println(b + i);
}
public int f(int d){
int n = 0;
while(n*n < d){
n++;
}
return n-1;
}
public int g(int a){
int b = 0;
int c;
for(int n = 0; n < a; n++){
c = f(n);
b = b + c;
}
return b;
}
}
The answer that this program prints which i got was: 22
Is my answer correct?
public class sample {
public static void main(String[] args){
Sample x = new Sample();
int i = 10;
int b = x.g(i);
System.out.println(b + i);
}
public int f(int d){
int n = 0;
while(n*n < d){
n++;
}
return n-1;
}
public int g(int a){
int b = 0;
int c;
for(int n = 0; n < a; n++){
c = f(n);
b = b + c;
}
return b;
}
}
Answers
Answered by
oobleck
I assume your answer is correct, if you ran the code and that's what it printed.
Answered by
anonymous
i didn't run the program (i swear). i only looked the the program and i was think if the answer was 22
Answered by
oobleck
so, run it, and see whether you were right!
Answered by
anonymous
ok thanks
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.