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

Fundamental (Cpp) Code 1 (2)

The document is a final examination for a programming fundamentals course, consisting of multiple-choice questions and code output tasks. It covers various programming concepts such as operators, loops, arrays, and data types in C++. The exam is prepared by Tewodros Getaneh and is set for October 2023.

Uploaded by

Alemseged Mammo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Fundamental (Cpp) Code 1 (2)

The document is a final examination for a programming fundamentals course, consisting of multiple-choice questions and code output tasks. It covers various programming concepts such as operators, loops, arrays, and data types in C++. The exam is prepared by Tewodros Getaneh and is set for October 2023.

Uploaded by

Alemseged Mammo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CPU Business and Information Technology College

Fundamentals of programming
Final Examination
Date October 2023
Time allowed 1:45
Name _______________________________________ Id___________

Prepared By: Tewodros Getaneh


Part I
Multiple Choice (1 points each)

______1. Evaluate the following


(1 && 0) || 1 || 0
a) 0 b) 1 c) false d) None of the mentioned
_____2. Which of the following is the inequality operator in CPP?
a) &= b) # c) != d) None of the above
______3. Which of the following if a clause determines whether the value in an int variable age
is between 20 and 25, including 20 and 25?
a) If (age <= 20 || age >=25)
b) If (age >= 20 && age <=25)
c) If (age <= 20 || age <=25)
d) None of the above
______4.What number will the following code display on the computer screen?
int sum = 0;
int y = 0;
do
{
for (int x = 1; x<5;x = x +1)
sum = sum + x;
y= y + 1;
} while(y < 3);
cout<<sum;
a)5 b)8 c) 15 d) 30
______5. Which of the following for clauses processes the loop instructions as long as the x variable’s
value is less than 500?
a) for (int x = 10; x < 500 ; x = x + 1)
b) for (int x = 10, x <= 500 , x = x + 1)
c) for (int x == 10; x < 500 ; x = x + 1)
d) for (int x = x + 10; x < 500 ; x = 1)
______6.The computer will stop processing the loop associated with the for clause from question 5
when the x variable contains the number __________
a) 500 b) 501 c) 499 d) 1
______7.What numbers will the following code display on the computer screen?
int total = 1;
do
{
cout<<total<<” “;
total = total + 2;
} while (total <=3);
a) 1 2 b) 1 3 c)1 3 5 d) 0 1 3
______8.Which of the following increases the total variable by the contents of the third
element in the orders array? The total variable and the orders array have the int data
type.
a) Orders[2] += total;
b) Orders [3] += total;
c) Total +=orders[2];
d) Total +=orders[3];
_____9. Which of the following is false?
a) The elements in an array are related in some way.
b) All the elements in an array have the same data type.
c) All the elements in one dimensional array have the same subscript.
d) The first element in a one-dimensional array has a subscript of 0(zero).

Use the sales array to answer review questions 10 through 12. The array was declared
using int sales [4][2] = {{10000,12000},{900,500},{350,600},{700,800}}; statement.

____10. The statement sales[0][1] = sales[1][1] + sales[2][1]; will replace the number _____
a) 12000 with 1100
b) 12000 with 1500
c) 10000 with 1100
d) 10000 with 1500
_____11.The statement sales [1][1] = sales [3][0]; will replace the number ___________
a) 10000 with 700
b) 12000 with 700
c) 700 with 500
d) 500 with 700
____12. The statement cout<<sales [2][0] + sales[3][1]<<endl; will ______
a) Display 1150
b) Display 1600
c) Display sales[2][0] + sales[3][1]
d) Result in an error
_____13.To loop through an array of size 6, a proper for-loop statement would be
a) for(i = 0; i < 6; i++)
b) for(i = array[0]; i < array[5]; i++)
c) for(array[0] = i = 0; i < array[5]; i += 1)
d) for(i = 0; i <= 6; i++)
______14.To create an array of 10 elements of type int, you would write
a) int [10] array;
b) int [9] array;
c) int array[10];
d) array [int 9];
_____15.The last element in an array of 50 elements is
a) array[0]
b) array[49]
c) array[50]
d) array[51]
_____16.The declaration statement for a named constant requires __________
a) A data type b)A name c)A value d)All the above
_____17. What built in data type should be used for true or false values in CPP?
a) bool b)float c) double d) a and b e) b and c
_____18. What are the values of x and y after the following program segment is executed?
int x = 4 , y = 3;
x += y;
y += x++
a) x = 8 and y = 10 b) x = 10 and y = 8 c. ) x = 5 and y = 9 d) None
_____19.What built in data type do you use if you want to enter alphabet in a variable?
a) alp b) char c) double d) float
______20.What is the output of this program?
int main()
{
int a = 10;
if (a <= 10) {
for (i = 0; i < 10; i++)
cout << i;
}
else {
cout << i;
}
return 0;
}
a) 0123456789 b) 123456789 c) 0 d) None of the above
____21Which of the following is a valid name for a memory location?
a) Jan.Sales
b) 2ndQuarterIncome
c) COMMISSION_RATE
d) March$
____22.Which of the following determines whether the value contained in sales variable is at
least 300.99?
a) if (sales >= 300.99)
b) if (sales => 300.99)
c) if(sales <=300.99)
d) None of the above
_____23. How many times will the computer process the cout<<numtimes<<endl; statement in
the following code?
int numtimes = 0;
while (numtimes > 0)
{
cout<<numtimes<<endl;
numtimes = numtimes + 1;
}

