Computer Science Exam 2

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which concept allows child classes to use, modify, or replace behaviors defined in their parent classes?

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance (correct)

What is the most direct consequence of failing to deallocate dynamically allocated memory when it is no longer needed?

  • Stack Overflow
  • Memory Leak (correct)
  • Compiler Error
  • Segmentation Fault

When is a class destructor typically called?

  • When the class object goes out of scope or is explicitly deleted (correct)
  • When a derived class object is created
  • When a new member function is called
  • When an object is first created

Which of the following best describes encapsulation?

<p>Bundling data and methods that operate on that data, and hiding internal state (C)</p> Signup and view all the answers

What is required for a derived class function to override a public member function of its base class?

<p>The same name, number, and types of parameters (A)</p> Signup and view all the answers

In a linked list, what two things must each node store?

<p>Data and a pointer to the next node (A)</p> Signup and view all the answers

What is the term for the ability of objects to take on many forms, allowing a single function name or operator to be used with different data types?

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

If nodePtr is a pointer to a node in a linked list and data is a member of that node, how do you access data?

<p>nodePtr-&gt;data (B)</p> Signup and view all the answers

What is the * operator commonly called in C++?

<p>Dereferencing operator (D)</p> Signup and view all the answers

What are the individual elements within a class typically called?

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

When a class inherits private members from a base class, what is their accessibility in the child class?

<p>Inherited but not directly accessible. (B)</p> Signup and view all the answers

What is the primary purpose of enums?

<p>To create a set of named integer constants (B)</p> Signup and view all the answers

What mechanism is used to ensure member variables of a class are initialized when an object is created?

<p>Constructors or Initialization Lists (C)</p> Signup and view all the answers

If Class A contains an object of Class B, this is known as what kind of relationship?

<p>Has-a (D)</p> Signup and view all the answers

Which preprocessor directives are used to prevent multiple inclusions of a header file?

<p>#ifndef, #define, #endif (A)</p> Signup and view all the answers

Why is the const keyword used with pass-by-reference parameters?

<p>To ensure the function does not modify the original variable (D)</p> Signup and view all the answers

In what scenario would using dynamically allocated arrays be preferred over vectors?

<p>When you already know the array's size at compile time. (D)</p> Signup and view all the answers

What best describes the role of virtual functions in polymorphism?

<p>They enable late binding, allowing derived classes to override base class functions. (D)</p> Signup and view all the answers

Consider the following. Parent *ptr = new Child(); What does this allow?

<p>This allows pointers of a parent class to utilize child class functions, (C)</p> Signup and view all the answers

What happens when the destructor of a class is automatically called, or when you call delete?

<p>When it goes out of scope. (A)</p> Signup and view all the answers

Flashcards

Child Class Behaviors

Child classes can add new features, modify existing ones, and reuse inherited behaviors.

Memory Leak

Memory that is allocated during runtime but never deallocated, leading to wasted memory resources.

Destructor

A special member function that deallocates memory occupied by an object when it goes out of scope.

Encapsulation

Bundling data and the methods that operate on that data, while hiding internal details from the outside world.

Signup and view all the flashcards

Function Overloading

The ability to use the same function name with different parameters to perform different tasks.

Signup and view all the flashcards

Linked List Node

Each element stores data and a pointer/address to the subsequent element.

Signup and view all the flashcards

Polymorphism

Using existing objects for new purposes.

Signup and view all the flashcards

Arrow Operator (->) for Pointers

Used to access the member of an object given a pointer when dealing with pointers to objects.

Signup and view all the flashcards

Dereferencing Operator (*)

A C++ operator used to get the value that the pointer is pointing to.

Signup and view all the flashcards

Class Members

Variables and functions that belong to a class.

Signup and view all the flashcards

Private Inheritance

Parent class properties that are inherited but not directly accessible or modifiable by the child class.

Signup and view all the flashcards

Function Overriding

A function in a derived class replaces a function in a base class if they have the same name, parameters and return type.

