Menu
Topics Index
...
`

Assert test


If you need explanation Read this topic

If you need Answer Take test on this topic

Assert test
Which line is an example of an inappropriate use of assertions?
public class LikeIt {
    public static int x;
    public static int foo(int y) {
        return y * 2;
    }
    public static void main(String[] args) {
        int z = 5;
        assert z > 0; //LINE M
        assert z > 2 : foo(z); //LINE N
        if (z < 7)
            assert z > 4; //LINE O
        switch (z) {
            case 4 :    System.out.println("4 ");
            case 5 :    System.out.println("5 ");
            default :    assert z < 10;
        }
        if (z < 10)
            assert z > 4 : z++; //LINE P
        System.out.println(z);
    }
}

A. LINE M
B. LINE N
C. LINE O
D. LINE P
Topic: Advanced Java Programming Concepts

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 C  line o because foo(z) returns 4 which is not greater than 4.

Posted by Shiva Goud    2014-02-25 08:24:52


Answer is D (LINE P). Assert statements should not cause any side effects. Here LINE P changes the value of z if the assert statement is false. An assert statement has two forms:             1)  assert Expression1;                                                                     2)  assert Expression1 : Expression2;                                                              LINE M and LINE O correspond to the first form and it is valid to call an assert statement conditionally and LINE N corresponds to the second form. So, LINE M, LINE N and LINE O are fine.

Posted by Shaileshwar Sharma    2014-02-25 12:47:17


No winner for this dose. Correct option is 'D' :(. Shaileshwar Sharma was already won in this week.

Posted by Merit Campus    2014-02-26 04:44:45


© meritcampus 2019

All Rights Reserved.

Open In App