a) 0 b) 1 c) 3 d) 4
____24.What numbers will the following code display on the computer screen?
int x = 20;
do
{
cout<<x<<” “;
x = x - 4;
} while (x > 10);
a) 16 12 18
b) 16 18
c) 20 16 12 8
d) 20 16 12
___25.What value of x causes the loop in question 24 to end?
a) 0 b)8 c) 10 d)12

Part II

Write the output of the following code fragments in the space provided (Total 15 pts)
1. Write the exact output that is produced by the following program segment. (1 pts)
int k, max = 10;
for(k=1; k <= max; k++) Your Answer:-
{
k++;
k +=max;
cout << "k = " << k <<”\n”;
}
cout << "All done!" ;
Answer:- #include <iostream>
using namespace std;

int main() {
int k, max = 10;
for(k = 1; k <= max; k++) {
k++;
k += max;
cout << "k = " << k << "\n";
}
cout << "All done!";
return 0;
}

Execution Steps:

1. Initialize k = 1, max = 10.


2. Enter the loop with condition k <= max.
3. Inside the loop:
o Increment k by 1 (k++): now k = 2.
o Add max to k (k += max): now k = 12.
o Print k = 12.
o Increment k by the for loop (k++), so the next value of k becomes 13.
4. Check the condition k <= max. Since k = 13 and max = 10, the condition fails, and the
loop ends.
5. Print "All done!".

Output:
bash
CopyEdit
k = 12
All done!

2 .What is the output of the following code segment?(10 pts)


int n;
Your Answer:-
cout << (n = 2) << endl;
cout << (n == 2) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;

Answer:- The code involves assignment operators (=), relational operators (==, >, <), logical
operators (&&, ||, !), and integer evaluations.

Code Segment:
cpp
CopyEdit
int n;
cout << (n = 2) << endl; // Line 1
cout << (n == 2) << endl; // Line 2
cout << (n > 3) << endl; // Line 3
cout << (n < 4) << endl; // Line 4
cout << (n = 0) << endl; // Line 5
cout << (n == 0) << endl; // Line 6
cout << (n > 0) << endl; // Line 7
cout << (n && 4) << endl; // Line 8
cout << (n || 4) << endl; // Line 9
cout << (!n) << endl; // Line 10

Step-by-Step Execution:

1. cout << (n = 2):


o Assigns 2 to n.
o Prints the value of n (which is 2).
o Output: 2.
2. cout << (n == 2):
o Checks if n is equal to 2.
o Since n = 2, the condition is true.
o In C++, true is represented as 1.
o Output: 1.
3. cout << (n > 3):
o Checks if n is greater than 3.
o Since n = 2, the condition is false.
o In C++, false is represented as 0.
o Output: 0.
4. cout << (n < 4):
o Checks if n is less than 4.
o Since n = 2, the condition is true.
o Output: 1.
5. cout << (n = 0):
o Assigns 0 to n.
o Prints the value of n (which is 0).
o Output: 0.
6. cout << (n == 0):
o Checks if n is equal to 0.
o Since n = 0, the condition is true.
o Output: 1.
7. cout << (n > 0):
o Checks if n is greater than 0.
o Since n = 0, the condition is false.
o Output: 0.
8. cout << (n && 4):
o Evaluates the logical AND of n and 4.
o n = 0 (false) and 4 is a non-zero value (true).
o Logical AND with false is always false.
o Output: 0.
9. cout << (n || 4):
o Evaluates the logical OR of n and 4.
o n = 0 (false) and 4 is a non-zero value (true).
o Logical OR with one true operand is always true.
o Output: 1.
10. cout << (!n):
o Evaluates the logical NOT of n.
o n = 0 (false), so !n is true.
o Output: 1.

