Cosc112 Second Sem Test Solution

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Page 1 of 3 PAPER REF.

NO

Name:……………………………………………..
Matric. No:………………………………………
[QUESTION PAPER] TITLE:
INTRODUCTION TO COMPUTER SCIENCE & PROGRAMMING II(COSC112)

MID SEMESTER MARKING GUIDE (2ND Semester)


SECTION A (Attempt all) (10 Marks, 1 for each)

1. In C++, a do{…}while(condition) statement ends 6. Translate the following into a C++ expression:
with a semi-colon: True/False? if x < y < z
solution: if((x<y) && (y<z))
2. Re-write the following mathematical expression in 7. Consider the following macro function created by
an acceptable C++ expression: a friend:
#define CUBE(y) (y*y*y)

If y is assigned a given value in the main program,


x= (a+b)/(c-d) write out how you would call the function and output
the result. Soln:
cout<<CUBE(y);
3. Mention 3 unique characteristics (features) of C++ 8. The following Floating Point Scientific Notation,
that distinguish it from C (a procedural language): 3.67e8 means:……………. 367000000
Inheritance, polymorphism and object instantiation
4. In the following code, what is the output from cout 9. What is the output if integer values 2 is received by
if cin is used to read in the following string data: variable choice in the following code:
“mount zion”
cin>>choice;
#include <iostream> switch(choice){
using namespace std; Case 1: cout<<"Hey "; break;
int main() Case 2: cout<<"How ";
{ Solution: mount Case 3: cout<<"Coding ";break;
string name; default: cout<<"\n Out of Range";
cin >> name; }
cout <<name; } Output: How Coding
5. From the following, identify and write out 10. What is the output of the flowing code chunk:
keyword tokens that are strictly (predominantly) {
owned by C++ language: int a = 5;
int b = 10;
i) break ii) bool iii) else iv) struct v) namespace cout << ( a > b? a : b);
}
Soln: 10

SECTION B (Attempt ONLY one Question; 5 marks for each) (Write your code in C++ language)
1) Write a C++ program to calculate overtime pay of 5 employees. Overtime is paid at the rate of
N150.00 per hour for every hour worked above 40 hours. Assume that employees do not work
for fractional part of an hour. The program accepts user data: Full Name and number of hours
worked. The program displays each employee’s name, hours worked, total pay and overtime pay.

Soln:
//Section B: Question 1
//Programm tocompute overtime for 5 employee
#include<iostream>
Page 2 of 3 PAPER REF. NO
using namespace std;
int main(){
int overtime_pay, numb_hr,emp_numb;
string name;
double overtime_hr_rate = 150.00;

for(emp_numb = 1;emp_numb <=5;emp_numb++){


cout<<"\nEnter Name: ";
cin>>name;
cout<<"Enter Number of Hours worked: ";
cin>>numb_hr;
if(numb_hr>40)
overtime_pay = (numb_hr-40)*overtime_hr_rate;
else
{
cout<<"You have less than 40 hours, no overtime pay"<<endl;
overtime_pay = 0.0;
}
cout<<"\nEMPLOYEE "<<emp_numb<<" PAY REPORT:\n";
cout<<"Over Time Pay for "<<name<<" = "<<overtime_pay<<endl;
}
return 0;
}

2) Suppose a bank in Lagos gives loan to business men and women in the following ranges for a
period of 2 years. Write a C++ program to accept the Loan P and compute the Interest using the
formula I given above and the respective rate [Hint: I = PRT/100]

N200,000 - N 500,000 rate of interest = 5.5%


N 550,000.00 - N 800,000 rate of interest = 7.5%
N 850,000.00 – N 1,000,000 rate of interest = 9.5%
N 1,500,000 - N 2,000,000 rate of interest = 10.5%

Soln:

//SECTION B:Question 2
//Program to calculate interest given a rate
#include<iostream>
using namespace std;
int main() {
double P, R;
double T = 2.0;
double I;

cout<<"Enter Your Principal P: ";


cin>>P;
if((P>=200000)&&(P<=500000)){
R = 0.055;
I = P*R*T/100;
cout<<"The interest with the interest rate of 5.5% is:"<<" "<<I;
}
else if((P >=550000)&&(P<=800000)){
Page 3 of 3 PAPER REF. NO
R = .075;
I = P*R*T/100;
cout<<"The interest with the interest rate of 7.5% is:"<<" "<<I;
}
else if((P>=850000)&&(P<=1000000)){
R = .095;
I = P*R*T/100;
cout<<"The interest with the interest rate of 9.5% is :"<<" "<<I;
}
else if((P>=1500000)&&(P<=2000000)){
R = .105;
I = P*R*T/100;
cout<<"The interest with the interest rate of 10.5% is :"<<" "<<I;
}

return 0;

You might also like