C Programming Quiz

1. What is the standard identifier for the first element in an array?

a) 0
b) 1
c) -1
d) None of the above

Answer:

a) 0

Explanation:

In C programming, array indices start at 0. Therefore, the first element of an array is accessed using index 0.

2. Which of the following is a logical operator in C?

a) &&
b) ==
c) =>
d) <=

Answer:

a) &&

Explanation:

The '&&' operator is a logical AND operator in C. It is used to combine two conditions.

3. What does the printf function do?

a) Reads input
b) Writes output
c) Allocates memory
d) Opens a file

Answer:

b) Writes output

Explanation:

The printf function in C is used to write output to the standard output, typically the screen.

4. How do you declare a pointer in C?

a) int *p;
b) int &p;
c) int p;
d) pointer p;

Answer:

a) int *p;

Explanation:

In C, a pointer is declared using an asterisk (*) before the pointer name. For example, int *p; declares a pointer to an integer.

5. Which header file is required for using the malloc and free functions?

a) <stdio.h>
b) <conio.h>
c) <memory.h>
d) <stdlib.h>

Answer:

d) <stdlib.h>

Explanation:

The malloc and free functions are part of the standard library, so the <stdlib.h> header file is required to use these functions.

6. What is the purpose of the return 0; statement in a C program?

a) To exit the program
b) To return control to the operating system
c) To indicate that the program executed successfully
d) All of the above

Answer:

d) All of the above

Explanation:

The return 0; statement in the main function of a C program indicates successful program termination and returns control to the operating system.

7. What does the 'sizeof' operator in C return?

a) The size of the data type
b) The memory address
c) The number of elements in an array
d) The value of a variable

Answer:

a) The size of the data type

Explanation:

The sizeof operator in C is used to determine the size (in bytes) of a data type or a variable.

8. Which of the following is the correct way to declare a multi-dimensional array in C?

a) int arr[3,4];
b) int arr[3][4];
c) int [3][4] arr;
d) array arr[3][4];

Answer:

b) int arr[3][4];

Explanation:

In C, a multi-dimensional array is declared using rows and columns like int arr[3][4];, representing a 3×4 array.

9. What is a segmentation fault in C?

a) A syntax error
b) An attempt to divide by zero
c) An error due to accessing memory that the program does not have access to
d) A compilation error

Answer:

c) An error due to accessing memory that the program does not have access to

Explanation:

A segmentation fault occurs when a program attempts to access a region of memory that it is not allowed to access, or attempts to access memory in a way that is not allowed.

10. Which of the following is true about the const keyword in C?

a) It makes a variable modifiable
b) It makes a variable unmodifiable
c) It is used to define constants
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

The const keyword in C is used to declare constants and make variables unmodifiable. Once a const variable is assigned a value, it cannot be altered by the program.

11. Which of the following correctly declares a function in C?

a) int function(int x, int y);
b) function int(int x, int y);
c) function(int x, int y) : int;
d) int(x, y) function;

Answer:

a) int function(int x, int y);

Explanation:

In C, a function is declared with the return type first (int), followed by the function name and parameters in parentheses.

12. In C, which library function is used to compare two strings?

a) compare();
b) strcomp();
c) strcmp();
d) strcompare();

Answer:

c) strcmp();

Explanation:

The strcmp() function from the string.h library is used in C to compare two strings.

13. In C, which function is used to allocate memory without initializing it?

a) malloc()
b) calloc()
c) realloc()
d) free()

Answer:

a) malloc()

Explanation:

The malloc() function is used to allocate a specified amount of memory without initializing it. Unlike calloc(), which initializes the allocated memory.

14. Which of the following is true about a static variable in C?

a) It is initialized each time the function is called
b) It retains its value between function calls
c) It is destroyed every time the function exits
d) It cannot be initialized with a value

Answer:

b) It retains its value between function calls

Explanation:

Static variables in C retain their value between function calls and are not reinitialized each time the function is called.

15. What is the purpose of the #include preprocessor directive in C?

a) To add comments to the code
b) To include the contents of a file in the program
c) To define constants
d) To create a new namespace

Answer:

b) To include the contents of a file in the program

