Mid Term Programming 2024
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;
e)
5.Consider the following block of code. std::cout << “x: “ << x << std::endl;
What is printed on console if the user
std::cout << “y: “ << y << std::endl;
Insrets 6 ?
std::cout << std::endl:
Int n;
std::string code;
Value of x : __________________
std::cout << “Enter a number: from 1 to 3 \n”;
Value of y : __________________
std::cin >> n;
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 ;
a) int 123abc;
std::cout << endl;
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
b) 1
getline(std::cin, str);
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.