CS 161 Final

¡Supera tus tareas y exámenes ahora con Quizwiz!

#define and #include are examples of preprocessor directives.

T

A memory address is where a variable is stored

T

A variable's scope determines who can see and access the variable.

T

Global variables are in scope while the program is executing.

T

In some cases, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve.

T

Local variables are in scope while the declaring function is executing.

T

Not all logical operators are binary.

T

Recursive functions usually take more memory space than non-recursive functions.

T

The if statement allows for complicated condition checking, the switch does not.

T

The size of a dynamic array is defined at runtime.

T

The stack is used to control recursion

T

To solve a problem recursively, you must identify at least one case in which the problem can be solved without recursion.

T

When using the rand() function, you can ensure that you will obtain the same series of numbers by passing srand() the same seed value.

T

You can declare an array on the stack using a variable for the size, i.e. int x[size], rather than a literal value or constant.

T

You cannot use a C++ reference to create dynamic memory on the heap.

T

1 string s = "math is fun!"; 2 int t = s.length(); 3 char n = s.at(5); 4 char y = s.at(12); 35. What is the value of t after line 2? a. 12 b. 11 c. The program will not compile. d. Crash. That's not how you call length().

a

Fill in the code to complete the following function for computing factorial. /*Return the factorial for a specified index */ long factorial(int n) { if (n == 0) // Base case return 1; else return _____________; // Recursive call } A. factorial(n - 1) * n B. n C. n * factorial(n--) D. n * (n - 1) E. A and C

a

How many times will this for loop execute? (Note: i is an int.) for(i = 0; i < 10; i+=2) a. 5 b. 6 c. 10 d. The loop is not written correctly. It would not compile.

a

Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5} and compiler stores it in the memory starting with address 04BFA810. Assume a double value takes eight bytes on a computer. &list[1] is ______. a. 04BFA818 b. 04BFA810 c. 3.4 d. 1

a

Suppose you declare the following: double *pValue, radius = 5.0; Which of the following statements are not allowed? A. *pValue = &radius; B. cout << *pValue; C. radius++; D. *pValue = 0; E. (*pValue)++;

a

What is the escape sequence for newline? a. \n b. /n c. endl d. Both A & C.

a

Which is an example of a relational operator? a. >= b. * c. += d. &&

a

Which of the following statements are correct? A. char charArray[2][2] = {{'a', 'b'}, {'c', 'd'}}; B. char charArray[][] = {{'a', 'b'}, {'c', 'd'}}; C. char charArray[2][] = {{'a', 'b'}, {'c', 'd'}}; D. All of the above

a

. In the Towers of Hanoi algorithm, how many times does the recursive towers() function call itself, not including the initial call, to move 3 disks with the two recursive calls in the case if(number of disks>=1)? A. 10 B. 14 C. 3 D. 7

b

1 string s = "math is fun!"; 2 int t = s.length(); 3 char n = s.at(5); 4 char y = s.at(12); What is the value of n after line 3? a. The value of n is space. b. The value of n is i. c. The program will not compile. d. The program crashes at line 3.

b

Syntax of C++ can be described as a. use of object-oriented programming techniques. b. grammatical rules. c. level of complexity. d. use of keywords.

b

What is the output from this code? int x = 0; if(x == 2 || 1) cout << "Number is 1 or 2"; else cout << "Number is not 1 or 2"; a. Number is not 1 or 2 b. Number is 1 or 2 c. This code will not compile. d. Nothing. The statement is written incorrectly.

b

