Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

4test-Mock Problem

 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code below produces...Runtime error.But, the answer given is d.Produces no output.
public class Test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
System.out.println(i * j);
}}}}
a.Generate a runtime error
b.Throw an ArrayIndexOutOfBoundsException
c.Print the values: 1, 2, 2, 4
d.Produces no output
Can you please...tell me if am wrong somewhere.
Mukti
 
Susan Hoover
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What value of a are you passing to the Test.add method? If you say Test.add(3), you will get 1,2,2,4 output, each number on its own line. If you say Test.add(5), you will get no output. What is the runtime error that you are getting?
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Susan,
I am getting runtime error....as Exception in thread "Main"...nosuch method found:Main.But in this code 'int a' is not initialzed.
Getting confused......
Please do reply.
Mukti
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The highlighted point is that there is no main() method in your
code. Without main() method, it is not an application program.
So you have to think first about main() method then other
variables and initialization.
So Runtime error is correct, but in other way, D. is correct,
because without main() method you would never get any output.
Thanks,
G. Newaz
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic