C Programming Online Quiz
Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.
Q 1 - What is the output of the following program?
#include<stdio.h> void f() { static int i; ++i; printf("%d", i); } main() { f(); f(); f(); }
Answer : D
Explanation
1 2 3, A static local variables retains its value between the function calls and the default value is 0.
Q 2 - Which operator is used to continue the definition of macro in the next line?
Answer : D
Explanation
\, the first two are stringize and token pasting operators respectively. There is no such operator called $.
Q 3 - What is the output of the following program?
#include<stdio.h> main() { int a[3] = {2,1}; printf("%d", a[a[1]]); }
Answer : B
Explanation
1, The inner indirection evaluates to 1, and the value at index 1 for outer indirection is 1.
Q 4 - What is the output of the following program?
#include<stdio.h> int* f() { int x = 5; return &x; } main() { printf("%d", *f()); }
Answer : D
Explanation
It is invalid to return local variable address as the local variable gets removed after the functions execution is completed.
Q 5 - What is the output of the following program?
#include<stdio.h> main() { char *s = "Abc"; while(*s) printf("%c", *s++); }
Answer : A
Explanation
Loop continues until *s not equal to ‘\0’, hence printing “Abc” where character is fetched first and address is incremented later.
Q 6 - In the standard library of C programming language, which of the following header file is designed for basic mathematical operations?
Answer : A
Explanation
math.h is a header file in the standard library designed for basic mathematical operations
Q 7 - Which standard library function can return a pointer to the last occurrence of a character in a string?
Answer : B
Explanation
The strrchr() function finds string for the last occurrence of a character and place the pointer to the last occurrence of character.
Q 8 - In the given below code, the P2 is
Typedef int *ptr; ptr p1, p2;
Answer : B
Explanation
Ptr is an alias to int*.
Q 9 - What will be the output of the following program?
#include<stdio.h> int main() { const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x); return 0; }
Answer : D
Explanation
The above program will return error
#include<stdio.h> int main() { const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x); return 0; }
Q 10 - If, the given below code finds the length of the string then what will be the length?
#include<stdio.h> int xstrlen(char *s) { int length = 0; while(*s!='\0') {length++; s++;} return (length); } int main() { char d[] = "IndiaMAX"; printf("Length = %d\n", xstrlen(d)); return 0; }
Answer : B
Explanation
Here, *s is char pointer that holds character string. To print whole string we use printf("%s",s) by using base address. s contains base address (&s[0]) and printf will print characters until '\0' occurs. *s only gives first character of input string, but s++ will increase the base address by 1 byte. When *s=='\0' encountered, it will terminate loop.
#include<stdio.h> int xstrlen(char *s) { int length = 0; while(*s!='\0') {length++; s++;} return (length); } int main() { char d[] = "IndiaMAX"; printf("Length = %d\n", xstrlen(d)); return 0; }