CS 161 final

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

A set of well-defined steps for performing a task or solving a problem is known as a(n): A. Hierarchy chart B. Algorithm C. Instruction set D. Statement

B. Algorithm

Given that x = 2, y = 1, z = 0, what will the following cout statement display? cout << "answer = " << (x && y && !z) << endl; A. answer = 0 B. answer = 1 C. answer = 2 D. answer = (x && y && !z)

B. Answer = 1

If a C-style string variable word stores "apple", what will strlen(word) return? A. 0 B. 4 C. 5 D. 6

C. 5

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. Both A and B

What will be the output after the following lines of code execute? bool choice; choice = true; cout << "Your choice is " << choice << endl; A. true B. Your choice is true C. Your choice is 1 D. Your choice is choice

C. Your choice is 1

Which of the following is a valid C string declaration? A. char array[5] = "hello" ; B. char array[4] = "hello"; C. char array[] = "hello"; D. None of the above

C. char array[] = "hello";

void read_strings( ③ ____ , int size) { for (int i = 0; i < size; i++) cin >> array[i]; } int main(){ string *arr = new string [5]; read_strings( ① , 5); //Function call ________②_________ //Free memory return 0; } Which of the following will not give you a memory leak at ②? A. delete arr; B. delete arr []; C. delete [] arr; D. delete *arr;

C. delete [] arr;

Which of the following is a legal call to the displayOutput function? void displayOutput(int total); A. void displayOutput(myTotal); B. displayOutput(int mytotal); C. displayOutput(myTotal); D. cout << displayOutput(myTotal);

C. displayOutput(myTotal);

T/F: Assume array1 and array2 are the names of two arrays. To assign the contents of array2 to array1, you would use the following statement: array1 = array2;

False

T/F: Default arguments can be located anywhere in the list of function parameters

False

T/F: In programming, the terms "line" and "statement" always mean the same thing

False

T/F: Just like pointers, you can change what any C++ reference refers to at any point

False

T/F: Machine language is an example of a high-level language.

False

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

False

You may nest while and do-while loops but you may not nest for loops

False

t/f: The C string company[12] can hold 12 characters and a '\0' character.

False

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

True

T/F: A static array name is a constant pointer because the address stored in it cannot be changed.

True

T/F: The elements accessed using two square brackets in a two-dimensional array are all the same data type

True

T/F: The size of a static array is defined at compile time.

True

T/F: You should not assign a non-zero, random integer value to a pointer variable

True

The while loop is considered a pre-test loop and the do-while loop is considered a post-test loop

True

t/f: (x>=15 || y < 3) is equivalent to (!(x<15 && y>=3)).

True

When a variable is assigned a number that is too large for its data type, it A. underflows B. overflows C. reverses D. converts

b. overflows

What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++; A. 1 2 3 4 5 B. 1 1 ... and on forever C. 1 2 3 4 5 6

b.1 1 ... and on forever

What is the output of the following function call, given the function definition below? int F(int n){ if (n == 1) return 1; if (n == 2) return 2; if (n == 3) return 3; return F(n-1) + F(n-2) + F(n-3); } cout << F(5) << endl; A. 5 B. 6 C. 11 D. 20

c. 11

What is the output of the following segment of code if the value 4 is input by the user? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout << total << endl;

c. 13

void tester (int a, int &b){ int c = 0; c = a + 2; a = a * 3; b = c + a; } int main () { int x = 2, y = 3; tester(y, x); cout << x << " " << y << endl; return 0; } A. 2 3 B. 2 10 C. 14 3 D. 14 9

c. 14 3

After the following code executes, what is the value of my_value if the user enters 3? cin >> my_value; if (my_value > 2) my_value = my_value + 5; else if (my_value > 5) my_value = my_value + 10; else my_value = my_value + 20;

c. 8

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. Require two operands

void test( ① ){ // code } int main () { int a = 0; int* ptr1 = &a; int** ptr2 = &ptr1; test( ② ); return 0; } Which of the following should be filled into ② if ① is int &var ? A. &a or *ptr1 B. a or ptr1 C. a or *ptr1 D. &a or ptr1

c. a or *ptr1

void test( ① ){ // code } int main () { int a = 0; int* ptr1 = &a; int** ptr2 = &ptr1; test( ② ); return 0; } Which of the following should be filled into ① if ② is &ptr2 ? A. int var B. int *var C. int **var D. int ***var

d. int ***var

A function can have no parameters, one parameter, or many parameters and can return only one value.

true

A function prototype is a declaration, but not a definition

true

It is possible for a void function to have parameters.

true

True(A)/False(B) You may use strcmp() to compare all the elements between two C-style strings.

true

When an if statement is placed within the conditionally-executed code of another if statement, this is known as nesting.

true

T/F: A recursive function must return a value.

False

A statement that causes a loop to terminate early is A. break B. terminate C. re-iterate D. continue E. None of above

A

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

A and B are correct

What is the output of the following code? int array[2][3]; for(int i = 0; i < 2; i++){ for(int j = 0; j < 3; j++){ array[i][j] = i + j; cout << array[i][j] << " "; } cout << endl; } A. 0 1 2 1 2 3 B. 0 1 1 2 2 3 C. 0 0 0 1 1 1 D. 0 0 1 1 2 2

A. 0 1 2 1 2 3

void func ( ① , int a, int b) { //code } int main(){ int **two_d_arr = new int*[3]; for(int i = 0; i < 3; i++){ two_d_arr[i] = new int[4]; for(int j = 0; j < 4; j++) two_d_arr[i][j] = 1; } func ( ② , 3, 4); ________③_________ //Free memory return 0; } C++ is row major, meaning that a two-dimensional array will be laid out by rows in memory. How many rows and columns does two_d_arr have, based on the code above? A. 3 rows, 4 columns B. 4 rows, 3 columns C. Cannot be defined D. None of the above

A. 3 rows, 4 columns

What is wrong with the following code? int *p1, *p2; p1 = new int; p2 = new int; *p1 = 11; *p2 = 0; delete p2; p2 = p1; delete p1; A. nothing B. cannot reuse p2 after deleting it C. You have a memory leak, since p2 is not deleted after re-assigned. D. B and C

A. nothing

Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr; A. The value in the variable whose address is stored in ptr. B. The string "*ptr". C. The address of the variable whose address is stored in ptr. D. The address of the variable stored in ptr.

A. The value in the variable whose address is stored in ptr.

void read_strings( ③ ____ , int size) { for (int i = 0; i < size; i++) cin >> array[i]; } int main(){ string *arr = new string [5]; read_strings( ① , 5); //Function call ________②_________ //Free memory return 0; } Assume ① - ③ are correct, is each element in arr filled after calling read_strings? A. Yes. The memory address of the array has been passed into the function, and therefore we can change the content in the original array. B. No. We passed the copy of the array into read_strings, and thus the content in the original array does not change.

A. Yes. The memory address of the array has been passed into the function, and therefore we can change the content in the original array

What is the output of the following code segment? string str = "Hello World"; cout << str.at(5) << endl; A. a space character B. o C. W D. Hello

A. a space character

void func ( ① , int a, int b) { //code } int main(){ int **two_d_arr = new int*[3]; for(int i = 0; i < 3; i++){ two_d_arr[i] = new int[4]; for(int j = 0; j < 4; j++) two_d_arr[i][j] = 1; } func ( ② , 3, 4); ________③_________ //Free memory return 0; } Which of the following will not give you a memory leak or segmentation fault at ③? A. for (int i = 0; i < 3; i++) delete [] two_d_arr[i]; delete [] two_d_arr; B. for (int i = 0; i < 4; i++) delete [] two_d_arr[i]; C. delete [] two_d_arr; D. delete [][] two_d_arr;

A. for (int i = 0; i < 3; i++) delete [] two_d_arr[i]; delete [] two_d_arr;

void func ( ① , int a, int b) { //code } int main(){ int **two_d_arr = new int*[3]; for(int i = 0; i < 3; i++){ two_d_arr[i] = new int[4]; for(int j = 0; j < 4; j++) two_d_arr[i][j] = 1; } func ( ② , 3, 4); ________③_________ //Free memory return 0; } Which of the following is valid for ① and ②? A. ① int** array ② two_d_arr B. ① int** array ② &two_d_arr C. ① int array[][] ② two_d_arr D. ① int array[3][4] ② two_d_arr

A. ① int** array ② two_d_arr

Which of the following statement is true about function overloading? A. Overloaded functions may have the same name, same parameters, and same return type. B. Overloaded functions may have the same name, same return type, but different parameters. C. Overloaded functions may have the same name, same parameters, but different return type. D. B and C

B. Overloaded functions may have the same name, same return type, but different parameters.

Which of the following is true about this statement: sum += *array++; A. This statement is illegal in C++. B. This statement adds the dereferenced pointer's value to sum, then increments the address stored in the pointer. C. This statement increments the dereferenced pointer's value by one, then adds that value to sum. D. None of the above

B. This statement adds the dereferenced pointer's value to the sum, then increments the address stored in the pointer

void read_strings( ③ ____ , int size) { for (int i = 0; i < size; i++) cin >> array[i]; } int main(){ string *arr = new string [5]; read_strings( ① , 5); //Function call ________②_________ //Free memory return 0; } Which of the following is valid at ①? A. arr[5] B. arr C. *arr D. &arr

B. arr

If your program makes too many recursive function calls, your program will cause a _______. A. compiler error B. runtime error C. syntax error D. heap error

B. runtime error

How many times will the following loop display "Looping!"? for (int i = 5; i > 0; i--) for (int j = 0; j <= i; j++) cout << "Looping!" << endl; A. 15 B. 20 C. 21 D. an infinite number of times

B.20

What does the following statement do? int *ptr = NULL; A. Create a variable named *ptr that will store an integer value. B. Create a variable named *ptr that will store an asterisk (*) and an integer value. C. Create a pointer variable named ptr that will store the address of an integer variable. D. Create a variable named *ptr that will store the NULL value

C. Create a pointer variable named ptr that will store the address of an integer variable.

Three primary activities of a program are: A. Variable definitions, operators, lists of key words B. Lines, statements, punctuation C. Input, processing, output D. Integer, floating-point, character definitions

C. input, processing, output

void test( ① ){ // code } int main () { int a = 0; int* ptr1 = &a; int** ptr2 = &ptr1; test( ② ); return 0; } Which of the following should be filled into ① if ② is &ptr1 ? A. int var B. int *var C. int **var D. int ***var

C. int **var

Which of the following will randomly generate number from 90 - 100 (inclusive)? A. int num = rand() % 101 - 10; B. int num = rand() % 100 - 10; C. int num = rand() % 11 + 90; D. int num = rand() % 10 + 90;

C. int num = rand() % 11 + 90;

________ are used to translate each source code instruction into the appropriate machine language instruction. A. Modules B. Runtime libraries C. Compilers D. Preprocessor directives

C.compilers

Which of the following is evaluated first, given the expression: A && B || C && !D A. A && B B. B || C C. C && !D D. !D

D. !D

void test( ① ){ // code } int main () { int a = 0; int* ptr1 = &a; int** ptr2 = &ptr1; test( ② ); return 0; } Which of the following should be filled into ① if ② is &ptr2 ? A. int var B. int *var C. int **var D. int ***var

D. &ptr1 or ptr2

void test( ① ){ // code } int main () { int a = 0; int* ptr1 = &a; int** ptr2 = &ptr1; test( ② ); return 0; } Which of the following should be filled into ② if ① is int *var ? A. &a or *ptr1 B. a or ptr1 C. a or *ptr1 D. &a or ptr1

D. &ptr1 or ptr2

What is the output of the following statement? cout << (float) (5 * (9 % 4) / 2) + 2.5 << endl; A. 5 B. 15 C. 2.5 D. 4.5

D. 4.5

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

D. 5

void read_strings( ③ ____ , int size) { for (int i = 0; i < size; i++) cin >> array[i]; } int main(){ string *arr = new string [5]; read_strings( ① , 5); //Function call ________②_________ //Free memory return 0; } Which of the following is valid at ③? A. string *array; B. string &array; C. string array[]; D. A and C;

D. A and C;

A collection of statements that performs a specific task is a(n) A. loop B. variable C. constant D. function

D. Function

What is the output of the following code segment? int x = 5; if (x = 2) cout << "This is true!" << endl; else cout << "This is false!" << endl; cout << "That's all, folks!" << endl; A. This is true! B. This is false! C. This is false! That's all, folks! D. This is true! That's all, folks!

D. This is true! That's all, folks!

A function is executed when it is A. defined B. prototyped C. declared D. called

D. called

Which of the following is a valid C++ array declaration? A. int []array; B. float payments[10.23]; C. string numbers[]; D. double scores[25];

D. double scores[25];

If you need to write a function that will compute the cost of some candy, where each piece costs 25 cents, which would be an appropriate function declaration? A. float calculateCost(char name); B. char calculateCost(int count); C. float calculateCost int count; D. float calculateCost(int count);

D. float calculateCost(int count);

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. lightest is weight2 regardless of the if statement.

Which of the following statement is valid C++ code? A. int num = 0; int ptr = &num; B. float *ptr = 10.04; C. char word1[5] = "word"; char word2[10] = "computer"; word1 = word2; D. None of the above

D. none of the above

A character literal is __________, whereas a string literal is __________. A. enclosed in quotation marks, enclosed in brackets B. enclosed in brackets, enclosed in quotation marks C. enclosed in double quotation marks, enclosed in single quotation marks D. enclosed in single quotation marks, enclosed in double quotation marks

D.enclosed in single quotation marks, enclosed in double quotation marks

T/F C++ performs array bounds checking, making it impossible for you to assign a pointer the address of an element out of the boundaries of an array.

False

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

False

T/F The name of an array stores the value of the first array element.

False

T/F: A recursive function can have at most one base case

False

. What is the value of the expression: !false || (true && false)

True

A variable's __________ is the part of the program that has access to the variable. A. Scope B. Value C. Data type D. Assignment

a.scope

void func ( ① , int a, int b) { //code } int main(){ int **two_d_arr = new int*[3]; for(int i = 0; i < 3; i++){ two_d_arr[i] = new int[4]; for(int j = 0; j < 4; j++) two_d_arr[i][j] = 1; } func ( ② , 3, 4); ________③_________ //Free memory return 0; } . According to the code above, which of the following is correct? A. The double pointer two_d_arr, the row pointers, and the columns are all on the stack B. The double pointer two_d_arr and row pointers is on the stack, and the columns are on the heap C. The double pointer two_d_arr is on the stack, and the row pointers and columns are on the heap D. The double pointer two_d_arr, the row pointers, and the columns are all on the heap

c.The double pointer two_d_arr is on the stack, and the row pointers and columns are on the heap

Which of the following is not a valid C++ identifier? A. April2019 B. employee_num C. _2user D. 2user

d. 2user

In C++, the expression if (x < y < z) will determine whether x is less than y and less than z.

false

T/F In a function with pass-by-reference parameters, the values of the actual arguments are passed to the function.

false

The default section is required in a switch statement.

false

There are 8 bytes in one bit

false

True(A)/False(B) Elements in a two-dimensional static array have contiguous memory on the heap

false

cin reads a line of input, including leading and embedded spaces, and stores it in a string object.

false


Set pelajaran terkait

Econ Final Chapter 14 Price Discrimination and Pricing Strategy

View Set

4.04 Quiz War's End - American World History

View Set

HTML5, CSS3, and JavaScript: Tutorial 12 test questions

View Set

Business Communication Essentials Ch 2

View Set

DSM ch. 10, DSM (ch. 6,, A&P Lecture Ch. 8,9,11, DSM ch 9, DSM ch. 8, HW (CH. 6-10, DSM (ch 7,

View Set