All mock tests are FREE till 31 December — register to save your progress
Unit 3 · Programming Languages and Computer Graphics Paper 2 CS · December 2022 (Shift 1)

Which of the following is correct using the constructor?

A. string s1;
B. string s2 (s1);
C. string s3{7};
D. string s4{'a'};
E. string s5{7, 'a'};

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: A. A, B, D, E only

Explanation

string s1; (default), string s2(s1); (copy), string s4{'a'}; and string s5{7, 'a'}; (initializer lists of chars) are listed as valid. Following the official key (after Stroustrup's examples), string s3{7}; is treated as an error, since there is no string(int) constructor.

More Programming Languages and Computer Graphics questions