Programming Language Chapter 7

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is polymorphism? What distinguishes its parametric and subtype varieties? What are generics?

Polymorphism is having same name but serving multiple purposes. Parametric polymorphism: when a function or data types are written generically where the logic and ocde doesn't depend on the type then it is called paramtric polymorphism. A sub type is a data type which is inherited from another data type (super type). A sub typing polymorphism is defining something for super type but at run time, depending on sub type semanctics will be decided. It can also be used at all places where super type is used.

Name two important programming languages that are strongly but dynamically typed.

1. Python 2. Ruby A dynamically typed language is a language where the type of variable can be changed dynamically during the execution of the program.

1. What purpose(s) do types serve in a programming language?

1. Types provide implicit context for many operations, so that the programmer does not have to specify the context explicitly. 2. Types limit the set of operations which can be performed in a valid program, that is, the compiler will know automatically as to what type of operations can be performed on the data. They prevent the programmer from adding a character and a record.

Discuss the differences among the denotational, constructive, and abstraction-based views of types.

Denotational: This point of view considers that a type is a set of values. A value has a given type if it belongs to the set; an object has a given type if its value to be in the set. Ex: Enumeration Constructive: It considers that a type is either one of a small collection of built-in types or a composite type created by applying a type constructor to one or more simpler types. Ex: Array Abstraction-based: It considers that a type is defined by the operations that may be performed by it and the effects of these operations. Ex: Integer

What is the difference between discrete and scalar types?

Discrete data type: Discrete data types are the data types which contain values that are ordered and countable that is each element knowledge about the next previous element. Discrete data types are also called ordinal types. It is a subset of scalar data types. EX: Integers, Booleans, and enumerations are all discrete data types. Scalar data type: Scalar data types are those types which contain only one value at a particular time Scalar data types are also known as simple types. It is a superset or discrete data types. Ex: Discrete, rational, real and complex types are all scalar types.

In what ways may an enumeration type be preferable to a collection of named constants? In what ways may a subrange type be preferable to its base type? In what ways may a string be preferable to an array of characters?

Enumeration types are user defined data types. It allows adding number of data to the enumeration type. The data or element of enumeration type is called members, enumerators or elements. Once the enumeration types are defined, we are able to use enumeration name as data type. This feature is allowed in C++. Enumeration data type is lake structure but instead of consisting different data type variables. It consists of constant named string or word elements. Elements may be strings or characters. Enumeration types allow assigning any enumerators and any number of enumerators. All enumerators are assigned integer number from 0 to enumerator-1 by default. So, enumeration type is preferable to a collection of named constants.

16. Explain the distinction between derived types and subtypes in Ada.

In Ada, both the loose and strict name equivalence can be achieved by allowing the programmer to indicate whether an alias represents a derived typed or a subtype. On creating a new alias, the user has to define if the alias is a derived type or a subtype. A subtype is compatible with it's parent type. A derived type is incompatible with its parent

15. Explain the differences among strict and loose name equivalence.

Strict Name Equivalence: A language in which an alias of an object is created then both the object and its alias are considered as of different type. These different type of consideration of an alias contained by strict name equivalence, that is, aliased types are considered distinct from each other. Loose Name Equivalence: A language in which an alias of an object is created, then both the object and its alias are considered as equivalent to each other. These same type considerations of an alias contained loose name equivalence, that is, aliased types are considered type equivalent to each other.

What does it mean for a language to be strongly typed? Statically typed? What prevents, say, C from being strongly typed?

Strongly typed: the language that requires each object to have type. In the strongly typed language, at run-time, it must be verified for an operation to be allowed on an object. It is not possible to work around the type system. In a strongly typed language, an error will occur if we are trying to apply an operation to an object which does not support that operation. Statically typed: the language if it performs type checking at compile time instead of run-time. In statically typed language, the error will be thrown at compile time not at run time. According to statically typed language the compiler can statically assign a correct type to every expression. C does not perform any check at run time and a s par the discussion in strongly typed, we concern that in strongly typed, checks perform at runt-time. Hence, C is prevented from being strongly typed.

13. What is the difference between type equivalence and type compatibility?

Type Equivalence: User can define types in a language. There are two ways to define type equivalence: structural equivalence and name equivalence. Name equivalence is based on the declarations whereas structural equivalence is based on the notion of meaning of the declarations. Type Compatibility: Most languages do not require type equivalence; instead they say that type must be compatible with that of context in which it appears. Type Equivalence: The object can be used if its type and the type expected by the context are equivalent, that is, the data type of the object is same as that expected. Equivalence is a tighter relationship. Objects and contexts can be equivalent only when their types are same. Type Compatibility: Type Compatibility determines where an objects of a certain type can be used, that is, it tells what kind of data can be held in the object depending on the compatibility of its type. Compatibility is a looser relationship equivalence. Objects and contexts can compatible even when their types are different, that is, if they can be used in the same context even after converting them from one type to the other, still they can be called as type compatible.

What is a type clash?

Type clash is the violation of the type checking rule which states that a program must obey its language's type compatibility rules. Type compatibility means that the value stored in a variable must be compatible with its data type. Ex: int a; a = "welcome";


Conjuntos de estudio relacionados

Texas Govt. Unit 2 Test Review Chapters 6-9

View Set

EMT Chapter 18: Altered Mental Status, Stroke, and Headache

View Set