C Questions and Answers – General Utilities – 3

«
»

This set of C Multiple Choice Questions & Answers focuses on “General Utilities – 3”.

1. void free(void *ptr) deallocates the memory previously allocated by a call to _______ ________ or _________
a) malloc,getenv,abort
b) calloc,malloc,exit
c) calloc,malloc,realloc
d) exit,getenv,abort
View Answer

Answer: c
Explanation: The free() function causes the space to be deallocated that are pointed to by ptr, that is, made available for further allocation. No action occurs if ptr is a null pointer. Otherwise, if the argument does not match a pointer earlier returned by the calloc. malloc, or realloc function.
advertisement

2. The ________ function returns no value.
a) malloc()
b) realloc()
c) free()
d) calloc()
View Answer

Answer: c
Explanation: The calloc() function is used to return either a null pointer or a pointer to the allocated space. The malloc() function is used to return either a null pointer or a pointer to the allocated space. The realloc() function is used to return either a null pointer or a pointer to the possibly moved allocated space. The free() function returns no value.

3. What is returned by the function if the space cannot be allocated by the function malloc(), realloc() and calloc()?
a) value
b) error
c) null pointer
d) no value
View Answer

Answer: c
Explanation: The pointer returned points to the lowest byte address of the allocated space. A null pointer is returned, if the space cannot be allocated.
advertisement
advertisement

4. What is the function of the void *realloc(void *str, size-t size);?
a) allocates space for an array of str objects, each of whose size is size
b) allocates space for an object whose size is specified by size and whose value is indeterminate
c) changes the size of the object pointed to by str to the size specified by size
d) causes the space pointed to by str to be deallocated, that is, made available for further allocation
View Answer

Answer: c
Explanation: void *realloc(void *str, size-t size);
The realloc() function is used to change the size of the object which is pointed to by str to the size specified by size.

5. Which among the given function causes abnormal program termination ?
a) exit()
b) abort()
c) atexit()
d) getenv()
View Answer

Answer: b
Explanation: void abort (void);
The abort() function causes abnormal program termination to occur, and comes out directly from that place.
advertisement

6. Which of the given statement is true with respect to the function atexit()?
a) The atexit() function cannot return to its caller
b) The atexit() function is used to return zero if the registration succeeds, nonzero if it fails
c) The atexit() function returns no value
d) The atexit() function causes abnormal program termination to occur
View Answer

Answer: b
Explanation: The prototype of function is int atexit (void (*func) (void) );
The atexit() function is used to register the function pointed to by func, to be called without arguments at normal program termination.

7. The behavior is undefined if more than one call to the exit function is executed by a program.
a) true
b) false
View Answer

Answer: a
Explanation: The exit() function causes normal program termination to occur. The behavior is undefined if more than one call to the exit function is executed by a program.
advertisement

8. Which function searches an environmenr list that are provided by the host environment?
a) getenv()
b) system()
c) srand()
d) rand()
View Answer

Answer: a
Explanation: The getenv() searches for the environment string pointed to, by name and returns the associated value to the string.The syntax of function is char *getenv(const char *name).

9. The system() function passes the string pointed to by string to the host environment to be executed by a command processor in an implementation-defined manner.

advertisement
int system(const char *string);

a) true
b) false
View Answer

Answer: a
Explanation: The system function returns nonzero only if a command processor is available provided if the argument is a null pointer. The system function returns an implementation-defined value, if the argument is not a null pointer.

10. What will be the output of the following C code?

int main(void)
{
    int rc;
    rc = system("time");
    exit(0);
}

a) produces error
b) no value is returned
c) returns the time
d) nothing can be said
View Answer

Answer: c
Explanation: The system() function is used to pass the string pointed to by string to the host environment to be executed by a command processor in an implementation-defined manner.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter