Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was I asked to return an array for this interview task? [closed]

Tags:

java

arrays

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?

like image 454
StormzyInnit99 Avatar asked Dec 11 '17 06:12

StormzyInnit99


People also ask

What will happen if you do not initialize an array?

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.

What is array in simple language?

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.

What is array in Python?

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.

What is array in Java?

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.


1 Answers

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.

like image 179
Sash Sinha Avatar answered Sep 30 '22 16:09

Sash Sinha