Saturday, October 1, 2016

C Programming Quiz - 1 (1st October 2016)


C Programming Quiz - 1 (1st October 2016) with answer


1. Predict the output:

int main()
{
            int number[5]={1,2,3,4,5};
            printf("%d\n", number[5]);
            return 0;
}

A. 5
B. 0
C. Error
D. Any random number

Answer: D

Description: This will print any random number as number [5] is out of boundary of declared array. In c language, it is programmer’s responsibility to track array index out of bound error.


2. Predict the output:

int main(){
            int matrixA[3][3]={};
            printf("MatrixA[1][0] = %d\n",  matrixA[1][0] );
            return 0;

}
A. 0
B. 3
C. Error
D. 10

Answer: A
MatrixA[1][0] automatically initialized to zero due to compile time initialization.


3. Predict the output:

#include<stdio.h>
#include<string.h>
int main()
{
            char str1[10]="abc", str2[10]="xyz", str3[10]="pqr", str4[10];
            printf("%s",strcat(strcat(strcpy(str4, str3),str1),str2));
            return 0;
}
A. pqrabcxyz
B. pqrxyz
C. abcxyzpqr
D. Error

Answer: A
Here, inner most function strcpy will be executed first; and then outer functions strcat will be executed.


4. Predict the output:

#include<stdio.h>
int main(){
    char *p, str[10]="Ahmedabad";
    p = &str;
    printf("%c",*p+1);
    return 0;
}
A. A
B. h
C. m
D. B

Answer: D
This will print B, *p is A (i.e. 65), when added 1 gives B (i.e. 66 )


5. Predict the output:

#include<stdio.h>
void main(){
    char arr[10]="The Indian Programmers";
    printf("%s",arr);
}
A. The Indian
B. The Indian Programmers
C. The
D. None of these

Answer: A
This will print first ten characters, as size of arr is 10.



No comments:

Post a Comment