In: Computer Science
Given:
struct Person
{
int id;
int stats[3]
};
Which is the correct way to initialise an array of Persons?
1. |
struct Person persons[2] = {7, "8,9,3", 8, "2,5,9"}; |
|
2. |
struct Person persons[2] = "7, {8,9,3}, 8, {2,5,9}"; |
|
3. |
struct Person persons[2] = "7, "8,9,3", 8, "2,5,9"; |
|
4. |
struct Person persons[2] = {7, {8,9,3}, 8, {2,5,9}}; |
Which of the following is not a primitive type in the C language?
1. |
string |
|
2. |
int |
|
3. |
long |
|
4. |
char |
Given:
struct Person
{
int id;
int stats[3]
};
Which is the correct way to access the 2nd member of stats in an instance named John?
1. |
John.stats[2] |
|
2. |
John->stats[2] |
|
3. |
John->stats[1] |
|
4. |
John.stats[1] |
Given:
struct Person
{
int id;
int stats[3]
};
Which is the correct way to initialise an instance of Person?
1. |
struct Person John = "5,{4,5,6}"; |
|
2. |
struct Person John = {"5", "4,5,6"}; |
|
3. |
struct Person John = {5, {4,5,6}}; |
|
4. |
Person John = {5, {4,5,6}}; |
Instead of using parallel arrays with a key and value array, we can create a derived type with members that represent the key – value pair.
True
False
A derived type is a collection of other types.
True
False
Given:
struct Person
{
int id;
int stats[3]
};
*John.stats[0]; will retrieve the fist element of stats in a Person instance named John.
True
False
What is the key word used to create a user defined (derived) type in the C language?
1. |
class |
|
2. |
object |
|
3. |
collection |
|
4. |
struct |
Which is the correct way to initialise an array of Persons?
Answer: 4. struct Person persons[2] = {7, {8,9,3}, 8, {2,5,9}};
Explanation: The array of structures can be initialize using the same syntax as that for initializing arrays.
Which of the following is not a primitive type in the C language?
Answer: 1. string
Explanation:
Which is the correct way to access the 2nd member of stats in an instance named John?
Answer: 4. John.stats[1]
Explanation: Member of structure is accessed through dot (.) operator by its instance so it would be John.stats.
And here we want to access 2nd member of stats. It would be stored at index 1 as indexing in array starts from 0. So it would be John.stats[1]
Which is the correct way to initialise an instance of Person?
Answer: 3. struct Person John = {5, {4,5,6}};
Explanation: We must have to declare person as type of struct. Id and stats are of type of int. So value we assigned must be int. And stats is an array so it should be initialise the same way we initialise any array.
Instead of using parallel arrays with a key and value array, we can create a derived type with members that represent the key – value pair.
Answer: True
Explanation: We can achieve the same using structure having data member array of key and value.
A derived type is a collection of other types.
Answer: True
Explanation: A derived type is formed by using one or more basic types in combination.
Example: Array, Structure, Union
*John.stats[0]; will retrieve the fist element of stats in a Person instance named John.
Answer: False
Explanation: If you are using pointer then you have to access data member using arrow (->) operator rather than (.) operator.
What is the key word used to create a user defined (derived) type in the C language?
Answer: 4. struct
Explanation: A structure is a user defined data type in C. A structure creates a data type that can be used to group items of possibly different types into a single type.