CSE240 - quizzes final
Defining a virtual function in class means that the function
can be redefined in its child classes.
What keyword is used for defining an exception handler?
catch
What does the | (pipe) symbol in a BNF rule mean?
It is an or statement. 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[2][3] = { { 'c', 'a', 't'}, { 'd', 'o', 'g'} }; int i, j; for (i = 0; i<2 ; i++) { for (j = 0; j<3; j++) printf("%c", a[i][j]); } What will happen?
It prints: catdog
Which of the following coding practice can lead to a higher memory efficiency (use fewer padding bytes) in defining a structure?
Keep the character type variables together.
What computing paradigm can solve a problem by describing the requirements, without writing code in a step-wise fashion to solve the problem.
Logic
Which implementation of a function has potentially the best performance in terms of execution speed?
Macro
Can the identifier "base_variable" be created from the following BNF ruleset? <char> ::= a | b | c | ... | s | ... | x | y | z <identifier> ::= <char> | <char> <identifer>
No - there is an underscore in the identifier name that cannot be generated
Which of the following orthogonality describe this example: If a block allows one statement, it should allow zero or more statements within that same block.
Number Orthogonality
What mechanism defines tail recursion?
The calculated answer is propagated to each layer of the recursion through the parameters
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
How do we include a user-defined library, say myMathLib.h, into a program that uses this library?
#include "myMathLib.h"
Given the following code and considering things like ASCII values and pointer addresses: int num1 = 5; //addressed at 1767612 int num2 = 10; //addressed at 1767600 int num3 = 15; //addressed at 1767588 char ch1 = 'a'; //addressed at 3734375 char ch2 = 'b'; //addressed at 3734363 char ch3 = 'c'; //addressed at 3734351 char* chPtr = &ch3; int* iPtr = &num3; *iPtr = num3 * 8; *chPtr = *iPtr; What will the following statement output? cout << ch3;
'x'
Which of the followings is a valid Scheme form?
(* 9 (/ (- 4 2) 7))
Which of the following statements will allow me to give the value of 10 to the memory int* myPtr points to?
*myPtr = 10;
Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y; y = &x; *y = 100; y = y + 1;
100
Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y, **z; y = &x; z = &y; **z = 1000;
1000
Given a declaration: char a[] = "Hello World"; What is the size (in bytes) of the array a[]?
12
Given the following code and considering things like ASCII values and pointer addresses: int num1 = 5; //addressed at 1767612 int num2 = 10; //addressed at 1767600 int num3 = 15; //addressed at 1767588 char ch1 = 'a'; //addressed at 3734375 char ch2 = 'b'; //addressed at 3734363 char ch3 = 'c'; //addressed at 3734351 char* chPtr = &ch3; int* iPtr = &num3; *iPtr = num3 * 8; *chPtr = *iPtr; What will the following statement output? cout << iPtr;
1767588
Given the following: int num1 = 5; //addressed at 1877112 int num2 = 15; //addressed at 1877300 int num3 = 20; //addressed at 1877192 double d1 = 1.05; //addressed at 1374376 double d2 = 2.25; //addressed at 1374360 double d3 = 3.14; //addressed at 1374344 After these statements: int* ptr1 = &num3 ptr1 = ptr1 + 5; What will be the address contained in ptr1?
1877212
Assume this is a 32-bit environment, what is the size of x? struct Terminal { char name[30]; char location[32]; struct Terminal* next; } *x;
4 bytes
Given: char: 1 short: 2 int: 4 long: 4 float: 4 double: 8 How many bytes is this array: char* myStrings[10]
40
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? [x]. 2. What value will be printed for variable y? [y]
6 7
Assume this is a 32-bit environment, what is the size of x? (HINT: Don't forget about padding.) struct Terminal { char name[30]; char location[32]; struct Terminal* next; } x;
68 bytes
Given the following code, what is the expected value for z? #define func(x, y) (x > y) ? y++ : x++ int main() { int x = 10; int y = 9; int z = func(x, y); }
9
A pointer variable can take the address of a memory location as its value. Read the given program. #include <stdio.h> main() { int a = 20, b = 30, *p, *q, **r; p = &a; *p = 50; q = &b; *q = 70; r = &p; **r = 90; printf("%d\n", a); // 1st printf statement printf("%d\n", b); // 2nd printf statement a = 20; b = 80; printf("%d\n", **r); // 3rd printf statement } Answer the following three questions. 1.The output of the 1st printf statement is [first]. 2. The output of the 2nd printf statement is [second]. 3.The output of the 3rd printf statement is [third].
90 70 20
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
C/C++ has 2 pointer operators, which operator represents the name of the address? (Commonly refer as l-value.)
Asterisk (*)
When do we need to use fflush() or cin.ignore()?
Between a formatted input and an unformatted input
Implicit type conversion is commonly refer to as __________
Coercion
If your application is composed of multiple modules (programs), which of the following program processing would you recommend?
Compilation
During data declaration, a name is binded to a memory location, what else can be identify as part of this process?
Data Type Scope Qualifier
Given: Very Simple Programming Language (VSPL) <char> ::= a | b | c | ... | z | 0 | 1 | ... | 9 <operator> ::= + | - | * | / | % | < | > | == | >= | <= <variable> ::= <char> | <char> <variable> <expr> ::= <variable> <operator> <variable> | ( <expr> ) <operator> ( <expr> ) <assign> ::= <variable> = <expr>; <statements> ::= <assign> | <assign> <statements> The following is valid: (a+b) = (x*y);
False
Given: Very Simple Programming Language (VSPL) <char> ::= a | b | c | ... | z | 0 | 1 | ... | 9 <operator> ::= + | - | * | / | % | < | > | == | >= | <= <variable> ::= <char> | <char> <variable> <expr> ::= <variable> <operator> <variable> | ( <expr> ) <operator> ( <expr> ) <assign> ::= <variable> = <expr>; <statements> ::= <assign> | <assign> <statements> The following is valid: a = b + c + d;
False
The Golden Rule of Recursion is "All base cases must make progress towards a recursive case."
False
This quote: "When piloting a helicopter - changing speed can/will change your direction and possibly vice versa" Demonstrates that changing speed and/or direction is Orthogonal.
False
We use "Pass by Constant Reference" when:
Function does not want to modify the value, the value is expensive to copy and NULL is not valid
We use "Pass by Constant Pointer" when:
Function does not want to modify the value, the value is expensive to copy and NULL is valid
What features are supported in C++? Select all that apply.
Function overloading Operator overloading Virtual operator Virtual function
We use "Pass by Pointer" when:
Function wants to modify the value, the value is expensive to copy and NULL is valid
What computing paradigm enforces stateless (no variable allowed) programming?
Functional
What programming paradigm does Fortran belong to?
Imperative
If you like to see accurate debugging information, which of the following program processing would you recommend?
Interpretation
If your program was designed to print "Hello World" ten (10) times, but during execution, it printed eleven (11) times. What type of error is it?
Semantics error
A programming languages can be broken down into four structural layers. Identify these layers. (Check all that applies.)
Syntactic Contextual Semantics
Given this snippet of code in C, char alpha = 'a'; int numeric = alpha + 10; which of the following statement is correct:
Syntactically correct, but contextually incorrect.
Given the C declaration: char s1[4], s2[ ] = "hello"; if a string copy function strcpy(s1, s2) is executed, what will happen?
The result is s1 will contain the string "hell", and the follwing two byte loactions will contain 'o' an '\0'.
What is the key difference between a static variable and a global variable?
They have different visibility
When evaluating a programming language the category Reusability describes:
This concept asks how tied down a language is to a particular platform, can code be distributed easily and can libraries be made and shared
(True or False) Multiple pointers can reference the same objects.
True
Autocode and FORTRAN are considered to be the first high-level programming languages.
True
C++ was designed to bring Object Orientation to C. In fact it was originally released with the unimaginative name "C with Classes"
True
Given: Very Simple Programming Language (VSPL) <char> ::= a | b | c | ... | z | 0 | 1 | ... | 9 <operator> ::= + | - | * | / | % | < | > | == | >= | <= <variable> ::= <char> | <char> <variable> <expr> ::= <variable> <operator> <variable> | ( <expr> ) <operator> ( <expr> ) <assign> ::= <variable> = <expr>; <statements> ::= <assign> | <assign> <statements> The following is valid: a = x + y; b = s * t; c = w + v;
True
There was an early focus on efficiency due to early programmable computers being themselves fairly inefficent being limited in power and storage.
True
What is the best way of deleting a linked list of object in C++?
Use a loop to delete every object in the linked list.
In contrast to Web 1.0, what is the key function of Web 2.0?
Web is the computing platform
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.
A merge-short is typically implemented using
a function with two recursive calls.
Assume a function requires 20 lines of machine code and will be called 10 times in the main program. You can choose to implement it using a function definition or a macro definition. Compared with the function definition, macro definition will lead the compiler to generate, for the entire program, ______
a longer machine code but with shorter execution time.
A tail-recursive function is structurely equivalent to
a while loop
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';
In Scheme, the form (symbol-length? 'James) will return:
an error message
Consider an array, a linked list, and a binary search tree. Which data structure requires fewest comparisons in average to search an element stored in the data structure?
binary search tree
Which of the following types is a C++ type, but NOT a C type?
bool
The size (number of bytes) of a structure-type variable can be changed by the following factors. Select all that apply.
changing the orders of the members in the structure. changing the computer from a 32-bit to a 64-bit processor. adding a member into the structure.
Given the following definition and declarations: #define size1 10 const int size2 = 20; char a1[size1]; char a2[size2]; which line of code can cause a compilation error?
char a2[size2];
In C++, what function can be used to input a string with spaces?
cin.getline(...);
If the relation between two C++ classes can be best described as "has-a" relation, we should
contain one class in the other (containment).
Given this snippet of code, identify what is the size-m (sub-divided) problem? 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;
If a function calls another function, the local variables in these two functions use the memory from
different stack frames.
In addition to functional programming, what other ideas are originated by John McCarthy?
e-commerce space fountain
What is a feature of object-oriented computing?
encapsulation of states
One of the major differences between the imperative and functional programming languages is that the functional programming languages do NOT
have side-effect.
Assume that you want to delete the entire linked list pointed to by head. Which of the following deletion operation will cause the most garbage of memory?
head = null;
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 a Binary Search Tree class with the properties: Node* root; And the node defined as: struct Node { int data; Node* left; Node* right; } I have a method: Node* findIt(int item) And a helper method: Node* helpFindIt(Node* current, int item) These methods will either return a pointer to the node that item is found in, or NULL signifying it was not found. What would be the recursive case or cases of helpFindIt?
if(item > current->data) return helpFindIt(current->right, item) if(item < current->data) return helpFindIt(current->left, item)
What notation requires parentheses in order to correctly define the order of computation?
infix notation
The data stored in a binary search tree is sorted, if the tree is traversed in
inorder
The imperative programming paradigm is popular and is a part of object-oriented computing paradigm, because it ____ (Select all answers that apply).
is based on computer organization and thus is efficient to execute on hardware. matches the culture of doing things by following the step-wise instructions.
What opeartions will acquire memory from heap? Select all that apply.
malloc new
Where is the main() function located in a C or C++ program?
outside any class
The semantics of multiple inheritance becomes complex and error prone, if the base classes have
overlapped members.
Given the information below, which of the following snippet of codes will insert a new node in the second place in the linked-list. Assume the linked-list contains already at least one node. struct Terminal { char name[30]; char location[32]; struct Terminal* next; } *head, *p, *q;
p = (struct contact *) malloc(sizeof(struct contact)); ... p->next = head->next; head->next = p;
What functional feature does the code below best exhibit? (define start-engine (lambda () (error-detection (wheel-velocity (wheel-sensor)) (body-velocity))))
procedures are first class object.
During compilation, linker is used for ___________________.
resolving external references (bring in code from other libraries).
What parameters are required when performing file operations fread and fwrite?
source destination item size number of items
Assume that the search function of a linked list is specified by struct Terminal* search(); What values can the search function return? Select all correct answers.
the address of a terminal node 0
What is NOT the purpose (functionality) of the forward declaration (prototype)?
to allow functions to return different types of values
The exception handling mechanism discussed in this course is at the level of
user program.
What members of a base class can be redefined in the derived classes?
virtual members
Given this snippet of code, identify what is the size-n (whole) problem? void deleteList(struct contact* node) { if (node != NULL) { deleteList(node->next); free(node); } else return; }
void deleteList(struct contact* node)
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 applies.) char array[] = "Hello Joe"; char *x;
x = array; *(x + 6) = 'D'; --------------------- x = &array[0]; x = x + 6; *x = 'D';
Given the information below, which of the following snippet of codes will print every 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 are more than one 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; }
Given the information below, how will you access the name for a terminal node pointed to by x? struct Terminal { char name[30]; char location[32]; struct Terminal* next; } *x;
x->name;
Given the following class definition and the variable declaration: class employee char *name; long id; } class manager { employee empl; char* rank; } x Which of the following assignment statement is correct?
x.empl.id = 12345;