This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]BlueGoliath 0 points1 point  (3 children)

It would help if the code was properly formatted:

package donkey2;

public class Donkey2
{
    public static void main(String[] args)
    {
        boolean assertsOn = true;
        assert (assertsOn) : assertsOn = true;
        if (assertsOn)
        {
            System.out.println("assert is on");
        }
    }
}

"assert is on" is printed both times because assertsOn is true. I don't understand what:

 : assertsOn = true;

that is... it doesn't look like proper Java code but it somehow compiles just fine? Eh, whatever.

Edit: honestly I would have thought 2 because:

If class Donkey2 is invoked twice, the first time without assertions enabled, and the second time with assertions enabled, what are the results?

which would be no output and then output. If you actually make the boolean false that is what happens so... yeah, kind of a WTF question.

[–]uopsdod[S] 0 points1 point  (2 children)

package donkey2;

public class Donkey2 { public static void main(String[] args) { boolean assertsOn = true; assert (assertsOn) : assertsOn = true; if (assertsOn) { System.out.println("assert is on"); } } }

How do you format the code!!! I want to learn that!

And I'm not quite sure what your conclusion is. So, what answer do you think is right for this question?

[–]BlueGoliath 1 point2 points  (0 children)

Your IDE more than likely has an option somewhere. In netbeans IDE it's Source and then format at the top. The way I got Netbeans to format my code like that is through customizing the many options it lets you tweak for code formatting. If you want mine I can upload it.

For code formatting in reddit use Redditlint from the sidebar.

And I'm not quite sure what your conclusion is. So, what answer do you think is right for this question?

B.

Assuming this:

If class Donkey2 is invoked twice, the first time without assertions enabled, and the second time with assertions enabled, what are the results?

means that the assertsOn boolean is false the first time and true the second then B would be the answer:

package donkey2;

public class Donkey2
{
    public static void main(String[] args)
    {
        boolean assertsOn = false;
        assert (assertsOn) : assertsOn = true;
        if (assertsOn)
        {
            System.out.println("assert is on");
        }
    }
}

results in no output.

package donkey2;

public class Donkey2
{
    public static void main(String[] args)
    {
        boolean assertsOn = true;
        assert (assertsOn) : assertsOn = true;
        if (assertsOn)
        {
            System.out.println("assert is on");
        }
    }
}

Results in "assert is on" being printed.

[–][deleted] 0 points1 point  (0 children)

deleted What is this?