ch 7

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Using pictures can help determine the structure of data and connections of data within a program. How are pointers usually represented in a diagram? Select one: a. Data storage locations and pointers are both drawn as arrows. b. Data storage locations and pointers are both drawn as boxes. c. Data storage locations are represented as boxes; pointers are drawn as arrows with the tip pointing to the boxes. d. Data storage locations are represented as arrow; pointers are drawn as boxes.

b a

Given the following code snippet, what is true about the statement ptr_num++? int num[5] = { 1, 2, 3, 4, 5 }; int* ptr_num = num; ptr_num++; Select one: a. ptr_num now points to num[0]; b. ptr_num now points to num[1]; c. ptr_num++ results in a run-time error. d. ptr_num++ results in a compilation error.

b. ptr_num now points to num[1];

Which of the variables in the following code snippet are valid pointer variables? int* ptr1, ptr2; int x = 5; ptr1 = &x; ptr2 = &x; Select one: a. ptr1 and ptr1 b. ptr1 only c. ptr2 only d. None of the variables are valid pointers.

a b d

What is the output of the following code snippet, assuming a correct program surrounds it? struct Point2D { double x; double y; } Point2D p1(2,3); cout << p1.x << " " << p1.y; Select one: a. x2 y3 b. Possible runtime error c. 2 3 d. No output, it won't compile

a c b

Given the following declaration of the variables p1 and p2, which code fragment prints "Maximum" if the value of x in the variable p1 is larger than the value of x in the variable p2? struct Point2D { double x; double y; }; Point2D *p1; Point2D p2; p1 = new Point2D; Select one: a.if (p1.x > p2->x) { cout << "Maximum";} b.if (p1->x > p2->x) { cout << "Maximum";} c.if (p1->x > p2.x) { cout << "Maximum";} d.if (p1.x > p2.x) { cout << "Maximum";}

a d b

The code snippet below creates what kind of a problem? int main() { string* myword[20]; for (int i = 0; i < 14; i++) { myword[i] = new string("YouHadMeAtHello"); } cout << *myword[15] << endl; return 0; } Select one: a. Off by one error b. Memory leak c. Null pointer dereference d. Array index out of bounds

a d b

Consider the following code snippet. int* age = new int; *age = 20; cout << age << endl; delete age; *age = *age * 3; cout << age << endl; Which of the following statements is correct? Select one: a. There are compilation errors. b. The age pointer is being used after the memory it is pointing to has been deleted. c. The age pointer was never initialized. d. There is no error in the code snippet.

a d c

How can you access the character 'n' in the following C++ string? string student_name = "Susan"; Select one: a. char last = student_name.substr(4, 5); b. char last = student_name[4]; c. char last = student_name.substr(5); d. char last = student_name[5];

a d c

What is the output from the code snippet below? int* index[5]; int data[10] = {4, 8, 1, 3, 5, 9, 3, 2, 6, 0}; int i = 0; int* p = &data[9]; for (int i = 0; i < 5; i++) { index[i] = p; p--; } for (int i = 0; i < 5; i++) { cout << *index[i] << " "; } Select one: a. 0 6 2 3 9 b. 5 3 1 8 4 c. 9 3 2 6 0 d. 4 8 1 3 5

a. 0 6 2 3 9

What library must be included by your code to use functions that copy and concatenate character arrays? Select one: a. <cstring> b. <string> c. <cstring.h> d. <c-string>

a. <cstring>

Describe how the two arrays index and data are related to one another after the code snippet below executes: int* index[5]; int data[10] = {4, 8, 1, 3, 5, 9, 3, 2, 6, 0}; int i = 0; int* p = &data[9]; for (int i = 0; i < 5; i++) { index[i] = p; p--; } Select one: a. The elements of index point to the elements of data as follows:index[0] points to data[9]index[1] points to data[8]etc. for all five elements of index b. The elements of data point to the elements of index as follows:data[0] points to index[0]data[1] points to index[1]etc. for the first five elements of data c. The elements of data point to the elements of index as follows:data[0] points to index[9]data[1] points to index[8]etc. for the first five elements of data d. The elements of index point to the elements of data as follows:index[0] points to data[0]index[1] points to data[1]etc. for all five elements of index

