C.S. 150 Chapter 12 Flashcards
31 Questions
100 Views

C.S. 150 Chapter 12 Flashcards

Created by
@SucceedingHexagon

Questions and Answers

What is a pointer variable?

A variable whose content is a memory address.

The character * describes a pointer variable. Which of the following statements is true?

  • int *p, q; - p is a pointer variable, q is not.
  • int *p, *q; - p and q are both pointer variables.
  • Both A and B are correct. (correct)
  • None of the above.
  • What is the address of operator (&)?

    A unary operator that returns the address of its operand.

    What does the dereferencing operator (or indirection operator) do when used as a unary operator?

    <p>It refers to the object to which its operand points.</p> Signup and view all the answers

    In C++, pointer variables are declared using the reserved word pointer.

    <p>False</p> Signup and view all the answers

    In the declaration int* p, q; both p and q are pointer variables.

    <p>False</p> Signup and view all the answers

    In C++, you declare a pointer variable by using the ____ symbol.

    <ul> <li></li> </ul> Signup and view all the answers

    Int*p; declares p to be a(n) ____ variable.

    <p>Pointer</p> Signup and view all the answers

    In C++, the member access operator arrow is >>.

    <p>False</p> Signup and view all the answers

    Variables that are created during program execution are called ______________.

    <p>dynamic variables</p> Signup and view all the answers

    The C++ operator ____ is used to create dynamic variables.

    <p>new</p> Signup and view all the answers

    What is a memory leak?

    <p>Previously allocated memory that cannot be reallocated.</p> Signup and view all the answers

    A memory leak is an unused memory space that cannot be allocated.

    <p>True</p> Signup and view all the answers

    The C++ operator ____ is used to destroy dynamic variables.

    <p>delete</p> Signup and view all the answers

    What is assignment in relation to pointer variables?

    <p>The value of one pointer variable can be assigned to another pointer of the same type.</p> Signup and view all the answers

    Which of the following operations is allowed on pointer variables?

    <p>==</p> Signup and view all the answers

    Which of the following arithmetic operations is allowed on pointer variables?

    <p>Increment</p> Signup and view all the answers

    If p is a pointer variable, the statement p = p + 1; is valid in C++.

    <p>True</p> Signup and view all the answers

    Given the statement double*p;, the statement p++; will increment the value of p by ____ byte(s).

    <p>Eight</p> Signup and view all the answers

    What is the value of x after the following statements execute? int x = 25; int *p; p = &x; *p = 46;

    <p>46</p> Signup and view all the answers

    What is a dynamic array?

    <p>An array created during program execution.</p> Signup and view all the answers

    Given the declaration int *a;, the statement a = new int; dynamically allocates an array of 50 components of the type ____.

    <p>int</p> Signup and view all the answers

    An array created during the execution of a program is called a(n) ____ array.

    <p>dynamic</p> Signup and view all the answers

    Pointer variable can be passed as a parameter either by _________ or by reference.

    <p>value</p> Signup and view all the answers

    A function can _______ a value of type pointer.

    <p>return</p> Signup and view all the answers

    What is a shallow copy?

    <p>When two or more pointers of the same types point to the same memory.</p> Signup and view all the answers

    What is a deep copy?

    <p>When the contents of the memory pointed to by a pointer are copied to the memory location of another pointer.</p> Signup and view all the answers

    In a ____ copy, two or more pointers of the same type point to the same memory.

    <p>shallow</p> Signup and view all the answers

    In a ____ copy, two or more pointers have their own data.

    <p>deep</p> Signup and view all the answers

    A class ____ automatically executes whenever a class object goes out of scope.

    <p>destructor</p> Signup and view all the answers

    What is a copy constructor?

    <p>A constructor that accepts an object of the same class as an argument.</p> Signup and view all the answers

    Study Notes

    Pointers

    • A pointer variable holds the memory address of another variable.
    • Declaring pointer variables uses the * symbol; for example, int *p declares p as a pointer to an integer.
    • The address of operator & retrieves the memory address of its operand.
    • The dereferencing operator * accesses the value at the memory address stored in a pointer.

    Memory Management

    • Dynamic variables are created during program execution and can utilize the new operator for allocation and the delete operator for destruction.
    • Memory leak occurs when allocated memory is no longer accessible, preventing reallocation.
    • Unused memory space that cannot be allocated signifies a memory leak.

    Pointer Operations

    • Pointer assignments allow values of one pointer variable to be assigned to another of the same type.
    • Pointer arithmetic, including operations like incrementing a pointer (p++), adjusts addresses based on the type pointed to (e.g., double*p increments by 8 bytes).
    • Valid operations on pointers include equality comparison (==) and incrementing.

    Dynamic Arrays

    • Dynamic arrays are arrays allocated during program execution.
    • Allocation of a dynamic array can be performed using new, such as a = new int.

    Copies

    • A shallow copy allows multiple pointers to reference the same memory location, while a deep copy creates independent copies of the pointed data.
    • Deep copy involves duplicating the contents of the memory to another pointer's location, ensuring both pointers maintain separate copies.

    Classes & Object Lifecycle

    • A destructor is a class member function that executes automatically when an object goes out of scope, facilitating resource deallocation.
    • A copy constructor is a constructor that creates a new object as a copy of an existing object from the same class.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge on pointers, classes, and virtual functions with these flashcards from C.S. 150 Chapter 12. Each card provides a term and its definition, helping you grasp key concepts essential for understanding memory management and object-oriented programming.

    Use Quizgecko on...
    Browser
    Browser