3210 Test 3

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

In C++, when passing two-dimensional array as a function parameter, we need to specify the size of columns, but not the size of rows. In Java or C#, when passing two-dimensional array as function parameter, we don't need to specify the size of columns or rows. Please explain why Java/C# can allow it and how to access the size of each dimension through the parameter.

Java and C# handle arrays as objects, not as contiguous blocks of memory like in C++. The lengths of each dimension can be accessed using the length property in Java or Length property in C#.

What's the output of the following statement if a = 20, b=15 and c = 10? if (a < b) if (c < b) cout <<"b is the greatest" << endl; else cout << "c is the greatest" << endl;

No output

What's the output of the following statement if oper is '^'? int num1 = 10, num2 = 20; switch (oper) { case '+': case '-': cout << num1 + num2 << endl; break; case '*': case '/': cout << num1 * num2 << endl; }

No output

Give the output of the following program. Make sure there is one space between numbers. No space allowed before the first number or after the last number. #include <iostream> int main() { int h = 10; int s = 2; for(int i=0; i<h; i += s) { std::cout<<i << " "; h ++; i ++; S ++; }}

0 4 9

Given the following two functions: f and g, what's the return value of f(0) and g(0), respectively? int f(int n) { int i = 1, total =0; while(i < n ) { total += i; i++; } return total;} int g(int n) { int i = 1, total =0; do { total += i; i++; } while(i < n ); return total;}

0 and 1

Consider the following C++ code fragment: void main() { ... f(); ... //line 1 } int f() { ... //line 2 g(); ... //line 3 } int g() { ... //line 4 } How many activation record instances are in the call stack at line 1?

1

Given the following Prolog facts and rules: mystery ( [X], X). mystery ( [Head | Tail], X) : - mystery (Tail, X) . What's the output of the following queries? 1) mystery([123], X)? 2) mystery([3, 2, 1], 3)? 3) mystery([1, 2, 3], X)?

1) X = 123 2) false 3) 3

Given the following two functions: f and g, what's the return value of f(5) and g(5), respectively? int f(int n) { int i = 1, total =0; while(i < n ) { total += i; i++; } return total;} int g(int n) { int i = 1, total =0; do { total += i; i++; } while(i < n ); return total;}

10 and 10

What's the output of the following Python program? def foo(): x = 3 def bar(): print(x) def ack(): nonlocal x x = 7 x = 5 return (bar, ack) bar, ack = foo() bar1, ack1 = foo() ack() bar() bar1()

7 and 5

Which of the following is FALSE? A. An activation record instance is created for each function. B. An activation record instance is dynamically created when a subprogram is called. C. The activation record format is static, but its size may be dynamic. D. Activation record instances reside on the run-time stack.

A. An activation record instance is created for each function.

Which of the following is not in the activation record? A. Environment pointer B. Static link C. Subprogram parameters. D. Dynamic link E Return address to the caller

A. Environment pointer

Which one of the following is FALSE about foreach loop? A. Foreach loop improves the efficiency of the program. B. Foreach loop is usually used in place of a standard for loop statement. C. Foreach loop usually maintains no explicit counter. D. Foreach loop avoids potential off-by-one errors.

A. Foreach loop improves the efficiency of the program.

Give the output of the following Java code and explain the meaning of the parameter "int ... a" in method f. class HelloWorld { public static void f(int ... a) { int sum = 0; for (int i=0; i<a.length; i++) sum += a [i]; System.out.println (a.length + " : " + sum) ; } public static void main (String[] args) { f () ; f (1, 2, 3, 4); f (5, 5); }}

Output: 0 : 0 4 : 10 2 : 10 Signifies you can take any number of integers

Give the output of the following Python code and use this as an example to explain the concept of function closure. def f(x) : def g (y) : return 2*x + y; return g foo = f (1) print (foo (10))

Output: 12 (2 * 1 + 10) function closure: a function plus a copy of its referencing environment

Which of the following is FALSE about the multi-way selection statement? A. In C++, the type of the expression in the switch statement can be integer or string. (i.e. switch (expression) { .... }). B. In C++, switch statement cannot return a value. C. Ruby doesn't support switch statement, instead, it support CASE expressions. D. In Java, switch statement can return a value.

