What is the primary purpose of the ofstream class in file handling?
- Reading files
- Writing to files
- Deleting files
- Copying files
The ofstream class stands for output file stream. It is primarily used for performing output operations on files, meaning it's used for writing data to files. This class provides methods and properties needed to write data.
Which of the following data types is suitable for storing a character in C++?
- boolean
- char
- float
- int
In C++, the char data type is specifically designed to store characters. It typically occupies 1 byte in memory and can represent a single character from the ASCII table.
To prevent a class from being inherited, we declare it as _______.
- final
- static
- private
- mutable
In C++, the "final" keyword is used to prevent a class from being inherited. This means that no other class can be derived from a class declared as "final".
When class members are labeled as private, they are inaccessible outside the class, enforcing _______.
- encapsulation
- abstraction
- polymorphism
- inheritance
Encapsulation is a key concept in object-oriented programming where the internal details of a class are hidden and only specific functionalities are exposed. Making class members private enforces this encapsulation.
Regarding memory alignment and data packing, which of the following is true for structs in C++?
- Struct members are always packed tightly with no padding.
- Structs cannot be aligned in memory.
- Struct members have a defined order, but might have padding.
- Structs use dynamic memory for data storage.
Memory alignment and data packing are important considerations in C++ for optimizing memory usage and performance. In a struct, the order of declaration of members matters because the compiler might introduce padding between members to align data appropriately for the target architecture. This can affect the overall size of the struct.
To handle an exception thrown, a _______ block is used to catch the exception.
- if
- catch
- try
- for
The catch block in C++ is used to handle exceptions. When an exception occurs in the try block, it is passed to the catch block where it can be handled. The type of exception that a particular catch block can handle is determined by the parameter it carries.
To create an infinite loop using a while loop, we can use the statement while(_______).
- FALSE
- 1
- 0
- "infinite"
A while loop continues execution as long as the condition within the parentheses is true. By using "while(1)", where 1 stands for true in the context of the loop's condition, it creates an infinite loop.
What is the primary purpose of the return statement in C++ functions?
- To loop the function
- To display a value
- To exit the function
- To allocate memory
In C++, the return statement is primarily used to conclude the execution of a function and provide a value back to the caller. It signifies the end of a function's execution and optionally returns a value to the calling function.
What will be the output of a loop if the continue statement is placed before the print statement inside the loop?
- The loop will not output anything
- The loop will output values continuously
- The loop will only output the first value
- The loop will output alternate values
If the continue statement is placed before the print statement inside a loop, it will cause the loop to skip the print statement during every iteration. Therefore, nothing will be outputted by the loop.
A function that does not return a value has a return type of _______.
- void
- int
- char
- bool
In C++, if a function is not returning any value, its return type is void. It indicates that the function does not return a value to the calling function.