Which of the following is the correct way to initialize

2022

Which of the following is the correct way to initialize an array?

  1. A.

    int num[6] = (2, 4, 12, 5, 45, 5)

  2. B.

    int num[ ] = {2, 4, 12, 5, 45, 5}

  3. C.

    int num{6} = {2, 4, 12}

  4. D.

    int num(6) = {2, 4, 12, 5, 45, 5}

Attempted by 549 students.

Show answer & explanation

Correct answer: B

Correct C syntax for array initialization uses curly braces { } , not parentheses. Correct initialization example: int num[] = {2, 4, 12, 5, 45, 5}; You can omit the size in the square brackets; the compiler infers the array length from the number of initializer elements.

Explore the full course: Dsssb Tgt Computer Science Paper 2