ЁЯУв Too many exams? DonтАЩt know which one suits you best? Book Your Free Expert ЁЯСЙ call Now!


    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
    More IT Operating System Questions
    ask-question