0% found this document useful (0 votes)
8 views12 pages

Computer Theory Answers

The document provides detailed answers for an M.Sc. I Laboratory 2018 Examination, covering topics such as function overloading, Fibonacci number checking, pointers, static member functions, access specifiers, and friend functions in C++. It includes code examples to illustrate each concept, such as the use of switch statements and inline functions. The document serves as a comprehensive guide to key C++ programming concepts relevant to the examination.

Uploaded by

Aarti Pathania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Computer Theory Answers

The document provides detailed answers for an M.Sc. I Laboratory 2018 Examination, covering topics such as function overloading, Fibonacci number checking, pointers, static member functions, access specifiers, and friend functions in C++. It includes code examples to illustrate each concept, such as the use of switch statements and inline functions. The document serves as a comprehensive guide to key C++ programming concepts relevant to the examination.

Uploaded by

Aarti Pathania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Detailed Answers for M.Sc.

I Laboratory 2018 Examination

1. What is function overloading? Explain using it in a simple example.

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

signature (the function's name and parameter types).

Example:

#include <iostream>

using namespace std;

void display(int i) {

cout << "Integer: " << i << endl;

void display(double d) {

cout << "Double: " << d << endl;

void display(string str) {

cout << "String: " << str << endl;

int main() {

display(5); // Calls the first display function

display(5.5); // Calls the second display function

display("Hello"); // Calls the third display function


Detailed Answers for M.Sc. I Laboratory 2018 Examination

return 0;

}
Detailed Answers for M.Sc. I Laboratory 2018 Examination

2. Write a program to check if a given number is a Fibonacci number.

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>

using namespace std;

bool isPerfectSquare(int x) {

int s = sqrt(x);

return (s * s == x);

bool isFibonacci(int n) {

return isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4);

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

if (isFibonacci(num)) {

cout << num << " is a Fibonacci number." << endl;

} 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

3. Define with example what is a pointer.

A pointer is a variable that stores the memory address of another variable. Pointers are used in C++

for dynamic memory allocation, arrays, and function arguments.

Example:

#include <iostream>

using namespace std;

int main() {

int var = 10;

int *ptr = &var;

cout << "Value of var: " << var << endl;

cout << "Address of var: " << &var << endl;

cout << "Value of ptr: " << ptr << endl;

cout << "Value at ptr (dereferencing): " << *ptr << endl;

return 0;

}
Detailed Answers for M.Sc. I Laboratory 2018 Examination

4. What is static member function? How it can be called in main function?

A static member function in C++ belongs to the class rather than any object instance. It can be

called on the class itself, rather than on instances of the class.

Example:

#include <iostream>

using namespace std;

class Example {

public:

static void display() {

cout << "Static member function called." << endl;

};

int main() {

Example::display(); // Calling static member function without an instance

return 0;

}
Detailed Answers for M.Sc. I Laboratory 2018 Examination

5. How many access specifiers does a class have? Explain them.

C++ has three access specifiers:

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

can modify the char value it points to.

- char const *p: p is a pointer to a constant char. You can change the address stored in p, but you

can't modify the char value it points to.


Detailed Answers for M.Sc. I Laboratory 2018 Examination

7. What is function overloading?

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

signature (the function's name and parameter types).


Detailed Answers for M.Sc. I Laboratory 2018 Examination

8. Define the friendly functions.

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>

using namespace std;

class Example {

int privateData;

public:

Example() : privateData(0) {}

friend void display(Example ex);

};

void display(Example ex) {

cout << "Private data: " << ex.privateData << endl;

int main() {

Example ex;

display(ex);

return 0;

}
Detailed Answers for M.Sc. I Laboratory 2018 Examination

9. Explain the switch statement.

A switch statement allows a variable to be tested for equality against multiple values. Each value is

called a case, and the variable is checked against each case.

Example:

#include <iostream>

using namespace std;

int main() {

int num = 2;

switch (num) {

case 1:

cout << "One" << endl;

break;

case 2:

cout << "Two" << endl;

break;

default:

cout << "Default case" << endl;

return 0;

}
Detailed Answers for M.Sc. I Laboratory 2018 Examination

10. What is an inline function?

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>

using namespace std;

inline int add(int a, int b) {

return a + b;

int main() {

cout << "Sum: " << add(5, 3) << endl;

return 0;

You might also like