Cse240 Final Notes

Ace your homework & exams now with Quizwiz!

(LISP) Which of the following lines (in Java, C, or C++) is equivalent to this expression in LISP: (- (* 1 2) (+ 3 4) (/ 5 6) 7)

(1 * 2) - (3 + 4) - (5 / 6) - 7

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

- global memory - stack memory - static memory

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

- new - malloc

(C Programming) What is the output of the below code? #include <stdio.h> int main() { int a[5]={3,1,5,20,25}; int i,j,m; i=*(a+1) - 1; j=a[1] + 1; m=a[j] + a[i]; printf("%d,%d,%d",i,j,m); return 0; }

0, 2, 8

(C Programming) What is the output of the following program? #include <stdio.h> typedef enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun} WEEK; int main() { WEEK day; day = Wed; printf("%d",day); return 0; }

2

(C++) How many times will the message "good bye!" be printed on the screen? #include<iostream> using namespace std; class CSE { public: CSE(int v) { cout<<"constructor\n"; } void add(int v) { cout<<"adding\n"; } int remove(){ cout<<"removing\n"; return 0; } ~CSE() { cout<<"good bye!\n"; } }; int main(){ CSE q1(5); CSE *q2 = new CSE(5); q1.add(2); q1.add(8); q1.remove(); q2->remove(); delete q2; return 0; }

2

(C++) In the following code, how many times does the destructor of the class "Base" execute? #include<iostream> using namespace std; class Base { public: Base(int n) { cout<<"Base Constructor\n"; } void function() { cout<<"function\n"; } ~Base() { cout<<"Base destructor\n"; } }; class Derived : public Base { public: Derived(int n) : Base(n) { cout<<"Derived Constructor\n"; } ~Derived() { cout<<"Derived destructor\n"; } }; int main(){ Derived myPQ1(50); myPQ1.function(); Derived *myPQ2 = new Derived(50); myPQ2->function(); delete myPQ2; return 0; }

2

(C Programming) What does the below program print on the screen? #include <stdio.h> int main() { int i=3, *j, k; j=&i; printf("%d\n",i* *j *i-*j); return 0; }

24

(LISP) What is printed by the print instruction in the second line? (setf theList '(2 3 7 9 10) ) (print (first ( rest theList ) ) )

3

(PROLOG) Given the following fact and rule: fun(1, 2). fun(N, F) :- N>0, N1 is N - 1, fun(N1, F1), F is N * F1. What is the value of the variable X after running the following query? ?- fun(4, X).

48

(LISP) What is the value returned for the following LISP expression: (+ 1 (if (< 2 1) (* 3 3) 6))

7

(C Programming) What is the output of the code below: #include <stdio.h> int fun (int n) { if (n == 4) return n; else return 2*fun(n+1); } int main() { printf("%d", fun(3)); return 0; }

8

(LISP) What is the last value printed on the screen after running the following LISP code? (setf num 1) (dotimes (x 3) (setf num (+ num num) ) ) (print num)

8

(C Programming) Which of the following are correct declarations of the main() method? a) int main() { return 0; } b) int main(int argc, char *argv [ ]){ return 0; } c) public static void main (string [ ] argv){ } d) void main() { return 0; } e) main(){ return 0; } f) void main(int argc, char *argv [ ]){ }

A, B, D, E, F

(C Programming) In a C program, which of the following is true: C allows only one parameter for main. C allows more than two parameters for main. C allows one parameter or two parameters for main. C allows empty parameters or two parameters for main.

C allows empty parameters or two parameters for main.

Syntactic Structure: Imperative Programming

Conditional statements; loop statements; variable declaration

(C++) The scope resolution operator ( :: ) is used to overload a function or an operator in object-oriented paradigm. (T/F)

False

(C++) in C++, implementations of member functions cannot be inside the class definition (for short functions) or outside of the class definition. (T/F)

False

(PROLOG) ?- 10 + 5 is 15. How will prolog respond to this query?

False

(PROLOG) Given the following fact and rule: fun(1, 2). fun(N, F) :- N > 0, N1 is N - 1, fun(N1, F1), F is N * F1. What is the result of the query: ?- fun(2, 2).

False

Compilation of a program is to execute all the statements of the program completely. (T/F)

False

Functional programming languages are low-level languages. (T/F)

