Question

In: Computer Science

Select all that applies. There may be more than one correct answers. Which of the following...

Select all that applies. There may be more than one correct answers. Which of the following is a correct initialization of a C string?

Group of answer choices

string s[20] = "C++";

string s = "C++";

char s[20];

char* s = "C++";

char s[20] = {'C', '+', '+', '\0'};

char s[20] = "";

char s[20] = "C++";

Solutions

Expert Solution

Given choices are:

  1. string s[20] = "C++";
  2. string s = "C++";
  3. char s[20];
  4. char* s = "C++";
  5. char s[20] = {'C', '+', '+', '\0'};
  6. char s[20] = "";
  7. char s[20] = "C++";

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.To represent a string, a set of characters are enclosed within double quotes (").'C' always treats a string a single data even though it contains whitespaces. A single character is defined using single quote representation. A string is represented using double quote marks.

Since it is an array of characters we cannot initialise it as just string.Hence 1 and 2 is incorrect

The third choice char s[20]; is actually declaration of the string of size 20.The basic syntax is char str_name[size];It doesn't come under initialisation.

Now the fourth choice char*s="C++" is correct as this initializes s to the first character of the readonly string "C++"(Pointer).A string initialized through a character pointer cannot be modified.If we try it produces undefines behavior,which is better to avoid.

The choice 5 is correct.When you initialize a character array by listing all of its characters separately then you must supply the '\0' character .To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word.So char s[20] = {'C', '+', '+', '\0'}; is correct

The 6th choice char s[20] = "";is incorrect as assigning string literals to char array is allowed only during declaration not initialisation.

The last choice char s[20] = "C++"; is correct as we can we can put the array size inside the [] of the declaration.

Hnece it is convinced that choices 4,5 and 7 are correct.


Related Solutions

From the options listed below, choose ALL correct answers. (There may be more than one.) In...
From the options listed below, choose ALL correct answers. (There may be more than one.) In the hunt to identify HTT, the disease-causing gene for Huntington’s disease (HD), it was discovered that human haplotypes A and C were highly associated with HD. The DNA sequence variants geneticists used to detect haplotypes A and C were: a) Extra repeats of a CAG codon located in the HTT gene b) Single-nucleotide polymorphisms located near the HTT gene c) Causal for Huntington’s disease...
Which of the following statements are correct (Select all that apply): Select one or more: A....
Which of the following statements are correct (Select all that apply): Select one or more: A. An income statement reports on financing activities. B. A balance sheet reports on investing and financing activities. C. The statement of cash flows reports on cash flows from operating, investing, and financing activities over a period of time. D. A balance sheet reports on a company's assets and liabilities over a period of time. E. The statement of equity reports on changes in the...
Which of the following statements are correct (Select all that apply): Select one or more: A....
Which of the following statements are correct (Select all that apply): Select one or more: A. A balance sheet reports on investing and financing activities. B. An income statement reports on financing activities. C. The statement of equity reports on changes in the accounts that make up equity. D. The statement of cash flows reports on cash flows from operating, investing, and financing activities over a period of time. E. A balance sheet reports on a company's assets and liabilities...
Which of the following statements are TRUE? Note that there may be more than one correct...
Which of the following statements are TRUE? Note that there may be more than one correct answer; select all that are true. There are countably infinite values of X in a continuous uniform distribution. For a continuous uniform distribution defined on the interval [a,b], P(X < a) and P(X > b) is undefined. The mean and the variance of a continuous uniform random variable are the same. In a continuous uniform distribution, the mean and the median are the same....
1. Which of the following describes an endocytosis? There may be more than one answer. Select...
1. Which of the following describes an endocytosis? There may be more than one answer. Select one or more: a. A cell engulfs a bacterium by pushing its cell membrane around the bacterium forming a phagosome that will pinched off from the cell membrane and enter the cytoplasm. b. Specific solutes accumulate along an indented cell membrane that will form into an endosome and it will pinch off from the cell membrane and enter the cytoplasm. c. A process that...
Which of the following is a characteristic of the chi-square distribution? Select all correct answers. Select...
Which of the following is a characteristic of the chi-square distribution? Select all correct answers. Select all that apply: The total area under the χ2-curve is equal to the degrees of freedom, df. The mean of the chi-square distribution is located to the left of the peak. The chi-square curve is skewed to the right. The chi-square curve is skewed to the left.
Which of the following is a redox reaction? More than one answer may be correct. HBr(aq)...
Which of the following is a redox reaction? More than one answer may be correct. HBr(aq) + KOH(aq) → H2O(l) + KBr(aq) SO3(g) + H2O(l) → H2SO4(aq) HBr(aq) + Na2S(aq) → NaBr(aq) + H2S(g) NH4+(aq) + 2 O2(g) → H2O(l) + NO3–(aq) + 2 H+(aq) 2 Na(s) + Cl2(g) → 2 NaCl(s) None of the above are redox reactions.
Which of the following statements is correct? More than 1 answer may be correct. a) Cilia...
Which of the following statements is correct? More than 1 answer may be correct. a) Cilia continue to work effectively in people with late-stage COPD b) The only cause of COPD is smoking c) People with COPD tend to develop chronic O2 retention d) Arterial blood was immediately drawn to analyze the levels of O2, CO2, and pH
2. There may be only one or there may be more than one correct response to...
2. There may be only one or there may be more than one correct response to this question. You need only mark any one of the possible correct responses to be marked correct. A - Touch the top of the electroscope with your finger B - Remove your finger from the top of the electroscope C - Touch the top of the electroscope with a glass rod that was just previously rubbed by silk D - Touch the top of...
1.Which of the following characterizes a relative polycythemia? There may be more than one answer. Select...
1.Which of the following characterizes a relative polycythemia? There may be more than one answer. Select one or more: a. A type of polycythemia that promotes erythropoiesis; therefore, increases the number of circulating erythrocytes above normal. b. A type of polycythemia that does not promote erythropoiesis but increases the number of circulating erythrocytes above normal. c. Hypovolemic shock d. Anemia e. High altitude 2.What is TRUE about diabetes? There may be more than one answer. Select one or more: a....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT