Menu
Topics Index
...
`

Cuckoo Clock


If you need explanation Read this topic

If you need Answer Take test on this topic

Cuckoo Clock
What will be the output of the following program?
import java.text.*;
import java.util.*;
public class CuckooClock {
    public static void main(String[] args) throws ParseException {
        printCuckoos("02:15", "04:55");
        printCuckoos("15:15", "17:55");
        printCuckoos("06:00", "09:00");
    }
    protected static void printCuckoos(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(sdf.parse(start));
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(sdf.parse(end));
        while (startDate.before(endDate)) {
            String currentDate = sdf.format(startDate.getTime());
            if (currentDate.endsWith(":00")) {
                printCuckoo(Integer.parseInt(currentDate.split(":")[0]));
            }
            startDate.add(Calendar.MINUTE, 1);
        }
        System.out.print("|");
    }
    private static void printCuckoo(int hours) {
        int i = 0;
        while (i < hours % 12) {
            System.out.print("Cuckoo".charAt(i++ % 6));
        }
        System.out.print("*");
    }
}

A. Cuc*Cuck*|Cuck*Cucko*|Cuckoo*CuckooC*CuckooCu*|
B. Cuc*Cuck*|CuckooCuckooCuck*CuckooCuckooCucko*|Cuckoo*CuckooC*CuckooCu*|
C. Cuc*Cuck*|Cuck*Cucko*|Cuckoo*CuckooC*CuckooCu*CuckooCuc*|
D. Some other output or Compilation Error
E. Runtime Error
Topic: Calendar In Java - java.util.Calendar Class

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.

E.runtime error as NumberFormatException araises.

Posted by Shabaaz Shaikz    2014-04-15 12:56:06


Ans is A. For the first time hours values is 3 so hours % 12 value will become 3 and it print characters at 0, 1 and 2 respectively i.e., Cuck and after that *. Next time hours value is 4 so Cuck will be printed. Next time hours value will be 16 and after that 17 and so on

Posted by Nikhil Sulibhavi    2014-04-15 13:35:52


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

Posted by Merit Campus    2014-04-16 04:17:22


© meritcampus 2019

All Rights Reserved.

Open In App