Menu
Topics Index
...
`

Buffers


If you need explanation Read this topic

If you need Answer Take test on this topic

Buffers
What will be the output of the following program?
import java.nio.*;

public class Buffers {
    public static void main(String[] args) {
        FloatBuffer floatBuf = FloatBuffer.allocate(10);
        floatBuf.put(0);
        floatBuf.put(1);
        floatBuf.put(0);
        floatBuf.put(0);
        System.out.print(floatBuf.position() + ", ");
        floatBuf.put(5);
        floatBuf.put(56);
        System.out.print(floatBuf.capacity() + ", ");
        floatBuf.put(66.6);
        System.out.print(floatBuf.limit() + ", ");
        System.out.println(floatBuf.remaining());
        floatBuf.put(56);
    }
}

A. 3, 9, 10, 3
B. 4, 10, 10, 3
C. Some other output
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

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.

option B as float buffer is allocated with size 10 while intialization.
After inserting 0100 its position will be at 4
capacity is remains as allocated i.e 10
limit will also be 10 and remaining space for byte is 3

Posted by Sasikanth Sharma    2014-08-19 04:05:38


b...allocate fnctn wil alocate space fr buffer content.
put() wil read singl float...
hence frst prnt stmtmt wil ptrnt 4.
nxt.stmtnt wil print capacity value -10

thrd prnt limit as  10
an d lsst wil prnt 1

Posted by Asma Mujtaba Khan    2014-08-19 04:05:43


No winner for this dose. Correct option is 'E' :(. Why because at line 14 we are passing 66.6 by default it will consider as a double but we need to pass float.The correct way to pass 66.6 is floatBuf.put(66.6F) or  floatBuf.put((float) 66.6).

Posted by Merit Campus    2014-08-20 05:32:28


© meritcampus 2019

All Rights Reserved.

Open In App