What is the output of the following code? int main() { int list[] = {10, 20, 30, 40}; cout << *(list + 1) << " " << *list + 1 << endl; return 0; A. 10 10 B. 20 11 C. 30 30 D. 20 20

b

What is the printout of the following code? char city[7] = "Dallas"; cout << strlen(city); a. 7 b. 6 c. 5 d. 8

b

What is wrong with this call statement? PrintName(string name); a. It's missing "void". b. The datatype should be removed. c. The semicolon should be removed. d. Nothing is wrong with it.

b

What organization developed the C language? a. Intel b. Bell Labs c. Honeywell d. IBM

b

Which was the first operating system is written in C? a. Windows b. UNIX c. Linux d. Mac OS

b

Analyze the following code: void f(int x[], int length) { for (int i = 0; i < length; i++) cout << " " << x[i]; } int main() { int x[] = {0, 1, 2, 3, 4, 5}; f(x, 5); } a. The program displays 0 1 2 3 4 5. b. The program displays 0 1 2 3 4 and then raises a runtime exception. c. The program displays 0 1 2 3 4. d. The program displays 0 1 2 3 4 5 and then raises a runtime exception.

c

Analyze the following code: void reverse(int list[], const int size, int newList[]) { for (int i = 0; i < size; i++) newList[i] = list[size - 1 - i]; } int main(){ int list[] = {1, 2, 3, 4, 5}; int newList[5]; reverse(list, 5, newList); for (int i = 0; i < 5; i++) cout << newList[i] << " "; } A. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. B. The program displays 1 2 3 4 6. C. The program displays 5 4 3 2 1. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

c

From the list below, which is an INVALID variable name? a. my_time b. _aboutTime c. 2_More d. Ans_4

c

How many times will the following function call itself, not including the initial call to the function, if 5 is passed as the argument? void showMessage(int n){ if (n > 0) { cout << "Good day!" << endl; showMessage(n - 1); } } a. 1 b. 4 c. 5 d. An infinite number of times

c

If you assign int cost = 395.75;. What actually will be stored in memory? a. 396 b. 395.75 c. 395 d. You will get a compiler error.

c

If you declare an array double list[] = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. A. 3.4 B. undefined C. 2.0 D. 5.5 E. 3.4

c

In this prototype, what is missing? void PrintNumber(int) a. Nothing is missing, it is complete. b. The variable name must be included in the input list. c. A semi-colon d. A return type.

c

Source code is a. A file that is created when a program is compiled. b. A list that tells you where your errors are. c. Text that you enter into a text editor. d. Machine instructions written in binary.

c

The difference between unary and binary operators is that binary operators a. return 2 values. b. require two statements to execute. c. require two operands. d. can only be used with numeric variables.

c

What is the difference between the following statements as seen by the compiler? 1) int y = 7; 2) int y = 7; a. The compiler won't accept 1) because of the separate lines. b. The compiler won't accept 1) because of the whitespace characters. c. The compiler assigns "7" into "y" in both cases. d. Neither can be compiled.

c

What is the output of the following code? int main() { int list[] = {1, 1, 1, 1}; *(list) = *(list) + 1; *(list + 1) = *(list + 1) + 2; *(list + 2) = *(list + 2) + 3; *(list + 3) = *(list + 3) + 4; cout << list[0] << " " << list[3] << endl; return 0; } a. 2 2 b. 3 5 c. 2 5 d. 1 2 e. 3 4

c

What is the output of the following code? int main() { int x[3] = {120, 200, 16}; for (int i = 0; i < 3; i++) cout << x[i] << " "; } A. 200 120 16 B. 16 120 200 C. 120 200 16 D. 16 200 120

c

What is wrong with this code? int ShowMeTheMoney() { int cents, dollars; cout << "Enter dollars and cents"; cin >> dollars , cents; return (dollars,cents); } a. You can't have a comma in the cin statement. b. You can't have a comma in the return statement. c. Both A & B. d. There is nothing wrong with it.

c

When you pass the name of an array to a function, the function receives __________. A. the length of the array B. a copy of the array C. the address of the array D. a copy of the first element

c

1 string s = "math is fun!"; 2 int t = s.length(); 3 char n = s.at(5); 4 char y = s.at(12); What is the value of y after line 4? a. The value of y is [] (an empty set) b. The value of y is ! c. The program will not compile. d. The program crashes at line 4.

d

Analyze the following code. int main() { char *p; cout << "Enter a string: "; cin >> p; cout << p << endl; return 0; } a. If you run the program and enter abc, unpredictable characters will be displayed. b. If you run the program and enter abc, abc will be displayed. c. If you run the program and enter abc, nothing will be displayed. The program runs without errors. d. If you run the program and enter abc, a runtime error will occur, because p is used without being initialized.

d

Given that s = 5, t = 2, p = 5, and q = 6 what will be the value of m in the following equation? (Note: assume that all variables are declared as integers.) double m = s / t + q / p; a. 3.7 b. 2.0 c. 0.7 d. 3.0

d

In this while loop statement, while(counter < 10) the variable counter is an int. Which statement below is an equivalent way to write this while statement? a. while(10 > counter) b. while( counter <= 9) c. while(9 > counter) d. A and B are correct

d

Suppose you declare an array double list[] = {1, 3.4, 5.5, 3.5}. &(list[1]) is same as ________. A. list[1] B. list C. list[0] D. list + 1 E. list + 2

d

Suppose you declare int count = 5; which of the following is true? A. &count is 5 B. *count is the address of count C. *count is 5 D. &count is the address of count

d

The machine language file that the Operating System reads and "runs" is the a. linker file. b. source code file. c. prototype statement. d. executable file.

d

This if statement should assign the heavier weight to heaviest and the lighter weight to lightest. What is wrong with this code? if (weight1 > weight2) heaviest = weight1; lightest = weight2; a. Nothing. It works fine. b. heaviest is weight1 regardless of the if statement. c. The statement is written incorrectly - crash. d. lightest is weight2 regardless of the if statement.

