Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

True or False: A string is the same thing as an array.

I had an interview the other day and the above question was asked. I said false, but the interviewer said it was actually true. I explained to her that a string is a data type and an array is an object that holds multiple instances of a variable, and that they aren't even similar concepts. But, she was an HR person who was just reading tech questions that were handed to her, so she couldn't elaborate or relate to my point.

A coworker later explained the True answer by stating that a string is actually a character array... thus, a string is an array. Now, if a string is a character array, that doesn't mean that document arrays or integer arrays qualify as strings. Thus, a string is not the same thing as an array.

But even so, (considering C#) a string isn't even a character array. In order to do character manipulation on a string (without using concatenation methods), you have to convert the string to a character array. Now, if a string was synonymous with a character array, why would you need to convert it? Wouldn't you just be able to call each character in the string implicitly as you would a character array? No! Why? Because a string is not a character array!

I liken it to asking, "Is a primary key the same as a unique identifier?" The answer is false. Reason being: A primary key is a unique identifier, but you can have unique identifiers in a table without setting them as keys. What do you all think? Is a string (data type) the same thing as an array (object type)? If so, why?

share|improve this question
    
A primary key is a unique identifier. If the question you are asked "is a primary key the only possible way to obtain a unique identifier," then you're right; the answer is no. You have to think carefully about how the question is asked. –  Robert Harvey Sep 27 '13 at 0:26
2  
The syntax of the question is key. Is a primary key a unique identifier? Sure. Of course. But to ask "are they the same thing" implies that they aren't mutually exclusive. Just like, a lion is a mammal... but not all mammals are lions. Thus, a lion isn't the same as a mammal. –  Chris Hill Sep 27 '13 at 0:29
1  
I just don't think that logic applies to the question you posed. "Is a primary key exactly the same as a unique identifier?" does, but "Is a primary key essentially the same as a unique identifier?" doesn't. You can always ask clarifying questions in an interview; in fact, that's what they want you to do. –  Robert Harvey Sep 27 '13 at 0:31
1  
If I got this question in an interview, my immediate response would mean "Do you mean "are strings implemented in physical memory just like arrays?", or do you mean "are can strings be seen as the same as arrays at a conceptual level?" –  Steven Burnap Sep 27 '13 at 2:46
4  
Look on the bright side, if you dont get the job you may have dodged a bullet. Ignoring the rediculousness of having non-programmers ask programming questions and then evaluate the answers, you have to ask what kind of programmers would provide to the HR people such a poorly thought out question in the first place. They would have been your co-workers. –  GrandmasterB Sep 27 '13 at 3:14
show 6 more comments

6 Answers

The question is a silly question without qualification. Without specifying the language in the original question, there is no way of knowing the intended answer.

If the question was "is the default implementation of a string an array in C" then the answer would be "yes". If the question was "is a string always an array" then the answer is no, as a string could be implemented in any number of ways, including but not limited to as an array of bytes or a linked list of characters. Even in C#, a string may be null, which is NOT an array.

The problem here is that the recruiter was given a question that they were meant to ask, expecting a binary answer. There was no opportunity to clarify what the question was asking.

In these circumstances my answer would have been:

"A string in C# is not an array, but is most likely implemented using an underlying array of characters. This is dependent on many factors and the implementation may be different depending on the implementation of the compiler. It would be perfectly valid for the compiler author to create a string implementation as a linked list of characters."

share|improve this answer
2  
"Even in C#, a string may be null, which is NOT an array." I think that sentence alone puts the whole question to rest. –  Chris Hill Sep 27 '13 at 11:54
1  
Huh? String and arrays are reference types, so a variable of either type can be null. And AFAIK there's no "null string" which is a string object and somehow represents absence of a string. There's the empty string, but there are also empty arrays. –  delnan Sep 27 '13 at 14:00
    
null is null. It does not have an inherent type. null is not a string. A string may have a value of null, but that doesn't mean that null is of type string. It's a very fine semantic point though and was only a very small, almost inconsequential part of my original response. –  Stephen Oct 1 '13 at 1:28
add comment

Well of course they aren't the same, in the same way that a cat is not the same as a dog because they are both mammals and a crab is not a spider even because they are both from phylum arthropoda; though I've had teachers tell my children the latter, thus necessitating a daddy lecture on evolution, biological classification of species, and the importance of never relying on single-sources - and yes, even daddy can be wrong, so here's how you use Google.

But I digress...

That a "string" is not the same as an "array" (and a "String" isn't the same as an "Array" either, and mix-match), so much so that either it was just a bad question (it happens), or it was a devious one; that is to say, see how someone responds to someone being incorrect about something, sloppy logic, "just being an HR person", etc. The base of Human Resources is Industrial/Organizational Psychology (or "work psychology" less formally and especially around Europe), and as a double-major in Psychology I assure you that there are few things more interesting to the common psychology student than observing how people respond to unexpected stimuli.

It might have been a silly bad question, of course, or it was a really simple trick to see how someone deals with someone they view as not technically inclined, to attempt to evaluate conflict resolution, communication, interpersonal/team skills, temperament, self-confidence, etc. Some people get indignant, insulting, condescending, falsely agreeable, overconfident, extremely self-conscious and unconfident...YMMV. No sensible person would throw out an applicant because they missed a trick/wrong question like this, but they absolutely would throw out someone who responded particularly poorly - or recommend someone for their excellent communication skills for handling it gracefully.

One must never assume they know who they are really talking to, and every moment is an invitation to show your KSAO's: Knowledge, Skills, Abilities, and Other Characteristics. Of course the interview has consistently been shown to be one of the least effective methods in existence for hiring - usually worse than blind resume-based picks and reference verification alone - but who listens to psychologists anyway? :)

Technically Speaking...

In C# .NET 4.5 Framework a String is a system class, inheriting from System.Object, which is made up chiefly of two pieces of data - a length of an internal array, and an array of Unicode Characters.

An Array, on the other hand, inherits from System.Object as well, and exposes functionality to handle arrays of many varieties.

While 'string' is an alias for String in C#, in addition to being a relatively fundamental programming concept and data structure, 'array' is just a concept and data structure and only appears in C# as a big-letter Array class and effectively aliased with brackets instead of a simple keyword.

One could thus say a "string" is a type (or involves an implementation) of an array, but:

String != Array

QED

share|improve this answer
add comment

In layman's words:

  • Most languages implement strings as arrays of bytes.
  • It may not be true for every language but I suspect most of them do.

Wikipedia:

A string is generally understood as a data type and is often implemented as an array of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding.

You say:

But even so, (considering C#) a string isn't even a character array. In order to do character manipulation on a string (without using concatenation methods), you have to convert the string to a character array.

But that is not true in other languages, for example, in Java you don't have to convert them to a char array.

Java Doc:

For example:

     String str = "abc";

is equivalent to:

     char data[] = {'a', 'b', 'c'};
     String str = new String(data);

It's clear that in Java a String is a wrapper around a char array.

share|improve this answer
    
Right, but we're talking about C#. Why would she ask a question that's been understood to reference development practices in C#, yet apply Java logic? –  Chris Hill Sep 27 '13 at 0:25
    
Besides, what about a document array or an Integer array? Are those strings? –  Chris Hill Sep 27 '13 at 0:40
    
Just to clarify one little thing, a byte array can only store single-byte character encodings like ASCII or Latin-1. Java Strings are arrays of chars which are 2 bytes each for supporting UTF-16. –  jiggy Sep 27 '13 at 2:33
    
Many languages use UTF-8 for strings, which means that characters are varying numbers of bytes. (Also true of UTF-16, actually.) –  Steven Burnap Sep 27 '13 at 2:42
    
@ChrisHill I see no logic in thinking a document array is a string. What's so hard to understand about strings ? –  user61852 Sep 27 '13 at 10:03
add comment

Though the question is baseless without more information, one can argue that from C programming point of view, an array has to boil down to consecutive memory and strings too does the same.

share|improve this answer
1  
Note that if you are using unicode in C, then the tenth index in the array may well not point to the tenth character. –  Steven Burnap Sep 27 '13 at 2:49
    
The real problem is that are we talking about the concept or the implementation? In the case of C that is the implementation of an array, but that may not be how the memory is structured for an array in a different language. The concept of an array is a sequence of values of a specific datatype that can be referenced by an index, but they might not be stored sequentially in memory if the language does not require that. –  Stephen Sep 27 '13 at 2:53
    
See, I don't think that matters. The question was the first in a series of questions that were specifically introduced by the interviewer as "questions to test your C# capabilities." So, as a C# developer, I could give a butt-nugget how a string or an array is stored in memory. If you need the functionality of a string, you declare and use a string. If you need the functionality of an array, you declare and use an array. I just really don't think the fact that "an array is technically a string of bits when it's stored in memory" is relevant at all. –  Chris Hill Sep 27 '13 at 11:11
add comment

Definetly 'False' IMO. While .NET string is indeed implemented using array data structure (and that's not the same as Array type) you can only assume that. Without looking at the source code of string know for sure. And even though I know it for sure, I still would answer 'False' because you can't use

char[] charStr = { 'h', 'e', 'l', 'l', 'o', '\0' };
string trueStr = "hello";

the same way.

This question can be answered as 'True' if it was asked as "String is, most probably, implemented internally using array data structure". Because in initial form (A string is the same thing as an array), it can still be considered false even in C.

share|improve this answer
add comment

True and false. Depending on how you word the question.

Bringing a programming language into this begins to mix concepts and actual types. An array for example is both of these, how they're implemented may introduce differences (is array based on vector? or is vector based on array? .. but a "vector" as a concept is a specialization of an array)

So, it really depends on which question you're asking.

share|improve this answer
    
Fair enough. The question was worded exactly as I mentioned... and the language is C#. But, it's an interview set up to test your ability in developing in C#. So to me, the question refers to how you might manipulate a string vs an array in code, not how it's stored in memory. Because if you wanna look at how data is actually stored in RAM, EVERYTHING could be a string... a string of bits. But when I'm being asked the question in reference to C#, I'm thinking about a string data type vs an array object. –  Chris Hill Sep 27 '13 at 0:23
    
Some languages don't even really have arrays at all, but instead use lists. –  Steven Burnap Sep 27 '13 at 2:54
    
RE the first line: true && fasle evaluates to false. –  MrFox Sep 27 '13 at 14:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.