Question

In: Computer Science

Given: struct Person { int id;     int stats[3] }; Which is the correct way to...

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

Solutions

Expert Solution

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:

  • The data type that is derived from primary data types is known as a non-primitive data type.
  • The non-primitive data types are used to store the group of values.

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.


Related Solutions

Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
3. Write a program which divides a given array (int array []) into two parts: -...
3. Write a program which divides a given array (int array []) into two parts: - Left part with all elements with values <= (less than or equal) pivot (int pivot) and the - Right part with all elements with values > (larger than) pivot. You should ask the user to enter the values of: int pivot, int size and the array
Question 2 Write a definition of a struct called Employee, which contains three fields: employment_id, int;...
Question 2 Write a definition of a struct called Employee, which contains three fields: employment_id, int; salary, float; marriage_status, bool. Given the above Employee struct, declare a variable x with this data type, and assign the three fields, respectively, to the values: 1234, 34000.00, true . Create a one-dimension array, y, of this struct data type with a length 4. Pass y into a function: PrintArrayElement( …).   Finish the interface of this function, and loop over each array element inside...
Which of the following statements ARE CORRECT about hypothesis tests we cover in Stats I? Select...
Which of the following statements ARE CORRECT about hypothesis tests we cover in Stats I? Select all that apply! Question 3 options: We use the t-distribution when we conduct a hypothesis test about a population mean. Researchers get to choose the significance level. A few popular choices are 10%, 5% and 1%. However, it is not possible to choose 0%! The Rejection Region approach and the p-value approach always agree. Meaning, our conclusion concerning whether or not to reject Ho...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int month; // 1-12 for January-December int date; // 1-31 int year; // like 2020 Define the function void tomorrow(int& day, int& month, int& date, int& year); which updates the variables to represent the next day. Test in main().
Determine the memory address of A[3], A[10], A[20] a) given int A[] b) given short A[]...
Determine the memory address of A[3], A[10], A[20] a) given int A[] b) given short A[] c) given char A[]
Which of the following illustrates the correct way to present the scientific name of humans?
Which of the following illustrates the correct way to present the scientific name of humans? Homo sapiens Homo sapiens homo sapiens Homo Sapiens
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT