0% found this document useful (0 votes)
5 views

lecture 10

The document contains multiple-choice questions (MCQs) focused on C++ concepts such as header files, function prototypes, variable scope, call by value/reference, and recursion. Each question is accompanied by the correct answer, providing a comprehensive overview of these programming topics. The questions test the understanding of best practices and fundamental principles in C++ programming.

Uploaded by

myvujourneywell
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

lecture 10

The document contains multiple-choice questions (MCQs) focused on C++ concepts such as header files, function prototypes, variable scope, call by value/reference, and recursion. Each question is accompanied by the correct answer, providing a comprehensive overview of these programming topics. The questions test the understanding of best practices and fundamental principles in C++ programming.

Uploaded by

myvujourneywell
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MCQs on Functions, Header Files, and Scope of Identifiers

1. What is the main purpose of header files in C++?


o A) To implement functions directly in the header file.
o B) To organize function declarations, constants, and reusable code.
o C) To store the source code for functions.
o D) To avoid using functions in the program.

Answer: B) To organize function declarations, constants, and reusable code.

2. How do you include a custom header file named "pi.h" in your C++ program?
o A) #import "pi.h"
o B) #include "pi.h"
o C) #define pi.h
o D) #include <pi.h>

Answer: B) #include "pi.h"

3. Which of the following is used to define constants in header files?


o A) #include
o B) #define
o C) #ifdef
o D) #return

Answer: B) #define

4. What is the scope of a global variable?


o A) It is only accessible inside the function it is defined in.
o B) It is accessible in all functions within the same file.
o C) It is only accessible within a block.
o D) It is only accessible in the main function.

Answer: B) It is accessible in all functions within the same file.

5. What is the scope of a variable declared inside a function?


o A) It is accessible throughout the program.
o B) It is only accessible inside that function.
o C) It is accessible within the entire block.
o D) It is accessible only in the main function.

Answer: B) It is only accessible inside that function.

6. What is the difference between call by value and call by reference in function calls?
o A) Call by value passes the actual variable; call by reference passes a copy.
o B) Call by value creates a copy of the argument; call by reference passes the
original argument.
o C) Call by value can modify the original variable; call by reference cannot.
o D) Call by reference is slower than call by value.

Answer: B) Call by value creates a copy of the argument; call by reference passes the
original argument.

7. Which of the following is true for a function that is passed by reference?


o A) The original argument cannot be modified.
o B) The function can modify the original argument.
o C) The function works with a copy of the argument.
o D) The function cannot access global variables.

Answer: B) The function can modify the original argument.

8. Which of the following is the correct syntax to pass a variable by reference in C++?
o A) void func(int x)
o B) void func(int *x)
o C) void func(int &x)
o D) void func(int x&)

Answer: C) void func(int &x)

9. What is a potential risk of using call by reference?


o A) The value of the original argument is never changed.
o B) The original variable can be inadvertently modified, causing side effects.
o C) Call by reference is more efficient than call by value.
o D) Call by reference requires more memory than call by value.

Answer: B) The original variable can be inadvertently modified, causing side effects.

10. Which of the following statements about recursion is true?


o A) Recursive functions call other functions, but do not call themselves.
o B) Recursive functions do not have a base case and can run indefinitely.
o C) Recursion is efficient for problems with repetitive structures.
o D) Recursive functions are always more efficient than iterative functions.

Answer: C) Recursion is efficient for problems with repetitive structures.

11. Which of the following is the base case in the recursive function to calculate the
factorial of a number?
o A) if (n == 0)
o B) if (n == 1)
o C) if (n <= 1)
o D) if (n > 1)
Answer: C) if (n <= 1)

12. What will be the output of the following code?

cpp
Copy code
int i = 10;
void f(int i) {
i *= 2;
}
int main() {
f(i);
cout << i;
return 0;
}

o A) 20
o B) 10
o C) 0
o D) Compilation Error

Answer: B) 10

