Download In-Class Exam Study Guide

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSCI 3310 Test 4 Study Guide Fall 2014
Chapter 6:
6: Data Types
1. Describe the two main options for string length. What do the descriptors look like for each?
Static length: Length is determined when the string is created as in the Java String type. The descriptor
consists of the name of the type (static), the length (in characters, not bits or bytes), and the address of the
first character of the string. This can be determined at compile time.
Limited dynamic length: Length can have a varying length up to a fixed maximum as in C. The descriptor
consists of the name of the type (limited dynamic), the maximum length (in characters, not bits or bytes),
the current length, and the address of the first character of the string. This can’t be determined until run
time.
(There is also a third type, dynamic length, which is unlimited. But I won’t ask that.)
2. a) What is an enumerated data type? b) How would you define the days of the week as an enumerated type
in Java? c) How do you declare a variable of type Day in Java? d) How do assign the value thurs to myDay?
e) What is the advantage of using enumerated data types?
Answers:
a) Like a “user-defined” data type. One in which all of the possible values are provided (enumerated or
listed) in the type definition.
b) enum Day {mon, tues, wed, thurs, fri, sat, sun};
c) Day myDay;
d) myDay=thurs;
e) The alternative is to let 0 represent mon, 1 represent tues, etc. But that is an unnatural abstraction. With
enumerated types, you can just let tues be represented by tues instead of a code of 1.
3. What is a heap?
An area of memory available to a running program for dynamically allocating and deallocating memory.
4. How is subscript binding for arrays done in Java?
Fixed heap-dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when
requested and storage is allocated from the heap).
5. How does Java implement single-dimension arrays?
When an array is declared, such as
int list[]=new int[5];
Java saves the address of list[0] as the value of list plus it saves the length of the array (in this case, 5).
6. What is the access function for the kth element of a single-dimension array in Java for an array called list?
Give an example.
Answer:
address(list[k]) = address (list[0])+ (k* element_size)
So if list is an arrays of integers (4-byte element size), and list[0] is in location 1000, then the address of
list[2] is computed as follows:
address(list[k]) = address (list[0])+ (k* element_size)=address(list[2]) = 1000+ (2* 4)=1008
7. What is an associative array? What is the advantage of using associative arrays?
An unordered collection of data elements that are indexed by an equal number of values called keys.
Requires less abstraction. Regular arrays: $hi_temps [0] is the high temp on Mon, $hi_temps [1] is the high
temp on Tues. With associative arrays you’d use $hi_temps[‘Mon’], $hi_temps[‘Mon’], etc. (PHP lets you
do both).
8. Give an example of an associative array definition in PHP and also an example of how to assign a value to
an element in an associative array.
$hi_temps = array(‘Mon’ => 77, ‘Tue’ => 79, ‘Wed’ => 65, …);
$hi_temps[‘Wed’] = 83;
9. What is a record? What is the advantage of this type?
A typically heterogeneous (of different data types) collection of data elements in which the individual
elements are identified by names. An array is typically a collection of data of the same type. But information
about a particular entity (person or thing) in the real world is of different types. For example, in an
employee file, a particular employee might be described by a first name (a string), a middle initial (a string
of length 1), a last name (a string), an employment date (date type), an hourly pay rate (a real number), and
number of hours worked (probably rounded off to an integer). A record can handle a collection of data of
different types. (BTW the parts of a file are called records, but files are stored on disk. The word “record” is
overloaded because in the context of this book, it refers to collections of data stored in RAM. That’s why
C++ uses struct for structure).
10. Give an example from C++.
struct movies
{
string title;
int year;
};
movies amovie;
amovie.title=”The Descendants”;
amovie.year=2011;
11. What is a pointer type?
A variable with pointer type has a value that is a memory address or nil (called NULL in C++).
12. What is type checking? How does Java handle this?
The activity of ensuring that the operands of an operator are of compatible types (either legal or coerced to
the correct type). Java does coercion if it makes sense. For example, when adding an int and a double, it
coerces the int to a double.
13. What is a strongly typed language? Is Java strongly typed?
A language that always detects type errors, both at compile time and run time. It is nearly strongly typed
because it allows the cast operator, as in
double average;
int sum,n;
average=(double)sum/n;
Chapter 7: Expressions and Assignment Statements
1. What are associativity rules? Give an example in Java.
Rules that determine which operator is evaluated first when an expression contains two adjacent occurrences
of operators with the same level of precedence (“grouping”).
In Java multiplication, division, addition, and subtraction are left associative, so a-b-c is computed as
(a-b) – c, not a - (b-c).
2. What is an overloaded operator? Give two examples in Java.
Multiple meanings of an operator. The + sign is used for both int and double arithmetic, yet adding integers
is totally different from adding reals, so if a=5 and b=7, then System.out.println (a+b) is 12. Also the + sign
is used for string concatenation, as in
System.out.println(“a”+”b”) is ab
3. What are narrowing type conversions? Widening type conversions? Which one presents problems and why?
Narrowing-converts a value to a type that can’t store even approximations of the original value, such as
converting a double to an int because not every real number is an integer. So the fractional part would
chopped off. Java doesn’t allow this because this is a problem; C++ does. Widening-converts a value to a
type that can store at least approximations of the original value, such as int to double.
4. How is explicit type conversion done in Java?
If x is an int and x=5, then System.out.println(“x=”+x) is x=5. In other words, the value of the integer x is
converted to the string 5, and then concatenated to x=.
5. What is a short-circuit evaluation? Give an example from Java.
When the result is determined without evaluating all of the operands and/or operators.
if (x>=0 && y<10) …
When x is less than 0, the first condition is false, so Java doesn’t evaluate y<10 because false “anded” with
anything is false.
Similarly,
if(x>=0 || y<10) …
When the first condition is true, so Java doesn’t evaluate y<10 because true “or’ed” with anything is true.