A. In C++, the type of the expression in the switch statement can be integer or string. (i.e. switch (expression) { .... }).

Which of the following is NOT a correct atomic proposition? A. Male(david) B. maLe(david) C. mAle(david) D. malE(david)

A. Male(david)

Which of the following is NOT true regarding the term: A is B + C? A. You can write an expression like: Sum is Sum + 5. B. If B and C are instantiated, but A is not, then this clause will cause A to be instantiated with the value of the expression, when this happens, the clause is satisfied. C. If B or C is not instantiated, or A is instantiated, the clause is not satisfied.

A. You can write an expression like: Sum is Sum + 5.

Assuming both parameters in the function f(int&, int&) are passed by reference. Which of the following function calls most likely will NOT cause unwanted alias ____? A. f(data[i], score[i]) B. f(list[i], list[i]) C. f(a, a) D. None of the above

A. f(data[i], score[i])

Which of the following is FALSE? A. Activation record instances reside on the run-time stack. B. An activation record instance is created for each function. C. The activation record format is static, but its size may be dynamic. D. An activation record instance is dynamically created when a subprogram is called.

B. An activation record instance is created for each function.

Which of the following is a correct rule in Prolog? A. shopping(today), bring(umbrella) :- raining(today). B. bring(umbrella) :- shopping(today), raining(today). C. Bring(umbrella) :- raining(today). D. bring(umbrella) :- Raining(today).

B. bring(umbrella) :- shopping(today), raining(today).

Assume both parameters of the function void f(int x, int y) are passed by reference. Which of the following function calls will NOT cause unwanted aliasing? A. f(data[i], data[j]) B. f(a, b) C. f(a, a)

B. f(a, b)

Given the following function definition in Python, which of the following is NOT a correct function call? def f(x, y, z = 0): return x + y + z A. f(10, z=30, y=20) B. f(y=20, z=30) C. f(z=30, y=20, x=10) D. f(10, 20) E. f(y=20, x=20)

B. f(y=20, z=30)

Given the following C++ function template and variable declarations, which function call is NOT correct? template<typename S, typename T> void f(S &container, T item[], int rows) { for(int i=0; i<rows; i++) container.push_back(item[i]);} vector<int> int_container; string str_container; stack<int> int_stack; int scores[5] = {10,20,30,40,50}; int chars[5] = {'a', 'b', 'c', 'd', 'e'}; A. f(int_container, scores, 5); B. f<stack<int>, int>(int_stack, scores, 5); C. f(str_container, chars, 5); D. f<vector<int>, int>(int_container, scores, 5);

B. f<stack<int>, int>(int_stack, scores, 5);

Which of the following is a legal rule or fact in Prolog? A. Raining. B. raining(today). C. Raining(Today) :- not(sunny(Today)), not(cloudy(Today)). D. Raining(Today).

B. raining(today).

Which one of the following functions is overloaded with the following function?int foo (string name, int size ) A. int whoo(string name); B. void foo (int size, string name); C. int foo(string city, int length); D. None of the above

B. void foo (int size, string name);

Which of the following languages can use delegate to pass a function as a parameter to another function? A. C++ B. Ruby C. C# D. Java

C. C#

Which pair of functions are NOT considered overloaded? A. void print(int data[][30], int rows); void print(int data[][20], int rows); B. void f(int x); void f(int x, int y); C. int sum(int x, int y); double sum(int a, int b); D. void search(Student data[], int rows, int age); void search(Student data[], int rows, string name);

C. int sum(int x, int y); double sum(int a, int b);

Which of the following C++ function prototypes is more appropriate? A. void f(int data[][y], int rows); B. void f(int data[][], int rows, int cols); C. void f(int data[][20], int rows); D. void f(int data[10][20]);

C. void f(int data[][20], int rows);

Which one of the following is true about for-each loop? A. Foreach loop is usually used in place of a standard for loop statement. B. Foreach loop usually maintains no explicit counter. C. Foreach loop avoids potential off-by-one errors. D. All of the above

D. All of the above

