C - Control Statements Practice Test.3

  1. What is a function in C?

    A) A variable declaration

    B) A block of code that performs a specific task

    C) A reserved keyword

    D) A data type

    Answer: B) A block of code that performs a specific task

  2. What is the syntax to declare a function in C?

    A) function return_type function_name(parameters);

    B) return_type function_name(parameters) function;

    C) function_name(parameters) return_type function;

    D) function(parameters) return_type function_name;

    Answer: A) function return_type function_name(parameters);

  3. Which keyword is used to call a function in C?

    A) call

    B) invoke

    C) run

    D) None, functions are automatically executed

    Answer: D) None, functions are automatically executed

  4. What is a function prototype in C?

    A) It defines the implementation of a function.

    B) It is used to declare a function's existence and provide its implementation.

    C) It is used to indicate that a variable or function is defined in another file.

    D) It is used to make a function static.

    Answer: C) It is used to indicate that a variable or function is defined in another file.

  5. What is the maximum number of arguments a function can accept in C?

    A) 64

    B) 256

    C) 1024

    D) There is no fixed limit.

    Answer: D) There is no fixed limit.

  6. What is the return type of a function that doesn't return any value in C?

    A) void

    B) int

    C) double

    D) float

    Answer: A) void

  7. Which function is automatically called when a C program starts execution?

    A) main()

    B) start()

    C) init()

    D) execute()

    Answer: A) main()

  8. What is the purpose of the return statement in a C function?

    A) It specifies the function's name.

    B) It is used to exit the program.

    C) It returns a value to the calling function.

    D) It defines the function's parameters.

    Answer: C) It returns a value to the calling function.

  9. In C, can a function have multiple return statements?

    A) No, only one return statement is allowed.

    B) Yes, as long as they all return the same data type.

    C) Yes, but only if the function has more than one parameter.

    D) Yes, there are no restrictions on the number of return statements.

    Answer: D) Yes, there are no restrictions on the number of return statements.

  10. What is function overloading in C?

    A) It is a technique to write multiple functions with the same name but different return types.

    B) It is not allowed in C.

    C) It is a technique to write multiple functions with the same name but different parameters.

    D) It is a technique to hide the implementation of a function.

    Answer: C) It is a technique to write multiple functions with the same name but different parameters.

  11. What is a function prototype in C?

    A) It is a declaration of the function before its actual implementation.

    B) It is a way to define a function's parameters.

    C) It is the same as a function definition.

    D) It is a function that has no parameters.

    Answer: A) It is a declaration of the function before its actual implementation.

  12. Which keyword is used to pass a variable by reference to a function in C?

    A) passbyref

    B) reference

    C) & (ampersand)

    D) * (asterisk)

    Answer: C) & (ampersand)

  13. What is the purpose of the "static" keyword in a function declaration in C?

    A) It indicates that the function is a built-in library function.

    B) It specifies that the function cannot be called by other functions.

    C) It tells the compiler to allocate memory for the function only once.

    D) It indicates that the function is not accessible from outside the source file.

    Answer: C) It tells the compiler to allocate memory for the function only once.

  14. What is a recursive function in C?

    A) It is a function that calls itself.

    B) It is a function that uses a loop instead of recursion.

    C) It is a function that has multiple return statements.

    D) It is a function that cannot be called by other functions.

    Answer: A) It is a function that calls itself.

  15. In C, can a function have the same name as a variable?

    A) Yes, it is allowed.

    B) No, function names and variable names must be different.

    C) Only if the variable is declared before the function.

    D) Only if the variable is declared inside the function.

    Answer: A) Yes, it is allowed.

  16. What is one of the primary advantages of using functions in C?

    A) Reducing code redundancy

    B) Increasing code complexity

    C) Eliminating the need for variables

    D) Slowing down program execution

    Answer: A) Reducing code redundancy

  17. Functions in C allow for:

    A) More bugs in the code

    B) Less readable code

    C) Code to be organized into manageable, reusable pieces

    D) Longer program execution times

    Answer: C) Code to be organized into manageable, reusable pieces

  18. Which advantage of using functions helps in debugging and maintenance?

    A) Code obfuscation

    B) Code modularity and readability

    C) Code duplication

    D) Code compression

    Answer: B) Code modularity and readability

  19. When a function is called multiple times in a program, how does it contribute to code maintenance?

    A) It increases the program's size

    B) It reduces code modularity

    C) It centralizes changes, reducing the need to update multiple locations

    D) It slows down program execution

    Answer: C) It centralizes changes, reducing the need to update multiple locations

  20. Which of the following is NOT an advantage of using functions?

    A) Improved code organization

    B) Enhanced code readability

    C) Elimination of the need for comments

    D) Code reuse and maintainability

    Answer: C) Elimination of the need for comments

  21. Functions in C allow for the division of a program into smaller, logical units, promoting:

    A) Code duplication

    B) Code reuse and maintainability

    C) Code obfuscation

    D) Code compression

    Answer: B) Code reuse and maintainability

  22. Which aspect of functions helps in keeping the main program shorter and more understandable?

    A) Code duplication

    B) Code obfuscation

    C) Code abstraction

    D) Code compression

    Answer: C) Code abstraction

  23. What type of code is easier to write and debug when functions are used?

    A) Longer and unorganized code

    B) Modular and organized code

    C) Code with more variables

    D) Code with fewer comments

    Answer: B) Modular and organized code

  24. Functions can be called from various parts of a program, allowing for:

    A) Code duplication

    B) Code encapsulation and reusability

    C) Code obfuscation

    D) Code compression

    Answer: B) Code encapsulation and reusability

  25. What is the primary benefit of using functions for code testing and validation?

    A) Reduced code size

    B) Increased execution speed

    C) Isolating and testing specific functionality

    D) Code compression

    Answer: C) Isolating and testing specific functionality

  26. What is a local variable in C?

    A) A variable declared outside of any function

    B) A variable declared within a function

    C) A variable declared in the main function only

    D) A variable declared in the global scope

    Answer: B) A variable declared within a function

  27. Where are local variables typically used in C?

    A) Throughout the entire program

    B) In all functions except the main function

    C) Inside the function where they are declared

    D) In the global scope

    Answer: C) Inside the function where they are declared

  28. What is a global variable in C?

    A) A variable declared inside a function

    B) A variable declared within the main function

    C) A variable declared in the global scope

    D) A variable declared without any data type

    Answer: C) A variable declared in the global scope

  29. Which of the following statements about local variables is true?

    A) They can be accessed from any part of the program

    B) They are known as global variables

    C) They have a limited scope

    D) They are initialized to zero by default

    Answer: C) They have a limited scope

  30. Which type of variable takes precedence when there is a conflict between a local and a global variable with the same name?

    A) Local variable

    B) Global variable

    C) Both variables are accessible

    D) It depends on the data type

    Answer: A) Local variable

  31. What is the scope of a local variable?

    A) Throughout the entire program

    B) Within the function where it is declared

    C) In the global scope

    D) In the main function only

    Answer: B) Within the function where it is declared

  32. Which of the following is NOT an advantage of using local variables?

    A) Encapsulation

    B) Reusability

    C) Reduced risk of naming conflicts

    D) Memory efficiency

    Answer: D) Memory efficiency

  33. Which type of variable can be accessed from any part of the program?

    A) Local variable

    B) Global variable

    C) Both local and global variables

    D) None of the above

    Answer: B) Global variable

  34. What is the scope of a global variable?

    A) Within the function where it is declared

    B) In the main function only

    C) Throughout the entire program

    D) In the global scope

    Answer: C) Throughout the entire program

  35. Which of the following is an advantage of using global variables?

    A) Encapsulation

    B) Reduced risk of naming conflicts

    C) Modularity

    D) Ease of debugging

    Answer: D) Ease of debugging

  36. What is the purpose of the main() function in a C program?

    A) To declare variables

    B) To define functions

    C) To specify command-line arguments

    D) To indicate the starting point of the program

  37. How many arguments does the main() function in C typically accept?

    A) None

    B) One

    C) Two

    D) Three

  38. What is the return type of the main() function in C?

    A) int

    B) void

    C) char

    D) float

  39. Which header file should be included to use the printf() and scanf() functions within the main() function?

    A) <stdio.h>

    B) <stdlib.h>

    C) <math.h>

    D) <string.h>

  40. What is the significance of the return statement in the main() function?

    A) It is used to exit the program.

    B) It is used to print a message.

    C) It is used to declare variables.

    D) It is used to define functions.

  41. What is recursion in C programming?

    A) A loop construct in C

    B) A function that calls itself

    C) A built-in library function

    D) A C data type

  42. What is the base case in a recursive function?

    A) The case where the function returns without making a recursive call

    B) The initial case of the function

    C) The most complex case of the function

    D) The last case of the function

  43. What is a recursive algorithm?

    A) An algorithm that uses loops

    B) An algorithm that solves a problem by solving smaller instances of the same problem

    C) An algorithm that has a fixed number of steps

    D) An algorithm that uses only one function

  44. What is the purpose of a recursive function's base case?

    A) To define the initial state of the function

    B) To specify the last case of the function

    C) To provide a way to exit the recursion

    D) To handle complex cases of the function

  45. What happens if there is no base case in a recursive function?

    A) The function will run infinitely

    B) The function will terminate immediately

    C) The function will return an error

    D) The function will work normally

  46. What is the recursive case in a recursive function?

    A) The case where the function returns without making a recursive call

    B) The initial case of the function

    C) The most complex case of the function

    D) The case where the function makes one or more recursive calls

  47. What is the role of the recursive case in a recursive algorithm?

    A) To provide a way to exit the recursion

    B) To specify the last case of the function

    C) To handle complex cases of the function

    D) To define the initial state of the function

  48. What is the difference between direct recursion and indirect recursion?

    A) There is no difference

    B) Direct recursion involves a single function calling itself, while indirect recursion involves multiple functions calling each other in a cycle

    C) Direct recursion is faster

    D) Indirect recursion is more memory-efficient

  49. What is the purpose of a recursive helper function?

    A) To make the code more complex

    B) To improve performance

    C) To encapsulate the recursion and provide additional parameters

    D) To eliminate the need for a base case

  50. What is tail recursion?

    A) A type of recursion that involves multiple recursive calls

    B) A type of recursion that is slower than regular recursion

    C) A type of recursion where the recursive call is the last operation in the function

    D) A type of recursion that always leads to a stack overflow