CSE 240 Midterm

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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 and is based on computer organization and thus is efficient to execute on hardware.

Which of the following problems require us to define more than one size-m problem in it's recursive formulation?

Mergesort and Towers of Hanoi

Can the identifier (a string) "base_variable" be created using the following BNF rules? <char> ::= a | b | c | ... | s | ... | x | y | z <identifier> ::= <char> | <char> <identifier>

No - there is an underscore in the identifier name that cannot be generated.

If a function calls another function, the local variables in these two functions use the memory from

different stack frames.

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? 6 . 2. What value will be printed for variable y? 7 (Hint: enums are really just integers...)

1. 6 2. 7

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 = 30; printf("%d\n, **r); //3rd printf statement }

1. The output of the 1st printf statement is 90 2. The output of the 2nd printf statement is 70 3.The output of the 3rd printf statement is 20

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, given a declaration: int a[10]; What is the size (in bytes) of the entire array?

40

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

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; } struct Terminal x;

68 bytes

Which of the following statements is true? A constant cannot be used as the actual parameter for pass-by-alias. A constant cannot be used as the actual parameter for pass-by-address. A structure type of variable cannot be as the actual parameter for pass-by-value. The return value of a function cannot be a pointer. A global variable cannot be used as the actual parameter for pass-by-alias.

A constant cannot be used as the actual parameter for pass-by-alias.

Type checking happens during compilation phase. It reinforces which of the following structural layers?

Contextual

C/C++ has 2 pointer operators, which operator will return the address of a variable?

Ampersand (&)

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.

Explicit type conversion is commonly refer to as __________ .

Casting

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, adding a member into the structure, and changing the computer from a 32-bit to a 64-bit processor.

What programming language characteristics impact the readability of the programs written in this language?

Data Structures, Syntax Design, and Control Structures

What parameters are required when performing file operations fread and fwrite? Select all that apply.

Destination, source, number of items, and item size

The reason that we use a buffer between the disk and the memory is _______

Disk access speed is low, but large blocks of data can be read from and written to the disk.

Which of the following statements are correct if a language is strongly typed. Select all that apply.

Each name in a program has a single type associated with it and type errors are always reported.

What are the key features of object orientation in programming languages? Select all that apply.

Encapsulation of state and dynamic memory allocation

What computing paradigm can solve a problem by describing the requirements, without writing code in a step-wise fashion to solve the problem?

Logic

What computing paradigm enforces stateless (no side-effects) programming?

Functional

What programming paradigm does Fortran belong to?

Imperative

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 and in the definition of size-m problem.

If you like to see accurate debugging information, which of the following program processing processing mechanism would you recommend?

Interpretation

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 *p = "hello", *s = "this is a string"; p = s; printf("%s\n", p); What will happen?

It prints: this is a string

Given the following snippet of C++ code: string name = "Hello"; ofstream myFile; myFile.open(myFile); myFile << name; myFile.close(); What does it do?

It saves the word Hello into a file in the file system.

The complexity of searching an arbitrary binary search tree with n nodes is the order of

O(n)

What is the time complexity of the insertion sort algorithm?

O(n*n)

Where is the main() function located in a C++ program?

Outside any class

Macros-Processing in C (or C++) takes place during which specific phase?

Pre-processing

What type of values can a throw-statement throw (return)?

Primitive type, string type, and object types.

In the memory hierarchy of a computer system, which type of memory is the fastest one?

Registers

What is the major improvement of structured programming languages over the earlier programming languages?

Removing Goto statement from the language.

If a program contains an error that divides a number by zero at the execution time. This error is typically a

Semantic error

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

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.

The try-throw-catch exception handling mechanism is at the level of

user program.

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 and 0.

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.

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.

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.

Defining a virtual function in class means that the function

can be redefined in its child classes.

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 scope, variable type

When will the buffer be created?

When the file operation fopen is performed.

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 and how far the search() function should go in searching the items of the array.

Given this snippet of codes, what is the expected output? #include <stdio.h> char *getString(char *str) { return str; } void main() { char *p, q[] = "Hello", *s = "World"; p = getString(s); printf("%s %s\n", p, q); }

World Hello

The search algorithm will be more efficient if a binary search tree is

a balanced binary tree.

A merge-sort is typically implemented using

a function with two recursive calls.

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

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.

Consider an array, a linked list, and a binary search tree. Which data structure requires fewest comparisons on average to search an element stored in the data structure?

binary search tree

Consider C++'s typing system. C++ uses (Select all correct answers): value semantics for its primitive types (such as integer and float) only. both value semantics and reference semantics for its primitive types . reference semantics for its object types only. both value semantics and reference semantics for its object types.

both value semantics and reference semantics for its primitive types . both value semantics and reference semantics for its object types.

Given the following definition and declarations: #define size1 10 const int size2 = 20; char a1[size1]; char a2[size2]; which line is most likely to cause a compilation error?

char a2[size2];

C++ allows a programmer to create an object through ___________ (Select all that apply)

declaration in compilation phase and 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;

If the relation between two C++ classes can be best described as "is-a" relation, we should

derive one class from the other (inheritance).

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 C, what function can be used for inputting a string containing spaces?

fgets();

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);

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;

A piece of memory must be explicitly garbage-collected, if it comes from

heap

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.

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.

A data type defines the

values and operations allowed

Which of the following C declarations (contextual level) will cause a compilation error?

int d[] = {12.24, 47.13};

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.

k, m, and j

Which C/C++ operations will acquire memory from heap? Select all that apply.

malloc and new

It does not cause memory (storage) overhead to create multiple

names (alias) for a variable.

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. Note that name and location won't be set up - the code is intended to only add a new node. Assume that the head variable will store the first node in the linked list and that it already contains 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;

The forward declaration of a function requires: (Select all answers that apply)

parameter types, return type, and function name

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

Given the snippet of codes, identify the passing mechanism used for y (in func). void func(int *x, int &y){ *x = *x + y; y = 2; }

pass-by-alias

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));

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

During compilation, linker is used for ___________________.

resolving external references (bring in code from other libraries).

Consider the following snippet of code in a 32-bit computer #define MAX 10 struct contact { char name[30]; char email[30]; int phone; ); struct contact A[MAX]; int tail = 0; Which statement can read a phone number into the phone field of the structure?

scanf("%d", &contactbook[tail].phone);

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.

You do not need to garbage-collect the memory, if it comes from (Select all that apply)

stack memory, global memory, and static memory

What type casting mechanism should be used if you want to cast an integer value to a double value?

static_cast

A throw statement is associated with a specific exception handler through the

the data type implied by the throw value.

If you declare a pointer to the object of the parent class and use the pointer to access the object of a child class, you can access

the members that exist in the parent class.

A variable declaration can involve the following attributes: (Select all answers that apply)

type and scope

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)

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; *(x + 6) = 'D'; and x = &array[0]; x = x + 6; *x = 'D';

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;

For the following BNF ruleset, which are terminal symbols? Select all that apply. <char> ::= a | b | c | ... | x | y | z <identifier> ::= <char> | <char> <identifier>

y and a


Set pelajaran terkait

Econ 102 Miyoung Oh Midterm 2 Practice Test 3

View Set

Exponent Rules, Multiplying and Dividing Exponents, Dividing Exponents with the Same Base

View Set

экономику сдадим на 5

View Set

Lecture 5: Warrants & Convertible bonds

View Set

Chapter 6 Performance Management

View Set

Advanced Accounting - Chapter 4: Consolidated Financial Statements and Outside Ownership

View Set