0% found this document useful (0 votes)
30 views

Answer Mid Term Programming 2024

Uploaded by

ahmedtarek544a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Answer Mid Term Programming 2024

Uploaded by

ahmedtarek544a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mid Term

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_______

std::cin >> n; Value of y : ______1→True_______

if (!(1<= n && n <= 3)) {


n = 3; } 8. Which of the following is a valid
variable name in C++ ?
code = std::string(n, ‘8’)+ “74”;
std::cout << “The secret code is” << code ;
std::cout << endl;
a) int 123abc;
b) int abc123;
c) int abc-123;
a) The secret code is
874874874874874874874874874 d) int abc 123;
b) The secret code is 874874874
c) The secret code is 88888874
d) The secret code is 88874
9. What is the output of the following
code ?
int a = 2 , b = 3 , c = 4;
6. Consider the following block code.
Answer the following question: cout << a * b / c ;

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;

What is the printed console if the user inserts


“An apple ”?
…………………..…Anaapple……..………….
10. What is the output of the following 11. What will be the output of the
code ? following code ?
int option = 2; string s = “12345”;
switch (option) { int num = stoi(s);
case 1: cout << num + stoi(“1”);
std::cout << “A”;
a) 123451
break;
b) “123451”
case 2: c) 12346
std::cout << “B”; d) None of the above
case 3:
std::cout << “C”;
12. What is the output of ceil (-2.9) in the
default: C math library ?
std::cout << “D”;
a) -2
} b) -3
c) 2
d) 3
Answer : …………BCD……….…

→ 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;

cout << "Enter a number: ";


cin >> number;

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;
}

cout << "Result = " << result << endl;

return 0;
}

You might also like