13. What will be the output of the following code?

cpp
Copy code
int i = 10;
void f(int *i) {
*i *= 2;
}
int main() {
f(&i);
cout << i;
return 0;
}

o A) 20
o B) 10
o C) 0
o D) Compilation Error

Answer: A) 20

14. Which of the following is true about a global variable?


o A) It is defined within a function and cannot be accessed outside.
o B) It can be accessed from any function within the file.
o C) It is used only in loops.
o D) It cannot be modified.
Answer: B) It can be accessed from any function within the file.

15. Which of the following is the correct declaration for a constant in a header file?
o A) #const pi 3.14
o B) #define pi 3.14
o C) const pi 3.14
o D) pi = 3.14

Answer: B) #define pi 3.14

16. What is the main advantage of using recursive functions?


o A) They can solve problems with repetitive structures more elegantly.
o B) They use less memory than iterative solutions.
o C) They are always faster than iterative solutions.
o D) They require no base case.

Answer: A) They can solve problems with repetitive structures more elegantly.

17. What happens if you forget to provide a base case in a recursive function?
o A) The function will stop calling itself.
o B) The function will run indefinitely, causing a stack overflow.
o C) The program will produce an error message.
o D) The function will execute faster.

Answer: B) The function will run indefinitely, causing a stack overflow.

18. In which situation would you prefer to use call by reference?


o A) When you want to prevent changes to the original variable.
o B) When you want to modify the original variable.
o C) When passing small, simple data types like integers.
o D) When the argument is passed as a copy.

Answer: B) When you want to modify the original variable.

19. What will happen if you try to use a variable outside of its scope?
o A) The program will compile successfully.
o B) The program will generate a warning but continue to run.
o C) The program will throw a compile-time error.
o D) The variable will be treated as a global variable.

Answer: C) The program will throw a compile-time error.

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


o A) To define the body of the function.
o B) To declare the function's return type, name, and parameters before its usage.
o C) To call the function.
o D) To handle errors in the function.

Answer: B) To declare the function's return type, name, and parameters before its usage.

These MCQs test knowledge on header files, function prototypes, scope of variables, call by
value/reference, recursion, and best practices for variable naming in C++.

You said:

more
ChatGPT said:

ChatGPT

Here are additional multiple-choice questions (MCQs) based on Functions, Header Files, and
Scope of Identifiers:

MCQs on Functions, Header Files, and Scope of Identifiers (continued)

21. Which of the following is NOT true about header files in C++?

 A) They can contain function prototypes.


 B) They can contain constant definitions.
 C) They must always contain function definitions.
 D) They can be included in multiple files using #include.

Answer: C) They must always contain function definitions.

22. Which of the following is the correct syntax to include a standard library header
file?

 A) #include "iostream.h"
 B) #include <iostream>
 C) #include "iostream"
 D) #include iostream

Answer: B) #include <iostream>

23. Which of the following variables has function-level scope?

 A) A variable declared inside a function.


 B) A variable declared inside a block (e.g., if statement).
 C) A global variable.
 D) A variable declared inside a loop.

Answer: A) A variable declared inside a function.

24. Which of the following best describes the scope of a variable declared inside a loop?

 A) It is global to the program.


 B) It is local to the loop block.
 C) It is accessible throughout the entire function.
 D) It can be accessed anywhere in the program.

Answer: B) It is local to the loop block.

25. In which case is the scope of a variable limited to the block it is defined in?

 A) Function-level scope.
 B) Block-level scope.
 C) Global scope.
 D) File-level scope.

Answer: B) Block-level scope.

26. What is the main reason to use #define in header files?

 A) To define variables that can be changed at runtime.


 B) To define constants that are replaced by the preprocessor before compilation.
 C) To define functions that will be called by the program.
 D) To define a header guard that prevents multiple inclusions.

Answer: B) To define constants that are replaced by the preprocessor before compilation.

27. What is the value of the global variable x after the following code executes?

cpp
Copy code
int x = 5;
void modify() {
x = 10;
}
int main() {
modify();
cout << x;
return 0;
}

 A) 5
 B) 10
 C) 0
 D) Compilation Error

Answer: B) 10

28. What will happen if a variable is declared with the same name in both the global
and local scope?

 A) The global variable will be overridden by the local variable.


 B) The local variable will hide the global variable within its scope.
 C) The global variable will remain unchanged.
 D) The program will crash.

Answer: B) The local variable will hide the global variable within its scope.

29. Which of the following statements is true for function calling in C++?

 A) Call by value is always faster than call by reference.


 B) In call by reference, the function cannot modify the original argument.
 C) Call by reference requires passing the address of the variable.
 D) Call by value always modifies the original variable.

Answer: C) Call by reference requires passing the address of the variable.

30. What is the purpose of using & in the function parameter list in C++?

 A) To pass the argument by value.


 B) To declare a reference variable, which allows call by reference.
 C) To dereference a pointer.
 D) To define a constant argument.

Answer: B) To declare a reference variable, which allows call by reference.

31. Which of the following is a disadvantage of using recursion in C++?

 A) Recursion is difficult to understand.


 B) Recursion can lead to a stack overflow if the recursion depth is too high.
 C) Recursion always runs slower than loops.
 D) Recursion cannot solve complex problems.

Answer: B) Recursion can lead to a stack overflow if the recursion depth is too high.

32. How can you avoid a stack overflow in a recursive function?

 A) By increasing the recursion depth.


 B) By providing a base case for the recursion.
 C) By using a goto statement.
 D) By calling the function iteratively.

Answer: B) By providing a base case for the recursion.

33. Which of the following is a key advantage of using function prototypes in C++?

 A) They allow the function body to be defined later in the code.


 B) They allow functions to be called before they are defined.
 C) They are used to declare global variables.
 D) They increase the speed of the program's execution.

Answer: B) They allow functions to be called before they are defined.

34. In which situation would you use call by value?

 A) When you want to modify the argument inside the function.


 B) When you want the original argument to remain unchanged.
 C) When working with large data structures.
 D) When you want to work with pointers.

Answer: B) When you want the original argument to remain unchanged.

35. What will happen if a recursive function lacks a proper base case?

 A) The recursion will stop after a few iterations.


 B) The program will produce the desired output.
 C) The function will call itself indefinitely, causing a stack overflow.
 D) The program will terminate with an error.

Answer: C) The function will call itself indefinitely, causing a stack overflow.

36. Which of the following statements is correct about recursive functions?

 A) Recursive functions should not have a base case.


 B) Recursive functions call themselves until the base case is met.
 C) Recursive functions can only be used for simple problems.
 D) Recursive functions always use less memory than iterative functions.

Answer: B) Recursive functions call themselves until the base case is met.

37. In C++, what type of variable has the largest scope?

 A) Local variable
 B) Block-level variable
 C) Function-level variable
 D) Global variable

Answer: D) Global variable

38. Which of the following is an advantage of using recursion over iteration in some
problems?

 A) Recursion is faster and requires less memory.


 B) Recursion allows the program to handle repetitive structures in a more natural way.
 C) Recursion always avoids the need for loops.
 D) Recursion does not require base cases.

Answer: B) Recursion allows the program to handle repetitive structures in a more natural way.

39. What happens if a global variable is declared with the same name inside a function?

 A) The global variable is modified inside the function.


 B) The function will use the global variable instead of the local one.
 C) The function will use the local variable, hiding the global one.
 D) A compile-time error occurs.

Answer: C) The function will use the local variable, hiding the global one.

40. What is the correct way to pass an array to a function in C++?

 A) By passing a pointer to the first element of the array.


 B) By passing the entire array.
 C) By passing the size of the array only.
 D) By passing each element of the array individually.

Answer: A) By passing a pointer to the first element of the array.

You might also like