Fundamental (Cpp) Code 1 (2)
Fundamental (Cpp) Code 1 (2)
Fundamentals of programming
Final Examination
Date October 2023
Time allowed 1:45
Name _______________________________________ Id___________
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:
Output:
bash
CopyEdit
k = 12
All done!
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:
Final Output:
CopyEdit
2
1
0
1
0
1
0
0
1
1
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
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)
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
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:
int main() {
// Task 1: Calculate Total Cost, VAT, and Net Pay
double unitPrice, quantity, totalCost, vat, netPay;
cout << "Enter marks for " << studentName << ": ";
cin >> marks;
return 0;
}
Sample Output:
yaml
CopyEdit
Enter unit price: 15.5
Enter quantity bought: 10
Total Cost: 155
VAT (15%): 23.25
Net Pay: 178.25
You can run this program in any C++ environment. It covers all three tasks and handles input
and output as required.