Uploaded by King Tamang

txt

advertisement
#include <iostream>
#include <string>
using namespace std;
int main() {
string questions[8] = {
"1. Which of the following is used to get user input in C++?\n(a) cout\n(b) cin\n(c) printf\n(d) scanf\n",
"2. What is the output of the following code snippet?\nint x = 10;\nif (x > 5) {\n cout << \"Greater than 5\";\n} else {\n cout << \"Less than or equal to 5\";\n}\n(a) Greater than 5\n(b) Less than or equal to 5\n(c) 10\n(d) Nothing, there is a syntax error\n",
"3. How many times will the following loop execute?\nfor (int i = 0; i < 5; i++) {\n cout << i;\n}\n(a) 4 times\n(b) 5 times\n(c) 6 times\n(d) It will not execute\n",
"4. What is the output of the following code snippet?\nint i = 0;\nwhile (i < 3) {\n cout << i;\n i++;\n}\n(a) 012\n(b) 123\n(c) 0123\n(d) 345\n",
"5. How do you declare an array of integers with 10 elements in C++?\n(a) int array[10];\n(b) int array();\n(c) int array[];\n(d) int array[10][10];\n",
"6. What is the value of `arr[2]` after the following code executes?\nint arr[5] = {1, 2, 3, 4, 5};\n(a) 1\n(b) 2\n(c) 3\n(d) 4\n",
"7. What will be the output of the following code?\n#include <iostream>\nusing namespace std;\n\nint main() {\n int arr[3] = {10, 20, 30};\n for (int i = 0; i < 3; i++) {\n if (arr[i] > 15) {\n cout << arr[i] << \" \";\n }\n }\n return 0;\n}\n(a) 10 20 30\n(b) 20 30\n(c) 10\n(d) 30\n",
"8. What does the following code print?\n#include <iostream>\nusing namespace std;\n\nint main() {\n int sum = 0;\n for (int i = 1; i <= 5; i++) {\n sum += i;\n }\n cout << sum;\n return 0;\n}\n(a) 5\n(b) 10\n(c) 15\n(d) 20\n"
};
char answers[8] = {'b', 'a', 'b', 'a', 'a', 'c', 'b', 'c'};
char userAnswers[8];
int score = 0;
for (int i = 0; i < 8; i++) {
cout << questions[i] << "Your answer: ";
cin >> userAnswers[i];
if (userAnswers[i] == answers[i]) {
score++;
}
cout << endl;
}
cout << "You scored " << score << " out of 8." << endl;
return 0;
}
Download