Consider the following C++ code fragment: void main() { ... f(); ... } int f() { ... g(); ... } int g() { ... } Which of the following statements is correct about activation record? A Before executing f, activation record instances of main and g are pushed in stack. B. Before executing main, activation record instance of f is created. C. Before executing g, activation record instances of main and x are popped from stack. D. Before executing g, activation record instances of main and f are pushed in stack.

D. Before executing g, activation record instances of main and f are pushed in stack.

Which programming languages use a delegate to represent a reference to methods? A. C++ B. Python C. Java D. C#

D. C#

Given the function prototype in C++: void f(int x=0, int y=1, int z = 2); the call: f(10) is equivalent to which of the following ? A. f(10, 0, 1) B. f(0, 10, 2) C. f(0, 1, 10) D. None of the above

D. None of the above

Given the following C++ function template template <typename T> T f(T x, T y){ return x * y;} Which of the following function calls will cause errors? A. f(5, 4); B. f<int> (4, 5); C. f<double> (4.3, 5.3); D. f( string("David"), string("John") );

D. f( string("David"), string("John") );

If a static-scoped programming language supports nested subprograms and allows subprograms to be passed as parameters, what binding should be used to decide the referencing environment for executing the passed subprogram?

Deep Binding

What's the output of the following C++ code? for (int k = 0, i=10; k < i; k+=2) { cout << k << " "; k ++; i = i - 2; A. 0 3 6 9 B. 0 3 6 C. 0 2 4 6 8 D. 0 2 4 6 10 E. None of the above

E. None of the above 0 3?

Which of the following is NOT equivalent to the list [a, b, c, d]? A. [a, b | [c, d]] B. [a | [b, c, d]] C. [a, b, c | [d]] D. [a, b, c, d | []] E. [a, b, c, d | ]

E. [a, b, c, d | ]

Which of the following Java function prototypes is NOT correct? A. public void meth ( int... a, int b) B. public void meth (double a, int... b) C. public void meth( int[]... a) D. public void meth ( Student... a) E. public void meth ( int... a, double... b)

E. public void meth ( int... a, double... b)

What's the output of the following statement if oper is '^'? int num1 = 10, num2 = 20; switch (oper) { case '+': case '-': cout << num1 + num2 << endl; break; case '*': case '/': cout << num1 * num2 << endl; default: cout << "Error!" << endl; break; }

Error!

class Helloworld { public static void main(String[] args) { int scores[][] = { {1, 2, 3, 4}, {5,6,7}, {8} }; loop: for(int i=0; i<scores.length; i++) for(int j=0; j<scores[i].length; j++){ if ( scores [i][j] == 5 ) continue loop; System.out.println(scores[i][j]); }}} Give the output: Give the output if "continue loop;" is replaced by "break loop;":

Give the output: 1 2 3 4 8 Give the output if "continue loop;" is replaced by "break loop;": 1 2 3 4

Given the following Prolog program, has_symptom(flu, fever). has_symptom(flu, headache). has_symptom(flu, body_aches). has_symptom(flu, cough). has_symptom(flu, sore_throat). has_symptom(flu, runny_nose). has_symptom(allergy, sneezing). has_symptom(allergy, watery_eyes). has_symptom(allergy, runny_nose). has_symptom(allergy, itchy_eyes). has_symptom(cold, sneezing). has_symptom(cold, watery_eyes). has_symptom(cold, runny_nose). has_symptom(cold, cough). has_symptom(cold, sore_throat). % Rule has_condition(X,C) :- has_symptom(C,X). What's the output of the query has_condition(X, Y). ?

Pair of values for X and Y to make the predicate true such as X = fever, Y = flu.

What programming languages support nested subprogram?

Python

The static link in the activation record is used for

Referring the non-local data held in other activation records

Design your own facts and rules to calculate the sum of all integers in a list. The predicate should be in the format: sum(List, Total). For example, both sum([1, 2, 3, 4], 10) and sum([4, 3, 2, 1], 10) will return true since 10 = 1 + 2 + 3 +4, but both sum([1, 2, 3, 4], 1000) and sum([1, 2, 3, 4], 4) will return false.