a. The elements of index point to the elements of data as follows:index[0] points to data[9]index[1] points to data[8]etc. for all five elements of index

You have declared the following string variable: string name_manager = "Susan"; Which of the following statements is a legal assignment statement? Select one: a. const char* cstr = name_manager.c_str(); b. const char* cstr = c_str(name_manager); c. string* strp = name_manager.c_str(); d. string str = c_str(name_manager);

a. const char* cstr = name_manager.c_str();

Which of the following statements represents a valid declaration of a C++ string variable? Select one: a. string str = "aeiou"; b. string str = null; c. string str = ''; d. string str = { 'a', 'e', 'i', 'o', 'u' };

a. string str = "aeiou";

The code snippet below creates what kind of a problem? int main() { string* myword; for (int i = 0; i < 14; i++) { myword = new string("YouHadMeAtHello"); } cout << *myword << endl; return 0; } Select one: a. Array index out of bounds b. Null pointer dereference c. Off by one error d. Memory leak

b a c

What does the new operator do in the following statement? double* num = new double[10]; Select one: a. It allocates enough memory for 10 pointers. b. This is not a legal statement, it will generate a compiler error. c. It allocates enough memory for a double value and initializes it with 10. d. It allocates an array of size 10, and yields a pointer to the starting element.

b a c

What is the output of the following code snippet? double balance = 10000; const double* p = *p = *p - 1000; cout << *p; Select one: a. The memory address of the variable balance will be printed. b. 9000 c. 10000 d. It will yield a compiler error.

b c a

What is the output of the following code snippet? int* c[4]; for (int i = 0; i < 4; i++) { c[i] = new int[i + 1]; for (int j = 0; j <= i; j++) *c[i][j] = i * j; } cout << *c[0][1]; Select one: a. Unpredictable due to bounds error b. 1 c. 0 d. Won't compile due to syntax error

b c a

Which location of the array num does ptr point to right after you assign an array to a pointer variable, as shown in the following code snippet? int num[5]; int* ptr = num; Select one: a. num[1] b. num[5] c. You cannot assign an array to a pointer. d. num[0]

b c a

What is a "dangling pointer"? Select one: a. A pointer which has not been initialized. b. A pointer which is used after the statement to delete it. c. A pointer whose value varies at runtime. d. A pointer which points to the end of an array instead of the beginning.

b. A pointer which is used after the statement to delete it.

Which statement is true about an uninitialized pointer? Select one: a. It always has the value null b. It may point to a memory location that a program does not own c. It will never cause a problem in a program d. It will always point to a memory location that a program owns

b. It may point to a memory location that a program does not own

What is the output of the following code snippet? int* c[4]; for (int i = 0; i < 4; i++) { c[i] = new int[i + 1]; for (int j = 0; j <= i; j++) c[i][j] = i*j; } cout << c[0][1]; Select one: a. 01 b. Unpredictable due to bounds error c. 0 d. 1

b. Unpredictable due to bounds error

A pointer describes which of the following? Select one: a. When a certain value is in memory b. Where a certain value in memory is c. What a certain value in memory is d. The size of a certain value in memory

b. Where a certain value in memory is

What is the output of the following code snippet? int main() { int i = 5; char* name = "Philip Roger"; cout << name[i] << endl; return 0; } Select one: a. Roger b. p c. ip d. The program does not compile due to a syntax error.

b. a d

What is a "memory leak" in computer programs? Select one: a. Allocating "new" memory dynamically without a corresponding "delete" b. Accessing null pointers c. Using out-of-bounds array indices d. When programs terminate due to run-time errors and their memory is not reclaimed by the operating system

c a

What is the output of the following code snippet? int num = 0; int* ptr = &num; num = 5; *ptr = *ptr + 5; cout << num << " " << *ptr << endl; Select one: a. 5 5 b. 10 5 c. 10 10 d. 5 10

c a b

Which of the following operators is used to reclaim memory in the free store? Select one: a. reclaim b. erase c. recycle d. delete

c a b

Consider the code snippet below. int arr[5] = { 1, 2, 3, 4, 5 }; Which of the following is the value of *(arr + 2)? Select one: a. 2 b. 3 c. 4 d. 1

c a d

