In: Computer Science
In C++, the concept of a "bag" may be represented as (click all that apply):
a vector of pointers of a specific object type |
a vector of objects |
a vector of generic pointers |
an array of whole numbers |
The C++ keywords used to implement the principle of least privilege are (click all that apply):
public |
#include |
const |
struct |
ifndef |
class |
If a class Employee has a data member Date hireDate , and Employee's member functions need to access private data members of Date ... (click all that apply)
..Date has to declare the class Employee as its friend, or... |
...Employee has to declare Date as its friend, or... |
...trick question -- private members are accessible only by other members of the same class. That's the whole point of making members private. |
What is the output from the code block below:
int i = 8; cout << i++ << " "; cout << ++i << endl;
8 10 |
9 10 |
9 9 |
8 9 |
1. A,B,D are correct
Explaination:
Here, Bag is a container class which can contain int, float, char
or any object or even a pointer. But, all elements in a Bag
container should be of same type.
Here, option A is a vector of pointers of a specific object type which implies all pointers are of the same type as mentioned in option itself
Option B is a vector of objects which implies the objects are of
same type
Because when declaring vector, object or data type is mentioned in
the declaration itself
e.g. vector<int> v1;
Option C is a vector of generic pointers i.e. all pointers are of different datatypes
So, a vector of different datatypes can't be elements of a bag as bag contains elements of same datatype
Option D is an array of whole numbers which are all of same datatype i.e. the elements can go from 0 to any positive integer which can either be int or long int
But, while declaring an array, we need to mention the datatype. So, all elements are of same datatype i.e. short int or int or long int or long long int
2. const
Explaination: Here, const is the keyword in C++ which is used to implement the principle of least privilege. When const is used with a variable of any datatype, it makes that variable's value fixed. Any attempt to change its value while running program, is marked as error. No method has any privilege to change its value.
3. Date has to declare the class Employee as its friend
Explaination:
For Employee's member functions to access private members of Date
class, Date class will have to declare Employee class as its friend
as follows:
class Date {
private:
int x;
friend class Employee;
};
4. 8 10
Explaination: Here, initial value of i is 8
In the line `cout<<i++<<" ";
i is post-incremented, i.e. first value of i is printed, then it is increased by 1
So, 8 is printed and value of i becomes 9
Then, i is pre-incremented, i.e. first value is incremented, then printed
So, the value becomes 10 and is printed