SH QS CH6 C++ Past Paper PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document contains a set of questions about fundamental concepts of C++ programming especially in relation to functions and recursion, which are common topics taught at secondary school level. The questions cover a range of C++ programming techniques, from basic function calls to more advanced recursion techniques. It is likely a past paper or quiz.
Full Transcript
CH 6 1. What is the primary purpose of functions in C++? A) To complicate the program structure B) To modularize a program into self-contained units C) To replace variables D) To avoid using libraries 2. What is the main advantage of dividing a program into functions? A) Increases memor...
CH 6 1. What is the primary purpose of functions in C++? A) To complicate the program structure B) To modularize a program into self-contained units C) To replace variables D) To avoid using libraries 2. What is the main advantage of dividing a program into functions? A) Increases memory usage B) Reduces readability C) Simplifies debugging and testing D) Makes the program more complex 3. Which header file contains the sqrt function in C++? A) B) C) D) 4. How are empty parameter lists specified in C++? A) By using the keyword null B) By leaving the parentheses empty or using void C) By omitting parentheses D) By writing none 5. Which keyword is used to specify default arguments in a function prototype? A) default B) void C) static D) Default arguments are directly included in the prototype 6. What does the term “argument coercion” refer to in C++? A) Changing argument names B) Forcing arguments to match the parameter types C) Declaring variables as const D) Using global variables 7. What is the output of rand() % 6? A) A random number between 1 and 6 B) A random number between 0 and 5 C) A random number between 0 and 6 D) A random number between 1 and 5 8. What is the base case in recursion? A) The first call of the function B) The simplest case that stops the recursion C) The main function in the program D) The largest input value 9. Which storage class makes a variable retain its value between function calls? A) auto B) static C) register D) extern 10. What does the unary scope resolution operator (::) do in C++? A) Access local variables B) Access global variables C) Declare functions D) Overload functions 11. Which of the following is a recursive function? A) A function that returns void B) A function that calls itself C) A function with multiple parameters D) A function that uses only loops 12. What happens when too many recursive calls are made in a program? A) Stack overflow error occurs B) Memory usage decreases C) Infinite loop is triggered D) The program speeds up 13. What is the signature of a function in C++? A) The function name and return type B) The function name and argument types C) The function body D) The main function 14. What does the keyword inline suggest in a function definition? A) Avoid function calls and insert code directly B) Use the function only once C) Optimize the program size D) Declare a static function 15. Which header file is required for using the time function? A) B) C) D) 16. Which function is used to generate random numbers in C++? A) random() B) rand() C) srand() D) generateRandom() 17. What is the purpose of the srand() function? A) To reset the program execution B) To seed the rand() function for randomizing C) To sort random numbers D) To return random numbers 18. Which storage class is used for global variables in C++? A) static B) auto C) extern D) register 19. What is a function prototype in C++? A) A full function definition B) A function declaration specifying its name, return type, and parameters C) A global variable declaration D) A header file inclusion 20. Which of the following is an example of an overloaded function? A) Functions with different names but similar functionality B) Functions with the same name but different parameters C) Functions that use recursion D) Functions that return void 21. What is the difference between recursion and iteration? A) Recursion uses loops, iteration uses functions B) Recursion uses repeated function calls, iteration uses loops C) Iteration is always faster than recursion D) Recursion is limited to numerical calculations 22. What does the register storage class suggest? A) To use the variable as a global variable B) To store the variable in high-speed registers C) To initialize the variable to zero D) To make the variable static 23. What happens if a recursive function lacks a base case? A) The program compiles with warnings B) The program runs indefinitely or crashes C) The function stops automatically D) The base case is created dynamically 24. What is the maximum value of rand() defined as in C++? A) RAND_MAX B) MAX_RAND C) INT_MAX D) LIMIT_RAND 25. How are arguments passed in pass-by-value? A) The original variable is passed directly B) A copy of the variable is passed C) The address of the variable is passed D) No value is passed 26. Which type of recursion uses multiple functions calling each other? A) Direct recursion B) Indirect recursion C) Tail recursion D) Single recursion 27. What is the default value of uninitialized static variables in C++? A) Undefined B) 0 C) Garbage value D) 1 28. What is the output of the Fibonacci function for fibonacci(0)? A) 1 B) 0 C) Undefined D) -1 29. Which scope is limited to the block in which an identifier is declared? A) File scope B) Block scope C) Function scope D) Global scope 30. What is the role of the activation record in the function call stack? A) To keep track of the memory usage of the program B) To store the return address and local variables of a function C) To initialize variables to zero D) To execute recursive functions 31. Which operator is used to access a global variable when a local variable with the same name exists? A). B) -> C) :: D) && 32. What is the primary disadvantage of recursion compared to iteration? A) Recursion is slower B) Recursion is harder to debug C) Recursion consumes more memory due to repeated function calls D) Recursion is not supported in all programming languages 33. How does the compiler differentiate between overloaded functions? A) By their return types B) By their names C) By their signatures (parameters and their types) D) By their memory addresses 34. What is the main reason to use inline functions? A) To avoid recursion B) To speed up execution by avoiding function call overhead C) To reduce program size D) To make functions private 35. Which of the following is true for global variables? A) They have block scope B) They retain their values throughout the program execution C) They must be declared as static D) They are initialized to garbage values by default 36. What is the divide and conquer technique in programming? A) Breaking a large problem into smaller, simpler parts B) Using multiple functions for the same task C) Dividing code into header files and implementation files D) Combining different programming languages 37. Which term describes a function that does not belong to any class in C++? A) Inline function B) Global function C) Static function D) Local function 38. What is the purpose of using std:: before function names like cout? A) To call a global function B) To specify the standard namespace C) To declare a static variable D) To initialize a function 39. How does a function prototype assist the compiler? A) It defines the function’s return value and parameters B) It checks if the function is overloaded C) It helps link the function to the correct library D) It ensures correct argument types and order in function calls 40. Which of the following is a correct way to declare a function with no return value? A) void functionName(); B) int functionName(void); C) void functionName(void); D) Both A and C 41. What is the value of the shiftingValue in the expression number = shiftingValue + rand() % scalingFactor;? A) The upper limit of the range B) The scaling factor C) The first number in the desired range D) The seed value 42. What happens if two functions in the same scope have the same signature but different return types? A) The program runs but produces a warning B) It results in a compilation error C) The first function is executed by default D) The compiler selects the one with a higher return type 43. What is the primary advantage of using static variables in functions? A) They retain their value between function calls B) They consume less memory C) They are global but hidden from other functions D) They can only be used in recursive functions 44. What is the result of applying argument coercion to a function call? A) Arguments are discarded if their types don’t match B) Arguments are converted to match the parameter types C) The function call is skipped if arguments are invalid D) Arguments are evaluated as void 45. What is a case study example of recursion provided in the document? A) Factorial calculation B) Fibonacci series C) Sorting algorithms D) Game of chance 46. How does the srand(time(0)) function work? A) It initializes the program with a fixed seed B) It generates the same sequence of random numbers every time C) It uses the system clock to produce a unique seed D) It resets the random number generator 47. What is the key difference between global variables and local variables? A) Global variables are faster to access B) Global variables can be used anywhere in the program C) Local variables are initialized by default D) Local variables have a larger scope 48. What does pass-by-reference allow in C++ functions? A) Passing only constant variables B) Modifying the original variable in the caller C) Returning multiple values D) Avoiding variable initialization 49. Which type of error occurs when converting a higher data type to a lower one in C++? A) Syntax error B) Data loss or corruption C) Compilation warning D) Runtime exception 50. How does the compiler treat overloaded functions? A) By combining them into a single function B) By encoding their names with parameter types (name mangling) C) By assigning default arguments to avoid conflicts D) By rejecting any functions with the same name 51. Which scope is assigned to variables declared inside a loop in C++? A) File scope B) Block scope C) Function scope D) Global scope 52. What is the key drawback of recursion compared to iteration? A) Increased execution time B) Higher memory usage due to function call overhead C) Less readable code D) Inability to handle large data sets 53. What does the term enum represent in C++? A) A set of floating-point constants B) A set of integer constants represented by identifiers C) A collection of functions D) A data structure for random numbers 54. Which C++ keyword explicitly declares variables of automatic storage class? A) register B) auto C) static D) extern 55. What is the purpose of the function call stack? A) To store all variables in the program B) To manage function calls and return addresses C) To prevent stack overflow errors D) To keep track of recursion depth only Q# Answer Q# Answer Q# Answer Q# Answer Q# Answer Q# Answer Q# Answer 1 B 8 B 15 A 22 B 29 B 36 A 43 A 2 C 9 B 16 B 23 B 30 B 37 A 44 B 3 B 10 B 17 B 24 A 31 C 38 B 45 B 4 B 11 B 18 C 25 B 32 C 39 D 46 C 5 D 12 A 19 B 26 B 33 C 40 D 47 B 6 B 13 B 20 B 27 B 34 B 41 C 48 B 7 B 14 A 21 B 28 B 35 B 42 B 49 B السؤال اللي باألحمر ماني متأكد من جوابه