I am 2nd-year computer science student and I just got back from my first ever interview.
Basically at the end my interviewer asked me to write a function that takes two arguments, an amount in pounds and a tax percentage and then return an array with the tax amount.
I eventually wrote something like this after some trial and error:
public static double[] taxAmount (double pounds, double taxPercentage) {
double taxAmount = pounds * taxPercentage/100;
double[] taxAmountArray = new double[1];
taxAmountArray[0] = taxAmount;
return taxAmountArray;
}
It worked and he seemed happy, what I am wondering is why I needed to return an array for this task? I just feel like the question was really stupid the array is useless for this task right?. Am I missing something?
Even if you do not initialize the array, the Java compiler will not give any error. Normally, when the array is not initialized, the compiler assigns default values to each element of the array according to the data type of the element.
Overview. An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. Depending on the language, array types may overlap (or be identified with) other data types that describe aggregates of values, such as lists and strings.
An array is defined as a collection of items that are stored at contiguous memory locations. It is a container which can hold a fixed number of items, and these items should be of the same type. An array is popular in most programming languages like C/C++, JavaScript, etc.
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application.
The interviewer most likely wanted you to comment on why you were being asked to return the tax amount in an array, just like you are doing right now.
If you voiced your confusion during the interview like you did in this question, you passed the test.
Essentially the question was likely designed not just to check if you could identify the bizarreness of returning an array, but also whether you would have the confidence to be able to communicate your confusion and challenge your future supervisor if you got the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With