Sum([],0). Sum ([head I Tail], X) :- sum (Tail, TailSum), X is Head + Tail sum.

What's the value of the dynamic link in an activation record instance?

The address of the activation record instance of its caller

What's the value of the static link in an activation record instance?

The address of the most recent activation record instance of its static parent

What's the output of the following statement if a = 10, b=15 and c = 20? if (a < b) if (c < b) cout <<"b is the greatest" << endl; else cout << "c is the greatest" << endl;

c is the greatest

What expression returns the number of columns at the 3rd row in a 2-dimensional array named data in Java?

data[2].length

Given the following Prolog program, has_symptom(flu, fever). has_symptom(flu, headache). has_symptom(flu, body_aches). has_symptom(flu, cough). has_symptom(flu, sore_throat). has_symptom(flu, runny_nose). has_symptom(allergy, sneezing). has_symptom(allergy, watery_eyes). has_symptom(allergy, runny_nose). has_symptom(allergy, itchy_eyes). has_symptom(cold, sneezing). has_symptom(cold, watery_eyes). has_symptom(cold, runny_nose). has_symptom(cold, cough). has_symptom(cold, sore_throat). % Rule has_condition(X,C) :- has_symptom(C,X). What's the output of the query has_condition(Headache, X). ?

false

Given the following Prolog program, has_symptom(flu, fever). has_symptom(flu, headache). has_symptom(flu, body_aches). has_symptom(flu, cough). has_symptom(flu, sore_throat). has_symptom(flu, runny_nose). has_symptom(allergy, sneezing). has_symptom(allergy, watery_eyes). has_symptom(allergy, runny_nose). has_symptom(allergy, itchy_eyes). has_symptom(cold, sneezing). has_symptom(cold, watery_eyes). has_symptom(cold, runny_nose). has_symptom(cold, cough). has_symptom(cold, sore_throat). % Rule has_condition(X,C) :- has_symptom(C,X). What's the output of the query has_condition(headache_and_cough, X). ?

false

Please come up with a semantically equivalent program to the following C++ program snippets without using goto, break/continue, or return statements. Feel free to introduce extra variables if needed. for (i=0; i<3; i++){ f (i, j); if ( j > 0 ) continue; g(i, j); }.

for(i=0; i < 3; i++){ f(i, j); if(j <= 0){ g(i, j); }}

Design facts and rules to find out the greatest integer in a given list of integers. The predicate should be in the format: greatest(List, Max). For example, greatest([1,2,3,4,3,2,1,0], 4). will be true, and greatest([10,20,60,4,20,50], X). will return X = 60.

greatest ([X], X). greatest (Head | TaiI], Head) :- greatest (Tail, X), Head > X.

Please come up with a semantically equivalent program to the following C++ program snippets without using goto, break/continue, or return statement. You may need to use two-way selection statement. switch ( i ) { case 1: f(i); case 2: g(i); break; case 3: h (i); default: foo (i);}

if (i == 1) { f(i); g(i); } else if (i == 2) { g(i); } else if (i == 3) { h(i); foo(i); } else { foo(i); }

Assume you need to write a function to search a given value in a one-dimensional array. To make sure the function can be applied to any type of one-dimensional array, you need to use function template in C++. Please give the prototype of the function template. No implementation needed).

template <typename T> bool searchArr (T &arr, size_t Len, T item) { for (size_t i = 0; i < Len; i++) { if (arr[i] == item) return true; } } return false; }

