Pointers & Structures

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/132

encourage image

There's no tags or description

Looks like no tags are added yet.

133 Terms

1
New cards

With pointer variables you can access but not modify data in other variables

-true

-false

false

2
New cards

An array name is a pointer constant because the address stored in it cannot be changed at runtime.

-true

-false

true

3
New cards

In C++11, the nullptr keyword was introduced to represent the address 0.

-true

-false

true

4
New cards

It is legal to subtract a pointer variable from another pointer variable.

-true

-false

true

5
New cards

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

-false

true

6
New cards

A pointer can be used as a function argument, giving the function access to the original argument.

-true

-fasle

true

7
New cards

The ampersand (&) is used to dereference a pointer variable in C++

-true

-false

false

8
New cards

Assuming myValues is an array of int values and index is an int variable, both of the following statements do the same thing.

1. cout << myValue [index] << endl;

2. cout << *(myValues + index) << endl;

-true

-false

true

9
New cards

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

-false

true

10
New cards

The weak_ptr can share ownership of a piece of dynamically allocated memory.

-true

-fasle

false

11
New cards

The unique_ptr is the sole owner of a piece of dynamically allocated memory.

-true

-false

true

12
New cards

To use any of the smart pointers in C++11 you must use the following directive in the header file:

-true

-false

true

13
New cards

The ________, also known as the address operator, returns the memory address of a variable.

astreric(*)

ampersand (&)

percent(%)

exclamation point(!)

none of these

ampersand(&)

14
New cards

With pointer variables you can ________ manipulate data stored in other variables.

never

seldom

indirectly

All of these

None of these

indirectly

15
New cards

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) None of these

c) the actual value of the variable whose address is stored in the pointer variable

16
New cards

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)still points to a valid object after the function ends

17
New cards

What does the following statement do?

double *num2;

a)Declares a double variable named num2

b)Declares and initializes a pointer variable named num2

c)Initializes a pointer variable named num2

d)Declares a pointer variable named num2

e)None of these

d)Declares a pointer variable named num2

18
New cards

In C++11, the ________ keyword was introduced to represent address 0.

a)nullptr

b)NULL

c)weak_ptr

d)shared_ptr

e)None of these

a)nullptr

19
New cards

When the less than operator (<) is used between two pointer values, 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)the address of the first variable comes before the address of the second variable in the computer's memory

20
New cards

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)created with the new operator

21
New cards

Which of the following is TRUE about this statement?

sum += *array++;

a)This statement is illegal in C++.

b)This statement will cause a compiler error.

c)This statement assigns the dereferenced pointer's value, then increments the pointer's address.

d)This statement increments the dereferenced pointer's value by one, then assign that value.

e)None of these

c)This statement assigns the dereferenced pointer's value, then increments the pointer's address.

22
New cards

In the following statement, what does int mean?

int *ptr = nullptr;

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 and will store the address of an integer variable.

d)The variable named *ptr will store the value in nullptr.

e)None of these

c)ptr is a pointer variable and will store the address of an integer variable.

23
New cards

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 whose address is stored in ptr

d)the address of the variable stored in ptr

e)None of these

a)the value stored in the variable whose address is contained in ptr

24
New cards

Which of the following statements is NOT valid C++ code?

a)int ptr = &num1;

b)int ptr = int *num1;

c)float num1 = &ptr2;

e)All of these are invalid

e)All of these are invalid

25
New cards

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)delete [] array;

26
New cards

Which of the following statements displays the address of the variable numb?

a)cout << numb;

b)cout << *numb;

c) cin >> &numb;

d)cout << &numb;

e)None of these

d)cout << &numb;

27
New cards

What will the following statement output?

a)cout << &num1;

b)the value stored in the variable named num1

c)the memory address of the variable named num1

the number 1

d)the string &num1

e)None of these

c)the memory address of the variable named num1

the number 1

28
New cards

The following statement ________.

cin >> *num3;

-stores the keyboard input in the variable num3

-stores the keyboard input into the pointer num3

is illegal in C++

-stores the keyboard input into the variable pointed to by num3

- None of these

-stores the keyboard input into the pointer num3

is illegal in C++

29
New cards

The following statement ________.

int *ptr = new int;

results in a compiler error

assigns an integer less than 32767 to the variable ptr

assigns an address to the variable ptr

