Values
1.
write down (a) the set of values and the cardinality of each of the following ADA types
( Color, Number, Card, Hand ) then draw diagrams to show how these types would be represented in a computer. type Number is (1, 2, 3); type Color is (white, black); type Card is record c: Color; n: Number; end record ; type Hand is array (Number) of Color;
Number = {1, 2, 3}
# Number = 3
Color = {white, black}
# Color = 2
Card = { (white, 1) , (black,1), (white, 2) , (black,2) , (white, 3) , (black,3) }
#Card = #Color x #Number = 3 x 2 = 6
Hand = 1 (white, 1)
2 (black,3)
3
(black,2)
Hand = #Card #index = 2 3 = 8
Integer type’s Cardinality = 2 n
Integer type’s range of values = (-2 n-1 . . . 2 n-1 -1)
1.
What is the cardinality and the range of values of the integer type (Byte ).
Byte type representation is 1 byte = 8 bits length, n=8
cardinality = 2 8 = 256
range of values = (-2 7 . . . 2 7 -1) = (-128 . . . 127)
2.
What is the cardinality and the range of values of the integer type (Short ).
Short type representation is 2 bytes = 16 bits length, n=16
cardinality = 2 16 = 64K
range of values = (-2 15 . . . 2 15 -1) = (-32K . . . 32K-1 )
3.
What is the cardinality and the range of values of the integer type (int ). int type representation is 4 bytes = 32 bits length, n=16
cardinality = 2 32 = 4G
range of values = (-2 31 . . . 2 31 -1) = (-2G . . . 2G-1 )
4.
What is the cardinality and the range of values of the integer type (long ). long type representation is 8 bytes = 64 bits length, n=64
cardinality = 2 64 = 16E
range of values = (-2 63 . . . 2 63 -1) = (-8E . . . 8E-1 )
5.
What is the smallest possible representation in principle of the following types and what is the compiler likely to choose? a.
Boolean type (bool), also write down the set of values b.
enumerands of type Month represented by the integers {0, …, 11} c.
enumerands of type number defined range of integers of {1, …, 10 9 }
answer: a.
Boolean values = {true, false} true = 1 , false =0
# Boolean = 2 = 2 1 smallest representation in principle is 1 bit <=8 bits the compiler likely to choose 1 byte b.
#Month = 12 <= 16 = 2 4 smallest representation in principle is 4 bit <=8 bits the compiler likely to choose 1 byte c.
#number = 1G <= 1G = 2 30 possible representation in principle is 30 bit <=32 bits the compiler likely to choose 4 byte
6.
Draw diagrams to show how the recursive types to represent integer lists of any length would be represented in a computer. type IntNode; type IntList is access IntNode; type IntNode is record head: Integer; tail: IntList; end record; new IntNode'(2, new IntNode'(3, new IntNode'(5, new IntNode'(7, null)));
Answer:
1.
JAVA arrays are classified as objects. How does this affect the use of arrays in JAVA programs?
Answer: Since a JAVA array is an object, it can be used wherever an object is allowed.
2.
What are the purposes of grouping values into types (type system)?
Answer:
1. to enable programmers to describe data effectively
2. to help prevent type errors.
3.
How type error occurs?
Answer: if a program performs a nonsensical operation such as multiplying a string by a
Boolean
4.
Nonsensical operations cannot be prevented in assembly languages, why?
Answer: There is no type system the only “types” in assembly languages are bytes and words
1.
Haskell treats strings as a.
lists of characters
2.
Ada treats strings as a.
lists of characters
3.
Java treats strings as b. arrays of characters b. arrays of characters c. objects of class String c. objects of class String a.
lists of characters b. arrays of characters
4.
The array index range type in C++, Java is a.
Intger with lower bound 0 c. objects of class String c. any discrete primitive type d. Intger with any lower boun b.
Intger with lower bound 1
5.
The array index range type in Ada is a.
Intger with lower bound 0 c. any discrete primitive type b.
Intger with lower bound 1 d. Intger with any lower bound
6.
accessing a[i] if i is out of range , a program to behave unpredictably in a.
Java b.
Ada c. C, C++ d. a+b
7.
accessing a[i] if i is out of range , a program throws a suitable exception in a.
b.
Java
Ada c. C, C++ d. Java and Ada
8.
The totally updated of array (a1 = a2) in C++ a.
it makes a1 contain a reference to the same array as a2 b.
it assigns to a1 a copy of the a2 array. c.
it assigns to a1 a copy of the a2 array if the two arrays have the same length d.
c. Illegal (not possible)
9.
The totally updated of array (a1 = a2) in Ada a.
it makes a1 contain a reference to the same array as a2 b.
it assigns to a1 a copy of the a2 array. c.
it assigns to a1 a copy of the a2 array if the two arrays have the same length d.
c.
Illegal (not possible)
10.
The totally updated of array (a1 = a2) in Java a.
it makes a1 contain a reference to the same array as a2 b.
it assigns to a1 a copy of the a2 array. c.
it assigns to a1 a copy of the a2 array if the two arrays have the same length d.
c. Illegal (not possible)
11.
int size[] = {31, 28, 30, 30, 31, 30, 31, 31, 30, 31, 30, 31}; the construct the third element in the array with 31 is a.
size[2] = 31; b. size[3] = 31; c. size = 31;
12.
int size[] = {31, 28, 30, 30, 31, 30, 31, 31, 30, 31, 30, 31}; the construct the first element in the array with 30 is a.
size[0] = 30; b. size[1] = 30; c.size = 30; d. [3] = 31; d.[1] = 30;
13.
Calculate the offset (relative to the base) of the third component in a structure if the first element occupies 2 bytes and second element occupies 1 bytes. a.
1 b. 2 c. 3 d. 4
14.
Calculate the offset (relative to the base) of the third element in an array if each component occupies 2 bytes. a.
1 b. 2 c. 3 d. 4
15.
String is a sequence of 0 or more characters
16.
all operands are type-checked at compile-time in Static typing
17.
all operands are type-checked at run-time in Dynamic type
18.
variables and expressions have fixed types in Static typing
19.
variables and expressions do not have fixed types in Dynamic type