📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • ✖

      Question

      A C function attempts to find a substring.

      #include #include char* find_substring(char* haystack, char* needle) {     if (haystack == NULL || needle == NULL) {         return NULL;     }     return strstr(haystack, needle); } int main() {     char* text = "Hello World";     char* pattern = NULL; // Potential bug here     char* result = find_substring(text, pattern);     if (result != NULL) {         printf("Found: %s ", result);     } else {         printf("Not found or invalid input. ");     }     return 0; } What will be the output of this C program?
      A Found: World Correct Answer Incorrect Answer
      B Not found or invalid input. Correct Answer Incorrect Answer
      C A segmentation fault (crash). Correct Answer Incorrect Answer
      D Found: (null) Correct Answer Incorrect Answer
      E A compile-time error. Correct Answer Incorrect Answer

      Solution

      Correct Answer: B (The if (haystack == NULL || needle == NULL) check correctly handles the NULL pattern, causing the function to return NULL and print "Not found or invalid input.")

      Practice Next
      ask-question