Consider the following program written in C syntax: void f(int x, int y, int z){ x = x + 2; y = 2; z = z + 1; } void main () { int value = 1; int list [5] = (5, 6, 7, 8, 9); f(value, list [value], value); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after the call to f? pass by value value = list[5] =

value = 1 list[5] = 5 6 7 8 9

Consider the following program written in C syntax: void f(int x, int y, int z){ x = x + 2; y = 2; z = z + 1; } void main () { int value = 1; int list [5] = (5, 6, 7, 8, 9); f(value, list [value], value); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after the call to f? pass by value-result (the addresses of the actual parameters are computed at the BEGIN of function f from left to right and parameters are copied back from left to right) value = list[5] =

value = 2 list[5] = 5 2 7 8 9

Consider the following program written in C syntax: void f(int x, int y, int z){ x = x + 2; y = 2; z = z + 1; } void main () { int value = 1; int list [5] = (5, 6, 7, 8, 9); f(value, list [value], value); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after the call to f? pass by reference value = list[5] =

value = 4 list[5] = 5 2 7 8 9

Consider the following program written in C syntax: void f(int a, int b) { a++; b++;} void main() { int value = 1; int list[5] = {1, 3, 5, 7, 9}; f(value, list[0]); f(list[0], list[1]); f(list[0], list[0]); f(value, list[value]);} If passed-by-value-result is used, what are all of the values of the variables value and list after the last call to swap? Assume the addresses of the actual parameters are computed at the END of function f and parameters are copied back to arguments from left to right. value = list[5] =

value: 1 list: 1 3 5 7 9 after 1st: value: 2 list: 2 3 5 7 9 after 2nd: value: 2 list: 3 4 5 7 9 after 3rd: value: 2 list: 4 4 5 7 9 after 4th/last: value = 3 list[5] = 4 4 5 6 9

Consider the following program written in C syntax: void f(int a, int b) { a++; b++;} void main() { int value = 1; int list[5] = {1, 3, 5, 7, 9}; f(value, list[0]); f(list[0], list[1]); f(list[0], list[0]); f(value, list[value]);} If passed-by-reference is used, what are all of the values of the variables value and list after the last call to swap? value = list[5] =

value: 1 list: 1 3 5 7 9 after 1st: value: 2 list: 2 3 5 7 9 after 2nd: value: 2 list: 3 4 5 7 9 after 3rd: value: 2 list: 5 4 5 7 9 after 4th/last: value = 3 list[5] = 5 4 6 7 9

What is a correct explanation of rule: playGuitar(vincent):-listens2Music(vincent); loveMusic(vincent). ?

vincent plays guitar if he listens to music or he loves music.

Given the following function definition in Python, what's the return value of function call f(0) ? def f(x=10, y=20, z = 30): return x + y + z

50

What is a correct explanation of rule: professor(X, Y) :- teaches(X, C), studies(Y, C). ?

X is a professor of Y if X teaches C, and Y studies C.

Please give the output of the following C# program. Please note that in C#, all object variables are reference-type variables, i.e., they are references to objects. Out means the parameter is passed by result, and ref means the parameter is passed by reference. Class Student { public int age {get; set;} public Student () { age = 0; }} public class Program { static void f(Student x, Student y, out Student z, ref Student u) { x.age = 10; y = new Student(); y.age = 20; z = new Student (); z.age = 30; u = new Student(); u.age = 40; } public static void Main() { Student first = new Student(); Student second = new Student(); Student third; //not initialized student fourth = new Student(); f(first, second, out third, ref fourth); Console.writeLine(first.age); console.Writeline(second.age) Console.WriteLine (third.age); Console.Writeline(fourth.age); }}

10, 0, 30, 40

Given the following Java function definition, what is the return value of the function call: f(1,2,3,4,5)? public static int f(int... a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; } return sum;}

15

Consider the following C++ code fragment: void main() { ... f(); ... //line 1 } int f() { ... //line 2 g(); ... //line 3 } int g() { ... //line 4 } How many activation record instances are in the call stack at line 2?

2

Consider the following C++ code fragment: void main() { ... f(); ... //line 1 } int f() { ... //line 2 g(); ... //line 3 } int g() { ... //line 4 } How many activation record instances are in the call stack at line 3?

2

Give the output of the following program. Make sure there is one space between numbers. No space allowed before the first number or after the last number. public class Main { public static void main(String[] args) { outer: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print(i+j); System.out.print(" "); if (i == 2 && j == 2) { break outer; }}}}}

2 3 4 3 4

Give the output of the following program. Make sure there is one space between numbers. No space allowed before the first number or after the last number. public class Main { public static void main(String[] args) { outer: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print(i+j); System.out.print(" "); if (i == 2 && j == 2) { break; }}}}}

2 3 4 3 4 4 5 6

