C Programming Quiz
Welcome to the C programming quiz. Test your knowledge and skills on various topics of C programming by answering multiple choice questions. there are total 15 questions with answers. Each question has four options and only one of them is correct. You can also view the correct answer and explanation for each question at the end of the each question. Good luck and have fun!
Q1. What is the advantage of using functions in C language?
A) To avoid repetition of code
B) To make the program modular and easy to understand
C) To facilitate code reuse and maintenance
D) All of the above
Answer: D) All of the above
Explanation: Functions are subprograms that can be called from anywhere in the program and carry out a specific task. Functions make programs modular and simple to understand, reduce the need for repetitive code, and make code reuse and maintenance easier.
Q2. What is the difference between static and dynamic memory allocation in C language?
A) Static memory allocation is done at compile time and dynamic memory allocation is done at run time
B) Static memory allocation is done using malloc() and free() functions and dynamic memory allocation is done using arrays and structures
C) Static memory allocation is fixed and cannot be changed and dynamic memory allocation can be changed as per the requirement
D) Both A and C
Answer: D) Both A and C
Explanation: Arrays and structures are used at compile time to allocate static memory. The amount of memory allotted is fixed and cannot be altered while the program is running. Using the malloc(), calloc(), realloc(), and free() functions, dynamic memory allocation is carried out at runtime. While the program is running, the size of the memory allocation can be changed as needed.
Q3. What is the meaning of mid-level programming language?
A) A programming language that supports both low-level and high-level features
B) A programming language that is neither low-level nor high-level
C) A programming language that is intermediate between assembly language and machine language
D) None of the above
Answer: A) A programming language that supports both low-level and high-level features
Explanation: Because it supports both low-level features like direct memory access and hardware manipulation as well as high-level features like structured programming and rich library functions, C is referred to as a mid-level programming language.
Q4. What are some of the applications of C language?
A) Operating systems
B) Databases
C) Compilers
D) All of the above
Answer: D) All of the above
Explanation: A general-purpose programming language called C can be used for a wide range of applications, including operating systems (like Windows and Linux), databases (like MySQL and Oracle), compilers (like GCC and Clang), and many more.
Q5. What are some of the benefits of using a structured programming language like C?
A) It reduces complexity and improves readability
B) It facilitates debugging and testing
C) It enhances modularity and maintainability
D) All of the above
Answer: D) All of the above
Explanation: A paradigm known as "structured programming" divides the program into logical components like functions, loops, conditional statements, etc. By taking a top-down approach, it lessens complexity and enhances readability. By identifying errors and flaws, it simplifies debugging and testing. By allowing code reuse and modification, it improves modularity and maintainability.
Q6. What are some of the built-in operators in C language?
A) Arithmetic operators (+, -, *, /, %)
B) Relational operators (==, !=, <, >, <=, >=)
C) Logical operators (&&, ||, !)
D) All of the above
Answer: D) All of the above
Explanation: Symbols known as operators carry out specific operations on operands (values or variables).
Q7. What is the difference between = and == operators in C language?
A) = is an assignment operator and == is an equality operator
B) = is an equality operator and == is an assignment operator
C) = and == are both assignment operators
D) = and == are both equality operators
Answer: A) = is an assignment operator and == is an equality operator
Explanation:The assignment operator = transfers the right operand's value to the left operand. For instance, x = 10 gives the variable x the value 10. The equality operator == compares the operand values, returning 1 if they are equal and 0 otherwise. For instance, the expression x == 10 returns 1 if x equals 10 and 0 otherwise.
Q8. What is the use of #include directive in C language?
A) To include a header file in the program
B) To include a source file in the program
C) To include a library function in the program
D) To include a comment in the program
Answer: A) To include a header file in the program
Explanation: Using the preprocessor command #include, the compiler is told to include a header file in the program. Function, macro, constant, and other declarations that can be used by the program are found in the header file. For instance, the standard input/output header file, which contains functions like printf(), scanf(), etc., is included when #include stdio.h> is used.
Q9. What is the difference between printf() and scanf() functions in C language?
A) printf() prints a formatted output to the standard output device (screen) and scanf() reads a formatted input from the standard input device (keyboard)
B) printf() reads a formatted input from the standard input device (keyboard) and scanf() prints a formatted output to the standard output device (screen)
C) printf() and scanf() both print a formatted output to the standard output device (screen)
D) printf() and scanf() both read a formatted input from the standard input device (keyboard)
Answer: A) printf() prints a formatted output to the standard output device (screen) and scanf() reads a formatted input from the standard input device (keyboard)
Explanation: The C language's printf() and scanf() library functions are used for input and output operations. A format string and optional arguments are passed to printf(), which prints the results to the standard output area (screen). As an illustration, printf("%dn", x); outputs the value of x and the newline character. Using the keyboard as the standard input device, scanf() reads a format string, pointers to variables, and a format string. The command scanf("%d", &x); for instance, reads an integer value from the keyboard and stores it in the variable x.
Q10. What is the difference between break and continue statements in C language?
A) break terminates the loop or switch statement and continue skips the current iteration of the loop
B) break skips the current iteration of the loop or switch statement and continue terminates the loop
C) break and continue both terminate the loop or switch statement
D) break and continue both skip the current iteration of the loop or switch statement.
Answer: A) break terminates the loop or switch statement and continue skips the current iteration of the loop.
Explanation: The control statements break and continue are used to change the loop's or switch's direction of execution. Break signals the end of a loop or switch statement and moves control to the statement that follows. Break, for instance, can be used to end a loop when a specific requirement is satisfied. continue instructs the loop to skip the current iteration and move on to the following one. Continue, for instance, can be used to skip some loop iterations based on a condition.
Q11. What is the difference between local and global variables in C language?
A) Local variables are declared inside a function and global variables are declared outside any function
B) Local variables have a scope within the function where they are declared and global variables have a scope throughout the program
C) Local variables are created when the function is called and destroyed when the function is exited and global variables are created at the start of the program and destroyed at the end of the program
D) All of the above
Answer: D) All of the above
Explanation: Based on their scope and lifetime, local and global variables are two different types of variables in the C language. The scope of local variables is limited to the function in which they are declared. When the function is called, they are created, and when the function is ended, they are destroyed. Global variables have a scope across the entire program and are declared outside of any function. At the beginning of the program, they are created, and at its conclusion, they are destroyed.
Q12. What is recursion in C language?
A) The process of calling a function by itself
B) The process of calling a function by another function
C) The process of calling a function by a pointer
D) The process of calling a function by a macro
Answer: A) The process of calling a function by itself
Explanation: In the C programming language, recursion is a technique that enables a function to call itself. Recursive problems, like factorial, the Fibonacci series, etc., can be solved using recursion. To stop the recursive calls and prevent infinite loops, recursion needs a base case.
Q13. What is portability in C language?
A) The ability to run a program on different platforms with little or no modification
B) The ability to write a program in different languages with little or no modification
C) The ability to use different compilers for a program with little or no modification
D) The ability to use different libraries for a program with little or no modification
Answer: A) The ability to run a program on different platforms with little or no modification
Explanation: The C language's portability allows programs to run with little to no modification on a variety of platforms, including Windows, Linux, and others. This is because most compilers and operating systems support a set of standard rules and guidelines that the C language adheres to.
Q14. What is the difference between a header file and a source file in C language?
A) A header file contains declarations of functions, macros, constants, etc., and a source file contains definitions of functions, variables, etc.
B) A header file has the extension .h and a source file has the extension .c
C) A header file can be included in a source file using #include directive and a source file can be compiled into an executable file
D) All of the above
Answer: D) All of the above
Explanation: In the C programming language, there are two different types of files: header files and source files. Functions, macros, constants, and other items that can be used in a source file are declared in a header file. The #include directive can be used to include a header file, which has the.h extension, in a source file. Definitions for variables, functions, and other elements that carry out the program's logic can be found in a source file. A compiler can turn a source file with the.c extension into an executable file.
Q15. What is the difference between a macro and a function in C language?
A) A macro is a preprocessor directive that replaces a name with a code snippet and a function is a subprogram that performs a specific task
B) A macro is executed at compile time and a function is executed at run time
C) A macro does not perform type checking and a function performs type checking
D) All of the above
Answer: D) All of the above
Explanation: In the C language, there are two ways to define reusable code: macros and functions. A macro is a preprocessor directive that, prior to compilation, replaces a name with a piece of code. For instance, the macro #define SQUARE(x) (x*x) creates calculates the square of x. A function is a type of subprogram that can be called from any other function in the program to carry out a specific task. For instance, the function int square(int x) return x*x; defines how to find the square of x. A macro does not perform type checking before it is executed at compile time, which could result in errors or inefficiency. At runtime, a function performs type checking to guarantee correctness and safety.