Predict Output MCQs Based On C Programming Strings - Answer
#include <stdio.h>
int main() {char str1[] = "hello";char str2[] = "world";printf("%s", str1);printf("%s", str2);return 0;
}
A) helloworldB) hello worldC) helloworldD) Compilation error
Answer: A) helloworld
Explanation: str1 array contains the string "hello", and str2 contains the string "world". The printf function outputs str1 followed by str2, which results in "helloworld".
2. What is the output of the following C program?
#include <stdio.h>
int main() {char str[] = "hello";printf("%c", str[3]);return 0;
}
A) hB) eC) lD) o
Answer: C) l
Explanation: The str array contains the string "hello". The expression str[3] retrieves the character at index 3 of the array, which is 'l'. Hence program outputs 'l'.
3. What is the output of the following C program?
#include <stdio.h>int main() {
char str1[] = "hello";char str2[] = "world";printf("%s", str1 + 3);printf("%s", str2 + 1);return 0;
}A) loorldB) loorldwC) loworldD) helloworld
Answer: A) loorld
4. What is the output of the following C program?
#include <stdio.h>
int main() {char str[] = "hello";str[2] = '\0';printf("%s", str);return 0;
}A) helB) heC) helloD) Compilation error
Answer: B) he
Explanation: The str contains the string "hello". The statement str[2] = '\0' sets the third character of str to the null character, which terminates the string. The printf function then outputs the characters of str up to the null character, which is "he".
5. What is the output of the following C program?
#include <stdio.h>
int main() {int arr[] = {2, 3, 4};int *p = arr;printf("%d", *(p + 1));return 0;
}A) 2B) 3C) 4D) Compilation error