C++ Language Test 5

    Question 1C++ Variable Initialization

    Which is the correct way to initialize a variable in C++?

    Question 2C++ Ternary Operator

    Which of the following is the correct syntax of the ternary operator in C++?

    Question 3C++ Pointers and Arrays

    Which of the following statements is true about arrays and pointers in C++?

    Question 4C++ Nested If

    What is the output of the following C++ code snippet?

    #include <iostream> using namespace std; int main() { int x = 15; if (x > 10) { if (x < 20) cout << "Between 10 and 20"; else cout << "Greater than 20"; } return 0; }

    Question 5C++ Continue Statement in Loops

    What is the effect of the continue statement inside a C++ loop?

    Question 6C++ Guess the Output – getline from File

    Suppose "note.txt" contains:
    C++ is fun!
    What will this program print?

    #include <iostream> #include <fstream> using namespace std; int main() { ifstream file("note.txt"); string line; getline(file, line); cout << line; return 0; }

    Question 7C++ Guess the Output – Recursion

    What will be the output of the following program?

    #include <iostream> using namespace std; int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } int main() { cout << factorial(4); return 0; }

    Question 8C++ Array and Functions

    When an array is passed to a function in C++, what is actually passed?

    Question 9C++ this Pointer

    What does the this pointer in C++ represent?

    Question 10C++ String to C-Style String

    Which function is used to convert a C++ string to a C-style character array?

    Question 11C++ Global vs Local Variable

    Which statement is true about global and local variables in C++?

    Question 12C++ Guess the Output – Mixed Operators

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

    #include <iostream> using namespace std; int main() { int a = 10, b = 4, c = 3; cout << a - b * c; return 0; }

    Question 13C++ Guess the Output – Pointer Size

    What will the output of the following program be (on a 64-bit system)?

    #include <iostream> using namespace std; int main() { int *p; double *q; cout << sizeof(p) << " " << sizeof(q); return 0; }

    Question 14C++ Ternary Operator as If-Else

    Which of the following C++ statements correctly uses the ternary operator to check if a number n is even or odd?

    Question 15C++ Guess the Output – Continue

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

    #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; i++) { if (i == 3) continue; cout << i << " "; } return 0; }

    Question 16C++ Closing File

    Why is file.close() used in C++?

    Question 17C++ Function Prototypes

    Why are function prototypes used in C++?

    Question 18C++ Guess the Output – Array and Pointers

    What will the following code print?

    #include <iostream> using namespace std; int main() { int arr[] = {5, 10, 15}; cout << *arr; return 0; }

    Question 19C++ Guess the Output – Destructor

    What will the program print?

    #include <iostream> using namespace std; class Sample { public: Sample() { cout << "Constructor "; } ~Sample() { cout << "Destructor"; } }; int main() { Sample s; return 0; }

    Question 20C++ Guess the Output – String Replace

    What will this code print?

    #include <iostream> #include <string> using namespace std; int main() { string s = "HelloWorld"; s.replace(5, 5, " C++"); cout << s; return 0; }