C++ Language Test 2

    Question 1C++ Character Data Type

    Which of the following data types is used to store a single character in C++?

    Question 2C++ Relational Operators

    Which operator is used to check if two values are not equal in C++?

    Question 3C++ Address-of Operator

    What does the & operator do in the context of pointers?

    Question 4C++ If-Else Statement

    Which keyword is used in C++ to specify an alternative block of code if the if condition is false?

    Question 5C++ While Loop

    Which of the following statements about the while loop in C++ is true?

    Question 6C++ Guess the Output – Writing File

    What will this program do?

    #include <iostream> #include <fstream> using namespace std; int main() { ofstream file("test.txt"); file << "Hello File"; file.close(); return 0; }

    Question 7C++ Guess the Output – Function Return

    What will be the output of the following C++ code?

    #include <iostream> using namespace std; int square(int x) { return x * x; } int main() { cout << square(4); return 0; }

    Question 8C++ Array Index

    What will happen if you try to access arr[10] in an array declared as int arr[5];?

    Question 9C++ Access Specifiers

    Which access specifier allows members to be accessed only inside the class?

    Question 10C++ String Length

    Which function is used to get the length of a string in C++?

    Question 11C++ Guess the Output - ASCII Conversion

    What will be the output of the following C++ code snippet?

    #include <iostream> using namespace std; int main() { int num = 'A'; cout << num; return 0; }

    Question 12C++ Guess the Output – Pre vs Post Increment

    What will be the output of the following C++ code snippet?

    #include <iostream> using namespace std; int main() { int a = 5; cout << a++ << " " << ++a; return 0; }

    Question 13C++ Guess the Output – Pointer and Variable

    What will be the output of this program?

    #include <iostream> using namespace std; int main() { int num = 25; int *p = # cout << &num << " " << p; return 0; }

    Question 14C++ Guess the Output – If-Else

    What will be the output of the following C++ code snippet?

    #include <iostream> using namespace std; int main() { int num = 8; if (num % 2 == 0) cout << "Even"; else cout << "Odd"; return 0; }

    Question 15C++ Guess the Output – While Loop

    What will be the output of the following C++ code snippet?

    #include <iostream> using namespace std; int main() { int i = 1; while (i < 4) { cout << i << " "; i++; } return 0; }

    Question 16C++ File Reading

    Which class is used to read from a file in C++?

    Question 17C++ Function Overloading

    Which of the following demonstrates valid function overloading in C++?

    int add(int a, int b); int add(float a, float b);

    Question 18C++ Guess the Output – Array Elements

    What will be the output of the following code?

    #include <iostream> using namespace std; int main() { int arr[] = {10, 20, 30, 40}; cout << arr[1] + arr[3]; return 0; }

    Question 19C++ Guess the Output – Encapsulation

    What will be the output of this program?

    #include <iostream> using namespace std; class Person { private: int age; public: void setAge(int a) { age = a; } int getAge() { return age; } }; int main() { Person p; p.setAge(21); cout << p.getAge(); return 0; }

    Question 20C++ Guess the Output – String Concatenation

    What will be the output of this code?

    #include <iostream> #include <string> using namespace std; int main() { string a = "Good"; string b = "Morning"; cout << a + " " + b; return 0; }