C++ Structures and Pointers Quiz
21 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is a characteristic of structures in C++?

  • Structures cannot contain functions.
  • All data members are private by default.
  • Structures can only hold variables of one data type.
  • All data members are public by default. (correct)
  • In what context are structure pointers particularly useful?

  • For serializing data to files.
  • For passing structures to functions. (correct)
  • For accessing global variables.
  • For creating static arrays.
  • Which operator is used to access data members of a structure through a pointer?

  • ::
  • &
  • -> (correct)
  • .
  • What can a structure in C++ contain?

    <p>An instance of another structure.</p> Signup and view all the answers

    What must be done if dynamic memory is used for any of the members of a structure?

    <p>It must be freed before the program exits.</p> Signup and view all the answers

    What type of structures can be declared in C++?

    <p>Both global and local structures.</p> Signup and view all the answers

    Which of the following is NOT a feature of nested structures?

    <p>Nested structures must contain at least one member variable.</p> Signup and view all the answers

    What happens if you forget to free dynamic memory used in a structure's members?

    <p>It causes a memory leak.</p> Signup and view all the answers

    What is the return value of the functions like find_last_of() if no matches are found?

    <p>string::npos</p> Signup and view all the answers

    Which function is used to erase a part of a string and reduce its length?

    <p>erase()</p> Signup and view all the answers

    What should ideally be used when passing a string during a function call to avoid unnecessary copies?

    <p>Pass by reference</p> Signup and view all the answers

    What is the function 'begin()' used for in relation to STL containers?

    <p>To return the beginning position of the container</p> Signup and view all the answers

    How many overloads does the insert() member function have based on the C++ version?

    <p>More than six</p> Signup and view all the answers

    What is a primary advantage of using std::string over C-style strings?

    <p>std::string does not require null termination.</p> Signup and view all the answers

    Which member function can be used to add a character to the end of a std::string?

    <p>push_back()</p> Signup and view all the answers

    What happens if you attempt to access an out-of-bounds index in a std::string using the at(i) function?

    <p>It throws an exception.</p> Signup and view all the answers

    What does the find() member function of std::string return if the specified sequence is not found?

    <p>std::string::npos.</p> Signup and view all the answers

    How is memory allocated for the characters in std::string managed?

    <p>It is handled automatically by the std::string class.</p> Signup and view all the answers

    Which std::string function would you use to retrieve the last character of a string without removing it?

    <p>back()</p> Signup and view all the answers

    Which character does a C-style string require at the end to define its termination?

    <p>'\0'</p> Signup and view all the answers

    What is the function of find_first_of() in std::string?

    <p>To search for the first character that matches any specified characters.</p> Signup and view all the answers

    More Like This

    Use Quizgecko on...
    Browser
    Browser