0% found this document useful (0 votes)
83 views4 pages

Answer Array and Strings

The document contains a series of questions and answers related to C programming, specifically focusing on arrays, their initialization, and memory allocation. It includes code snippets and expected outputs for various scenarios, along with explanations for the answers. The document serves as a quiz or study guide for understanding array manipulation in C.

Uploaded by

lakshmiv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

Answer Array and Strings

The document contains a series of questions and answers related to C programming, specifically focusing on arrays, their initialization, and memory allocation. It includes code snippets and expected outputs for various scenarios, along with explanations for the answers. The document serves as a quiz or study guide for understanding array manipulation in C.

Uploaded by

lakshmiv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] are the elements present in the array of the following C code?

int array[5] = {5};

a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5
View Answer
Answer: b
Explanation: None.

2. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int a[2][3] = {1, 2, 3, 4, 5};
5. int i = 0, j = 0;
6. for (i = 0; i < 2; i++)
7. for (j = 0; j < 3; j++)
8. printf("%d", a[i][j]);
9. }

a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
View Answer
Answer: a
Explanation: None.

3. What is the output of the given code?

array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3],[0,0,0,0]]
if array1==array2
print "Equal"
else
print "Not equal"
end

a) [[1, 2, 3, 4], [0, 0, 0, 0]].


b) Equal
c) Not equal
d) None of the mentioned
View Answer
Answer: c
Explanation: Number of elements in both the arrays are not same hence they are
unequal
Output:
Not equal

4. What will be the output of the following C code?


1. #include <stdio.h>
2. void foo(int *ary[]);
3. int main()
4. {
5. int ary[2][3];
6. foo(ary);
7. }
8. void foo(int *ary[])
9. {
10. int i = 10, j = 2, k;
11. ary[0] = &i;
12. ary[1] = &j;
13. *ary[0] = 2;
14. for (k = 0;k < 2; k++)
15. printf("%d\n", *ary[k]);
16. }

a) 2 2
b) Compile time error
c) Undefined behaviour
d) 10 2
View Answer
Answer: a
Explanation: None.

5. What is the output of the following C


code?

int arr[] = {1, 2, 3};


printf("%d", *(arr + 1));

a) 1
b) 2
c) 3
d) Error
Answer:
b) 2

6.

How do you initialize an array in C?


(a) int arr[3] = (1,2,3);
(b) int arr(3) = {1,2,3};
(c) int arr[3] = {1,2,3};
(d) int arr(3) = (1,2,3);
7. What is the output of the following C code?
c
Copy code
#include <stdio.h>
int main() {
int arr[3][2] = {{1, 2}, {3, 4}, {5, 6}};
printf("%d", arr[2][1]);
return 0;
}

a) 1
b) 2
c) 4
d) 6

Answer: d) 6

What will be the output of the following code?


c
Copy code
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d", arr[1][2]);
return 0;
}

a) 1
b) 3
c) 4
d) 6

Answer: d) 6

How can you dynamically allocate a 2D array in C?

a) int**arr = malloc(5 * 5 * sizeof(int));


b) intarr[5][5];
c) int*arr = malloc(5 * sizeof(int *));
d) int**arr = malloc(5 * sizeof(int *)); for (int i = 0; i < 5; i++) arr[i]
= malloc(5 * sizeof(int));

How can you dynamically allocate a 2D array in C?

a) int**arr = malloc(5 * 5 * sizeof(int));


b) intarr[5][5];
c) int*arr = malloc(5 * sizeof(int *));
d) int**arr = malloc(5 * sizeof(int *)); for (int i = 0; i < 5; i++) arr[i]
= malloc(5 * sizeof(int));

Answer: d) int **arr = malloc(5 * sizeof(int *)); for (int i = 0; i < 5; i+


+) arr[i] = malloc(5 * sizeof(int));

You might also like