False

The compiler executes the program (T/F).

False

(C Programming) What is printed on the screen after running this program: #include <stdio.h> int main() { int i=0; char a[ ] = "H2o2"; while(a[i] != '\0') { *(a+1)= *(a+i) + 2; i++; } char *q = a; q[2] = '#'; printf("%s", a); return 0; }

H4#2

What memory must be explicitly garbage collected :

Heap memory

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.

Programming language uses two-step translation with intermediate codes for execution.

Java

Java programmers do not need to do garbage collection because:

Java uses a system program to collect the garbage.

There are various consequences to not using the C++ built-in exception handling mechanisms. Which of them is the most serious consequence?

Not restoring the stack pointer to its original position before entering the function.

(LISP) What is printed on the screen by the print instruction in the code below: (print ( and (< (* 3 5 ) ) ( not (>= 4 6 ) ) ) )

T

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.

(C Programming) In C, when you pass an array as a parameter to a function, what is actually passed?

The address of the first element in the array.

A goal succeeds if there are facts (rules) that match or unify the goal. A goal clause and a fact unify if:

Their predicates are the same; Their arity is the same; Their corresponding arguments are identical

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.

(C Programming) The following code is correct and print "Hello": (T/F) if(2+2+2+2) if(1) printf("Hello");

True

(C Programming) char, unsigned char, signed char and int are all numeric (integer) types in C programming. (T/F)

True

(LISP) Is the following line correct in LISP: (+ 1 2 3 4 5 6 7 8 9 0)

True

(LISP) One feature of the functional paradigm is a higher level of abstraction compared with Imperative and Object-Oriented paradigms. (T/F)

True

(PROLOG) 15 is 10 + 5. How will Prolog respond to this query?

True

(PROLOG) Logic programming describes what the problem is by a set of conditions and constraints, and leaves the computer to match the problem to the existing knowledge of facts and rules and to find solutions to the problem. (T/F)

True

During compilation, all the statements of a program in high-level language are converted (translated) to a low-level language. (T/F)

True

Interpretation of a program is the direct execution of one statement at a time sequentially. (T/F)

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.

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.

In the implementation of the getMax() function in the PriQueue class, we need to read and write the variables "front" and "rear", which are defined as protected members in the base class Queue. Are the functions in the derived class allowed to access the protected members in the base class?

Yes, always.

(C Programming) Define the term: Data Type

a classification specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it.

(C Programming) Define the term: Pointer

a name given to a storage location that store an address.

(C Programming) Define the term: Variable

a name given to a storage location.

What is a data type?

a set of primary values and the operations defined on these values.

(C++) The principle behind the object-oriented paradigm consists of a number of programming concepts, which does not include the following: a) Arrays b) Classes c) Pointers d) Inheritance e) Polymorphism

a) Arrays c) Pointers

(PROLOG) Prolog is based on : a) predicate logic b) turing machine c) boolean logic d) lambda calculus

a) predicate logic

(C++) The following declaration allows all elements in the standard C++ library to be accessed in an unqualified manner (without the std:: prefix): a) using namespace std; b) #include <iostream.h> c) using namespace iostream; d) #include <iostream>

a) using namespace std;

If a class needs to inherit from multiple parent classes, the parent classes must

be inherited as virtual classes.

(C Programming) Which of the following are NOT primitive data types in C?

bool String short long

Defining a virtual function in class means that the function

can be redefined in its child classes.

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

If A is a base class and B is a class derived from A, then:

class B has the members of class A.

(C++) Which of the following classes creates and initializes correctly a static variable in C++?

class Something { public: static int v; } int Something::v = 1;

(LISP) Which of the following lines represent in LISP the operation of 3 multiplied by 4? a) 3*4 b) (multiply 3 4) c) (3*4) d) (* 3 4)

d) (* 3 4)

(LISP) functions are created by calling a function-making macro. This macro is called: a) function b) progn c) let d) defun

d) defun

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.

(PROLOG) We apply an anonymous variable in the definition of a rule if we:

do not want to obtain a return value to the variable.

What type casting mechanism should be used if you want to change pointer type for pointing to a different object in an inheritance hierarchy?

dynamic_cast

