CSC204 Chapter 6: Data Types

BrightHydrangea avatar
BrightHydrangea
·
·
Download

Start Quiz

Study Flashcards

40 Questions

How are characters commonly stored?

As numeric coding

What is the most commonly used coding for characters?

ASCII

Which programming languages support Unicode?

Java and JavaScript

How are strings implemented in C and C++?

Using char arrays and a library of functions

What is a typical operation for strings?

Substring reference

Which language supports all three string length options?

Ada

What is the advantage of having strings as a primitive type?

It aids to writability and is inexpensive to provide

What kind of descriptor is needed for dynamic length strings?

Run-time descriptor for current length only

What is a data type?

A collection of data values and a set of predefined operations on those values

What is a descriptor?

The collection of the attributes of a variable

What are primitive data types?

Data types that are reflections of the hardware or require little non-hardware support for their implementation

What is the most common primitive numeric data type?

Integer

How are negative integers stored in most computers?

In two's complement notation

What is the range of values for a Boolean data type?

Two elements, one for 'true' and one for 'false'

What do floating-point values represent?

Approximations of real numbers

How do double variables usually compare to float variables in terms of storage?

They occupy twice as much storage

What is a characteristic of enumeration types?

All possible values are provided in the definition.

What is an advantage of using enumeration types?

It enhances the readability of the code.

What is a limitation of enumeration types in C++?

Enumeration variables cannot be assigned a value outside its defined range.

What is a characteristic of arrays?

Elements are of the same data type.

What is the purpose of indexing in arrays?

To map indices to elements.

Which language uses parentheses to specify array references?

FORTRAN

What is the benefit of using enumeration types in terms of reliability?

It prevents operations that are not defined for the enumeration type.

What is the difference between array references and function calls in Ada?

Array references use parentheses, while function calls use brackets.

What is the purpose of using arrays?

To store data values of the same type and process them in the same way

What is the range of values for a pointer type variable?

Memory addresses and the value nil

What is the purpose of dereferencing?

To get the value stored at the location represented by the pointer’s value

What is the main difference between arrays and records?

Access to array elements is slower than access to record fields

How are pointers used in C and C++?

To manage dynamic storage and addressing

What is the purpose of the dereferencing operator in C++?

To get the value stored at the location represented by the pointer’s value

What is the main characteristic of pointer assignment in C++?

Explicit dereferencing is used via the * operator

What is the purpose of records?

To store heterogeneous data values and process them differently

What does the asterisk (*) denote in C and C++?

The dereferencing operation

What is the purpose of type checking in programming languages?

To ensure the operands of an operator are of compatible types

Which of the following languages is nearly strongly typed?

Java

What is the result of assigning an integer value to a reference variable in C++?

The reference variable is updated with the new value

What is coercion in the context of programming languages?

The automatic conversion done by the compiler/interpreter

What is the purpose of the ampersand (&) in C and C++?

To produce the address of a variable

What is a type error in programming languages?

The application of an operator to an operand of an inappropriate type

Which of the following languages is not strongly typed?

C

Study Notes

Introduction to Data Types

  • A data type defines a collection of data values and a set of predefined operations on those values.
  • A descriptor is the collection of the attributes of a variable.
  • An object represents an instance of a user-defined (abstract data) type.

Primitive Data Types

  • Primitive data types are those not defined in terms of other data types.
  • Some primitive data types are merely reflections of the hardware, while others require only a little non-hardware support for their implementation.
  • Examples of primitive data types include:
    • Integer: a common numeric data type, with signed integer sizes in Java including byte, short, int, and long.
    • Floating-Point: a data type that models real numbers, but only as approximations, represented as fractions and exponents.
    • Boolean: a simple data type with a range of values consisting of two elements, one for "true" and one for "false".
    • Character: a data type stored as numeric coding, with the most commonly used coding being ASCII.

Character String Types

  • Character string types are values that are sequences of characters.
  • In C and C++, character string types are not primitive, but rather use char arrays and a library of functions that provide operations.
  • In Java, character string types are primitive via the String class.
  • Typical operations on character string types include:
    • Assignment and copying
    • Comparison (=, >, etc.)
    • Catenation
    • Substring reference
    • Pattern matching
  • String length options include:
    • Static: COBOL, Java's String class
    • Limited Dynamic Length: C and C++
    • Dynamic (no maximum): Perl, JavaScript
    • Ada supports all three string length options

Enumeration Types

  • Enumeration types are defined by specifying all possible values, which are named constants.
  • Examples of enumeration types include:
    • enum days {mon, tue, wed, thu, fri, sat, sun};
    • enum colors {red, blue, green, yellow, black};
  • Evaluation of enumeration types:
    • Aid to readability, e.g., no need to code a color as a number
    • Aid to reliability, e.g., compiler can check operations and prevent assignment of values outside the defined range

Array Types

  • An array is a homogeneous aggregate of data elements, where an individual element is identified by its position in the aggregate, relative to the first element.
  • The individual data elements of an array are of the same type.
  • References to individual array elements are specified using subscript expressions.
  • Indexing (or subscripting) is a mapping from indices to elements.
  • Index syntax varies between languages, with some using parentheses and others using brackets.

Record Types

  • A record is a heterogeneous aggregate of data elements, where each element is a field.
  • Fields are identified by their names.
  • Access to record fields is much faster than access to array elements, because field names are static.

Pointer and Reference Types

  • A pointer type variable has a range of values that consists of memory addresses and a special value, nil.
  • Pointers provide the power of indirect addressing and a way to manage dynamic memory.
  • A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap).
  • Pointer operations include:
    • Assignment: setting a pointer variable's value to some useful address
    • Dereferencing: yielding the value stored at the location represented by the pointer's value

Type Checking and Strong Typing

  • Type checking is the activity of ensuring that the operands of an operator are of compatible types.
  • Compatible type is one that either is legal for the operator or is allowed under language rules to be implicitly converted to a legal type.
  • Coercion is the automatic conversion done by the compiler/interpreter.
  • Type error is the application of an operator to an operand of an inappropriate type.
  • Strongly typed programming languages are those that detect type errors.
  • Examples of strongly typed languages include Java and C#, while C and C++ are not strongly typed languages.

This quiz covers the topics of primitive data types, character string types, enumeration types, array types, associative arrays, record types, pointers and reference types, and type checking in programming languages.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser