
Arrays, strings and structures are the building blocks of every C program, and they are full of subtle traps that examiners love: array decay, padding, string literals and storage classes. This second C set walks through all of them with short code snippets. Assume a 64-bit system where int is 4 bytes and stdio.h and string.h are included.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Q1. What is the value of a[3]?
int a[5] = {1, 2};
a) 1
b) 2
c) 0
d) Garbage value
Q2. What is printed?
int a[10];
printf("%zu", sizeof(a) / sizeof(a[0]));
a) 4
b) 10
c) 40
d) 8
Q3. On a 64-bit system, what is printed?
void f(int a[]) {
printf("%zu", sizeof(a));
}
int main() {
int x[10];
f(x);
}
a) 40
b) 10
c) 8
d) 4
Q4. What is printed?
char s[] = "hello";
printf("%zu %zu", sizeof(s), strlen(s));
a) 5 5
b) 6 5
c) 5 6
d) 6 6
Q5. What happens here?
char *s = "hello"; s[0] = 'H';
a) s becomes "Hello"
b) Compilation error
c) Undefined behaviour - string literals must not be modified
d) s becomes "H"
Q6. strcmp(s1, s2) returns 0 when:
a) s1 is shorter than s2
b) The two strings are equal
c) s1 is greater than s2
d) The strings are at the same address only
Q7. What is printed?
void f() {
static int c = 0;
c++;
printf("%d", c);
}
int main() { f(); f(); f(); }
a) 111
b) 123
c) 000
d) 012
Q8. What does the extern keyword do in extern int count; ?
a) Defines count and allocates memory for it
b) Declares count, telling the compiler it is defined in another place (often another file)
c) Makes count constant
d) Makes count visible only in this file
Q9. What is the default storage class of a variable declared inside a function?
a) static
b) extern
c) auto
d) register
Q10. Which operation is NOT allowed on a variable declared with register?
a) Reading its value
b) Assigning to it
c) Taking its address with &
d) Using it in a loop
Q11. On a typical system with 4-byte int and 4-byte alignment, what is sizeof this struct?
struct S {
char c;
int i;
};
a) 5
b) 8
c) 4
d) 6
Q12. With 4-byte int and 4-byte alignment, what is sizeof this union?
union U {
int i;
char c[6];
};
a) 6
b) 8
c) 10
d) 4
Q13. If p is a pointer to a struct, p->x is equivalent to:
a) *p.x
b) (*p).x
c) &p.x
d) p.x
Q14. For int a[3][4], counting in ints from the start of the array, at what offset is a[1][2] stored?
a) 5
b) 6
c) 7
d) 9
Q15. Which is true about char str[3] = "abc"; in C?
a) It is a compilation error in C
b) It is valid and str is null-terminated
c) It is valid in C, but str has no room for the null terminator
d) It causes a runtime error immediately
Recommended MCQs
C Programming MCQs - Pointers and Dynamic Memory (Set 1)
C++ MCQs - Classes, Objects, Constructors and References (Set 1)
Comments
Post a Comment