Answer Mid Term Programming 2024
Answer Mid Term Programming 2024
Programming 2024
→ Q1 ( 12 marks ) ←
1. Consider the following block of code.
What is printed on console if the user
Inserts 18 ?
int age;
a) i
cout << "How old are you?";
b) 10
cin >> age; c) 12
if (age >= 18){ d) 8
cout << "You are old enough to drivel";
}
3. Consider the following block of
else if (age > 16){ code.
cout << "I think you can only drive in the US"; What needs to be written instead of
} /* condition */ so that we see if the
student in from Cairo?
else {
std::string student = "Jhon Doe";
cout << "You have to wait a bit, buddy";
std:string address ="street of Streets number
} 74 , cairo";
if (/* condition */) {
a) "I think you can only drive in the US" std:: cout << student << "is a local!\n";
b) "You are old enough to drive!"
}
c) 18
d) "You need to wait a bit, buddy!" else {
std:: cout << student << "comes from a
different city! \n";
2. What needs to be the initial value of z }
(i.e., what needs to be inserted instead
of ( ) in order for 22 to be printed
on console ? a) address.find("cairo")! = -1
b) address ==" cairo"
int i = 0;
c) address.compare("cairo") == 0
int z = 0;
d) address.equals("cairo")
while (i < 6) {
z += 2;
I += 1;
}
cout << z ;
4. Consider the following block of code. 7. Considering the following block of
What is printed on console? code, Fill in the following information:
for (int i = 2 ; i < 10 ; i += 2) {
std::cout << i << std::endl; int a = 18;
a) 2, 6, 8 int b = 4;
b) 2, 4, 6, 8, 10
bool x = false && true || true;
c) 2, 3, 4, 5, 6, 7, 8, 9
d) 2, 4, 6, 8 bool y = a % 4 < 3 || 2 * a < 100;
5. Consider the following block of code. std::cout << “x: “ << x << std::endl;
What is printed on console if the user
Insrets 6 ? std::cout << “y: “ << y << std::endl;
std::cout << std::endl:
Int n;
std::string code;
std::cout << “Enter a number: from 1 to 3 \n”; Value of x : ______1→True_______
string str;
cout << “Enter a word”; a) 0
getline(std::cin, str); b) 1
c) 2
str.replace(str.find(“a”), 1, “aa”);
d) None of the above
cout << str << std::endl;
→ Q2 ( 8 marks ) ←
Write C++ program that asks the user for a string and a number.
• The string can be either “add”, “subtract”, “multiply”, “divide”.
• The number can be any integer.
• Depending on the string, the program should perform the corresponding operation
with the number and a predefined number (for example, 100), and prints the result.
• If the string is not one of the four options, the function should print an error message.
↓↓ Code ↓↓
#include <iostream>
#include <string>
using namespace std;
int main() {
string operation;
int number;
int result;
cout << "Enter the operation (add, subtract, multiply, divide): ";
cin >> operation;
if (operation == "add") {
result = 100 + number;
} else if (operation == "subtract") {
result = 100 - number;
} else if (operation == "multiply") {
result = 100 * number;
} else if (operation == "divide") {
if (number != 0) {
result = 100 / number;
} else {
cout << "Error: Cannot divide by zero" << endl;
return 1;
}
} else {
cout << "Error: Invalid operation" << endl;
return 1;
}
return 0;
}