ExamVeda
Login
Home
31
What is right way to Initialize array?
Discuss
Answer & Solution
Answer: Option A
Solution:

option (B), (C) and (D) are incorrect because array declaration syntax is wrong. Only square brackets([]) must be used for declaring an array.

32
What will be the output of the program?
#include<stdio.h>
void main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
}
Discuss
Answer & Solution
Answer: Option A
Solution:

>> int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25.

>> int i, j, m; The variable i, j, m are declared as an integer type.

>> i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

>> j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

>> m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

>> printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m

Hence the output of the program is 3, 2, 15.

33
What will be the output of following program code?
#include <stdio.h>
int main(void)
{
    char p;
    char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
    p = (buf + 1)[5];
    printf("%d", p);
    return 0;
}
Discuss
Answer & Solution
Answer: Option C
Solution:

x[i] is equivalent to *(x + i),
so (buf + 1)[5] is *(buf + 1 + 5), i.e. buf[6].

34
An array elements are always stored in ________ memory locations.
Discuss
Answer & Solution
Answer: Option A
Solution:
In C programming, an array is a data structure that stores multiple elements of the same type. The key feature of an array is that its elements are stored in sequential memory locations. Here’s a detailed explanation:

1. Memory Contiguity:
When an array is declared, the compiler allocates a block of memory large enough to hold all the elements. These elements are stored one after the other in contiguous memory addresses. For example, if you declare an array of 5 integers, the memory allocated for the array will be continuous, and each integer will be placed next to the previous one.

2. Accessing Elements:
Due to the sequential storage, the array elements can be accessed easily using their index. The index represents the position of the element in the array, and since the memory is sequential, calculating the memory address of any element becomes straightforward. The address of any element in the array can be calculated as:
Address=Base Address+(Index×Size of each element)

Here, the Base Address is the memory address of the first element in the array.

3. Efficiency:
The sequential nature of arrays makes accessing and iterating through elements efficient. Since the elements are stored contiguously, the CPU can access them quickly by simply moving through the memory locations.

4. Contrast with Other Storage Methods:
Unlike arrays, other data structures like linked lists store elements in non-contiguous memory locations. This requires pointers to link elements together, which can lead to more complex memory management and slower access times compared to arrays.

5. Implications for Multidimensional Arrays:
In multidimensional arrays (e.g., 2D arrays), the elements are still stored sequentially in memory, typically in row-major order. This means the elements of the first row are stored first, followed by the elements of the second row, and so on.

In summary, the sequential storage of array elements is a fundamental characteristic of arrays in C, contributing to their simplicity and efficiency in memory usage and access.
35
Let x be an array. Which of the following operations are illegal?
I.   ++x
II. x+1
III. x++
IV. x*2
Discuss
Answer & Solution
Answer: Option D
Solution:
int x[10]; * x will store the base address of array. *
Statement I, III and IV is invalid.

Statement I and III : ++x and x++ are throwing en error while compile (lvalue required as increment operand )
Since, x is storing in the address of the array which is static value which cannot be change by the operand.

Statement IV : x*2 is also throw an error while compile (invalid operands to binary * (have 'int *' and 'int') )

Statement II : x+1 is throw a warning: assignment makes integer from pointer without a cast [enabled by default]
36
What is the maximum number of dimensions an array in C may have?
Discuss
Answer & Solution
Answer: Option E
No explanation is given for this question. Let's Discuss on Board
37
Size of the array need not be specified, when
Discuss
Answer & Solution
Answer: Option A
Solution:
In C programming, the size of the array does not need to be specified when initialization is a part of definition.

Initialization during Definition:
When an array is defined and initialized at the same time, the size of the array can be omitted. The size is automatically determined by the number of elements provided in the initialization. For example:
int arr[] = {1, 2, 3, 4, 5}; // Size is determined by the number of elements

Declaration:
When declaring an array without initializing it, the size must be specified if the array is not initialized. For example:
int arr[5]; // Size must be specified

Formal Parameter:
In function prototypes or definitions where an array is used as a formal parameter, the size does not need to be specified. For example:
void function(int arr[]); // Size not required

All of These:
This option is incorrect because the size must be specified in cases other than initialization during definition.

Therefore, the correct answer is Option A: Initialization is a part of definition.
38
Consider the following type definition.
typedef char x[10];
x myArray[5];

What will sizeof(myArray) be ? (Assume one character occupies 1 byte)
Discuss
Answer & Solution
Answer: Option C
Solution:
In this case, the type definition typedef char x[10]; creates a new type x, which is an array of 10 characters. Then, an array of 5 elements of type x is declared as x myArray[5];.

To calculate the size of myArray, you can multiply the size of one element (sizeof(x)) by the number of elements (5):

sizeof(myArray) = sizeof(x) * 5

Since each element x is an array of 10 characters, and assuming each character occupies 1 byte, the size of x is 10 bytes. Therefore:

sizeof(myArray) = 10 bytes * 5 = 50 bytes

So, sizeof(myArray) will be 50 bytes.
39
What will be printed after execution of the following code?
void main()
{
      int arr[10] = {1,2,3,4,5};
      printf("%d", arr[5]);
}
Discuss
Answer & Solution
Answer: Option D
Solution:

When an array is partially initialized at the time of declaration then the remaining elements of the array is initialized to 0 by default.

40
What will be the output of the following program?
void main()
{
      char str1[] = "abcd";
      char str2[] = "abcd";
      if(str1==str2)
            printf("Equal");
      else
            printf("Unequal");
}
Discuss
Answer & Solution
Answer: Option B
Solution:

Strings are compared using strcmp() function defined under string.h header file.