Monday, 27 June 2011

Basic Interview question. What is a variable in programming language?

Narrating after conducting a core level technical interview for some bright .Net based experienced candidates.

Every one had quite good resume and their communication standard varied from poor to average to excellent. But towards the interview desk, hmmm, the play was extremely different. Around 70% of the applicants failed in the "knowledge of basics".

The one basic question one can come across is "What is a variable?". Be it a fresher or an experienced, without knowing the base facts how can a company trustfully assign you projects or work?

General figure, syntax: DataType variableName;

PROGRAMMER'S VIEW

Q.What is a variable.
A.
i) In simple words, it is a named memory location.
ii) Thinking wider, it is a name assigned to a memory location for the programmers to understand and use.
iii) Further wide, it is a name assigned to a memory location in a programming language for the programmers to understand no matter what the datatype is (better not to use it).

Q. Why do we use variables?
A. To store values. As simple as that.

Q. Why do we store values?
A. In a program it is the choice of a programmer whether to use a variable or not. By using variables we can use the same memory location to store different but same type of values at different instance of time (at a time only one value can be stored in a simple variable or else the answer give way to question of arrays and other stacks).

Q. What is the size of memory that a variable holds?
A. Size of memory depends on the type of data that variable can hold. (any guess about the next question)

Q. What is a 'type of data' or 'DataType'?
A. Dataype is a class that is used to hold data.
FYI, a datatype decides what amount of memory to be reserved for a variable (do not include this line while answering unless you are that confident as the questions may go further deeper ;-)).

Q. Give examples of DataType.
A. boolean, byte, char, int, decimal(System.Decimal), double(System.Double), short(System.Int16), float, Integer(System.Int32), long (System.Int64).

Q. What memory size does the variable "uint myUInt"; occupy?
A : 32 bit
FYI: Know the basics
The base of all datatype is bit, the tiny particle that you see while going deeper.
1 byte = 8 bits, maximum value = (2^8) -1 = 255, range = 0 to 255
2 bytes = 16bits, maximum value = (2^16) -1 = ?
4 bytes = 32 bits, maximum value = (2^32) -1 = ?
8 bytes = 64 bits., maximum value = (2^64) -1 = ?
Do not confuse with bit and byte.
The reason why I said this is because, always remember an Int (Integer type) is always 32bits.
So no matter it is uint or int, the size is 32bits.
summary : int = System.Int32;

Lot more about variables, like what are the various types of variables, etc etc. Shall update you with those in my next blog.

No comments:

Post a Comment