AmbitionBox

Discover Best Places to work in India

C Programming

C Programming Questions and Answers

Showing 1 - 8 of 8 questions

1

In C, if you pass an array as an argument to a function, what actually gets passed?

a

Value of elements in array

b

First element of the array

c

Base address of the array

d

Address of the last element of array

correct answer c

Answer : Option C

Explanation :

The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.

2

What will be the output of the program ?

#include < stdio.h >int 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);
    return0;
}
a

2, 1, 15

b

1, 2, 5

c

3, 2, 15

d

2, 3, 20

correct answer c
Answer :

Option C

Explanation :

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

Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: 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

3

What will be the output of the program ?

#include < stdio.h >int main()
{
    staticint a[2][2] = {1, 2, 3, 4};
    int i, j;
    staticint *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), 
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return0;
}
a

1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4

b

1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2

c

1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3

d

1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3

correct answer c
Answer :

Option C

4

What will be the output of the program ?

#include < stdio.h >int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}
a

2, 3, 4, 5

b

1, 2, 3, 4

c

0, 1, 2, 3

d

3, 2, 1 0

correct answer b
Answer :

Option B

Explanation :

Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.
Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4

Step 3: int i; The variable i is declared as an integer type.
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.
Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.

Hence the output of the program is 1,2,3,4

5

What will be the output of the program if the array begins at address 65486?

#include < stdio.h >int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return0;
}
a

65486, 65488

b

65486, 65486

c

65486, 65490

d

65486, 65487

correct answer b
Answer :

Option B

Explanation :

Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.
Step 2: printf("%u, %u\n", 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

6

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);
    return0;
}
a

1200, 1202, 1204

b

1200, 1200, 1200

c

1200, 1204, 1208

d

1200, 1202, 1200

correct answer b
Answer :

Option B

Explanation :

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

7

Which of the following is correct way to define the function fun() in the below program?

#includeint main()
{
    int a[3][4];
    fun(a);
    return0;
}
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])
{
}

correct answer a
Answer :

Option A

Explanation :

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.

8

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.

a

1

b

1,4

c

2,3

d

2,4

correct answer b
Answer :

Option B

Explanation :

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.

Select a company to compare with

vs

Similar Companies