Conceptual Question-Answer
Conceptual question-answer
1. Q: What is a pointer in C?
A: A pointer in C is a variable that holds the memory address of another variable.
2. Q: How do you declare a pointer variable in C?
A: A pointer variable is declared by using the asterisk (*) symbol before the variable name. For example, `int *ptr;` declares a pointer to an integer.
3. Q: What is the difference between `malloc()` and `free()` functions in C?
A: `malloc()` is used to dynamically allocate memory on the heap, while `free()` is used to release that allocated memory when it is no longer needed.
4. Q: What is a function prototype in C?
A: A function prototype in C declares the function's name, return type, and the number and types of its parameters. It provides the compiler with the necessary information about the function before it is called.
5. Q: How do you pass arguments to a function by reference in C?
A: Arguments can be passed by reference in C by using pointers. The address of the variable is passed to the function, allowing the function to modify the original variable.
6. Q: What is recursion in C?
A: Recursion in C is a programming technique where a function calls itself. It enables solving complex problems by breaking them down into smaller, simpler subproblems.
7. Q: What is the purpose of the `const` keyword in C?
A: The `const` keyword in C is used to define constants that cannot be modified. It ensures that the value of a variable remains unchanged throughout the program.
8. Q: What is the `static` keyword used for in C?
A: In C, the `static` keyword has different meanings depending on the context. It can be used to create local variables with static storage duration or to limit the scope of functions and global variables to the current file.
9. Q: What is the difference between `strcpy()` and `strncpy()` functions in C?
A: `strcpy()` function copies a string from source to destination until it encounters a null terminator, while `strncpy()` function allows specifying the maximum number of characters to be copied, even if a null terminator is not encountered.
10. Q: What is a structure in C?
A: A structure in C is a composite data type that allows grouping related variables under a single name. It is used to represent a record or an entity with multiple properties.
11. Q: What is the difference between `NULL` and `nullptr` in C?
A: `NULL` is a macro defined as a null pointer constant, typically defined as `(void *)0`, whereas `nullptr` is a keyword introduced in C++11 specifically for representing a null pointer.
12. Q: What is the purpose of the `const` keyword in C?
A: The `const` keyword is used to declare constants in C. It ensures that the value of a variable cannot be modified once it is assigned.
13. Q: What is the purpose of the `static` keyword in C?
A: The `static` keyword can be used with variables and functions. For variables, it preserves the value across different function calls. For functions, it limits the scope of the function to the file in which it is defined.
14. Q: What are the storage classes in C?
A: The storage classes in C are `auto`, `register`, `static`, and `extern`. They determine the lifetime, scope, and linkage of variables.
15. Q: What is the ternary operator in C?
A: The ternary operator, denoted by `? :`, is a shorthand conditional operator that evaluates an expression based on a condition and returns one of two values depending on whether the condition is true or false.
16. Q: What is the purpose of the `sizeof` operator in C?
A: The `sizeof` operator is used to determine the size, in bytes, of a variable or data type.
17. Q: What is the difference between `calloc` and `malloc` in C?
A: Both `calloc` and `malloc` are used for dynamic memory allocation, but `calloc` initializes the allocated memory to zero, while `malloc` does not.
18. Q: What is the purpose of the `volatile` keyword in C?
A: The `volatile` keyword is used to indicate that a variable can be modified by external entities, such as hardware or other threads, and should not be optimized by the compiler.
19. Q: What is a pointer in C?
A: A pointer is a variable that stores the memory address of another variable. It allows direct manipulation of memory and enables dynamic memory allocation.
20. Q: What is the purpose of the `enum` keyword in C?
A: The `enum` keyword is used to define an enumeration, which is a user-defined data type consisting of named constants. It provides a way to associate names with values for improved code readability.
21. Q: What is the purpose of the `const` keyword in C?
A: The `const` keyword is used to declare variables as constants, indicating that their values cannot be modified once assigned.
22. Q: What is the difference between `calloc()` and `malloc()` in C?
A: `malloc()` is used to allocate memory dynamically, while `calloc()` not only allocates memory but also initializes it with zeros.
23. Q: How can you convert a string to an integer in C?
A: The `atoi()` function can be used to convert a string to an integer in C.
24. Q: What are the storage classes in C?
A: C provides four storage classes: `auto`, `register`, `static`, and `extern`, which determine the scope and lifetime of variables.
25. Q: What is the purpose of the `return` statement in C?
A: The `return` statement is used to exit a function and return a value to the caller.
26. Q: What is the difference between `strcpy()` and `strncpy()` functions?
A: `strcpy()` copies a string until it encounters a null character, while `strncpy()` allows you to specify the maximum number of characters to copy.
27. Q: What is a function prototype in C?
A: A function prototype declares the function's name, return type, and parameters, allowing the compiler to perform type checking and detect errors.
28. Q: What is the purpose of the `continue` statement in C?
A: The `continue` statement is used within loops to skip the remaining code within the loop body and move to the next iteration.
29. Q: What is the difference between a structure and a union in C?
A: A structure is a collection of different data types, while a union is a single variable that can hold different data types, but only one at a time.
30. Q: How can you read a character from the keyboard without waiting for the user to press Enter in C?
A: The `getchar()` function can be used to read a single character from the keyboard without waiting for the Enter key.
31. Q: What is the difference between `malloc()` and `calloc()` in C?
A: `malloc()` is used to dynamically allocate memory block of a specified size, while `calloc()` additionally initializes the allocated memory to zero.
32. Q: How do you pass arguments by reference in C?
A: In C, arguments can be passed by reference by using pointers. By passing the address of a variable, modifications made to the parameter inside a function will affect the original variable.
33. Q: What is the purpose of the `const` keyword in C?
A: The `const` keyword is used to declare constants, variables whose values cannot be modified once assigned. It ensures that the value remains constant throughout the program.
34. Q: What is the difference between a stack and a queue in C?
A: In C, a stack is a Last-In-First-Out (LIFO) data structure, while a queue is a First-In-First-Out (FIFO) data structure.
35. Q: What is the purpose of the `static` keyword in C?
A: The `static` keyword has multiple uses in C. It can declare static variables with a lifespan that extends throughout the program's execution and limit the scope of variables to a particular file.
36. Q: How do you read and write a text file in C?
A: To read a text file in C, you can use the `fopen()` function to open the file in read mode and then use `fscanf()` or `fgets()` to read the file's contents. To write to a text file, you can use `fopen()` to open the file in write mode and then use `fprintf()` or `fputs()` to write data to the file.
37. Q: What is a function prototype in C?
A: A function prototype in C declares the function's name, return type, and parameters before the actual function definition. It serves as a forward declaration, allowing the compiler to verify function calls and promote code modularity.
38. Q: How do you concatenate strings in C?
A: In C, strings can be concatenated using the `strcat()` function from the standard library or by manually copying the characters from one string to the end of another.
39. Q: What is the ternary operator in C?
A: The ternary operator (`?:`) is a shorthand conditional operator in C that takes three operands. It evaluates a condition and returns one of two expressions based on the condition's result.
40. Q: What is the purpose of the `#include` directive in C?
A: The `#include` directive is used in C to include external header files in a program. It allows access to pre-defined functions, constants, and definitions provided by the included files.
41. Q: What is the difference between `malloc()` and `calloc()` in C?
A: `malloc()` is used to allocate memory dynamically, while `calloc()` is used to allocate and initialize memory, setting all bytes to 0.
42. Q: What is the purpose of the `const` keyword in C?
A: The `const` keyword is used to declare constants, indicating that the value of a variable cannot be modified after initialization.
43. Q: What are the storage classes in C?
A: C has four storage classes: `auto`, `register`, `static`, and `extern`. They define the scope, visibility, and lifetime of variables.
44. Q: What is the purpose of the `strcpy()` function in C?
A: The `strcpy()` function is used to copy a string from one character array to another.
45. Q: What is the role of the `typedef` keyword in function pointers?
A: `typedef` can be used to create a custom name for a function pointer type, making the code more readable and maintainable.
46. Q: What is the difference between a null pointer and a void pointer?
A: A null pointer points to nothing, while a void pointer is a generic pointer that can be used to point to any data type.
47. Q: What is recursion in C? Provide an example.
A: Recursion is a technique in which a function calls itself directly or indirectly. Example: calculating the factorial of a number using recursion.
48. Q: What is the purpose of the `getchar()` and `putchar()` functions in C?
A: `getchar()` is used to read a single character from the standard input (keyboard), while `putchar()` is used to write a character to the standard output (console).
49. Q: What is the difference between a function declaration and a function definition in C?
A: A function declaration provides information about the function's name, return type, and parameters, while a function definition includes the actual implementation of the function.
50. Q: Explain the concept of file handling in C.
A: File handling in C involves reading from and writing to files using functions such as `fopen()`, `fclose()`, `fread()`, `fwrite()`, `fprintf()`, and `fscanf()`.
0 Comments