C++ Interview Questions and Answers

Welcome to your C++ Part 2

1. 
What is the output of the following code snippet? ```cpp #include using namespace std; void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int x = 5; int y = 10; swap(x, y); cout << x << endl; cout << y << endl; return 0; } ```

2. 
Which of the following is not a valid way to declare and initialize a string variable in C++?

3. 
Which of the following is the correct way to access a member variable of an object in C++?

4. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = x % y; cout << z << endl; return 0; } ```

5. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr; cout << *ptr++ << endl; cout << *ptr << endl; return 0; } ```

6. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = ++x; cout << x << endl; cout << y << endl; return 0; } ```

7. 
Which of the following is not a valid storage class in C++?

8. 
Which of the following is not a valid way to deallocate dynamic memory in C++?

9. 
Which of the following is not a valid C++ comment style?

10. 
Which of the following is the correct way to declare a pointer variable in C++?

11. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = x - --y; cout << z << endl; return 0; } ```

12. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = (x > y) ? (x *= y) : (y /= x); cout << x << endl; cout << y << endl; return 0; } ```

13. 
Which of the following is the correct way to open a file in C++ for reading?

14. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = (x != y) ? x : y; cout << z << endl; return 0; } ```

15. 
Which of the following is the correct way to define a pure virtual function in C++?

16. 
What is the output of the following code snippet? ```cpp #include using namespace std; void printValue(int x) { cout << x << endl; } void printValue(double x) { cout << x << endl; } int main() { printValue(5); printValue(3.14); return 0; } ```

17. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; cout << (x < y) << endl; cout < y) << endl; return 0; } ```

18. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = (x < y) ? (x += y) : (y -= x); cout << x << endl; cout << y << endl; return 0; } ```

19. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = (x > y) ? x : y; cout << z << endl; return 0; } ```

20. 
Which of the following is the correct way to check if two strings are equal in C++?

21. 
Which of the following is not a valid way to define a constant in C++?

22. 
What is the output of the following code snippet? ```cpp #include using namespace std; class Base { public: virtual void print() { cout << "Base" << endl; } }; class Derived : public Base { public: void print() override { cout << "Derived" <print(); return 0; } ```

23. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; int z = (x < y) ? x++ : y++; cout << x << endl; cout << y << endl; return 0; } ```

24. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr + 3; cout << *(ptr - 2) << endl; return 0; } ```

25. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 10; int y = 5; int z = x / y; cout << z << endl; return 0; } ```

26. 
Which of the following is the correct way to define a destructor in C++?

27. 
Which of the following is the correct way to declare a constant variable in C++?

28. 
Which of the following is the correct way to define and initialize a constant pointer to an integer in C++?

29. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int arr[5] = {1, 2, 3, 4, 5}; cout << arr[10] << endl; return 0; } ```

30. 
Which of the following is the correct way to declare a function in C++?

31. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; cout << arr[3] << endl; cout << *(arr + 3) << endl; return 0; } ```

32. 
Which of the following is not a valid way to initialize an array in C++?

33. 
Which of the following is the correct way to allocate dynamic memory for a single integer variable in C++?

34. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int* ptr = &arr[2]; cout << *(ptr - 1) << endl; return 0; } ```

35. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int arr[3] = {1, 2, 3}; int* ptr = arr; cout << *(ptr + 2) << endl; return 0; } ```

36. 
Which of the following is not a valid way to define a constructor in C++?

37. 
What is the output of the following code snippet? ```cpp #include using namespace std; int main() { int x = 5; int y = 2; bool result = (x == y); cout << result << endl; return 0; } ```

38. 
Which of the following is not a valid way to close a file in C++?

39. 
Which of the following is the correct way to declare and define a class in C++?

40. 
Which of the following is not a valid type of loop in C++?

C++ interview questions and answers

20 additional questions and answers on Object-Oriented Programming, Input and Output Operations, File Handling, and Exception Handling in C++:

Q: What is object-oriented programming (OOP)?

A: Object-oriented programming is a programming paradigm that organizes code around objects, which are instances of classes. It emphasizes encapsulation, inheritance, and polymorphism.

Q: What is a class in C++?

A: A class is a blueprint for creating objects. It defines the properties and behaviors that objects of that class will have.

Q: How do you define a class in C++?

A: The syntax for defining a class in C++ is: class ClassName { // class members };

Q: What is encapsulation in OOP?

A: Encapsulation is the practice of bundling data and the methods that operate on that data within a class. It provides data hiding and abstraction.

Q: What is inheritance in C++?

A: Inheritance is a mechanism that allows a class to inherit the properties and behaviors of another class. It promotes code reusability and hierarchical relationships.

Q: How do you declare a derived class in C++?

A: The syntax for declaring a derived class in C++ is: class DerivedClassName : accessSpecifier BaseClassName { // class members };

Q: What is polymorphism in OOP?

A: Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables method overriding and dynamic binding.

Q: What is function overloading in C++?

A: Function overloading is a feature that allows multiple functions with the same name but different parameter lists to be defined in a class. The appropriate function is determined by the arguments used.

Q: What is input/output (I/O) in C++?

A: Input/output refers to the process of reading data from a source (input) or writing data to a destination (output) in a C++ program.

Q: How do you read user input in C++?

A: You can read user input in C++ using the cin object. For example, cin >> variableName; reads input from the user and stores it in the variable.

Q: What is the output stream object in C++?

A: The output stream object in C++ is cout. It is used to display output to the console or other output devices.

Q: How do you write formatted output in C++?

A: You can use the cout object with the insertion operator (<<) to write formatted output. For example, cout << "The value is: " << value; displays the value with a message.

Q: What is file handling in C++?

A: File handling involves working with files in a C++ program. It includes operations like creating, opening, reading, writing, and closing files.

Q: How do you open a file for reading in C++?

A: To open a file for reading in C++, you can use the ifstream class and its open() function. For example, ifstream inputFile; inputFile.open("filename.txt");

Q: How do you write to a file in C++?

A: To write to a file in C++, you can use the ofstream class and its open() function. For example, ofstream outputFile; outputFile.open("filename.txt");

Q: What is exception handling in C++?

A: Exception handling is a mechanism to handle runtime errors or exceptional conditions that may occur during program execution. It allows for graceful error handling and program flow control.

Q: What is the purpose of the try-catch block in exception handling?

A: The try-catch block is used to catch and handle exceptions. Code that may throw an exception is placed within the try block, and any exceptions thrown are caught and processed in the catch block.

Q: How do you throw an exception in C++?

A: To throw an exception in C++, you can use the throw keyword followed by an expression. For example, throw runtime_error("An error occurred"); throws a runtime error exception.

Q: What is the difference between a runtime error and a compile-time error?

A: A compile-time error occurs during the compilation phase and prevents the program from being successfully compiled. A runtime error occurs during program execution and can cause the program to terminate abnormally.

Q: How can you define your own custom exception class in C++?

A: You can define a custom exception class in C++ by inheriting from the exception class or one of its derived classes. For example, class CustomException : public exception { // class members };

More c++ interview questions and answers