creates a new pointer named int

None of these

assigns an address to the variable ptr

30
New cards

A pointer variable is designed to store

any legal C++ value

only floating-point values

an integer

a memory address

None of these

memory address

31
New cards

The ________ and ________ operators can be used to increment or decrement a pointer variable.

] addition, subtraction

++, --

modulus, division

All of these

None of these

++, --

32
New cards

Not all arithmetic operations can be performed on pointers. For example, you cannot ________ or ________ pointers.

multiply, divide

+=, -=

add, subtract

increment, decrement

None of these

multipy, divide

33
New cards

Dynamic memory allocation occurs

when a new variable is created by the compiler

when a new variable is created at runtime

when a pointer fails to dereference the right variable

when a pointer is assigned an incorrect address

None of these

when a new variable is created at runtime

34
New cards

Every byte in the computer's memory is assigned a unique

pointer

address

dynamic allocation

name

None of these

address

35
New cards

A pointer variable may be initialized with

any nonzero integer value

a valid address in the computer's memory

an address less than zero

any nonzero number

None of these

a valid address in the computer's memory

36
New cards

If a variable uses more than one byte of memory, for pointer purposes its address is




the address of the last byte of storage

the average of all the addresses used to store that variable

the address of the first byte of storage

the address of the second byte of storage

None of these

the address of the first byte of storage

37
New cards

If you are using an older computer that does NOT support the C++11 standard, you should initialize pointers with

the integer 0 or the value NULL

the null terminator '\0'

a nonzero value

Any of these

None of these

the integer 0 or the value NULL

38
New cards

When you pass a pointer as an argument to a function, you must



declare the pointer value again in the function call

dereference the pointer value in the function prototype

use the #include statement

not dereference the pointer in the function's body

None of these

none

39
New cards

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.

null pointer

smart pointer

dereferenced pointer

None of these

smart pointer

40
New cards

What will the following code output?

int number = 22;

int *var = &number;

cout << *var << endl;

the address of number

22

an asterisk followed by 22

an asterisk followed by the address of number

22

41
New cards

What will the following code output?

int number = 22;

int *var = &number;

cout << var << endl;



the address of number

22

an asterisk followed by 22

an asterisk followed by the address of number

the address of number

42
New cards

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;

five memory addresses

0

3

2

1

2

43
New cards

After the code shown executes, which of the following statements is TRUE?

int numbers[] = {0, 1, 2, 3, 4};

int *ptr = numbers;

ptr++;

ptr will hold the address of numbers[0]

ptr will hold the address of the second byte within the element numbers[0]

ptr will hold the address of numbers[1]

this code will not compile

ptr will hold the address of numbers[1]

44
New cards

Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int?

unique_ptr int( new int );

unique_ptr int( new uniq );

unique_ptr uniq( new int );

unique_ptr uniq( new int );

None of these

unique_ptr uniq( new int );

45
New cards

Which of the following can be used as pointers?

array names

numeric constants

keywords

None of these

array name

46
New cards

elect all that apply. Select as many of the following options that make this sentence TRUE:

The contents of pointer variables may be changed with mathematical statements that perform

multiplication

division

addition

subtraction

modulus

addition, subtraction

47
New cards

Select all that apply. Of the following, which statements have the same meaning?

int *ptr = nullptr;

int ptr = nullptr;

*int ptr = nullptr;

int* ptr = nullptr;

int ptr* = nullptr;

int *ptr = nullptr;

int* ptr = nullptr;

48
New cards

A struct can contain members with varying data types.

T

49
New cards

Any mathematical operation that can be performed on regular C++ variables can be performed

on structure members.

T

50
New cards

Structure variables may be passed as arguments to functions.

T

51
New cards

The structure pointer operator is used to dereference a pointer to a structure, not a pointer that is a member of a structure.

T

52
New cards

If a function is legally prototyped to return an integer value, it can return a structure member

that is an integer data type.

T

53
New cards

A function cannot modify the members of a structure.

F

54
New cards

The expression s->m; indicates that s is a structure pointer and m is a structure member.

T

55
New cards

It is possible to output the contents of all members of a structure variable using a cout <<

statement followed by the name of the structure variable.

F

56
New cards

When a programmer creates an abstract data type, he or she can decide what values are

acceptable for the data type, as well as what operations may be performed on the data type.

T

