OA. free
Free
IBM Core Computer Science Core Computer Science Medium

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

Choose 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:

  1. char *sample = "abc" declares a pointer to a string literal stored in the read-only data segment.
  2. Inside main(), *sample = 'd' attempts to dereference the pointer and write to that memory location.
  3. String literals in C are read-only; modifying them violates memory protection rules enforced by the operating system.
  4. The program compiles successfully (no syntax error), but crashes at runtime when it tries to write to protected memory.
  5. Result: Runtime error (typically segmentation fault or access violation).