What is the output of the program: Pick ONE option
IBM technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What is the output of the program:
char *sample = "abc";
int main()
{
*sample = 'd';
printf("%s",sample);
return 0;
}
Pick ONE option
Show answer & explanation
Answer: D. run time error
The pointer sample points to a string literal "abc", which is stored in read-only memory. Attempting to modify it with *sample = 'd' causes undefined behavior—typically a segmentation fault or runtime crash on most systems. This is a runtime error, not a compile-time error, because the compiler permits pointer assignments to string literals without complaint.
Step-by-step Derivation:
char *sample = "abc"declares a pointer to a string literal stored in the read-only data segment.- Inside
main(),*sample = 'd'attempts to dereference the pointer and write to that memory location. - String literals in C are read-only; modifying them violates memory protection rules enforced by the operating system.
- The program compiles successfully (no syntax error), but crashes at runtime when it tries to write to protected memory.
- Result: Runtime error (typically segmentation fault or access violation).