Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

1,000 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 501–510 of 1,000
Topics in C Programming
Q.501 Easy Arrays & Strings
Consider the following code:

char str[] = "HELLO";
int len = 0;
while(str[len] != '\0') {
len++;
}
printf("%d", len);

What will be the output?
A 5
B 6
C 4
D Undefined behavior
Correct Answer:  A. 5
EXPLANATION

The string "HELLO" has 5 characters. The while loop counts characters until it encounters the null terminator '\0'. The output will be 5. The null terminator is not counted in the length.

Test
Q.502 Medium Arrays & Strings
For char arr[5] = {'a', 'b', 'c', 'd', 'e'}, is this a valid string?
A Yes, always valid
B No, missing null terminator
C Only if last element is '\0'
D Depends on compiler
Correct Answer:  B. No, missing null terminator
EXPLANATION

String functions expect null terminator. This array has no '\0', so it's a character array but NOT a proper string for str* functions.

Test
Q.503 Hard Arrays & Strings
What does the strspn() function do?
A Finds substring in a string
B Spans the initial segment containing only characters from specified set
C Compares two strings
D Reverses a string
Correct Answer:  B. Spans the initial segment containing only characters from specified set
EXPLANATION

strspn(str, charset) returns length of initial segment of str containing only characters from charset. Useful for token parsing.

Test
Q.504 Medium Arrays & Strings
What is the correct syntax to pass a 2D array to a function?
A void func(int arr[][])
B void func(int arr[3][4])
C void func(int **arr)
D Any of the above
Correct Answer:  B. void func(int arr[3][4])
EXPLANATION

First dimension can be omitted, but second must be specified: int arr[][4]. Option C (int **arr) is not equivalent - it's pointer to pointer.

Test
Q.505 Hard Arrays & Strings
In what scenario would you use memcpy() instead of strcpy()?
A memcpy() is always faster
B When copying binary data or strings without null terminators
C When the destination is smaller
D memcpy() is safer
Correct Answer:  B. When copying binary data or strings without null terminators
EXPLANATION

strcpy() assumes null-terminated strings. memcpy() copies exact number of bytes, suitable for binary data or embedded nulls.

Test
Q.506 Medium Arrays & Strings
What is the output of: char str[20]; scanf("%s", str); when input is 'Hello World'?
A Entire 'Hello World' is stored
B Only 'Hello' is stored (stops at whitespace)
C Compilation error
D Buffer overflow occurs
Correct Answer:  B. Only 'Hello' is stored (stops at whitespace)
EXPLANATION

%s format specifier stops reading at whitespace. To read entire line including spaces, use fgets() or %[^\n].

Test
Q.507 Hard Arrays & Strings
Which of the following correctly declares a pointer to an array (not array of pointers)?
A int *arr[5];
B int (*ptr)[5];
C int [5]*ptr;
D int &arr[5];
Correct Answer:  B. int (*ptr)[5];
EXPLANATION

Parentheses change precedence. int (*ptr)[5] is pointer to array of 5 ints. int *arr[5] is array of 5 pointers.

Test
Q.508 Easy Arrays & Strings
What will be the size in bytes of int arr[5] on a 32-bit system?
A 5 bytes
B 10 bytes
C 20 bytes
D Depends on compiler
Correct Answer:  C. 20 bytes
EXPLANATION

On 32-bit systems, int is typically 4 bytes. 5 elements × 4 bytes = 20 bytes total.

Test
Q.509 Hard Arrays & Strings
In string handling, what is the difference between fgets() and gets()?
A fgets() is slower
B fgets() includes newline character, gets() doesn't
C fgets() takes size parameter and is safer; gets() is unsafe (removed from C11)
D gets() works only with integers
Correct Answer:  C. fgets() takes size parameter and is safer; gets() is unsafe (removed from C11)
EXPLANATION

gets() has no buffer overflow protection and was removed in C11. fgets(str, size, stdin) is the safer alternative with size limiting.

Test
Q.510 Easy Arrays & Strings
What is the output of: int arr[] = {10, 20, 30}; printf("%d", *(arr+1));?
A 10
B 20
C 30
D Garbage value
Correct Answer:  B. 20
EXPLANATION

arr+1 points to second element (arr[1]). Dereferencing gives value 20.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips