-2

Which of the following statements is FALSE?

(A) In statically typed languages, each variable in a program has a fixed type

(B) In un-typed languages, values do not have any types

(C) In dynamically typed languages, variables have no types

(D) In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program

Can you please explain the theory as well?

surbhi bakshi
  • 160
  • 1
  • 1
  • 17

1 Answers1

1

C) (In dynamically typed languages, variables have no types) Is false.

The variable has a type, however it is simply not stated or decided until run time. This implies there is no type checking prior to running the program.

a useful link describing Types and what it means:

http://en.wikipedia.org/wiki/Type_system

If you have ever done much with PHP you will notice that when you declare a varialbe, you do not have to say whether it is an INT or a STRING. However, sometimes you know that you will be receiving a string, but need an int, so you can still type cast variables at runtime, even though when you declared the variable you did not explicitly state the variable would hold an int.

<?php
#some more code here.....
# over here $myValue could be of some different type, but it can dynamically change to another type
$myValue = '5'; #storing a string...so $myValue is currently of type String
$myNewValue = (int)$myValue + 5 #type casted to integer, so in this case $myValue is currently of type integer
?>

If that doesn't help, maybe take a look at this.

myPythonVariable = "I am currently a string" #the variable is of type string
myPythonVariable = 5                         #the variable is now of type integer

In the above code sample, myPythonVariable always has a type, whether or not that type changes doesn't matter.

TheOneWhoPrograms
  • 629
  • 1
  • 8
  • 19
  • "The variable has a type, however it is simply not stated or decided until run time." Do you agree that in a dynamically typed language a variable at one time can store a value of type `Int`, at another time a value of type `Bool`? If so, then what is the type of that variable? – xxa Apr 24 '14 at 23:38
  • I would personally just state its a dynamic language and variables change throughout the run time. With that being said you can say that at x point in the program the variable is an Int, but at y point in the program is it a boolean. I personally try not to change it too much from its 'original' type to avoid confusion of what the variable is suppose to be at some point in the program. – TheOneWhoPrograms Apr 25 '14 at 06:18
  • Ok, but then your statement "The variable has a type, however it is simply not stated or decided until run time" does not stand in my opinion. The variable cannot have **a** (one) type assigned to it. – xxa Apr 25 '14 at 10:32
  • Does my edit help? It always has A type assigned to it (one). But the type CAN change. When it changes, it is no longer at all the old type, but it is a new type, therefore it is still only ONE type. – TheOneWhoPrograms Apr 25 '14 at 10:39
  • To be honest, I do not think this is the right approach to the concept "has a type". By using the same logic, you could say that a variable is a constant, since at any time during execution it holds one particular value. For "has a type" to be of any value, we should be able to know it _in advance_, so that checks could be made (by the compiler, for example) that the use of the variable conforms to its type. – xxa Apr 25 '14 at 11:46
  • But languages with dynamic typing often don't have compilers or ignore typing in that case. The whole point of it is it can theoretically be of any type and you have to control it properly to ensure it is the type you want. The whole idea around it makes it so a compiler would not be able to know. You can still check its type during execution, take a look at: `http://stackoverflow.com/questions/378927/what-is-the-best-idiomatic-way-to-check-the-type-of-a-python-variable`. Anyways, this is becoming a chat in a comment section. If you would like, you can start a chat room. – TheOneWhoPrograms Apr 25 '14 at 12:00