What does the function f do? struct Point2D { double x; double y; }; struct Triangle { Point2D v1; Point2D v2; Point2D v3; }; void f(Triangle& t) { int temp = 12.5; temp = t.v1.x; t.v1.x = t.v1.y; t.v1.y = temp; } int main () { Triangle mytri; mytri.v1.x = 1.0; mytri.v1.y = 22.5; f(mytri); } Select one: a. Swaps value of x in vertex 1 with value of x in vertex 2, for an argument of type Triangle b. Swaps values of x and y in vertex 1 of an argument of type Triangle c. Sets all x,y values in all vertices of an argument of type Triangle d. Initializes value of x in vertex 1 of an argument of type Triangle

c d a

What is output of the code snippet given below? string name = "Oscar DeGama"; cout << name[7] << name[8] << name[9] << endl; Select one: a. There is no output due to compilation error. b. eGa c. DeG d. Gam

c d a

Which of the following statements is true about pointers? Select one: a. Pointers do not have any address location of their own. b. Pointers contain address locations of variables. c. Pointers contain values as well as addresses of variables. d. Pointers occupy permanent storage locations inside the hard disk.

c d a

What is the output of the following code snippet? int main() { char mychar = 'W'; char *p; { char mychar = 'X'; p = &mychar; } cout << mychar << *p << endl; return 0; } Select one: a. Unpredictable result b. XX c. WW d. WX

c d b

Which of the following statements hold true when you want to pass an array to a function? Select one: a. The function cannot make changes to the array. b. You cannot pass an array to a function. c. A function always receives the starting address of the array. d. You have to pass all the values of the array as parameters to the function.

c. A function always receives the starting address of the array.

Examine the following code snippet. Which statement best describes p_array? int* p_array[10]; Select one: a. It is a pointer to an array of 10 integers. b. It is a pointer to an integer initialized with 10. c. It is an array of 10 integer pointers. d. There is a compilation error.

c. It is an array of 10 integer pointers.

A programmer writes the sum_array function, as shown in the following code snippet. The programmer was surprised to find incorrect output from the main program. What is wrong with the following code snippet? int sum_array(int arr[], int size) { int result = 0; for (int i = 0; i < size; i = i + 2) { result = result + arr[i]; } return result; } int main() { int sum_array(int arr[], int size); int num[5] = { 1, 2, 3, 4, 5 }; int sum = sum_array(num, 7); cout << "Sum: " << sum << endl; return 0; } Select one: a. The main function does not initialize the num array correctly before passing it to the sum_array function. b. It is not possible to pass an array as an argument to a function. c. The main function passes an incorrect size for the num array. d. The sum_array function works only with an array with an even number of values.

c. The main function passes an incorrect size for the num array.

Given the following code, describe the diagram that would represent the situation with the variables: int A = 10; int B = 42; int* p; int* q; int* x; p = &A; q = &B; x = p; p = q; Select one: a. Two boxes - one with 10, one with 42 - and three arrows which all point to 10 b. Two boxes - one with 10, one with 42 - and three arrows which all point to 42 c. Two boxes - one with 10, one with 42 - and one arrow pointing to 10 and two pointing to 42 d. Two boxes - one with 10, one with 42 - and two arrows pointing to 10 and one pointing to 42

c. Two boxes - one with 10, one with 42 - and one arrow pointing to 10 and two pointing to 42

You have declared the following string variable: string name = "Houdini"; Which of the following statements is a legal assignment statement? Select one: a. string str = c_str(name); b. string* strp = name.c_str(); c. const char* cstr = name.c_str(); d. const char* cstr = c_str(name);

c. const char* cstr = name.c_str();

In order to convert a string containing digits (such as converting the string "1249" to the integer 1249), which of the following expressions can be used? Assume the following variables have been defined: string mystring = "1249"; int myInt; Select one: a. myint = mystring.atoi(); b. myint = mystring.c_str(); c. myint = atoi(mystring.c_str()); d. myint = mystring.convert();

c. myint = atoi(mystring.c_str());

Consider the code snippet below. int ch = 100; int* ptr = &ch; Which of the following observations are true based on the code snippet? Select one: a. &ptr gives the value of ch b. &ptr gives the address of ch c. ptr stores the address of ch d. ptr stores the value of ch

