Exercise: Python

Questions for: Arrays

Which of the following statements are correct about the program below?
#include<stdio.h>

int main()
{
    int size, i;
    scanf("%d", &size);
    int arr[size];
    for(i=1; i<=size; i++)
    {
        scanf("%d", arr[i]);
        printf("%d", arr[i]);
    }
    return 0;
}
A:
The code is erroneous since the subscript for array used in for loop is in the range 1 to size.
B:
The code is erroneous since the values of array are getting scanned through the loop.
C:
The code is erroneous since the statement declaring array is invalid.
D:
The code is correct and runs successfully.
Answer: C

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().

Which of the following statements mentioning the name of the array begins DOES NOT yield the base address?
1: When array name is used with the sizeof operator.
2: When array name is operand of the & operator.
3: When array name is passed to scanf() function.
4: When array name is passed to printf() function.
A:
A
B:
A, B
C:
B
D:
B, D
Answer: B

The statement 1 and 2 does not yield the base address of the array. While the scanf() and printf() yields the base address of the array.

Which of the following is correct way to define the function fun() in the below program?
#include<stdio.h>

int main()
{
    int a[3][4];
    fun(a);
    return 0;
}
A:
void fun(int p[][4])
{
}
B:
void fun(int *p[4])
{
}
C:
void fun(int *p[][4])
{
}
D:
void fun(int *p[3][4])
{
}
Answer: A

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.

What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %u\n", arr, &arr[0], &arr);
    return 0;
}
A:
1200, 1202, 1204
B:
1200, 1200, 1200
C:
1200, 1204, 1208
D:
1200, 1202, 1200
Answer: B

Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array and initialized.

Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here,

The base address of the array is 1200.

=> arr, &arr is pointing to the base address of the array arr.

=> &arr[0] is pointing to the address of the first element array arr. (ie. base address)

Hence the output of the program is 1200, 1200, 1200

What will be the output of the program ?
#include<stdio.h>

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%d\n", sizeof(arr)/sizeof(arr[0]));
    return 0;
}
A:
5
B:
4
C:
6
D:
7
Answer: B

The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes

Step 1: 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.

Step 2: printf("%d\n", 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'.

Ad Slot (Above Pagination)
Quiz