Final Output:
CopyEdit
2
1
0
1
0
1
0
0
1
1

3. Write output of the following program Segment. (2 pts)


Your Answer:-
int X=0 , Y = 4;
Y++;
X + = Y;
cout<< “ X = \t ”<<X++;
cout<<” Y= \t ”<<X + Y;
Answer:- Code Segment:
cpp
CopyEdit
int X = 0, Y = 4;
Y++; // Increment Y by 1
X += Y; // Add Y to X
cout << " X = \t " << X++; // Print X and then increment it
cout << " Y= \t " << X + Y; // Print the sum of X (incremented) and Y

Step-by-Step Execution:

1. Initialization:
o X = 0
o Y = 4
2. Y++:
o Increment Y by 1.
o Y = 5.
3. X += Y:
o Add the value of Y to X.
o X = 0 + 5 = 5.
4. cout << " X = \t " << X++:
o Print the current value of X (which is 5).
o After printing, increment X by 1.
o X = 6 after the increment.
o Output so far: X = \t 5.
5. cout << " Y= \t " << X + Y:
o Add X (which is now 6) and Y (which is 5).
o X + Y = 6 + 5 = 11.
o Output so far: X = \t 5 Y= \t 11.

Final Output:
CopyEdit
X = 5 Y= 11

4. Write the output of the following code fragment. (1 pts)


Your Answer:-
int billy[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0 ;n < 7 ;n++) {
result += billy[n];
}
cout << result;
Answer:- Code Fragment:
cpp
CopyEdit
int billy[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0; n < 7; n++) {
result += billy[n];
}
cout << result;

Step-by-Step Execution:

1. Initialization:
o billy[] = {0, 2, 4, 6, 7, 5, 3}
o result = 0
2. Loop Execution (n = 0 to 6):
o n = 0: result += billy[0] → result = 0 + 0 = 0
o n = 1: result += billy[1] → result = 0 + 2 = 2
o n = 2: result += billy[2] → result = 2 + 4 = 6
o n = 3: result += billy[3] → result = 6 + 6 = 12
o n = 4: result += billy[4] → result = 12 + 7 = 19
o n = 5: result += billy[5] → result = 19 + 5 = 24
o n = 6: result += billy[6] → result = 24 + 3 = 27
3. Final Step:
o After the loop ends, result = 27.
o Print result.

Final Output:
CopyEdit
27

5. What is the output when the following code fragment is executed?(1 pts)

int found = 1, count = 5; Your Answer:-


if (found || --count == 0)
cout << "danger" << “\n”;
cout << "count = " << count ;
Answer:- Code Fragment:
cpp
CopyEdit
int found = 1, count = 5;
if (found || --count == 0)
cout << "danger" << "\n";
cout << "count = " << count;

Step-by-Step Execution:

1. Initial Values:
o found = 1 (non-zero, evaluates to true in a logical context).
o count = 5.
2. if (found || --count == 0):
o The condition uses the logical OR (||), which short-circuits if the first operand is
true.
o Since found is 1 (true), the condition evaluates to true without evaluating --
count.
o As a result, count is not decremented and remains 5.
3. Inside the if block:
o "danger" is printed, followed by a newline.
4. After the if block:
o count is printed, which is still 5.

Final Output:
java
CopyEdit
danger
count = 5
Part III
Write a complete CPP Program on the attached Sheet of paper clearly.(10 pts)
1. Write a CPP program which accepts unit price and quantity bought and display Total
Cost, VAT (15% of total cost) and Net pay (3 pts).
2. Write a CPP program which prints even numbers from 100 to 200.(3 pts)
3. Write a CPP program which accept student’s name, mark and calculate a display bonus
based on the following criteria.(4 pts)

Criteria Table
Mark Bonus
>=80 Bonus = 5
70 - <80 Bonus = 3
<=70 Bonus = 2