57
New cards

It is possible for a structure variable to be a member of another structure variable.

T

58
New cards

The expression *s->p; indicates that s is a structure pointer and p, which is also a pointer, is a member of the structure pointed to by s.

T

59
New cards

It is possible for a structure to contain, as a member, a pointer to its own structure type.

T

60
New cards

You cannot directly assign an integer value to an enum variable.

T

61
New cards

You cannot directly assign an enumerator to an int variable.

F

62
New cards

When you use a strongly typed enumerator in C++11 you must prefix the enumerator with the

name of the enum followed by the :: operator.

T

63
New cards

The names of enumerators in an enumerated data type must be enclosed in quotation marks.

F

64
New cards

In C++11 if you want to retrieve a strongly typed enumerator's underlying integer value, you

must use a cast operator.

T

65
New cards

Given the structure definition shown, assume that circle1 and circle2 are variables of

the Circle type and their members have been initialized.

struct Circle

{

double centerX;

double centerY;

double radius;

};

Then, is it true or false that the following statement correctly determines whether the two variables' members

contain the same data?

if (circle1 == circle2)

F

66
New cards

Which of the following describes only the general characteristics of an object?

a. initialization

b. abstraction

c. detailed specification

d. initiation

e. None of these

B

67
New cards

Which of the following is required after the closing brace of the structure definition?

a. square bracket

b. period

c. semicolon

d. colon

e. None of these

C

68
New cards

Which of the following is an example of a C++ primitive data type?

a. unsigned short int

b. long double

c. unsigned char

d. All of these

e. None of these

D

69
New cards

The following statement __________.

bookList[2].publisher[3] = 't';

a. is illegal in C++

b. will change the name of the second book in bookList to 't'

c. will store the character 't' in the fourth element of the publisher member of

bookList[2]

d. will result in a runtime error

e. None of these

C

70
New cards

When a structure is passed __________ to a function, its members are not copied.

a. by reference

b. by value

c. Either of these

d. Neither of these

A

71
New cards

Passing a structure as a constant reference parameter to a function

a. can potentially result in changes to the structure's members

b. guarantees not to result in changes to the structure's members

c. will always change the structure's members

d. None of these

B

72
New cards

You may use a pointer to a structure as a

a. function parameter

b. structure member

c. function return type

d. All of these

e. None of these

D

73
New cards

Data types that are created by the programmer are known as

a. variables

b. abstract data types (ADTs)

c. functions

d. parameters

e. None of these

B

74
New cards

Before a structure can be used it must be

a. declared

b. dereferenced

c. initialized

d. All of these

e. None of these

A

75
New cards

The name of a structure is referred to as its

a. data type

b. argument

c. parameter

d. tag

e. None of these

E

76
New cards

A function __________ return a structure.

a. may

b. may not

c. will always

d. can never

e. None of these

A

77
New cards

A structure pointer contains

a. the address of a structure variable

b. the dereferenced address of a structure tag

c. the name and address of the structure tag

d. the address of a structure tag

e. None of these

A

78
New cards

Which of the following assigns a value to the hourlyWage member of employee[2]?

a. employee[2] -> hourlyWage = 50.00;

b. employee2.hourlyWage = 7.50;

c. hourlyWage[2].employee = 29.75;

d. employee[2].hourlyWage = 75.00;

e. None of these

D

79
New cards

If Circle is a structure tag, then the following statement can be the header line for a function

that __________.

Circle doSomething(Circle c2)

a. determines and returns the area of a circle

b. takes a Circle structure as a parameter, does something, and returns a Circle

structure

c. operates on a constant reference to a Circle structure

d. takes two Circle parameters and does something

e. None of these

B

80
New cards

Which of the following will allow you to access structure members?

a. the structure access operator

b. the dot operator

c. the #include directive

d. the getmember function

e. None of these

B

81
New cards

To dereference a structure pointer, the appropriate operator is

a. the ampersand (&)

b. an asterisk (*)

c. the -> operator

d. the <- operator (<-)

e. None of these

C

82
New cards

If a is a structure variable and p, a pointer, is a member of the structure, what will the

following statement do?

cout << *a.p;

a. output the dereferenced value pointed to by p

b. result in a compiler error

c. output the address stored in p

d. output the value stored in a

e. None of these

A

83
New cards

A structure __________ contain members of the same data type.