c. ptr stores the address of ch

What is the output of the following code snippet? int* speed = NULL; cout << *speed << endl; Select one: a. NULL b. There are no compilation errors but when the code snippet is run, it results in an illegal data access error. c. There is no output due to a compilation error. d. 0

d a c

Which of the following is a common pointer error? Select one: a. Dereferencing a pointer b. Using a pointer that has not been initialized c. Setting a pointer variable to NULL d. Setting a pointer variable to a new value

d a c

Examine the following code snippet. Which statement best describes c[]? int* c[4]; for (int i = 0; i < 4; i++) { c[i] = new int[i + 1]; } Select one: a. It is a triangular array of int pointers. b. It is 4 arrays of ints. c. It is an array of 10 pointers. d. It is an array of 4 intergers.

d b c

Which of the following is true about the chars variable? char chars[] = "Hello World"; Select one: a. chars is an array of size 12, and the last value in the array is 0. b. chars is an array of size 12, and the last value in the array is "d". c. chars is an array of size 11, and the last value in the array is 0. d. chars is an array of size 11, and the last value in the array is "d".

d c

What is true about the statement given below? int* ptr_num; Select one: a. ptr_num contains the memory location of an integer variable. b. ptr_num can store two memory addresses simultaneously. c. ptr_num can also store a string value. d. ptr_num is an integer variable.

d c b

What is the output of the following code snippet? double* temperature = NULL; cout << temperature << endl; Select one: a. There is no output due to a compilation error. b. There are no compilation errors but when the code snippet is run, it results in an illegal data access error. c. "NULL" d. 0

d. 0

A programmer writes the sum_num_array function, as shown in the following code snippet. The programmer was surprised to find incorrect output from the main program. What is wrong with the following code snippet? int sum_num_array(int num_array[], int size) { int result = 0; for (int i = 0; i < size; i = i + 2) { result = result + num_array[i]; } return result; } int main() { int sum_num_array(int num_array[], int size); int num[6] = { 1, 2, 3, 4, 5, 6 }; int sum = sum_num_array(num, 8); cout << "Sum: " << sum << endl; return 0; } Select one: a. It is not possible to pass an array as an argument to a function. b. The sum_num_array function works only with an array with an even number of values. c. The main function passes an incorrect size for the num array. d. The main function does not initialize the num array correctly before passing it to the sum_num_array function.

d. The main function does not initialize the num array correctly before passing it to the sum_num_array function.

If you get a compiler error on the following code, what is the probable reason? int* ptr = nullptr; int x = 5; ptr = Select one: a. The first line should read int *ptr = nullptr;. b. The first line is unneeded, as ptr gets assigned a value in the 3rd line. c. You won't get a compiler error on this code. d. Your compiler does not support C++ 11.

d. Your compiler does not support C++ 11.

Which of the following is a legally correct way of declaring a variable that is a pointer to an integer? Select one: a. int& ptr b. int ptr* c. pointer<int> ptr d. int* ptr

d. int* ptr

Which of the variables in the following code snippet are valid pointer variables? int *ptr1, *ptr2; int x = 5; ptr1 = &x; ptr2 = &x; Select one: a. ptr2 only b. None of the variables are valid pointers. c. ptr1 and ptr2 d. ptr1 only

d. ptr1 only

Consider the code snippet below. int num = 500; int* ptr_num = &num; Which of the following observations are true based on the code snippet? Select one: a. &ptr_num gives the address of num b. &ptr_num gives the value of num c. ptr_num stores the value of num d. ptr_num stores the address of num

d. ptr_num stores the address of num

What is the output of the following code, assuming proper libraries have been included? char* s = "string1"; char t[20] = "string2"; strcat(t,s); cout << t; Select one: a. string1string2 b. No output, compiler error c. Unpredictable runtime error d. string2string1

d. string2string1


Kaugnay na mga set ng pag-aaral

Chemistry: Notable Elements - Set #2 (Official Set)

View Set

Chapter 13: psychological testing/ intelligence

View Set

Prep U Ch 32: Skin Integrity and Wound Care

View Set

TLB-Chapter 3: Health, Wellness, and Health Disparities

View Set

Certification Review | Chapter 12/Chapter 24: The Physical Exam

View Set