Translate

C++ Programming Questions and Answers

C++ Practical Exam FAQs

1. What is the formula for calculating simple and compound interest?

Simple Interest (SI) formula: SI = (P × R × T) / 100

Compound Interest (CI) formula: CI = P × (1 + R/100)^T - P

2. How do you determine the greatest of three numbers using if-else statements?

if (a > b && a > c)
    cout << "Greatest: " << a;
else if (b > c)
    cout << "Greatest: " << b;
else
    cout << "Greatest: " << c;
        

3. What is the difference between even and odd numbers?

Even numbers are divisible by 2, while odd numbers are not.

if (num % 2 == 0)
    cout << num << " is even.";
else
    cout << num << " is odd.";
        

4. How can you check if a number is prime without using loops?

bool isPrime(int num, int divisor = 2) {
    if (num < 2) return false;
    if (divisor > num / 2) return true;
    if (num % divisor == 0) return false;
    return isPrime(num, divisor + 1);
}
        

5. Explain how the Fibonacci series is generated.

int n1 = 0, n2 = 1, nextTerm;
for (int i = 1; i <= 10; ++i) {
    cout << n1 << " ";
    nextTerm = n1 + n2;
    n1 = n2;
    n2 = nextTerm;
}
        

6. What is the purpose of the 'break' statement in loops?

The 'break' statement is used to exit a loop prematurely when a certain condition is met.

7. How do you declare a pointer in C++?

int* ptr;
        

8. What is the difference between 'struct' and 'class' in C++?

In C++, 'struct' members are public by default, while 'class' members are private by default.

9. How do you create a constructor in a class?

class MyClass {
public:
    MyClass() {
        // Constructor code
    }
};
        

10. What is operator overloading?

Operator overloading allows you to define custom behavior for operators (like +, -, etc.) for user-defined types.

11. How do you handle exceptions in C++?

try {
    // Code that may throw an exception
} catch (const std::exception& e) {
    std::cout << "Exception: " << e.what() << std::endl;
}
        

12. What is the purpose of the 'virtual' keyword?

The 'virtual' keyword is used to declare a method in a base class that can be overridden in a derived class.

13. How do you create a template function?

template 
T add(T a, T b) {
    return a + b;
}
        

14. What is the difference between deep copy and shallow copy?

A deep copy duplicates all objects and their values, while a shallow copy duplicates only the references to the objects.

15. How do you define a constant variable in C++?

const int MAX_VALUE = 100;
        

16. What is a namespace in C++?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables) inside it.

17. How do you read from a file in C++?

#include 
std::ifstream inputFile("data.txt");
std::string line;
while (std::getline(inputFile, line)) {
    std::cout << line << std::endl;
}
        

18. What is the purpose of the 'this' pointer?

The 'this' pointer is an implicit pointer available in non-static member functions that points to the object for which the member function is called.

19. How do you implement inheritance in C++?

class Base {
public:
    void display() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    void show() {
        std::cout << "Derived class" << std::endl;
    }
};
        

20. What is a destructor in C++?

A destructor is a special member function that is called when an object goes out of scope or is explicitly deleted, used to release resources.

21. How do you create a multi-dimensional array in C++?

int arr[3][4]; // A 3x4 multi-dimensional array
        

End of Questions

Previous Post
No Comment
Add Comment
comment url