CSE 240 Midterm Exam Review
If a function calls another function, the local variables in these two functions use the memory from
different stack frames.
Given the following code, what is the expected value for z? Assume that you are using a compiler that doesn't do any special treatment of macro parameters. #include <stdio.h> #define func(x, y) (x > y) ? y : x int main() { int x = 10; int y = 9; int z = func(++x, y++); }
10
Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y; y = &x; y = y + 1; *y = 100;
10
Assume this is a 32-bit environment, what is the size of x? (hint: BE CAREFUL.) struct Terminal { char name[30]; char location[32]; struct Terminal* next; }; struct Terminal *x;
4 bytes
Given the following snippet of code, answer the following two questions based on the code: typedef enum {Sun, Mon, Tue, Wed, Thu, Fri, Sat} days;days x = Mon, y = Sat;while (x != y) { x++; }y++;printf("x = %d, y = %d", x, y); 1. What value will be printed for variable x? 2. What value will be printed for variable y? (Hint: enums are really just integers...)
6 7
Consider the following snippet of code in a 32-bit computer. (Hint: no padding is needed before email since it is made of chars, not word sized values like int or a pointer.) #define max 10 struct contact { char name[30]; char email[30]; int phone; }; struct contact A[max]; What is the size of the entire array A in bytes?
640
Consider the following snippet of code on a 32-bit computer: struct contact { char name[30]; int phone; char email[30]; } x; What is the size of variable x in bytes? (x is just a variable containing a struct contact)
68
A pointer variable can take the address of a memory location as its value. Read the given program. #include <stdio.h> main() { int i = 10, j = 20, *p, *q, **r; p = &i; *p = 40; q = &j; *q = 60; r = &p; **r = 80; printf("%d\n", j); //1st printf statement printf(%d\n", j);//2nd printf statement I = 10; j = 70; print(%d\n",**r);//3rd printf statement } Answer the following three questions. 1.The output of the 1st printf statement is 2. The output of the 2nd printf statement is 3.The output of the 3rd printf statement is
80 60 10
Which of the following statements is true?
A constant cannot be used as the actual parameter for pass-by-alias.
If you want to create a linked list of Container nodes, which can contain Publication node, Book node, Thesis node, and Report node, what type of pointer should be declared in the Container to point to all these nodes?
A pointer to Publication node
What type of values can a throw-statement throw (return)?
All of the above.
C/C++ has 2 pointer operators, which operator will return the address of a variable?
Ampersand (&)
What programming language characteristics impact the readability of the programs written in this language?
Control structures Data structures Syntax design
Converting an integer value 5 to a float number 5.0 takes
At least one important machine instruction to perform conversion between data binary formats.
Implicit type conversion is commonly refer to as __________ .
Coercion
If your application is composed of multiple modules (programs), which of the following program processing mechanism would you recommend? Be sure to consider performance.
Compilation
What are the key features of object orientation in programming languages? Select all that apply.
Dynamic memory allocation Encapsulation of state
If you want to change the insertion sort function with a size-(n-1) problem, as discussed in class, to a merge sort function, then where do you need to make changes?
In the code that constructs the solution of size-n problem from the size-m problem. In the definition of size-m problem.
What does the | (pipe) symbol in a BNF rule mean?
It is an "or" operation. Choose one of the options.
Given the following code char a[2][4] = { { 'c', 'a', 'r', 'b' }, { 'i', 'k', 'e', '\0' } }; char *p = &a[0][0]; while (*p != '\0') { printf("%c", *p); p++; } What will happen?
It prints: carbike
Given the following code char a[] = {'c', 'a', 't', '\0'}; char *p = a; while (*p != 0) { *p = *p + 1; printf("%c", *(p++)); } What will happen?
It prints: dbu
Given the following snippet of C++ code: string name = "Hello";ofstream myFile; myFile.open("data.txt");myFile << name;myFile.close(); What does it do?
It saves the word Hello into a file in the file system.
How is Java's garbage collection implemented?
It uses a reference counter to indicate if an object is still referenced by any variable.
Which of the following languages is most likely to report a compilation error for the following snippet of code? int i = 3; double n, j = 3.0; n = i + j;
Java
The complexity of searching a balanced binary search tree with n nodes is in the order of
O(lg n)
What is the time complexity of the insertion sort algorithm?
O(n*n)
Which of the following would be the most significant motivation for applying two-step translation in high level programming language?
One compiler for all machines
Which of the following statements are correct if a language is strongly typed. Select all that apply.
Type errors are always reported. Each name in a program has a single type associated with it.
Macros-Processing in C (or C++) takes place during which specific phase?
Pre-processing
What is the major improvement of structured programming languages over the earlier programming languages?
Removing Goto statement from the language.
Which of the following cannot be checked by an imperative or object-oriented compiler. (Select the most problematic one.)
Semantic
Given this snippet of code in C, char alpha = 'a';float numeric = alpha + 10; which of the following statement is correct:
Syntactically correct, but contextually incorrect.
What is the best way of deleting a linked list of objects in C++?
Use a loop to delete every object in the linked list
Consider a path from the root to a leaf of a class tree based on the inheritance. Which class has the most class members?
The class at the leaf of the tree.
The complexity of searching an arbitrary binary search tree with n nodes is the order of
The complexity of searching an arbitrary binary search tree with n nodes is the order of
Given the C declaration: char s1[4], s2[ ] = "hello"; if a string copy function strcpy(s1, s2) is executed, what will happen?
The result s1 will contain the string "hell", and the following two byte locations will contain 'o' and '\0'.
What is the key difference between a static variable and a global variable?
They have different visibility.
Which of the following problems require us to define more than one size-m problem in it's recursive formulation?
Towers of Hanoi Mergesort
What is the best way of deleting all the nodes in a binary tree pointed to by the root pointer?
Traverse the tree and delete each node individually.
In a given programming and execution environment, a variable declaration in C binds a name to a memory location. What else are also determined in declaration? Select all that apply.
Variable size (number of bytes) Variable type Variable scope
When will the buffer be created?
When the file operation fopen is performed.
When is padding required for a structure type variable?
When the structure contains a word-type variable, such as integer, float, and pointer, and the total number of bytes is not a multiple of four.
The search algorithm will be more efficient if a binary search tree is
a balanced binary tree.
The function searching a binary search tree can be easily implemented using
a recursive function with two recursive calls.
A tail-recursive function is structurally equivalent to
a while loop
Defining a virtual function in class means that the function
can be redefined in its child classes
Which of the following C assignment statements (assign a value to a variable at the semantic level) will NOT cause a compilation error? Assume the array has been declared as: char a[5];
a[0] = 'h';
When inserting a data into a binary search tree, the data should be inserted
at the position that keeps the entire tree a binary search tree.
If we want to store an array of structures into a disk file, what file type should we choose? (Hint: consider what would potentially require less space.)
binary file
Consider C++'s typing system. C++ uses (Select all correct answers)
both value semantics and reference semantics for its primitive types . both value semantics and reference semantics for its object types.
In C++, what function can be used for inputting a string containing spaces?
cin.getline(...);
What C/C++ constant variable can potentially be modified during the execution?
const x = 5;
If the relation between two C++ classes can be best described as "has-a" relation, we should
contain one class in the other (containment).
C++ allows a programmer to create an object through ___________ (Select all that apply)
declaration in compilation phase. the "new" operation during execution.
Given this snippet of code, identify the size-m problem (based on the four step approach discussed in class). void deleteList(struct contact* node) { if (node != NULL) { deleteList(node->next); free(node); } else return; }
deleteList(node->next);
What is the best way of deleting an array created by "p = new StructType[size];" in C++?
delete[] p;
What type casting mechanism should be used if you want to safely change (cast) a pointer type for pointing to a different object in an inheritance hierarchy?
dynamic_cast
What is a feature of object-oriented computing?
encapsulation of states
Using the principle of information hiding in OOP, in an array-implemented queue class, what members should be declared as "public" members?
enqueue and dequeue functions
In the hanoi towers function, what part of the code represents step 4: Construction of size-n problem from size-(n-1) problems?
hanoitowers(n-1, S, D, M); hanoitowers(1, S, M, D); hanoitowers(n-1, M, S, D);
Java programmers do not need to do garbage collection, because Java
uses a system program to collect garbage.
What memory must be garbage-collected by a destructor?
heap memory created in the constructors in the same class
We need to write a destructor for a class, if
heap memory is used in the constructor of the class.
Given this snippet of code, identify the stopping condition and the corresponding return (based on the four step approach discussed in class). void deleteList(struct contact* node) { if (node == NULL) return; else { deleteList(node->next); free(node); } }
if (node == NULL) return;
What computing paradigm can solve a problem by describing the requirements, without writing code in a step-wise fashion to solve the problem.
imperative
What programming paradigm does Fortran belong to?
imperative
The data stored in a binary search tree is sorted, if the tree is traversed in
inorder
The reason that we need to call fflush() or cin.ignore() is because the previous
input leaves a character in the buffer.
Which of the following C declarations (contextual level) will cause a compilation error?
int d[] = {12.24, 47.13};
What parameters are required when performing file operations fread and fwrite? Select all that apply.
item size number of items source destination
Given the snippet of code: int x = 5;int bar(int j) { int *k = 0, m = 5;k = &m;return (j+m);}void main(void) { static int i =0;i++;i = bar(i) + x; } Which variables obtain their memory from the stack? Select all that apply.
j k m
Which implementation of a function has potentially the best performance in terms of execution speed?
macro
The imperative programming paradigm is popular and is a part of object-oriented computing paradigm, because it ____ (Select all answers that apply).
matches the common culture of doing things by following the step-wise instructions. is based on computer organization and thus is efficient to execute on hardware.
How many different identifiers can the following BNF ruleset generate? <char> ::= a | b | c | ... | x | y | z <identifier> ::= <char> | <char> <identifier>
more than 26
Which C/C++ operations will acquire memory from heap? Select all that apply.
new malloc
The semantics of multiple inheritance becomes complex and error prone, if the base classes have
overlapped members.
Given this snippet of codes, identify the passing mechanism used for x (in func). void func(int *x, int y) { *x = *x + y; y = 2; }
pass-by-address
Which parameter passing mechanism does not have side-effects?
pass-by-value
The purpose of the scope resolution operator is to allow a function to be
placed outside the class.
What data types in C have the same data range, assuming that the computer used is a 32-bit computer?
pointer type and unsigned int
Given the declaration: char A[3] = {'C', 'a', 'r'}, *p = A; what statement prints character a?
printf("%c", *(++p));
In a pure object-oriented language, all data should be objects, and all objects should be accessed by
reference (pointer) only.
What type casting mechanism should be used with caution as it may truncate a larger object to a smaller one while casting an object of one class to an object of a different class?
reinterpret_cast
Assume pointer variable p points to node x of a linked list, and node x's next pointer points to node y. What does free(p) operation mean?
return the memory held by x to the free memory pool.
The forward declaration of a function requires: (Select all answers that apply)
return type. function name parameter types.
If a program contains an error that divides a number by zero at the execution time. This error is typically a
semantic error
A critical step in writing a recursive function is to assume that the
size-m problem has been solved by the underlying recursive mechanism, where m < n.
Assume a variable is declared in a block of code within a pair of curly braces. The scope of the variable ______
starts from its declaration point and extends to the end of the block.
A variable declaration can involve the following attributes: (Select all answers that apply)
type. scope.
The try-throw-catch exception handling mechanism is at the level of
user program.
How do you properly delete the first node of a linked list pointed to by head? Assuming that the linked list is not empty and temp is another pointer of the same type.
temp = head; head = head->next; free(temp);
A throw statement is associated with a specific exception handler through the
the data type implied by the throw value.
A data type is defined by (Select all answers that apply)
the values allowed. the operations allowed.
What is NOT the purpose (functionality) of the forward declaration (prototype)?
to allow a function to return different types of values
Given this snippet of code, identify the size-n problem (based on the four step approach discussed in class). void deleteList(struct contact* node) { if (node != NULL) { deleteList(node->next); free(node); } else return; }
void deleteList(struct contact* node)
When using an array of structures to store a collection, we typically have a variable storing the number of entries (called something like: tail, count, or size). In addition to the number of entries, what does this variable help to indicate? The tail variable in an array of structures is used for indicating (Select all that apply)
where to insert a new item, if the items in the array do not need to be sorted. where to insert a new item, if the items in the array do not need to be sorted.
Given this snippet of code, determine which of the following options will change the text in array to "Hello Doe" after execution. (Check all that apply.) char array[] = "Hello Joe"; char *x;
x = &array[0]; x = x + 6; *x = 'D'; x = array; *(x + 6) = 'D';
Given the information below, which of the following snippet of C code will print every airport terminal in the linked list without any side-effects of changing the state. Assume head is the only reference to the linked list and there is more than one airport terminal in the list. struct Terminal { char name[30]; char location[32]; struct Terminal* next; } *head, *x;
x = head; while(x != NULL) { printf("%s - %s", x->name, x->location);x = x->next;}
If A is the base class and B is a class derived from A, and x and y are pointers to objects of A and B, respectively, which assignment statement can pass the compiler check?
x = y;
Given the information below, how would you access the name for a terminal node pointed to by x? struct Terminal { char name[30]; char location[32]; struct Terminal* next; }; struct Terminal *x;
x->name;
Given the following class definition and the variable declaration: class employee { public:char *name;long id;}; class manager { public:employee empl;char* rank;} x; Which of the following assignment statement is correct?
x.empl.id = 12345;
Given this snippet of codes, what is the expected output? void func(int *x, int y)re { *x = *x + y; x = 10; } void main(){ int x = 10, y = 10; func(&x, y); printf("x: %d, y: %d", x, y); }
x: 20, y: 10