Share on Google+Share on Google+

SCJP Module-5 Question-11

Advertisement
The Sample program given below will test your understanding about the nested loops in Java.

Given a sample code:

1    import java.util.ArrayList;

2    public class Test2 {
3    public static void main(String[] args) {
4    ArrayList<Integer> numbers = new ArrayList<Integer>();
5    for (int i = 1; i <= 3; i++) {
6    for (int j = 1; j <= 3; j++) {
7    int value = i * 2 / 2;
8    {Integer intObj = new Integer(value); numbers.add(intObj);}
    } }
9    { System.out.println(numbers); }
    }}

What will be result when compiled and run?

(A)  [1, 2, 3, 1, 2, 3, 1, 2, 3]
(B)  [1, 1, 1, 2, 2, 2, 3, 3, 3]
(C)  Compilation error at line no-8. 
(D)  Compilation error at line no-9. 

Answer:

(B)

Advertisements

Posted on: July 22, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.