Arrays And Strings - Study Mode

[#86] What will be the output of the program if the array begins at address 65486? #include<stdio.h>
void main()
{
int arr[] = {12, 14, 15, 23, 45}

printf("%u, %u", arr, &arr)

}
Correct Answer

(D) 65486, 65486

Explanation

Solution: >> int arr[] = {12, 14, 15, 23, 45}

The variable arr is declared as an integer array and initialized. >> printf ("%u, %u", arr, &arr)

Here, The base address of the array is 65486. => arr, &arr is pointing to the base address of the array arr. Hence the output of the program is 65486, 65486.

[#87] What will be the output of the program? #include<stdio.h>
void main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7}

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

}
Correct Answer

(B) 4

Explanation

Solution: The sizeof function return the given variable. Example: float a=10

sizeof(a) is 4 bytes >> float arr[] = {12.4, 2.3, 4.5, 6.7}

The variable arr is declared as an floating point array and it is initialized with the values. >> printf ("%d", sizeof(arr)/sizeof(arr[0]))

The variable arr has 4 elements. The size of the float variable is 4 bytes. Hence 4 elements x 4 bytes = 16 bytes sizeof(arr[0]) is 4 bytes Hence 16/4 is 4 bytes Hence the output of the program is '4'.

[#88] Which of the following is correct way to define the function fun() in the below program? #include<stdio.h>
void main()
{
int a[3][4]

fun(a)

}
Correct Answer

(A) void fun(int p[][4]){}

Explanation

Solution: void fun(int p[][4]){ } is the correct way to write the function fun(). while the others are considered only the function fun() is called by using call by reference.

[#89] Which of the following statements are correct about the program below? #include<stdio.h>
void main()
{
int size, i

scanf("%d", &size)

int arr[size]

for(i=1

i<=size

i++)
{
scanf("%d", arr[i])

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

}
}
Correct Answer

(A) The code is erroneous since the statement declaring array is invalid.

Explanation

Solution: The statement int arr[size]

produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here. Example: int arr[10]

One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]

is placed after a function call scanf().

[#90] Which of the following statements are correct about an array? 1. The array int num[26]

can store 26 elements. 2. The expression num[1] designates the very first element in the array. 3. It is necessary to initialize the array at the time of declaration. 4. The declaration num[SIZE] is allowed if SIZE is a macro.
Correct Answer

(B) 1, 4

Explanation

Solution: 1. The array int num[26]

can store 26 elements. This statement is true. 2. The expression num[1] designates the very first element in the array. This statement is false, because it designates the second element of the array. 3. It is necessary to initialize the array at the time of declaration. This statement is false. 4. The declaration num[SIZE] is allowed if SIZE is a macro. This statement is true, because the MACRO just replaces the symbol SIZE with given value. Hence the statements '1' and '4' are correct statements.