Explanation:

The #include directive is used to include the contents of a specified file into the current source file.

16. What is the purpose of the continue statement in a loop in C?

a) It terminates the loop
b) It forces the next iteration of the loop to start
c) It skips the rest of the code and goes to the next condition check
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

The continue statement in C skips the remaining code inside the loop for the current iteration and jumps to the next iteration of the loop.

17. What is the effect of the #define directive in C?

a) It creates a new variable
b) It includes a file into the program
c) It defines a macro
d) It declares a function

Answer:

c) It defines a macro

Explanation:

The #define directive in C is used to define a macro, which is a fragment of code that is given a name. Whenever the macro name is encountered by the compiler, it is replaced by the contents of the macro.

18. Which of the following operators checks if two values are not equal in C?

a) !=
b) <>
c) ~=
d) =!

Answer:

a) !=

Explanation:

The != operator in C is used to check if two values are not equal.

19. How are command-line arguments passed to a main() function in C?

a) int main()
b) int main(int argc, char *argv[])
c) int main(char argc, char *argv[])
d) int main(int argc, int argv[])

Answer:

b) int main(int argc, char *argv[])

Explanation:

Command-line arguments are passed to a main() function in C using int main(int argc, char *argv[]). Here argc represents the number of arguments, and argv[] is an array of pointers to the arguments.

20. Which data type is suitable for storing a large text such as a book in C?

a) char
b) int
c) char array
d) double

Answer:

c) char array

Explanation:

A char array is suitable for storing large texts such as books in C, as it can hold multiple characters, including special characters and spaces.

21. What is the return type of the calloc() function in C?

a) int*
b) void*
c) char*
d) float*

Answer:

b) void*

Explanation:

The calloc() function in C returns a void pointer. It allocates memory and initializes all bits to zero.

22. What does the NULL macro represent in C?

a) A macro representing zero value for integer types
b) A macro representing zero value for floating-point types
c) A macro representing a zero value pointer
d) A macro representing an empty string

Answer:

c) A macro representing a zero value pointer

Explanation:

In C, NULL is a macro representing a zero value pointer, used in pointer initialization and comparisons.

23. Which of the following is not a storage class specifier in C?

a) static
b) register
c) volatile
d) extern

Answer:

c) volatile

Explanation:

volatile is a type qualifier in C, not a storage class specifier. The storage class specifiers are static, register, extern, and auto.

24. What is the purpose of a function prototype in C?

a) To terminate a function
b) To define a function
c) To declare the existence and signature of a function
d) To initialize a function

Answer:

c) To declare the existence and signature of a function

Explanation:

A function prototype in C is a declaration of a function that specifies the function's name, return type, and parameters without the body.

25. Which keyword is used to prevent any changes in the variable within a C program?

a) constant
b) fixed
c) immutable
d) const

Answer:

d) const

Explanation:

The const keyword is used in C to declare a constant value that cannot be modified after it is set.

26. What is the purpose of the lvalue and rvalue in C?

a) lvalue refers to memory location and rvalue refers to the value stored in the memory location
b) lvalue refers to a register and rvalue refers to a constant value
c) lvalue refers to a literal value and rvalue refers to a variable
d) lvalue and rvalue are types of variables

Answer:

a) lvalue refers to memory location and rvalue refers to the value stored in the memory location

Explanation:

In C, an lvalue (left value) refers to an expression that points to a memory location, whereas an rvalue (right value) is the value stored at that location.

27. What does the auto keyword do in C?

a) Automatically initializes variables
b) Indicates automatic storage duration
c) Converts a variable type automatically
d) Generates automatic constants

Answer:

b) Indicates automatic storage duration

Explanation:

The auto keyword in C specifies automatic storage duration for a variable, which is the default for local variables.

28. What is the effect of the break statement inside a switch statement in C?

a) Pauses the execution of the case statements
b) Exits the switch statement
c) Jumps to the next case statement
d) None of the above

Answer:

b) Exits the switch statement

Explanation:

The break statement in a switch case in C causes the control to exit from the switch statement, preventing fall-through to subsequent cases.

29. How can you pass an array to a function in C?

