CSE 240 Midterm

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given this procedure, what is the return result? (define (guess value) (cond ((number? value) "I'm a number") ((char? value) "I'm a character") ((integer? value) "I'm a integer"))

"I'm a number"

How do we include a user-defined library, say myMathLib.h, into a program that uses this library?

#include "myMathLib.h"

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 *(times) 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))

Convert the following expression into prefix-p notation (Scheme statement): 10 + (5 - 3) + 2 / 4

(+ 10 (- 5 3) (/ 2 4))

Given the Scheme code, answer the following two questions. ((lambda (x) ((lambda (x y) (+ x y)) 5 (* 7 x))) 3) (1) The value to be passed to the parameter y is [y]. (2) The final output of the piece of code is [output].

(1) The value to be passed to the parameter y is Correct 21. (2) The final output of the piece of code is Correct 26.

Given the Scheme code below, answer the following questions related the Fantastic Four abstract approach. (define dtob(lambda(N) ; line 1 (if (= 0)(list0) ; line 2 (append (dtob (quotient N 2)) ; line 3 (list (remainder N 2)))))) ; line 4 (1) What line of code defines the stopping condition and the return value? Choose [Size1] (2) What line of code contains the size-M problem, where M < N? Choose [SizeM] (3) What lines of code define the step that construct the solution to size-N problem? Choose [SizeM_N

(1) What line of code defines the stopping condition and the return value? Choose Correct Line 2 (2) What line of code contains the size-M problem, where M < N? Choose Correct Line 3 (3) What lines of code define the step that construct the solution to size-N problem? Choose Correct Lines 3 & 4

What statements contain non-functional features of Scheme? Select all that apply.

(begin (write x) x) Correct (display x)

Which of the following is equivalent to the expression below? (car (cdr '(1 2 3 4 5))

(cadr '(1 2 3 4 5))

Compare the follow two Scheme forms: (append '(1 2) '(4 5)) and (cons '(1 2) '(4 5)).

(cons '(1 2) '(4 5)) returns '((1 2) 4 5).

Given a C declaration: char a[] = "Hello World"; What can be used as the initial address of array a[]? Select all correct answers.

*a[0] &a[0] (check with professor)

Which of the following statements will allow me to give the value of 10 to the memory int* myPtr points to?

*myPtr = 10;

Given the Scheme code, answer the following two questions. ((lambda (x) ((lambda (x y) (+ x y)) 5 (* 7 x))) 3) (1) The value to be passed to the parameter y is [y]. (2) The final output of the piece of code is [output].

1) The value to be passed to the parameter y is Correct 21. (2) The final output of the piece of code is Correct 26.

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].

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

Given the following code, what is the expected value for z? #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

Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y; y = &x; *y = 100;

100

Given a declaration: char a[] = "Hello World"; What is the size (in bytes) of the array a[]?

12 bytes

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

What is the return value of the code below? (define lst '(3 5 2 9)) (min (min (car lst) (cadr lst)) (min (caddr lst) (cadddr lst)))

2

Consider the following snippet of code in a 32-bit computer. struct contact { char name[30]; int phone; char email[30]; } x; What is the size of variable x in bytes?

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

68 bytes

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

? main class

What is "func" in this example? #include <stdio.h> #define func(x, y) (x > y) ? y : x int main() { int x = 10; int y = 9; int z = func(x, y); }

A macro

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

von Neumann Architecture is

A state based programming structure which loads and interprets instructions from memory into action

C/C++ has 2 pointer operators, which operator represents the name of the address? (Commonly refer as l-value.)

Asterisk (*)

In the following query language statement, which function acts as the filter function? var myQuery = from b in Books where b.price < 80 orderby b.title select b;

C. where

Explicit type conversion is commonly refer to as __________ .

Casting

If your application is composed of multiple modules (programs), which of the following program processing would you recommend?

Compilation

Given this procedure, what is the return result? (define (guess value) (cond ((number? value) "I'm a number") ((char? value) "I'm a character") ((integer? value) "I'm a integer"))) (guess 10)

Correct "I'm a number"

Given this procedure, what is the expected result? (map (lambda (x) (+ x x 2)) '(1 2 3 4 5 6))

Correct (4 6 8 10 12 14)

What statements contain non-functional features of Scheme? Select all that apply.

Correct (begin (write x) x) Correct (display x)

Which operators can be overloaded? Select all that apply

Correct + Correct ++ Correct >>

Given this procedure, what is the expected result? (map (lambda (x) (+ x x 2)) '(1 2 3 4 5 (6 7)))

Correct Error Message

A prolog program may contain: (Select all that apply.)

Correct facts. Correct rules. Correct questions.

Given these facts: is_dessert(cookie). is_dessert(ice_cream). is_dessert(pie). is_dessert(cheesecake). is_fruit(strawberry). is_fruit(apple). is_fruit(peach). contains(cookie, chocolate_chips). contains(ice cream, cookie). contains(ice cream, strawberry). contains(ice cream, peach). contains(pie, apple). contains(pie, peach). contains(pie, strawberry). contains(cheesecake, strawberry). Which of the following rule can help summarize all desserts that contains fruits:

Correct fruit_dessert(X) :- is_dessert(X), is_fruit(Y), contains(X, Y).

A goal succeeds, if there are facts (rules) that match or unify the goal. What are required in order for a goal clause and a fact to unify? Select all that apply.

Correct their corresponding arguments match. Correct their predicates are the same. Correct they have the same number of arguments.

Given the Scheme code as follows. What is the output? (define not-gate (lambda(x) (if (= x 0) 1 0))) (define onescomplement (lambda (a-list) (map not-gate a-list))) (onescomplement '(0 1 0 2 0 3))

CorrectA. (1 0 1 0 1 0)

A fact starts with a relationship followed by a list of arguments. The arguments of a fact

CorrectA. can have a structure similar to that of a fact.

What notation does Prolog use for expressing arithmetic operations?

CorrectB. infix notation

For functional programming languages, the scope of a local name

CorrectB. is in the body part of the declaration or definition.

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

CorrectB. j CorrectC. k CorrectD. m

If you want to change the insertion sort function with a size-(n-1) problem, as discussed in the lecture, to a merge sort function, where do you need to make changes?

CorrectC. In the definition of size-m problem CorrectD. In the code code that constructs the solution of size-n problem from the size-m problem.

Which predicate logic matches most closely with this statement? Bill listens to music and the news

CorrectC. listensto(bill, music); listensto(bill, news).

A higher order function is a function that takes the

CorrectC. operator of a function as an argument.

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

Data Structures, Syntax Design, Control Structures

What parameters are required when performing file operations fread and fwrite?

Destination Item Size Number of Items Source

Functional programming language is more friendly to parallel computing, because it supports

Eager Evaluation

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

Interpretation

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

Given this snippet of code, what is the value of z after executing the last statement? int x = 10, *y, **z; z = &y; y = &x; *y = 100;

None of the above (Memory Address)

Which of the following orthogonality describe this example: If a block allows one statement, it should allow zero or more statments within that same block.

Number Orthogonality

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

O(n)

What is the main reason of applying two-step translation of high level programming language?

One compiler for all machines

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

Removing Go to statement from the language.

Given an expression: x1 + x2 + x3 + x4 Which language allows to evaluate the expression in this order: (1) x1 plus x2; (2) x3 plus x4; (3) sum of ( x1 + x2 ) plus sum of ( x3 + x4 );

Scheme

If a program contains an error that divides a number by zero at the execution time. This error is 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

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.

What statement is accurate and correct?

There is only one Scheme list that is not a pair.

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

What is the best of deleting all the nodes in a binary tree pointed to by the root pointer?

Traverse the tree and delete each node individually.

(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

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

How does Scheme implement the function such as: for (i = 1; i< 100, i++) {sum = sum + i;}

Use recursion

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.

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.

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 to keep the entire tree as a binary search tree.

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

The statement "a function is a first-class object" means that a function

can be placed in a place where a value is expected.

A fact starts with a relationship followed by a list of arguments. The arguments of a fact

can have a structure similar to that of a fact.

What keyword is used for defining an exception handler?

catch

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

catdog

The size (number of bytes) of a structure-type variable can be changed by the following factors. Select all that apply.

changing the computer from a 32-bit to a 64-bit processor. changing the orders of the members in the structure. 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 A is the base class and B is a class derived from A, then

class B has all the members of class A.

In Scheme, the form (symbol-length? 'James) will return:

error message

A prolog program may contain: (Select all that apply.)

facts. rules. questions.

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

In the hanoi towers function, what part of the code represent step 4: Constructiopn 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);

One of the major differences between the imperative and functional programming languages is that the functional programming languages do NOT

have side-effect.

What memory must be garbage-collected by a destructor?

heap memory created in the constructors in the same class

What notation requires parentheses in order to correctly define the order of computation?

infix notation

The reason that we need to call fflush() or cin.ignore() is because the previous

input leaves a character in the file buffer.

Which of the following statements will assign the address of the variable int myInt to the pointer int* myPtr?

int* myPtr = &myInt;

For functional programming languages, the scope of a local name

is in the body part of the declaration or definition.

What data structure is used in Scheme for representing extremely large integers?

list

Which predicate logic matches most closely with this statement? "Bill listens to music or news."

listensto(bill, music); listensto(bill, news).

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

Assume pointer variable p points to node x, and node x's next pointer points to node y. What does free(p) opeartion mean?

return the memory held by x to the free memory pool.

In the C-Style input function scanf("%d", &i); What does the character "&" mean?

scanf takes the address of an variable as its parameter.

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.

In addition to functional programming, what other ideas are originated by John McCarthy?

space fountain e-commerce

Assume a varible 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.

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

static_cast

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.

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 goal succeeds, if there are facts (rules) that match or unify the goal. What are required in order for a goal clause and a fact to unify? Select all that apply.

they have the same number of arguments. their predicates are the same. their corresponding arguments match.

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: myvar = (x + y) * (a - c);

true

The Scheme form (char? #\5) will return

true (#t)

A data type defines the

values and operations allowed

Functional programming languages do NOT allow to define

variables whose value can be modified.

Which commands (constructs) do NOT have a loop when expressed in syntax graphs? Select all that apply

while (condition) do {statements;} if-then-else for ( <init-expr>; <test-expr>; <increment-expr> ) {<statements>}

The scope of a Prolog variable is

within a single fact, rule, or query.

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]

x = 6, y = 7

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: How to initialize id class employee char *name; long id; } class manager { employee empl; char* rank; } x

x.empl.id = 12345;

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

y, a

With over 500 programming languages in the world, the best way approach to learning languages is to focus on memorizing syntax and structure. Then learn languages with simimlar syntaxes and structures.

False

We use "Pass by Value" when:

Function does not want to modify the parameter and the value is easy to copy

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

Assume that Publication is the root class of an inheritance tree. You want to form a linked list of different publications in the inheritance tree, including Book, Report, Newspaper, etc. What is the best way to create a linked list using PublListNode and Publication classes?

The PublListNode class contains the Publication class.

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 teminal node 0

What programming paradigm does Fortran belong to?

Imperative

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.

Event-driven computing paradigm is to

define a set of events and write an event handler for each event.

Given this snippet of code, identify what is the stopping condition and return value? void deleteList(struct contact* node) { if (node == NULL) return; else { deleteList(node->next); free(node); } }

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.


Ensembles d'études connexes

Which OSI layer contains/does this?

View Set

Tostevin World since 1500 S1 Final: Terms

View Set

Personal Finance: Practice midterm exam (Ch. 7,8,&9)

View Set

Soil and Fertilizer Chapter 2 review

View Set