Java Programming - Objects and Collections - Discussion

6. 

What will be the output of the program?

public class Test 
{ 
    private static float[] f = new float[2]; 
    public static void main (String[] args) 
    {
        System.out.println("f[0] = " + f[0]); 
    } 
}

[A]. f[0] = 0
[B]. f[0] = 0.0
[C]. Compile Error
[D]. Runtime Exception

Answer: Option B

Explanation:

The choices are between Option A and B, what this question is really testing is your knowledge of default values of an initialized array. This is an array type float i.e. it is a type that uses decimal point numbers therefore its initial value will be 0.0 and not 0


@Jyotig said: (May 12, 2014)  
Why 0.0 not 0F, as per previous questions 9 of general ques. of Collection topic 0.0 indicate double value because any literal number with a decimal point you declare the computer will implicitly cast to double unless you include "F or f".

Josh B said: (Feb 24, 2016)  
This is testing knowledge of the format in which 0 is printed. The value 0 and 0 of are the same.

Arif Shaikh said: (Dec 23, 2016)  
How come a private member is accessed in the main function?

Saswata said: (Oct 22, 2017)  
I also agree @Arif.

Sneha said: (Aug 22, 2019)  
Here, f = new float[2]; is array size.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.