Give the output of the following program. Make sure there is one space between numbers. No space allowed before the first number or after the last number. public class Main { public static void main(String[] args) { outer: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) if (i == 2 && j == 2) { continue outer: } System.out.print(i+j); System.out.print(" "); }}}}

2 3 4 3 4 5 6

Give the output of the following program. Make sure there is one space between numbers. No space allowed before the first number or after the last number. public class Main { public static void main(String[] args) { outer: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { continue; System.out.print(i+j); System.out.print(" "); }}}}

2 3 4 3 5 4 5 6

Please give the output of the following C# program. Please note that in C#, all object variables are reference-type variables, i.e. they are references to objects. Class Student { public int age {get; set;} public Student () { age = 30; }} public class Program { static void f(Student x, Student y, out Student z, ref Student u) { x.age = 20; y = new Student(); y.age = 30; z = new Student (); z.age = 20; u = new Student(); u.age = 10; } public static void Main() { Student first = new Student(); Student second = new Student(); Student third; //not initialized student fourth = new Student(); f(first, second, out third, ref fourth); Console.writeLine(first.age); console.Writeline(second.age) Console.WriteLine (third.age); Console.Writeline(fourth.age); }}

20, 30, 20, 10

What's the output of the following statement if oper is '*'? int num1 = 10, num2 = 20; switch (oper) { case '+': case '-': cout << num1 + num2 << endl; break; case '*': case '/': cout << num1 * num2 << endl; default: cout << "Error!" << endl; break; }

200 Error!

Consider the following C++ code fragment: void main() { ... f(); ... //line 1 } int f() { ... //line 2 g(); ... //line 3 } int g() { ... //line 4 } How many activation record instances are in the call stack at line 4?

3

Please give the number of elements in Prolog list: [a | [a | [a] ] ]?

3

What's the output of the following statement if oper is '+'? int num1 = 10, num2 = 20; switch (oper) { case '+': case '-': cout << num1 + num2 << endl; break; case '*': case '/': cout << num1 * num2 << endl; default: cout << "Error!" << endl; break; }

30

What is printed as a result of the following C++ code segment? for (int k = 0; k < 10; k+=2){ if (k % 3 == 1) cout <<k << " ";

4

Please explain the concept of duck typing.

The type of an object or class is not checked because of the implementation of the methods are what matters. This allows for greater flexibility.

Both functions f and g given below are to calculate the sum from integer lower to upper. One uses while loop and the other uses do ... while loop. Are they semantically equivalent? If not, justify your answer by showing function calls to them with same arguments but expecting different return values. int f(int lower, int upper) { int j = lower; int sum = 0; while(j < upper){ sum += j; j++; } return sum;} int g(int lower, int upper) { int j - lower; int sum = 0; do { sum += j; j++;} while(j < upper); return sum;}

They are not semantically equivalent. While checks the conditions before running the body of the loop, but do-while checks the conditions after. f(10, 5) //doesn't execute g(10, 5) //executes once

A call stack may contain multiple activation record instances created for the same function.

True

Prolog, the most popular logic programming language, is an example of declarative programming languages. True or False

True

Given the following Prolog program, has_symptom(flu, fever). has_symptom(flu, headache). has_symptom(flu, body_aches). has_symptom(flu, cough). has_symptom(flu, sore_throat). has_symptom(flu, runny_nose). has_symptom(allergy, sneezing). has_symptom(allergy, watery_eyes). has_symptom(allergy, runny_nose). has_symptom(allergy, itchy_eyes). has_symptom(cold, sneezing). has_symptom(cold, watery_eyes). has_symptom(cold, runny_nose). has_symptom(cold, cough). has_symptom(cold, sore_throat). % Rule has_condition(X,C) :- has_symptom(C,X). What's the output of the query has_condition(headache, X). ?

X = flu


Ensembles d'études connexes

AP Human Geography Chapter 9 - Agriculture

View Set

ANS 105 Final Exam review (NCSU-AngeVH-2017spring)

View Set

Chapter 45: Caring for Clients with Disorders of the Upper Gastrointestinal Tract

View Set

California Legal Aspects of Real Estate Chapter 5

View Set