Menu
Topics Index
...
`

Array declaration and foreach loop


If you need explanation Read this topic

If you need Answer Take test on this topic

Array declaration and foreach loop
What will be the ouput of the following program?
public class Marks {
    public static void main(String s[]) {
        boolean[] s1 = new boolean[4];
        boolean[] s2 = {true, false, true, false};
        System.out.print((s1[0] == s2[0]) + " ");
        System.out.print(s1[1] == s2[1]);
        int i = 0;
        System.out.println();
        for (Boolean b : s2) {
            while (b.equals(s1[i])) {
                System.out.print(s2.equals(s1) + " ");
                i++;
            }
        }
    }
}

A. false true
false false false false
B. false false
false false
C. false true
true false true false
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java equals method vs == Operator

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is E,
Array index out of bounds error as i value incrementing every time when i=4, b.equals[4] will be array out of bounds error, only 0to 3 is applicable.

Posted by Amar Kumar Biradar    2013-11-27 12:46:03


A is the answer. .(s1[0]==s2[0]) is false and (s1[1]==s2[1]) true and remainng all false...along with it arrayOutofbound may araises

Posted by Shabaaz Shaikz    2013-11-27 12:51:34


d

Posted by Aruna Dantu    2013-11-27 12:52:36


Congratulations Amar Kumar Biradar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-11-28 03:18:53


Thanks Merit Campus :)
Got Recharge ;)

Posted by Amar Kumar Biradar    2013-11-28 05:46:37


© meritcampus 2019

All Rights Reserved.

Open In App