a) By passing the array elements as separate arguments
b) By passing the array name as an argument
c) By passing the first element of the array
d) Arrays cannot be passed to functions in C

Answer:

b) By passing the array name as an argument

Explanation:

In C, an array can be passed to a function by passing the array name, which is a pointer to the first element of the array.

30. What is a macro in C programming?

a) A function that is expanded inline
b) A block of code that can be reused
c) A preprocessor directive for code substitution
d) A tool for debugging

Answer:

c) A preprocessor directive for code substitution

Explanation:

In C, a macro is a preprocessor directive that defines a name for a block of code or a value, allowing code substitution before the program is compiled.

31. What does the exit(0) function do in a C program?

a) Terminates the program with an error code 0
b) Terminates the program successfully
c) Pauses the program
d) Restarts the program

Answer:

b) Terminates the program successfully

Explanation:

The exit(0) function in C is used to terminate a program execution successfully. An exit code of 0 typically indicates successful completion.

32. What is a pointer in C programming?

a) A variable that stores the address of another variable
b) A special keyword used to create variables
c) A function that points to another function
d) A label to an array

Answer:

a) A variable that stores the address of another variable

Explanation:

A pointer in C is a variable that stores the memory address of another variable. Pointers are used for various purposes, including dynamic memory allocation, arrays, functions, and data structures.

33. What is the purpose of the & operator in C?

a) To determine the address of a variable
b) To perform a bitwise AND operation
c) To concatenate two strings
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

The & operator in C serves two purposes: when used with a variable, it gives the address of the variable (address-of operator), and when used between two numbers, it performs a bitwise AND operation.

34. In C, what is a header file?

a) A file containing the first few lines of a C program
b) A file used for linking programs
c) A file containing function definitions
d) A file that contains preprocessor directives and declarations

Answer:

d) A file that contains preprocessor directives and declarations

Explanation:

In C, a header file contains preprocessor directives, function prototypes, and definitions of types and macros, which are included in a C program using the #include directive.

35. What is the purpose of the do…while loop in C?

a) To execute a block of code at least once before checking the condition
b) To execute a block of code repeatedly until a condition is true
c) To iterate over arrays
d) To execute a block of code only if a condition is true

Answer:

a) To execute a block of code at least once before checking the condition

Explanation:

The do…while loop in C executes its block of code at least once and then continues to execute it as long as the specified condition is true.

36. What does a static variable within a function do?

a) It retains its value between different function calls
b) It gets destroyed and re-initialized in each function call
c) It increases the execution speed of the function
d) It makes the function thread-safe

Answer:

a) It retains its value between different function calls

Explanation:

A static variable within a function retains its value between successive calls to that function, maintaining its previous value across various calls.

37. What is the correct syntax to return a pointer from a function in C?

a) int* function();
b) pointer function();
c) int function*();
d) *int function();

Answer:

a) int* function();

Explanation:

In C, to return a pointer from a function, the return type should be a pointer type. For example, int* function(); declares a function returning a pointer to an int.

38. Which of the following is true about the const qualifier in C?

a) It can only be used with integers
b) It makes a variable unmodifiable
c) It increases the storage size of the variable
d) It's a storage class specifier

Answer:

b) It makes a variable unmodifiable

Explanation:

The const qualifier in C is used to declare a constant that cannot be modified after its initialization.

39. How is dynamic memory allocation done in C?

a) Using functions like malloc() and free()
b) Automatic by the compiler
c) Using the new and delete keywords
d) Through global variables

Answer:

a) Using functions like malloc() and free()

Explanation:

Dynamic memory allocation in C is done using functions like malloc() to allocate memory and free() to deallocate memory.

40. In C, which header file is necessary for using the printf and scanf functions?

a) <conio.h>
b) <stdio.h>
c) <string.h>
d) <stdlib.h>

Answer:

b) <stdio.h>

Explanation:

The printf and scanf functions are part of the standard input/output library in C, and they require the inclusion of the <stdio.h> header file.

41. What does the goto statement do in C?

a) Terminates the program immediately
b) Skips to another part of the program using a label
c) Creates an infinite loop
d) Jumps out of the current function

Answer:

b) Skips to another part of the program using a label

Explanation:

The goto statement in C allows the program to jump to a specified label. It is generally advised to use goto sparingly as it can make the program flow difficult to follow.

42. Which of the following correctly declares an array of pointers in C?

a) int *ptr[10];
b) int ptr[10];
c) int &ptr[10];
d) int ptr*[10];

Answer:

a) int *ptr[10];

Explanation:

int *ptr[10]; declares an array of 10 integer pointers in C.

43. What is the purpose of the strncpy function in C?

a) To concatenate two strings
b) To find the length of a string
c) To copy a specified number of characters from one string to another
d) To compare two strings

Answer:

c) To copy a specified number of characters from one string to another

Explanation:

The strncpy function copies a specified number of characters from one string to another, ensuring that the destination string does not get overflowed.

44. What is the default return type of a function in C if it is not specified?

a) int
b) void
c) float
d) char

Answer:

a) int

Explanation:

In C, if the return type of a function is not explicitly specified, it defaults to returning an integer (int).

45. What is a union in C?

a) A data structure that can hold any type of data
b) A data structure where all members share the same memory location
c) A user-defined data type that groups different types of variables
d) A special kind of structure that can access database

Answer:

b) A data structure where all members share the same memory location

Explanation:

A union in C is a user-defined data type where all members share the same memory location. A union can store different data types, but only one at a time.

46. What is a void pointer in C?

a) A pointer that points to a function
b) A pointer that cannot store an address
c) A pointer that can hold the address of any data type
d) A pointer that points to a character

Answer:

c) A pointer that can hold the address of any data type

Explanation:

A void pointer, also known as a generic pointer, is a special type of pointer that can be pointed at objects of any data type. It is declared as void * and is used for general purpose storage.

47. What is the main difference between malloc() and calloc() functions in C?

a) malloc() allocates single block of memory, whereas calloc() allocates multiple blocks
b) malloc() does not initialize the allocated memory, whereas calloc() initializes the allocated memory to zero
c) malloc() is used for memory allocation, whereas calloc() is used for memory deallocation
d) malloc() can allocate memory of any size, whereas calloc() can only allocate memory up to a fixed size

Answer:

b) malloc() does not initialize the allocated memory, whereas calloc() initializes the allocated memory to zero

Explanation:

Both malloc() and calloc() are used for dynamic memory allocation in C, but calloc() additionally initializes the allocated memory to zero, while malloc() leaves it uninitialized.

48. What does the realloc() function do in C?

a) Allocates memory
b) Frees allocated memory
c) Resizes previously allocated memory
d) Checks the size of allocated memory

Answer:

c) Resizes previously allocated memory

Explanation:

The realloc() function is used to resize a memory block that was previously allocated with malloc() or calloc(). It allows the memory block to be expanded or contracted, and can move it to a new location if necessary.

49. How do you free dynamically allocated memory in C?

a) Using the remove() function
b) Using the free() function
c) By setting the pointer to NULL
d) Dynamically allocated memory is automatically freed

Answer:

b) Using the free() function

Explanation:

Dynamically allocated memory in C must be manually freed using the free() function. This helps prevent memory leaks by releasing memory that is no longer needed.

50. Which of the following is a possible consequence of not freeing memory in a C program?

a) Syntax error
b) Memory leak
c) Increased program speed
d) Automatic memory deallocation

Answer:

b) Memory leak

Explanation:

Failing to free memory that has been allocated dynamically (e.g., with malloc() or calloc()) can lead to a memory leak. This is where memory that is no longer needed is not returned to the system, potentially causing a program to consume more and more memory over time.

51. In the context of C programming, what is a memory leak?

a) The process of allocating memory dynamically
b) A situation where allocated memory is not used efficiently
c) A failure to release unused memory, causing a program to consume more memory over time
d) A security vulnerability that exposes memory content

Answer:

c) A failure to release unused memory, causing a program to consume more memory over time

Explanation:

A memory leak in C programming refers to the failure to free memory that has been allocated dynamically and is no longer needed. Over time, this can cause the program to consume an increasing amount of memory, potentially leading to performance issues or crashes.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top