Arrays And Strings - Study Mode

[#81] The . . . . . . . . function appends not more than n characters.
Correct Answer

(C) strncat()

[#82] What will be the output of the following code? void main()
{
int a[10]

printf("%d %d", a[-1], a[12])

}
Correct Answer

(D) Garbage vlaue Garbage Value

Explanation

Solution: In c compiler does not check array with its bounds, value at the computed location is displayed.

[#83] What does the following declaration mean? int ( *ptr ) [ 10 ]

Correct Answer

(B) ptr is a pointer to an array of 10 integers

Explanation

Solution: The declaration int (*ptr)[10]

means that ptr is a pointer to an array of 10 integers. Here's the breakdown: int specifies the type of elements in the array (integers). (*ptr) indicates that ptr is a pointer. [10] specifies that the pointer points to an array of 10 integers. Thus, ptr is not an array of pointers, nor an array of integers, but a pointer to an array of integers with a size of 10.

[#84] What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes? #include<stdio.h>
void main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}

printf("%u, %u", a+1, &a+1)

}
Correct Answer

(C) 65480, 65496

Explanation

Solution: >> int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}

The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions. >> printf ("%u, %u
", a+1, &a+1)

The base address(also the address of the first element) of array is 65472. For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480 Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes". Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496 Hence the output of

[#85] What will be the output of the program? #include<stdio.h>
int main()
{
int arr[1] = {10}

printf("%d", 0[arr])

return 0

}
Correct Answer

(C) 10

Explanation

Solution: >> int arr[1]={10}

The variable arr[1] is declared as an integer array with size '2' i.e. arr[0] and arr[1] and it's first element is initialized to value '10'(means arr[0]=10) and arr[1] = garbage value or zero >> printf ("%d", 0[arr])

It prints the first element value of the variable arr. Hence the output of the program is 10.