Menu
Topics Index
...
`

Print HEADS or TAILS


If you need explanation Read this topic

If you need Answer Take test on this topic

Print HEADS or TAILS
What will be the output of the following program?
import java.util.*;
public class SideBySide {
    public static final SideBySide HEADS = new SideBySide("Heads");
    public static final SideBySide TAILS = new SideBySide("Tails");
    private final String name;
    private SideBySide(String name) {
        this.name = name;
    }
    public String toString() {
        return name;
    };
    private static Random rnd = new Random();
    public static SideBySide flip() {
        return rnd.nextBoolean() ? HEADS : TAILS;
    }
    public static void main(String[] args) {
        System.out.println(flip());
    }
}

A. Tails
B. Heads
C. Some other output
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: Java Random Class - java.util.Random Package

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.

ans is D..random may give anything so we cant determin output

Posted by Uday Kumar    2014-07-14 13:14:49


Answer d.
in tis problm 2 possible values r thr true n false.
if value  true thn heads get printd.
if value false thn tails get printd.
bt we cant determine occurate result.

Posted by Maheshwari Natarajan    2014-07-14 13:36:47


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

Posted by Merit Campus    2014-07-15 11:10:12


© meritcampus 2019

All Rights Reserved.

Open In App