lecture 10
lecture 10
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) #define
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.
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: B) The original variable can be inadvertently modified, causing side effects.
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)
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
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
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: 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.
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: 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:
21. Which of the following is NOT true about header files in C++?
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
24. Which of the following best describes the scope of a variable declared inside a loop?
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) 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?
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++?
30. What is the purpose of using & in the function parameter list in C++?
Answer: B) Recursion can lead to a stack overflow if the recursion depth is too high.
33. Which of the following is a key advantage of using function prototypes in C++?
35. What will happen if a recursive function lacks a proper base case?
Answer: C) The function will call itself indefinitely, causing a stack overflow.
Answer: B) Recursive functions call themselves until the base case is met.
A) Local variable
B) Block-level variable
C) Function-level variable
D) Global variable
38. Which of the following is an advantage of using recursion over iteration in some
problems?
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?
Answer: C) The function will use the local variable, hiding the global one.