When performing a bitwise AND operation with a number and 0, the result is always _______.
- positive
- zero
- negative
- unchanged
When any number is bitwise AND-ed with 0, the result is always 0. This is because the AND operation yields 1 only if both bits being compared are 1; otherwise, it yields 0.
What is the output of the logical NOT (!) operator when applied to a non-zero operand?
- 0
- 1
- Undefined
- None
In C++, any non-zero value is considered as true. The logical NOT (!) operator inverts the truthiness of its operand. So, if applied to a non-zero (true) operand, the output will be false, which is represented as 0.
Unlike if-else, switch-case in C++ does not support _______ type for case values.
- floating-point
- integer
- character
- boolean
In C++, switch-case constructs don't support floating-point types for case values. This means you can't use float or double values as cases. On the other hand, if-else structures can easily handle conditions based on floating-point values.
The maximum value that can be stored in an unsigned char is _______.
- 127
- 1024
- 256
- 255
The unsigned char data type in C++ uses 8 bits (1 byte) for storage. This means it can store values ranging from 0 to 2^8-1, which is 0 to 255.
A class whose objects cannot be created is known as a _______ class.
- abstract
- virtual
- static
- inline
An abstract class in C++ is a class that cannot be instantiated, meaning objects of the class cannot be created directly. Abstract classes are intended to be inherited by other classes.
Which of the following is not a legitimate reason to use function templates?
- To support multiple types with the same function logic
- To increase execution speed
- To reduce code size
- To create a list of unrelated types
Function templates are utilized primarily to enable generic programming, meaning writing code that works for multiple data types without repetition. While they may have indirect effects on execution speed or code size, using them to group unrelated types doesn't align with their primary purpose.
Which of the following data types has the smallest size in C++?
- int
- float
- char
- double
In C++, the char data type typically takes up 1 byte of memory, which is smaller than int, float, or double. The exact size of int can vary depending on the platform, but char is defined by the standard to always be at least 1 byte, making it the smallest of the given options.
What does the new operator do in C++?
- Deletes memory
- Compares memory
- Allocates memory
- Modifies memory
The new operator in C++ is used to allocate memory on the heap at runtime. When you use new, it returns a pointer to the beginning of the block of memory it allocated. This is essential for dynamic memory management, as it allows more flexible storage allocation during program execution.
When dynamically allocating an array of integers using new, which of the following syntax is correct?
- int* arr = new int;
- int arr = new int[10];
- int arr[10] = new int;
- int* arr = new int[10];
The correct way to dynamically allocate an array of integers in C++ using the new operator is int* arr = new int[10];. This syntax allocates memory for 10 integers on the heap and returns a pointer to the first element.
You are developing a multi-threaded application where multiple clients are sending data to a server. Each client is handled in a separate thread and communicates with the server in a loop. If a client sends an incorrect data format, the server should ignore it and wait for the next data. How might continue be used in this case?
- To terminate the client's thread.
- To send an error message back to the client.
- To disconnect the client.
- To skip the processing of the incorrect data and wait for the next input.
In a multi-threaded context, when a client sends data in an incorrect format, the server can utilize the continue statement to skip any further processing of that particular piece of data and return to the beginning of the loop to wait for the next data. This ensures that the server doesn't waste resources processing invalid data but remains ready to handle valid inputs.