C++

Ace your homework & exams now with Quizwiz!

Quiz 02.03 Suppose we are writing the following function that is intended to return a pointer to a location in memory holding an integer value initialized to zero. int *allocate_an_integer() { // declare variable i here *i = 0; return i; } How should variable i be declared? 1. int *i = new int; 2. int *i; 3. int i; 4. int j; int *i = &j;

1. int *i = new int;

What is the namespace of the C++ Standard Library? 1. std 2. csl 3. stdio 4. cstl

1. std

Quiz 02.08 c++ int *i = new int; *i = 0; int &j = *i; j++; What does the last line of the above code segment do? 1. Increments the value of j by one, where the value of j is a local copy stored on the stack of the value of i stored on the heap. 2. Increments the value pointed to by variable i by one. 3. Causes an error. 4. Increments the address pointed to by variable i by one.

2. Increments the value pointed to by variable i by one.

Quiz 02.02 Which one of the following is true? 1. The "new" operator allocates memory on the stack that gets removed from the stack by the "delete"operator. 2. The address of any memory location in the stack is larger than the address of any memory location in the heap. 3. You should avoid using the memory address 0x0 for pointers whose value is not yet set, because memory location 0x0 is a valid location for the system to allocate to hold the contents of a variable. 4. The C++ statement "int i;" allocates memory for one integer on the heap.

2. The address of any memory location in the stack is larger than the address of any memory location in the heap.

One of these statements below is true and the other three are false. Which one is true? 1. Any functions that operate on a class's member data variables must be implemented independent of the class in a separate .cpp file. 2. The member functions of a class always have access to every member data variable of that class. 3. The member functions of a class can only operate on member data variables of that class. 4. The member data variables in a class can only be accessed by the member functions of that class.

2. The member functions of a class always have access to every member data variable of that class.

Quiz 02.06 c++ int i = 0; int *j = &i; How many memory allocations are made on the stack and on the heap for the above code? For example, declaring an integer would count as one memory allocation. 1. One allocation on the stack and one allocation on the heap. 2. Two allocations on the stack and zero allocations on the heap. 3. Zero allocations on the stack and one allocation on the heap. 4. Zero allocations on the stack and two allocations on the heap. 5. One allocation on the stack and zero allocations on the heap.

2. Two allocations on the stack and zero allocations on the heap.

int i = 0, j = 1; int *ptr = &i; i = 2; *ptr = 3; ptr = &j; j = i; *ptr = 4; Enter the number of different values stored in the same address that variable i has during the execution of the code above. (Your answer should be a single integer, which is the total number of different values assigned to that address.)

3

Quiz 02.05 Suppose we declare a variable as int i; Which of the following expressions returns the address of the memory location containing the contents of variable i? 1. *i 2. i.addr 3. &i 4. i->addr

3. &i

Quiz 02.01 Recall that every variable in C++ has these four things: a name, a type, a value and a memory location. int *p; p = new int; *p = 0; For the code above, which one of the following is NOT true for variable p? 1. The name of the variable is p 2. The type of the variable is a pointer to an integer, specifically the type int * 3. The value of the variable is 0 4. The memory address of the variable is the value returned by the expression &p

3. The value of the variable is 0

According to the C++ standard, what is the name of the function is the starting point for a program? 1. begin() 2. start() 3. main() 4. init()

3. main()

Given only the following code: namespace uiuc { class Pair { double a,b; }; } Which keyword is used to indicate which namespace(s) to search to find classes and variables when they are referenced throughout the rest of the program? 1. name 2. space 3. std 4. using 5. uiuc

3. std

Which operator is used to send a sequence of strings, numbers and other values to the standard library's cout object in a specific order so that they will be printed to the console? 1. & 2. = 3. + 4. <<

4. <<

One of these statements below is true and the other three are false. Which one is true? 1. A class can consist of multiple member data variables, but the type of each data variable does not need to be specified until the class is used to declare a variable. 2. A class can consist of multiple member data variables of different types, but each member variable must be one of the built-in types. 3. A class can consist of multiple member data variables, but all must be of the same type. 4. A class can consist of multiple member data variables of different types, but each type must be specified when the class is defined.

4. A class can consist of multiple member data variables of different types, but each type must be specified when the class is defined.

Why do we use namespaces in C++ programming? 1. Because all variable and class names must be defined using a namespace. 2. Because it allows a library to claim a variable or class name that cannot be used by any other library. 3. Because all references to variable and class names must be made through namespace. 4. Because two different libraries might use the same label for a class or variable

4. Because two different libraries might use the same label for a class or variable

Quiz 02.04 Suppose we have this alternative function that returns a pointer to a memory location to an integer value of zero. int *allocate_an_integer() { int i = 0; return &i; } int main() { int *j; j = allocate_an_integer(); int k = *j; return 0; } What value is variable k assigned and why? 1. Variable k is not assigned a value, because even if the compiler is set to ignore warnings and continue with compilation, the compiled program will still automatically detect that a local variable's address is being used after the function has returned, and exit to the operating system with a non-zero error code. 2. Assuming that the program compiles with just a warning and not an error due to the settings, the variable k will not be assigned a value, because the running program will crash the whole operating system. 3. Variable k is certainly assigned the value zero, because the C++ runtime will automatically move the local variable to the heap and return the address of that heap variable instead. 4. Unknown. Depending on the compiler settings, the compiler may report that a local variable address is being returned, which could be treated as a warning or as a compilation error; Or, if the program is allowed to compile, then at runtime the variable k could be assigned zero, or some other value, or the program may terminate due to a memory fault.

4. Unknown. Depending on the compiler settings, the compiler may report that a local variable address is being returned, which could be treated as a warning or as a compilation error; Or, if the program is allowed to compile, then at runtime the variable k could be assigned zero, or some other value, or the program may terminate due to a memory fault.

Given only the following code: namespace uiuc { class Pair { double a,b; }; } which of the following syntax can be written outside of the namespace declaration to properly create a variable named "p" of type Pair? 1. (uiuc) Pair p; 2. uiuc/Pair p; 3. Pair p; 4. uiuc::Pair p;

4. uiuc::Pair p;

Quiz 02.07 c++ int *i = new int; How many memory allocations are made on the stack and on the heap for the above code? For example, allocating space for one integer would count as one memory allocation. 1. One allocation on the stack and zero allocations on the heap. 2. Zero allocations on the stack and one allocation on the heap. 3. Two allocations on the stack and zero allocations on the heap. 4. Zero allocations on the stack and two allocations on the heap. 5. One allocation on the stack and one allocation on the heap.

5. One allocation on the stack and one allocation on the heap.


Related study sets

Unit 2: Nutrition, Metabolism, and Thermoregulation (Connect)

View Set

child, older adult, and intimate partner violence quiz 1116 (not finished)

View Set

CH 27 - Assessment of the Respiratory System

View Set

Quiz Chapter 13 - Spinal Cord Spinal Nerves

View Set

CH 4 & 5: job analysis & job based structures

View Set