Tuesday, April 18, 2023

C Programming Predict Output Strings MCQs Set 3 - Answer

Predict Output MCQs Based On C Programming Strings - Answer

1. What is the output of the following C program?

#include <stdio.h>

int main() {
char str1[] = "hello";
char str2[] = "world";
printf("%s", str1);
printf("%s", str2);
return 0;
}

A) helloworld
B) hello world
C) hello
   world
D) 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) h
B) e
C) l
D) 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) loorld
B) loorldw
C) loworld
D) helloworld

Answer: A) loorld

Explanation: The expression str1 + 3 evaluates to a pointer to the fourth character of str1, which is 'l'. The printf function then outputs the remaining characters of str1 starting from the fourth character, which is "lo". Similarly, the expression str2 + 1 evaluates to a pointer to the second character of str2, which is 'o'. The printf function then outputs the remaining characters of str2 starting from the second character, which is "orld". Hence, final output will be "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) hel
B) he
C) hello
D) 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) 2
B) 3
C) 4
D) Compilation error

Answer: B) 3

Explanation: The pointer p points to the first element of the array arr. The expression *(p + 1) adds 1 to the pointer p to point to the second element of the array, and then evaluates to the value pointed to by the new pointer, which is 3.




No comments:

Post a Comment