C++ Language Test 3

    Question 1C++ Boolean Data Type

    What values can a bool data type hold in C++?

    Question 2C++ Logical Operators

    Which of the following is the correct logical AND operator in C++?

    Question 3C++ Null Pointer

    How can we assign a null value to a pointer in modern C++?

    Question 4C++ Else-If Ladder

    Which statement is true about an else-if ladder in C++?

    Question 5C++ Do-While Loop

    Which of the following differentiates a do-while loop from a while loop?

    Question 6C++ Guess the Output – Reading File

    Suppose "data.txt" contains 1234. What will this program print?

    #include <iostream> #include <fstream> using namespace std; int main() { ifstream file("data.txt"); int x; file >> x; cout << x; file.close(); return 0; }

    Question 7C++ Guess the Output – Pass by Value

    What will be the output of the following code?

    #include <iostream> using namespace std; void change(int x) { x = 20; } int main() { int a = 10; change(a); cout << a; return 0; }

    Question 8C++ Size of Array

    If we declare int arr[10]; then what is sizeof(arr) (assuming int takes 4 bytes)?

    Question 9C++ Inheritance

    Which symbol is used to denote inheritance in C++?

    Question 10C++ Character Access

    How can we access the 3rd character of a string s?

    Question 11C++ Guess the Output - Floating Point Precision

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

    #include <iostream> using namespace std; int main() { float x = 5.0/2; cout << x; return 0; }

    Question 12C++ Guess the Output – Logical NOT

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

    #include <iostream> using namespace std; int main() { bool x = true; cout << !x; return 0; }

    Question 13C++ Guess the Output – Pointer Arithmetic

    What will be the output of this program?

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

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

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

    #include <iostream> using namespace std; int main() { int marks = 75; if (marks > 90) cout << "Grade A"; else if (marks > 60) cout << "Grade B"; else cout << "Grade C"; return 0; }

    Question 15C++ Guess the Output – Do-While Loop

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

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

    Question 16C++ File Opening Multiple Modes

    Which of the following opens a file for both reading and writing?

    Question 17C++ Pass by Reference

    Which of the following correctly demonstrates pass by reference in C++?

    Question 18C++ Guess the Output – Array Traversal

    What will be the output of the following code?

    #include <iostream> using namespace std; int main() { int arr[3] = {1, 2, 3}; for(int i = 0; i < 3; i++) { cout << arr[i] << " "; } return 0; }

    Question 19C++ Guess the Output – Function Overloading

    What will this program print?

    #include <iostream> using namespace std; class Math { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }; int main() { Math m; cout << m.add(2, 3) << " " << m.add(2.5, 3.5); return 0; }

    Question 20C++ Guess the Output – String Substring

    What will the following code print?

    #include <iostream> #include <string> using namespace std; int main() { string s = "Programming"; cout << s.substr(0, 4); return 0; }