a. cannot

b. can

c. shouldn't

d. None of these

B

84
New cards

Which of the following statements outputs the value of the gpa member of element [1] of the

student array?

a. cout << student1.gpa;

b. cout << firstStudent.gpa;

c. cout << student[1].gpa;

d. cout << student1->gpa;

e. None of these

C

85
New cards

If Circle is a structure, what does the following statement do?

Circle *pcirc = nullptr;

a. It declares an empty structure variable named *pcirc.

b. It declares a structure pointer called pcirc initialized with a null pointer.

c. The statement is illegal in C++.

d. It initializes a null pointer with the value of the Circle pointer.

e. None of these

B

86
New cards

A good reason to pass a structure as a constant reference is

a. to prevent changes to the structure's members

b. to ensure changes to the structure's members

c. to slow down the function's execution which helps prevent errors

d. to speed up the function's modification of the structure's members

e. None of these

A

87
New cards

With an enumerated data type, the enumerators are stored in memory as

a. strings

b. integers

c. characters

d. doubles

B

88
New cards

In C++11, you can use a new type of enum known as a(n) __________ (also known as an

enum class) to have multiple enumerators with the same name, within the same scope.

a. universal enum

b. auto enum

c. multi-cast enum

d. strongly typed enum

e. None of these

D

89
New cards

A declaration for an enumerated type begins with the __________ key word.

a. enumerated

b. enum_type

c. enum

d. ENUM

e. None of these

C

90
New cards

After the following statement executes, what value will the MAPLE enumerator be stored as, in

memory?

enum Tree { OAK, MAPLE, PINE };

a. "MAPLE"

b. 2

c. 'M'

d. 1

e. 1.0

D

91
New cards

Given the following decaration:

enum Tree { OAK, MAPLE, PINE };

What is the value of the following relational expression?

OAK > PINE

a. true

b. false

c. This is an error. You cannot compare enumerators with relational operators.

B

92
New cards

Given the following structure decaration, Employee is

struct Employee

{

string name;

int idNum;

};

a. a member

b. an array

c. a tag

d. None of these

C

93
New cards

Given the following structure decaration, idNum is

struct Employee

{

string name;

int idNum;

};

a. a member

b. an array

c. a tag

d. None of these

A

94
New cards

Given the structure

struct TwoVals
{
int a, b;
};

Select what is INCORRECT.

  

int main() 
{
  TwoVals tv;
   tv.a =1;
   tv.b =2;
}

 

int main() 
{
TwoVals.a = 10;
TwoVals.b = 20;
return 0;
}

  

int main() 
{
TwoVals * p = new TwoVals();

return 0;
}

  

int main() 
{
TwoVals.a = 10;
TwoVals.b = 20;
return 0;
}

95
New cards

Select what is INCORRECT.

struct FourVals
{
int a, b, c, d;
};


  

int main ()
{
FourVals nums = {1, 2, , 4};
return 0;
}

  

int main ()
{
FourVals nums = {1, 2, 3};
return 0;
}



int main ()
{
FourVals nums = {1, 2, 3, 4};
return 0;
}

 

int main ()
{
FourVals nums;
return 0;
}


 int main ()
{
FourVals nums = new FourVals();
return 0;
}

  

 int main ()
{
FourVals nums = new FourVals();
return 0;
}

 

int main ()
{
FourVals nums = {1, 2, , 4};
return 0;
}

96
New cards

Destructors can take arguments.

false

97
New cards

When an array of  3 structures is declared


  • The constructor is called 3 times.


  • The constructor is called 0 times, you have to use operator new for the constructor to be called.


  • he constructor is called 1 time.


  • None of the answers is correct.

 

The constructor is called 3 times.


98
New cards

When a member function’s body is written inside a struct declaration, the function is _________.


  • embedded


  • inline


  • local


  • instance


inline

99
New cards

The destructor of structures  is called automatically when a local vaiable goes out of scope

true

100
New cards

If the code below compiles what is the the value of ptr after execution?

 

int numbers[] = {0, 1, 2, 3, 4 };
int *ptr = numbers;
ptr++;


  • ptr will hold the address of numbers[0].


  • ptr will hold the address of the 2nd byte within the element numbers[0].


  • ptr will hold the address of numbers[1].

  

 

ptr will hold the address of numbers[1].