Practical 1

    Master this deck with 19 terms through effective study methods.

    Generated from YouTube video

    Created by @knuffes

    What is a vector in programming?

    A vector is a dynamic array that can grow in size, allowing for the addition of elements without needing to specify the size in advance. It provides more flexibility compared to traditional arrays.

    How do you access elements in a vector?

    Elements in a vector can be accessed using the 'at' method or the traditional array indexing method. However, using 'at' is recommended as it provides bounds checking and can throw exceptions for out-of-bounds access.

    What happens when you access an out-of-bounds element in a vector using traditional indexing?

    Accessing an out-of-bounds element using traditional indexing can lead to a segmentation fault, causing the program to crash. In contrast, using the 'at' method will throw an exception, allowing for error handling.

    What is the advantage of using namespaces in programming?

    Namespaces help organize code and prevent naming conflicts by allowing the same name to be used for different entities in different contexts. This is particularly useful in larger projects or when integrating multiple libraries.

    How can you avoid naming conflicts when defining structs?

    To avoid naming conflicts when defining structs with the same name but different members, you can use namespaces. This allows you to encapsulate each struct within its own namespace.

    What is the purpose of the 'std' namespace in C++?

    The 'std' namespace contains all the standard library functions and objects in C++. It is used to avoid naming conflicts with user-defined identifiers and to organize standard library components.

    What is the difference between 'std::endl' and the newline character?

    'std::endl' is similar to the newline character but also flushes the output buffer, ensuring that all output is written immediately. The newline character simply moves the cursor to the next line without flushing.

    Can you mix data types in a C++ vector?

    No, C++ vectors must contain elements of the same data type. Unlike some other languages, C++ does not allow heterogeneous collections within a single vector.

    What is the significance of using references in function parameters?

    Using references in function parameters allows you to pass variables without copying them, which can improve performance and allow the function to modify the original variable.

    What is a call by value?

    Call by value is a method of passing arguments to a function where a copy of the actual value is made. Changes made to the parameter inside the function do not affect the original variable.

    What is a call by reference?

    Call by reference is a method of passing arguments to a function where a reference to the actual variable is passed. This allows the function to modify the original variable directly.

    What is the output of modifying a vector inside a function without using a reference?

    If a vector is modified inside a function without using a reference, the changes will not affect the original vector. The function works on a copy of the vector, and any modifications are lost when the function exits.

    What is the purpose of the 'auto' keyword in C++?

    The 'auto' keyword in C++ allows the compiler to automatically deduce the type of a variable based on its initializer. This can simplify code and improve readability, but it cannot be used with vectors to define mixed types.

    What is a struct in C++?

    A struct in C++ is a user-defined data type that allows grouping of variables under a single name. It can contain different data types and is used to represent a record or a complex data structure.

    How do you create a vector in C++?

    A vector can be created in C++ by including the vector header and declaring it using the syntax 'std::vector<Type> name;', where 'Type' is the data type of the elements.

    What is the role of the 'include' directive in C++?

    The 'include' directive is used to include the contents of a file or library in a C++ program. This allows the use of functions and classes defined in external libraries, such as the standard library.

    What is the significance of the null pointer in C++?

    A null pointer in C++ is a pointer that does not point to any valid memory location. It is often used to indicate that a pointer is uninitialized or to signify the end of a data structure.

    What is the output of a function that modifies a vector passed by reference?

    If a vector is passed to a function by reference and modified, the changes will be reflected in the original vector. This is because the function operates on the same memory location as the original vector.

    What is the purpose of the 'length' or 'size' method for strings in C++?

    The 'length' or 'size' method for strings in C++ returns the number of characters in the string, excluding the null terminator. This allows for accurate measurement of the string's content.