Answer:- Code Fragment:


cpp
CopyEdit
int found = 1, count = 5;
if (found || --count == 0)
cout << "danger" << "\n";
cout << "count = " << count;

Step-by-Step Execution:

1. Initial Values:
o found = 1 (non-zero, evaluates to true in a logical context).
o count = 5.
2. if (found || --count == 0):
o The condition uses the logical OR (||), which short-circuits if the first operand is
true.
o Since found is 1 (true), the condition evaluates to true without evaluating --
count.
o As a result, count is not decremented and remains 5.
3. Inside the if block:
o "danger" is printed, followed by a newline.
4. After the if block:
o count is printed, which is still 5.

Final Output:
java
CopyEdit
danger
count = 5
4o

You said:
Write a complete CPP Program on the attached Sheet of paper clearly.(10 pts) 1. Write a CPP
program which accepts unit price and quantity bought and display Total Cost, VAT (15% of total
cost) and Net pay (3 pts). 2. Write a CPP program which prints even numbers from 100 to 200.(3
pts) 3. Write a CPP program which accept student’s name, mark and calculate a display bonus
based on the following criteria.(4 pts) Criteria Table Mark Bonus >=80 Bonus = 5 70 - <80
Bonus = 3 <=70 Bonus = 2
ChatGPT said:

ChatGPT

Sure! Here is the complete C++ program for the three tasks you requested:

C++ Program for All Tasks


cpp
CopyEdit
#include <iostream>
#include <string>
using namespace std;

int main() {
// Task 1: Calculate Total Cost, VAT, and Net Pay
double unitPrice, quantity, totalCost, vat, netPay;

// Accepting user input


cout << "Enter unit price: ";
cin >> unitPrice;

cout << "Enter quantity bought: ";


cin >> quantity;

// Calculating total cost


totalCost = unitPrice * quantity;

// Calculating VAT (15% of total cost)


vat = totalCost * 0.15;

// Calculating Net Pay (total cost + VAT)


netPay = totalCost + vat;

// Displaying the results


cout << "\nTotal Cost: " << totalCost << endl;
cout << "VAT (15%): " << vat << endl;
cout << "Net Pay: " << netPay << endl;

// Task 2: Print even numbers from 100 to 200


cout << "\nEven numbers from 100 to 200:" << endl;
for (int i = 100; i <= 200; i++) {
if (i % 2 == 0) {
cout << i << " ";
}
}
cout << endl;
// Task 3: Calculate bonus based on student's marks
string studentName;
int marks, bonus;

// Accepting student's name and marks


cout << "\nEnter student's name: ";
cin.ignore(); // to ignore the newline character from previous input
getline(cin, studentName); // Using getline for multi-word names

cout << "Enter marks for " << studentName << ": ";
cin >> marks;

// Calculating bonus based on the marks


if (marks >= 80) {
bonus = 5;
} else if (marks >= 70 && marks < 80) {
bonus = 3;
} else {
bonus = 2;
}

// Displaying the bonus


cout << studentName << "'s bonus is: " << bonus << endl;

return 0;
}

Explanation of the Tasks:

1. Task 1: Total Cost, VAT, and Net Pay Calculation


o The program accepts the unit price and quantity.
o The total cost is calculated by multiplying the unit price by the quantity.
o VAT is calculated as 15% of the total cost.
o The net pay is the sum of the total cost and the VAT.
2. Task 2: Print Even Numbers from 100 to 200
o The program uses a loop to iterate through the numbers from 100 to 200.
o It checks if each number is even (divisible by 2) and prints it.
3. Task 3: Bonus Calculation Based on Marks
o The program accepts the student's name and marks.
o It calculates the bonus based on the given criteria:
 If the marks are 80 or above, the bonus is 5.
 If the marks are between 70 and 79, the bonus is 3.
 If the marks are below 70, the bonus is 2.

Sample Output:
yaml
CopyEdit
Enter unit price: 15.5
Enter quantity bought: 10
Total Cost: 155
VAT (15%): 23.25
Net Pay: 178.25

Even numbers from 100 to 200:


100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136
138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174
176 178 180 182 184 186 188 190 192 194 196 198 200

Enter student's name: John Doe


Enter marks for John Doe: 75
John Doe's bonus is: 3

You can run this program in any C++ environment. It covers all three tasks and handles input
and output as required.

You might also like