d

This statement needs to define rate as .1. #define rate = .1; Which is true? a. There should not be a ; at the end of the line. b. It's correct as is. c. The = should be omitted. d. The = should be omitted and the ; should be omitted.

d

Two bytes consists of how many bits? a. 2 b. 4 c. 8 d. 16 e. 32

d

What is the output of the following code? void f1(int x, int &y, int *z) { x++; y++; (*z)++; } int main() { int i = 1, j = 1, k = 1; f1(i, j, &k); cout << "i is " << i; cout << " j is " << j; cout << " k is " << k << endl; return 0; } a. i is 2 j is 2 k is 2 b. i is 1 j is 2 k is 3 c. i is 1 j is 1 k is 1 d. i is 1 j is 2 k is 2

d

What is the printout of the following code? void f(int &p1, int p2) { p1++; p2++; } int main() { int x1 = 1; int x2 = 1; f(x1, x2); cout << "x1 is " << x1 << " x2 is " << x2; } a. x1 is 2 x2 is 2 b. x1 is 1 x2 is 2 c. x1 is 1 x2 is 1 d. x1 is 2 x2 is 1

d

Which index value do you use to access the last element in double list[5]? A. 5 B. 6 C. 0 D. 4

d

Which library needs to be included when using C++ strings? a. <sstring> b. <iostring> c. <cstring> d. <string>

d

Which of the following statements are correct to delete a dynamic variable/object from a pointer p? A. delete [] p; B. delete [] *p; C. delete *p; D. delete p;

d

Which of the following statements are invalid? A. int i[4] = {3, 4, 3, 2}; B. int i[] = {3, 4, 3, 2}; C. double d[30]; D. int[] i = {3, 4, 3, 2}; E. B and D.

d

Given the definition and code fragment: int matrix[2][3]; int k = 0; for(int i =0; i < 2; i++) for (int j=0, j < 3; j++) matrix[i][j] = ++k; The value of matrix[1][2] is a. 2 b. 3 c. 4 d. 5 e. 6

e

Which of the following function declarations are illegal? a. void t1(int x, int y = 0, int z); b. void t4(int x = 0, int y = 0, int z = 0); c. void t2(int x = 0, int y = 0, int z); d. void t3(int x, int y = 0, int z = 0); e. a and c

e

Which of the following function declarations can be passed the following array? char myArray[6][8]; A. void f1(char a[][], int sizeOfFirst); B. void f1(char a[][8], int sizeOfFirst); C. void f1(char& a, int sizeOfFirst); D. void f1(char a[6][8], int sizeOfFirst); E. B and D

e

Which of the following is correct? A. int a(2); B. int a[]; C. int a = new int[2]; D. int a() = new int[2]; E. int a[2];

e

Which variables are local to the Add Numbers function? int AddNumbers( int x, int y) { int z; z = x + y; return z; } a. x b. y c. x & y d. z e. x, y, & z

e

The getline function works like cin and stops reading characters when any whitespace is encountered.

F

The number of bytes of memory used by a variable depends on its value.

F

The return statement takes you to the previously called function.

F

These two if conditions will both test if x is between 0 and 50 inclusive. if(0 <= x <= 50) if(x >= 0 && <= 50)

F

When deallocating a dynamic 2-d array on the heap, you delete the row pointers first and then you delete the columns for each row pointer.

F

With a 2-d dynamic array on the heap, you are required to specify the stride in the parameters of a function.

F

You could use if(x = 34) to see if x is equal to 34.

F

You would use rand()%13 to generate random numbers 0 to 13 inclusive.

F

The following two declarations are the same: char city[8] = "Dallas"; char city[] = "Dallas";

F

A multidimensional array can have elements of different data types.

F

A programmer should select a while loop when the loop must be executed at least once.

F

If the compiler says there is an error on line 10, then there has to be an error on that line.

F

In C++, the array elements are automatically initialized to zero when an array is created.

F

In some 1-D arrays, the array elements do not necessarily have to be contiguous

F

Indices of an array are numbered starting at one for the first element

F

Just like pointers, you can change what any C++ reference refers to at any point.

F

Not all recursive functions can be replaced by a non-recursive function.

F

The anagram CPU stands for Computer Programming Unit.

F

The compiler will give you a warning if you return a value in a void function.

F

The following function declaration guarantees the values in the array argument are not changed. void function1(int array[][4], int numRows);

F

The following statement declares 3 integer pointers. int* p1, p2, p3;

F


Conjuntos de estudio relacionados

Patho Unit 3 PrepU with Explanation Chapter 26

View Set

Chapter 21: Teacher and Counselor

View Set