C++ Quiz 10
5) ________ can be used as pointers. A) Array names B) Numeric constants C) Punctuation marks D) All of these E) None of these
A
12) A function may return a pointer, but the programmer must ensure that the pointer ________. A) still points to a valid object after the function ends B) has not been assigned an address C) was received as a parameter by the function D) has not previously been returned by another function E) None of these
A
19) Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr; A) The value stored in the variable whose address is contained in ptr. B) The string "*ptr". C) The address of the variable stored in ptr. D) The address of the variable whose address is stored in ptr. E) None of these
A
2) To test whether a character is a printable character, use this function. A) isprint B) isprintable C) isprintok D) isoktoprint E) None of these
A
21) Not all arithmetic operations may be performed on pointers. For example, you cannot ________ or ________ a pointer. A) multiply, divide B) add, subtract C) +=, -= D) increment, decrement E) None of these
A
25) After the following statement executes, what value is stored in the variable num? num = atoi("1000"); A) 1000 B) 999 (1000 minus 1 for the null terminator) C) "1000" D) "thousand" E) None of these
A
26) If you are using an older compiler that does not support the C++ 11 standard, you should initialize pointers with ________. A) the integer 0, or the value NULL B) the null terminator '\0' C) a nonzero value D) All of these E) None of these
A
32) What will the following code output? int number = 22; int *var = &number; cout << var << endl; A) The address of the number variable B) 22 C) An asterisk followed by 22 D) An asterisk followed by the address of the number variable
A
7) In C++ 11, the ________ key word was introduced to represent the address 0. A) nullptr B) NULL C) weak_ptr D) All of these E) None of these
A
7) Using a linear search to find a value that is stored in the last element of an array of 20,000 elements, ________ element(s) must be compared. A) 20,000 B) only the first C) only half D) 2000 E) None of these
A
8) This function accepts a C-string as an argument and converts the string to a long integer. A) atol B) strlong C) strtolong D) stringlong E) None of these
A
9) Data that is sorted in ascending order is ordered ________. A) from lowest to highest value B) from highest to lowest value C) always with a binary sort algorithm D) always with a linear sort algorithm E) None of these
A
6) The strcpy function's arguments are: A) two C-strings B) two addresses C) three pointers D) one array and one pointer E) None of these
B
11) This function will return true if its argument is a printable character other than a digit, letter, or space. A) isprint B) ispunct C) ischar D) isnotdls E) None of these
B
12) The ________ is adequate for searching through small arrays. A) binary search B) linear search C) unary search D) bubble sort E) None of these
B
16) What will the following statement output? cout << &num1; A) The value stored in the variable called num1 B) The memory address of the variable called num1 C) The number 1 D) The string "&num1" E) None of these
B
18) To determine whether a character is whitespace, use this function. A) iswhite B) isspace C) iswhitespace D) isblank E) None of these
B
19) To change a character argument from lower to upper case, use this function. A) isupper B) toupper C) tolarge D) fromlower E) None of these
B
20) What is the output of the following statement? cout << tolower(toupper('Z')) << endl; A) upper case Z B) lower case z C) a lower case z followed by an upper case Z D) a compiler error E) None of these
B
24) Dynamic memory allocation occurs ________. A) when a new variable is created by the compiler B) when a new variable is created at runtime C) when a pointer fails to dereference the right variable D) when a pointer is assigned an incorrect address E) None of these
B
24) This function accepts pointers to two C-strings and an integer argument, which indicates how many characters to copy from the second C-string to the first. A) strcpy B) strncpy C) copystring D) strintcpy E) None of these
B
27) Every byte in the computer's memory is assigned a unique ________. A) pointer B) address C) dynamic allocation D) name E) None of these
B
29) A pointer variable may be initialized with ________. A) any non-zero integer value B) a valid address in the computer's memory C) an address less than 0 D) A and C only E) None of these
B
29) Which of the following lines of code defines an array of C-strings that will hold 49 characters and the null terminator? A) char [49]; B) char str[50]; C) char[50] str; D) character str[50]; E) None of the above
B
30) The statement: char var1 = tolower('A'); will result in: A) var1 storing the character value 'A'. B) var1 storing the ASCII value for lower case 'a'. C) A is output to the monitor. D) a is output to the monitor. E) None of these
B
31) What will the following code output? int number = 22; int *var = &number; cout << *var << endl; A) The address of the number variable B) 22 C) An asterisk followed by 22 D) An asterisk followed by the address of the number variable
B
35) To help prevent memory leaks from occurring in C++ 11, a ________ automatically deletes a chunk of dynamically allocated memory when the memory is no longer being used. A) null pointer B) smart pointer C) dereferenced pointer D) A and C only E) None of these
B
7) A library function that can find one C- string inside another is: A) strcmp B) strstr C) strfind D) strsearch E) None of these
B
Starting Out with C++ from Control Structures to Objects, 8e (Gaddis) Chapter 8 Searching and Sorting Arrays 8.1 Multiple Choice Questions 1) A ________ algorithm is a method of locating a specific item of information in a larger collection of data. A) sort B) search C) standard D) linear E) None of these
B
Starting Out with C++ from Control Structures to Objects, 8e (Gaddis) Chapter 9 Pointers 9.1 Multiple Choice Questions 1) The ________, also known as the address operator, returns the memory address of a variable. A) asterisk ( * ) B) ampersand ( & ) C) percent sign (%) D) exclamation point ( ! ) E) None of these
B
3) True/False: When using the strcat function, you must be careful not to overwrite the bounds of an array.
TRUE
4) True/False: It is legal to subtract a pointer variable from another pointer variable.
TRUE
5) True/False: C++ does not perform array bounds checking, making it possible for you to assign a pointer the address of an element out of the boundaries of an array.
TRUE
5) True/False: The itoa function is similar to atoi, but it works in reverse.
TRUE
6) True/False: A pointer can be used as a function argument, giving the function access to the original argument.
TRUE
8) True/False: Assuming myValues is an array of int values, and index is an int variable, both of the following statements do the same thing. cout << myValues[index] << endl; cout << *(myValues + index) << endl;
TRUE
9) True/False: In C++ 11, you can use smart pointers to dynamically allocate memory and not worry about deleting the memory when you are finished using it.
TRUE
10) Look at the following statement: sum += *array++; This statement ________. A) is illegal in C++ B) will always result in a compiler error C) assigns the dereferenced pointer's value, then increments the pointer's address D) increments the dereferenced pointer's value by one, then assigns that value E) None of these
C
15) When this is placed in front of a variable name, it returns the address of that variable. A) asterisk ( * ) B) conditional operator C) ampersand ( & ) D) semicolon ( ; ) E) None of these
C
22) To use the strlen function in a program, you must also write #include ________. A) <strlen> B) <iostring> C) <cstring> D) <stringlib> E) None of these
C
25) The following statement: int *ptr = new int; A) results in a compiler error B) assigns an integer less than 32767 to the variable named ptr C) assigns an address to the variable named ptr D) creates a new pointer named int E) None of these
C
30) If a variable uses more than one byte of memory, for pointer purposes its address is ________. A) the address of the last byte of storage B) the average of the addresses used to store the variable C) the address of the first byte of storage D) general delivery E) None of these
C
34) Look at the following code: int numbers[] = {0, 1, 2, 3, 4 }; int *ptr = numbers; ptr++; After this code executes, which of the following statements is true? A) ptr will hold the address of numbers[0]. B) ptr will hold the address of the 2nd byte within the element numbers[0]. C) ptr will hold the address of numbers[1]. D) This code will not compile.
C
6) Array elements must be ________ before a binary search can be performed. A) summed B) set to zero C) sorted D) positive numbers E) None of these
C
10) Which statement converts the string "10" to the integer value 10? A) itoa("ten") B) atoi("ten") C) atoi("10") D) itoa(10) E) None of these
C
11) Use the delete operator only on pointers that were ________. A) never used B) not correctly initialized C) created with the new operator D) dereferenced inappropriately E) None of these
C
11) When an array is sorted from highest to lowest, it is said to be in ________ order. A) reverse B) forward C) descending D) ascending E) None of these
C
12) The C-string company[12] can hold ________. A) twelve characters B) thirteen characters C) eleven characters and the null terminator D) twelve characters and the null terminator E) None of the above
C
13) ________ algorithms are used to arrange random data into some order. A) Standard search B) Linear C) Sorting D) Binary search E) None of these
C
14) Which of the following statements deletes memory that has been dynamically allocated for an array? A) int array = delete memory; B) int delete[ ]; C) delete [] array; D) new array = delete; E) None of these
C
15) A practical application of this function is to allow a user to enter a response of 'y' or 'Y' to a prompt. A) tolower B) toupper C) A or B D) ignorecase E) None of these
C
16) This function accepts a C-string containing a number as its argument and returns the integer equivalent. A) strToInt B) itoa C) atoi D) int_from E) None of these
C
17) A pointer variable is designed to store ________. A) any legal C++ value B) only floating-point values C) a memory address D) an integer E) None of these
C
18) Look at the following statement: int *ptr = nullptr; In this statement, what does the word int mean? A) The variable named *ptr will store an integer value. B) The variable named *ptr will store an asterisk and an integer value. C) ptr is a pointer variable that will store the address of an integer variable. D) All of these E) None of these
C
2) The advantage of a linear search is its ________. A) complexity B) efficiency C) simplicity D) speed E) None of these
C
2) With pointer variables, you can ________ manipulate data stored in other variables. A) never B) seldom C) indirectly D) All of these E) None of these
C
20) The ________ and ________ operators can be used to increment or decrement a pointer variable. A) addition, subtraction B) modulus, division C) ++, -- D) All of these E) None of these
C
21) In C++, a C-string is a sequence of characters stored in consecutive memory, terminated by a ________. A) period B) space C) null character D) semicolon E) None of these
C
23) This function concatenates the contents of one C-string with another C-string. A) strcopy B) strappend C) strcat D) stradd E) None of these
C
28) To define a C-string that will store students' last names of up to 25 characters in length, which is an appropriate statement? A) char lastName[25]; B) string lastName[25]; C) char lastName[26]; D) string lastName[24]; E) None of these
C
3) A(n) ________ search is more efficient than a ________ search. A) character, string B) integer, double C) binary, linear D) linear, binary E) None of these
C
3) This is the escape sequence representing the null terminator. A) \n B) \t C) \0 D) nullptr E) None of these
C
4) When you work with a dereferenced pointer, you are actually working with ________. A) a variable whose memory has been allocated B) a copy of the value pointed to by the pointer variable C) the actual value of the variable whose address is stored in the pointer variable D) All of these E) None of these
C
5) The ________ sort usually performs fewer exchanges than the ________ sort. A) bubble, selection B) binary, linear C) selection, bubble D) ANSI, ASCII E) None of these
C
5) This function accepts a pointer to a C-string as an argument, and it returns the length of the C-string (not including the null terminator). A) numchar B) strlength C) strlen D) countstring E) None of these
C
3) True/False: In the average case, an item is just as likely to be found near the beginning of an array as near the end.
TRUE
6) The contents of pointer variables may be changed with mathematical statements that perform ________. A) all mathematical operations that are legal in C++ B) multiplication and division C) addition and subtraction D) B and C E) None of these
C
8) A(n) ________ search uses a loop to sequentially step through an array. A) binary B) unary C) linear D) relative E) None of these
C
9) When the less than ( < ) operator is used between two pointer variables, the expression is testing whether ________. A) the value pointed to by the first is less than the value pointed to by the second B) the value pointed to by the first is greater than the value pointed to by the second C) the address of the first variable comes before the address of the second variable in the computer's memory D) the first variable was declared before the second variable E) None of these
C
17) To determine whether a character entered is a letter of the alphabet, use this function. A) isdigit B) fromkeyboard C) alphaok D) isalpha E) None of these
D
13) Look at the following statement. if (!isdigit(var1)) The expression being tested by this statement will evaluate to true if var1 is: A) an alphabetic character B) 9 C) a symbol such as $ or & D) both A and C E) None of these
D
14) "Whitespace" encompasses which of the following? A) tab B) newline C) space D) All of these E) None of these
D
22) Which statement displays the address of the variable num1? A) cout << num1; B) cout << *num1; C) cin >> &num1; D) cout << &num1; E) None of these
D
23) The following statement: cin >> *num3; A) stores the keyboard input into the variable num3 B) stores the keyboard input into the pointer called num3 C) is illegal in C++ D) stores the keyboard input into the variable pointed to by num3 E) None of these
D
26) To change a lower case character to an upper case character, use this function. A) atoi B) itoa C) ltou D) toupper E) None of these
D
3) The statement: int *ptr = nullptr; has the same meaning as ________. A) int ptr = nullptr; B) *int ptr = nullptr; C) int ptr* = nullptr; D) int* ptr = nullptr; E) None of these
D
33) What will the following code output? int *numbers = new int[5]; for (int i = 0; i <= 4; i++) *(numbers + i) = i; cout << numbers[2] << endl; A) Five memory addresses B) 0 C) 3 D) 2 E) 1
D
4) A binary search begins with the ________ element of an array. A) first B) last C) largest D) middle E) None of these
D
4) The null terminator stands for this ASCII code. A) 57 B) 100 C) 1000 D) 0 E) None of these
D
8) What does the following statement do? double *num2; A) Declares a double variable named num2. B) Declares and initializes an pointer variable named num2. C) Initializes a variable named *num2. D) Declares a pointer variable named num2. E) None of these
D
9) This function converts a C-string to an integer and returns the integer value. A) atoint B) strtoint C) strint D) atoi E) None of these
D
Starting Out with C++ from Control Structures to Objects, 8e (Gaddis) Chapter 10 Characters, C-Strings, and More About the string Class 10.1 Multiple Choice Questions 1) To test whether a character is a numeric digit character, use this function. A) isnumber B) notAlpha C) isnumeric D) isdigit E) None of these
D
27) This library function reverses the order of a C-string. A) reverstr B) strrev C) reversit D) backward E) None of these
E
10) Regardless of the algorithm being used, a search through an array is always performed ________. A) from lowest to highest element B) from highest to lowest element C) beginning with the middle element D) using a binary search E) None of these
E
13) Which of the following statements is not valid C++ code? A) int ptr = &num1; B) int ptr = int *num1; C) float num1 = &ptr2; D) All of these are valid. E) All of these are invalid.
E
28) When you pass a pointer as an argument to a function, you must ________. A) declare the pointer variable again in the function call B) dereference the pointer variable in the function prototype C) use the #include<func_ptr.h> statement D) not dereference the pointer in the function's body E) None of these
E
10.2 True/False Questions 1) True/False: A test using the isupper function will return false if the argument is an uppercase character.
FALSE
4) True/False: A linear search can only be implemented with integer values.
FALSE
4) True/False: The C++ compiler performs strict array bounds checking when it encounters an array of characters.
FALSE
5) True/False: Before you can perform a selection sort, the data must be stored in ascending order.
FALSE
6) True/False: Before you can perform a bubble sort, the data must be stored in descending order.
FALSE
7) True/False: The ampersand (&) is used to dereference a pointer variable in C++.
FALSE
7) True/False: Using a binary search, you are more likely to find an item than if you use a linear search.
FALSE
8.2 True/False Questions 1) True/False: The bubble sort is an easy way to arrange data into ascending order, but it cannot arrange data into descending order.
FALSE
9.2 True/False Questions 1) True/False: With pointer variables you can access, but you cannot modify, data in other variables.
FALSE
10) True/False: To use any of the smart pointers in C++ 11, you must #include the memory header file with the following directive: #include <memory>
TRUE
2) True/False: An array name is a pointer constant because the address stored in it cannot be changed during runtime.
TRUE
2) True/False: The isdigit function will return a true if its argument is a digit between 0 and 9.
TRUE
2) True/False: The number of comparisons made by a binary search is expressed in powers of two.
TRUE
3) True/False: In C++ 11, the nullptr key word was introduced to represent the address 0.
TRUE