(PROLOG) Given the following facts: instructor(john,cse300). instructor(mary,cse400). instructor(paul,cse500). enrolled(joseph,cse300). enrolled(joseph,cse400). enrolled(joseph,cse500). enrolled(james,cse300). enrolled(james,cse500). What query provides a list of all students enrolled in cse300?

enrolled(Who, cse300).

A prolog program may contain:

facts rules questions

(C Programming) Define the term: Constant

fixed value that the program may not alter during its execution.

What memory must be garbage-collected by a destructor?

heap memory created in the constructors in the same class

(C++) We need to write a destructor for a class if:

heap memory is used in the constructor of the class.

(C Programming) What is printed by the following code: #include <stdio.h> int i=10; int bar(int m, int *n) { printf("i=%d k=%d l=%d\n", i,m,*n); } int foo(int k, int *l) { printf("i=%d k=%d l=%d\n", i,k,*l); k = 3; *l = 4; bar(k, l); } int main() { int j = 15; foo(j, &i); printf("i=%d j=%d\n", i, j); return 0; }

i = 10 k = 15 l = 10 i = 4 k = 3 l = 4 i = 4 j = 15

(C Programming) What is printed by the following code? #include <stdio.h> int foo(int *n) { *n = 30; } int main() { int i=15; foo(&i); printf("i=%d\n",i); i=10; foo(&i); printf("i=%d\n",i); return 0; }

i = 30 i = 30

Lexical structure of all programming languages include:

identifiers keywords operators literals

(LISP) What is the equivalent code in C++ for the following LISP statement: (if (< 1 2) (* 3 4) (/ 5 6))

if (1 < 2) return 3 * 4; else return 5 / 6;

(C Programming) Consider the following code: struct emp { int id; char *name; }; struct emp john; Which of the following lines are correct?

int a = 1; char b[ ] = "John Doe"; john.id = a; john.name = b; printf("%d, %s", john.id, john.name);

(C Programming) Which of the following lines print a "-15" on the screen?

int x = -15; int *point = &x; printf("%d", *point);

(C++) Which lines in C++ are equivalent to this code in Java: int x = 5; char a = 'A'; System.out.print( "Hello " + x + ", " + a );

int x = 5; char a = 'A'; cout << "Hello" << x << "," << a ;

(C Programming) Which code in C is equivalent to this Java code: int x = 5; float y = 10.3f; System.out.println("hello " + x + " bye " + y);

int x = 5; float y = 10.3; printf("hello %d bye %f", x, y);

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, j

(PROLOG) Which rule defines the following sentence: "If someone owns something, he loves it"

love(Who, What) :- owns(Who, What).

(PROLOG) Write in Prolog the question "Which food is meal and lunch?"

meal(X), lunch(X).

(PROLOG) How many arguments can be in a fact?

n, where n >= 0

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

names (alias) for a variable.

(PROLOG) Which rule defines in Prolog the following sentence: "mary owns a Pet if it is a cat and it has black spots."

owns(mary, Pet) :- cat(Pet), black_spots(Pet).

(C++) The purpose of the scope resolution operator ( :: ) is to allow a function to be:

placed outside the class.

A prolog variable represents a:

placeholder

(C Programming) %c

printing format for characters

(C Programming) %f

printing format for floating point values

(C Programming) %d

printing format for integer values

(C Programming) %p

printing format for memory addresses (pointer stored values)

(C Programming) %s

printing format for strings of characters

(C Programming) Given the following struct: struct contact { char name[32]; int phone; char email[32]; }; Which code can be used to create a contact and store data?

struct contact x; scanf("%s", x.name); scanf("%d", &x.phone); scanf("%s", x.email);

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.

What is the key difference between a static and a global variable?

they have different visibility scope.

(C++) C++ allows a programmer to create and object through _______

through declaration at compilation phase; through new operation during execution

Semantic structure: Imperative Programming

type matching; parameters type in a function declaration should match these in the function call; unicity

The exception handling mechanism discussed in this course is at the level of

user program.

The key idea of logic programming paradigm is to solve a problem by describing:

what the problem is.

The scope of a prolog variable is:

within a single fact, rule or query.


Related study sets

Practice Questions Endocrine System

View Set

Chapter 21 - Respiratory Care Modalities Prep U

View Set

Fundamentals of Entrepreneurship Unit 1 quiz

View Set

Lippincott Q & A The client with a head injury

View Set