Signup and view all the flashcards

Enums

A special data type that enables you to create named constants.

Signup and view all the flashcards

Constructor/Initialization list

A way to initialize member variables when a class object is created.

Signup and view all the flashcards

Has-a (Aggregation) Relationship

A relationship where one class contains an instance of another class.

Signup and view all the flashcards

Header Guards (#ifndef, #define, #endif)

Directives that prevent a header file from being included more than once.

Signup and view all the flashcards

Study Notes

  • Exam 2 will cover chapters 1, 2, 3, 4, 5, 6, 7, 8, 10, 14, and the linked list portion of chapter 17.
  • Expect questions from lab materials, including debugging and makefiles.
  • An extra presentation on makefiles is available on Blackboard.
  • All material in the book and slides from meetings 1-18 is testable.

Fill-in-the-Blanks

  • Child classes extend, replace, and reuse parent class behaviors.
  • Dynamically allocated memory no longer referenced by a pointer results in a memory leak.
  • The destructor deallocates memory used by list nodes when the class object goes out of scope.
  • Encapsulation is a method of hiding information and abstraction.
  • The function to overload the <= operator would be named operator <=.
  • Each linked list node stores both data and the pointer/address of the next node.
  • Polymorphism refers to reusing defined objects, potentially modifying them.
  • To access a "data" member via a pointer to a node, use nodePtr -> data, and for objects . is used to access data.
  • * is called the dereferencing operator in C++.
  • Class components are called members.
  • Classes inheriting from another class inherit private properties, but they are not directly accessible by the child class.
  • To override a public member function, the derived class's function must have the same name, number, and types of parameters.
  • Enums are special variables that let you define sets of named constants.
  • A constructor/initialization list is used to ensure class member variables are initialized.
  • Aggregation represents a has-a relationship.
  • The preprocessor directives #ifndef, #define, and #endif prevent multiple inclusions of a header file.

Short Answer

  • "Pass by reference" in C++ shares with pointers the ability to modify the original variable. However, references are automatically dereferenced and cannot be null, unlike pointers.
  • A variable declared within a while loop's control statement scope is limited to the while loop.
  • Overriding parent functions in derived classes can implemented using Virtual functions, which implement late binding.
  • Virtual functions enables a parent class pointer to use child class functions.
  • The const keyword can be used to prevent modification of the parameter being passed.
  • Dynamically allocated arrays are useful when the size is known beforehand. Vectors are preferred for dynamically sized containers, despite being more expensive.

True or False

  • True, enums generate integer constants that can be named.
  • True, access modifiers in a class affect the accessibility of the class's members.
  • False, arrays in C++ are passed by value. They are passed by reference/pointer
  • False, inheritance creates an is-a relationship
  • False, a derived class cannot directly intialize private data, use setters or initialization lists.
  • False, an abstract class cannot be instantiated, as abstract objects do not exist.
  • False, a class's destructor is called when it goes out of scope, or when delete is called.
  • True, each node in a linked list points to another node or to NULL.
  • False, accessing the last element in a singly-linked list takes the most time if there is no tail.
    • Singly linked lists end with node -> node -> node -> null. Doubly linked lists are null node null.
  • False, you can free memory in a linked list by iterating and calling delete instead of using delete[] m_head.
  • False, the -> operator is used to access members via pointers. The . operator is used for objects.
  • False, the ?: operator cannot be overloaded.
  • False, accessing the nth node in a linked list requires iteration, unlike arrays.
  • True, vectors store all their data contiguously in memory.
  • False, In the provided class, bar is constructed before sour due to the order in the initialization list.

Code Evaluation Questions

  • The code outputs:
    • UMBC CHALLENGE
    • CHALLENGE CHALLENGE
    • HACKATHON HACKATHON
  • Based on the enum enum foo{A=2, B=5, C=6, D, E=10}, D's integer value is 7.
  • The output would be c = 456
    • if 3 then 123, if not 3 then 456

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser