Computer Theory Answers
Computer Theory Answers
Function overloading is a feature in C++ where two or more functions can have the same name but
different parameters (number or type). The compiler differentiates between these functions by their
Example:
#include <iostream>
void display(int i) {
void display(double d) {
int main() {
return 0;
}
Detailed Answers for M.Sc. I Laboratory 2018 Examination
A Fibonacci number is a number that appears in the Fibonacci sequence. The sequence starts with
0 and 1, and each subsequent number is the sum of the previous two.
Program:
#include <iostream>
bool isPerfectSquare(int x) {
int s = sqrt(x);
return (s * s == x);
bool isFibonacci(int n) {
int main() {
int num;
if (isFibonacci(num)) {
} else {
Detailed Answers for M.Sc. I Laboratory 2018 Examination
cout << num << " is not a Fibonacci number." << endl;
return 0;
}
Detailed Answers for M.Sc. I Laboratory 2018 Examination
A pointer is a variable that stores the memory address of another variable. Pointers are used in C++
Example:
#include <iostream>
int main() {
cout << "Value at ptr (dereferencing): " << *ptr << endl;
return 0;
}
Detailed Answers for M.Sc. I Laboratory 2018 Examination
A static member function in C++ belongs to the class rather than any object instance. It can be
Example:
#include <iostream>
class Example {
public:
};
int main() {
return 0;
}
Detailed Answers for M.Sc. I Laboratory 2018 Examination
1. Public: Members declared as public are accessible from outside the class.
2. Private: Members declared as private are accessible only within the class.
3. Protected: Members declared as protected are accessible within the class and by derived class
instances.
Detailed Answers for M.Sc. I Laboratory 2018 Examination
6. How do you define the difference between char * const p and char const *p?
- char * const p: p is a constant pointer to a char. You can't change the address stored in p, but you
- char const *p: p is a pointer to a constant char. You can change the address stored in p, but you
Function overloading is a feature in C++ where two or more functions can have the same name but
different parameters (number or type). The compiler differentiates between these functions by their
A friend function is a function that is not a member of a class but can access the class's private and
protected members. To declare a friend function, use the friend keyword inside the class.
Example:
#include <iostream>
class Example {
int privateData;
public:
Example() : privateData(0) {}
};
int main() {
Example ex;
display(ex);
return 0;
}
Detailed Answers for M.Sc. I Laboratory 2018 Examination
A switch statement allows a variable to be tested for equality against multiple values. Each value is
Example:
#include <iostream>
int main() {
int num = 2;
switch (num) {
case 1:
break;
case 2:
break;
default:
return 0;
}
Detailed Answers for M.Sc. I Laboratory 2018 Examination
An inline function is a function that is expanded in place when it is called. When the function is
small, making it inline can increase the program's speed by avoiding the overhead of a function call.
Example:
#include <iostream>
return a + b;
int main() {
return 0;