CS Exam 1 Review
What are the atomic types?
- short, int, long, and unsigned variants - float, double, and long double - char - bool
What is 42₁₀ in binary?
00101010₂
What size is assigned for char or bool?
1 byte (8 bits)
What is binary notation?
128-64-32-16-8-4-2-1
What size is assigned for long double
16 bytes (128 bits)
What is the ouput of this code fragment? int a = 1; int &refA = a; int* ptrA = &a; *ptrA += 1; cout << a << " "; cout << refA << " "; cout << *ptrA << endl;
2 2 2
What is the ouput of this code fragment? int a = 1; int &refA = a; int* ptrA = &a; refA += 1; cout << a << " "; cout << refA << " "; cout << *ptrA << endl;
2 2 2
What is the output of this code fragment? int a = 1; int &refA = a; int* ptrA = &a; a += 1; cout << a << " "; cout << refA << " "; cout << *ptrA << endl;
2 2 2
What size is assigned for short?
2 bytes (16 bits)
What size is assigned for int or float
4 bytes (32 bits)
What size is assigned for long or double
8 bytes (64 bits)
What is the fundamental unit of memory inside a computer called?
A bit
What is the unit that represents the most common integer size on a particular hardwire called?
A word
What will the running time for N=64,000 be? A. <4 sec. B. Between 5 and 10 sec. C. Between 10 and 20 sec. D. More than 20 sec.
A. <4 sec.
What size do arrays have?
Arrays take up the element size times the number of elements
Suppose that there are about 8,000,000 phone numbers in a single area code How many numbers need to be inspected to search for a number inside an area code? A. 10 B. 23 C. 54 D. 127
B. 23
How many moves does it take to solve a two disk problem? A. 2 B. 3 C. 5 D. 8
B. 3
bool search(int items[], int size, int value) { for(int i=0; i,<size; i++) if (items[i] == value) return true; return false; } What is the running time of the search function if items has N values? A. O(1) B. O(N) C. O(N lg N) D. O(N²)
B. O(N)
void foo(int items[], int size) { int sum = 0; for(int i=1; i,<size; i*=2) sum += items[i]; } What is the running time of the function foo if size is N? A. O(1) B. O(lg N) C. O(N) D. O(N²)
B. O(lg N)
How do pointers and references increase efficiency?
By eliminating the need to copy an argument
How can you allocate a single value (as opposed to an array)?
By writing new followed by the type name.
Suppose that there are about N phone numbers in a single area code How many numbers need to be inspected to search for a number inside an area code? A. 5 B. 12 C. 22 D. 23
C. 22
How many moves does it take to solve a three disk problem? A. 3 B. 5 C. 7 D. 10
C. 7
What will the running time for N=128,000 be? A. <8 sec. B. Between 8 and 15 sec. C. Between 16 and 25 sec. D. More than 25 sec.
C. Between 16 and 25 sec.
How many levels "deep" is the recursion to generate $N? A. 10 B. N/10 C. log10(N) D. N
C. log(N)
How many moves does it take to solve a k disk problem? A. 2k+1 B. k! C. k² D. 2ᵏ-1
D. 2ᵏ-1
What will the running time for N=8M (~64*128,000) be? A. About an hour B. About 4 hours C. About midnight D. It will finish tomorrow E. Saturday
D. It will finish tomorrow (~20 hours)
bool arraySearch(int items1[], int items2[], int size1, int size2) { for(int i=0; i<size1; i++) for(int j=0; j<size2; j++) if (items2[j] == items1[i]) return true; return false; } What is the running time of the arraySearch function if items1 and items2 both have N values? A. O(1) B. O(N) C. O(N lg N) D. O(N²)
D. O(N²)
How do you allocate a Point object?
Declare it as a local variable on the stack, Point pt(3, 4);
True or False: A C-String is an instance of the string class A) True B) False
False
What are C++'s Rule of 3s
If a class needs to define any of these 3 methods, then likely the class should define all 3: 1. Destructor 2. (Deep) Copy Constructor 3. (Deep Copy) Assignment Operator And maybe the rule of 5 4. Move Constructor 5. Move Assignment Operator
What are new objects in a particular class created as?
Instances
When you invoke a function the following occur. 3. C++ then evaluates the statements in the function body, using the new stack frame to look up the values of local variables. 4. When C++ encounters a return statement, it does what?
It computes the return value and substitutes that value in place of the call.
What does the delete operator do?
It frees memory previously allocated → for arrays you need to include empty brackets. delete ip; delete[] array;
What states can a bit be denoted in?
It has 2 states, it must be one of the two, either a 1 or a 0
Does the prototype for a destructor have a return type?
It has no return type, no arguments, and is the name of the class preceded by a tilde (~)
What is the old C style of strings?
It is simply a pointer to a character, which is the first element of a character array terminated by the null character ('\0')
What is Big-O Notation
It represents the running time as a function of the size of the input (usually denoted by N)
What does a destructor do?
It specifies how to free the storage in a class definition
How does the class definition specify the representation of the objects?
It specifies the representation of the object by naming its fields and the behavior of the object by providing a set of methods.
What is the object model based on?
It's based on the idea of a class, which is a template describing all objects of a particular type
To call a method given a pointer to an object, what do you need to write? A) pp.getX() B) *pp.getX() C) pp->getX()
Not A) because pp is not a structure Not B) because . takes precedence over * Thus, it's C
for (int k = 0; k < 1000; k++) z++; What is the running time?
O(1)
int z = 0; for (int i = 0; i < n; i++) z++; for (int j = 0; j < n; j++) z++; What is the running time?
O(2*N) = O(N)
int max(int items[], int size) { int largest = items[0]; for(int i=0; i<size; i++) if (items[i] > largest) largest = items[i]; return largest; } What is the running time of the max function if items has N values? 2N + 2?
O(N)
for (int i = 0; i < n*n; i++) z++; What is the running time?
O(N²) Same as nested for loop
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) z++; for (int k = 0; k < n; k++) z++; What is the running time?
O(N²+N) = O(N²)
for (int i = 0; i*i < n; i++) z++; What is the running time?
O(√N)
What is the name of base 8 notation?
Octal
How do you allocate a Point object on the heap?
Point* pp = new Point(5, 12);
How would you declare a variable pptr as a pointer to a Point structure?
Point* pptr;
What size do pointers take up?
Pointers take up the space needed to hold an address, which is 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine
What type of string is the highlighted text? char s[] = "A string"; string x = "Another string"; ifstream infile("foo.txt"); cout << x << endl; A) c-string B) string object
String object
What size do struct types have?
Structure types have a size equal to the num of their fields.
What is a constructor that takes in no arguments called?
The default constructor
What is the code for the library itself called?
The implementation
How do you copy an object?
The process of copying an object is controlled by two standard methods, each of which has a specific prototype. The assignment operator has the following form: const type & type::operator=(const type & rhs)
What space are enumerated types typically assigned?
The space of an int
What are object-oriented languages characterized by?
They are characterized by representing most data structures as objects that encapsulate representation and behavior in a single entity
What are C Strings?
They are pointers to characters
What is the purpose of contructors?
They initialize an object
What region of memory is reserved for variables that persist throughout the lifetime of the program, such as constants?
This information is called static data
The default behavior of C++ is to copy only the top-level fields in an object, which means that all dynamically memory is shared between the original and the copy. What is this default behavior called?
This is called shallow copying, which violates the semantics of the collection
Const - Call by Reference Adding const to the declaration of a reference parameter signifies that the function will not change the value of that parameter. What does this guarantee allow the compiler to do?
To share the contents of a data structure without allowing methods to change it. void someFun(const MyClass & x);
How is memory allocated on the heap?
Using the new operator
For heap objects (using new) do you need to explicitly free that object?
Yes, by calling delete, which also calls the destructor
When you create memory diagrams, how do you know the actual memory addresses at which values are stored?
You don't, just make something up
It is often convenient to use the same names for parameters and instance variables. If you do, what must you do?
You must use the keyword this (defined as a pointer to the current object) to refer to the instance variable, as in the constructor for the Point class: Point::point(int x, int y){ this->x = x; this->y= y; }
What do you need to implement instead of shallow copying?
You need to implement deep copying, which copies the data in the dynamically allocated memory as well
What is the sequence of eight consecutive bits called?
a byte
What is code that uses a library called?
a client
Conceptually, how does it often make more sense to represent a pointer?
as an arrow
When you invoke a function, the following actions occur: 1. C++ evaluates the argument expressions in the context of the calling function. 2. C++ then copies each argument value into the corresponding parameter variable, which is allocated in a newly assigned region of memory called a _____ _____.
called a stack frame. This assignment follows the order in which the arguments appear: the first, argument is copied into the first parameter variable, and so on.
Classes that use the const specification for all appropraite parameters and methods are said to be what?
const-correct
In Java, objects are created on the heap and are automatically reclaimed by a what, when those objects are no longer accessible
garbage collector
What is the name of base 16 notation?
hexadecimal
How would you allocate an array of 100 integers?
int* array = new int[100]
How would you allocate space for an int on the heap?
int* ip = new int;
what does the interface do?
it serves as both a barrier and a communication channel
How many bytes are in a word?
it varies per machine
Suppose that there are about 4,000,000 phone numbers in a single area code How many numbers need to be inspected to search for a number inside an area code?
log2(n) = lg(n)
What do computer scientists call programs that fail to free the heap memory dynamically allocated?
memory leaks
How can you allocate an array of values?
new type[size]
What are the names of addresses which memory can be identified with?
pointers
When you invoking a function the following occur. 5. C++ then discards the stack from for the called function and what?
returns to the caller, continuing from where it left off
Each time you call a method, C++ allocates a new block of memory called a ______ to hold its local variables
stack frame
Constant Methods What does adding cosnt after the parameter list of a method guarantee?
that the method will not change the object void someFun() const;
How are enumerated types defined?
the enum keyword
It is also possible to allocate memory dynamically. This space comes from a pool of memory called what?
the heap
What is the name of the point at which the client and the implementation meet called?
the interface
What does the expression sizeof(t) return?
the size of type t
What region of memory do the stack frames come from?
the stack
What happens if you assign one pointer value to another?
the two pointers end up indicating the same data value
In classical architectures, the stack and heap grow in which direction?
they grow toward each other to maximize the available space
How do you declare a pointer variable?
type* var; ex double* px;
What does the prototype for the copy constructor look like?
type::type(const type & rhs)
How are structure types defined?
using the struct keyword
What qualities do good programs have?
→ Correct / Complete → Robust → Maintainable → Efficient
How do we measure efficiency?
→Empirical/Experimental - Use clock to get running times → Loop Trials - For loop - Nested for loop