Menu
Topics Index
...
`

Years


If you need explanation Read this topic

If you need Answer Take test on this topic

Years
What will be the output of the following program?
import java.io.*;
public class Years {
    public static void main(String args[]) throws IOException {
        byte[] years = {20, 30, 40, 50};
        ByteArrayInputStream leap = new ByteArrayInputStream(years);
        int count = 0;
        while ((count = leap.available()) > 0) {
            char ch = (char) leap.read();
            System.out.print(count + ", ");
        }
    }
}

A. 3, 2, 1, 0
B. 4, 3, 2, 1
C. Some other output
D. Compilation Error or Runtime Error
Topic: Java ByteArrayInputStream

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.

c) Output is 4, 3, 2, 1,     available method gives the no.of bytes left in the stream.. For the first iteration the result would be 4, and a char is taken from the input stream, so the bytes available in the input stream decreases by one. for the second iteration the value of count will be 3 and 3, would be printed, and so on...

Posted by Nikhil Sulibhavi    2014-02-07 13:53:09


Ans is B.... bcoz available() method returns the number of bytes left to be read from this input stream..thus the result would be B

Posted by Sneha Rapool    2014-02-07 13:56:28


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

Posted by Merit Campus    2014-02-10 04:01:32


© meritcampus 2019

All Rights Reserved.

Open In App