Exam 1 Spring 2016 Solution: 3. Source Code - Compile - Link - Execute

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

EXAM 1 SPRING 2016 SOLUTION

Question 1 (10 Points):


[A] (5 Points) Circle the letter associated with the right answer
1) Which of the following is the right sequence on building C++ program?
1. Source code – execute – Compile – Link
2. Source code – Link– Compile – execute
3. Source code – Compile – Link – execute
4. Source code – Compile – execute – Link

2) Which of the following is “Nonprintable machine readable” file?


1. Object program
2. Source code program
3. Executable program
4. All of the above

3) Which of the following describes “division by zero” error?


1. Syntax error
2. Run time error
3. Logical error
4. Linking error

4) One of the following C++ libraries has to be included before your program can
use toupper(ch) system function
1. iostream
2. cmath
3. iomainp
4. cctype

5) Which of these is not a valid variable name?


1. MAIN
2. 9X
3. Total_amount
4. _Salary
EXAM 1 SPRING 2016 SOLUTION
[B] [5 Points] Write C++ statement(s) that accomplish the following. You may
use multiple C++ statements unless explicitly mentioned:

No. Task C++ statement(s)


Declare and initialize two double double M(5.0), n(2.0);
variables “M” and “n” initializing
1
them to 5.0 and 2.0 respectively (use
Single C++ statement only)

int temp;
temp=M;
Swap the contents of the double M=n;
2 variables “M” and “n”. (Declare N=Temp;
additional variables, if necessary.)

3 Increment the variable “M” by 5 M+=5; //M=M+5;

Print the variable “n” and the cout<<n<<”\t“<<2*M+5-n;


expression (2 * M + 5 – n) separated
4
by tab. (use Single C++ statement
only)

if (gender==’M’)
cout<<”Male”;
On the output screen, print “Male” if else if (gender==’F’)
cout<<”Female”;
the gender is 'M', “Female” if the else
5 gender is 'F', and invalid gender cout<<” invalid gender”;
otherwise. Assume gender is already
declared character variable
EXAM 1 SPRING 2016 SOLUTION
Question 2 (10 Points):
[A] (5 Points) Circle five (5) syntax errors in the following C++ program and
rewrite the corrected line in the provided space in the next column.
Rewrite the whole line (if any error exist)
1 #include <iostream>

2 using namespace std;

3 int main(); int main()

4 { int KPI // Declarations of variables ; int KPI ; // Declarations of variables

5 double salary(0), basic_Bonus(0),

6 extra_Bonus(0), total_Bonus(0);

7 cout<<"Please enter the employee's salary

8 and KPI:";

9 cin>>salary,KPI; cin>>salary>>KPI;

10 if (KPI <=0)

11 { cout<<"Invalid KPI”; return 1; }

12 if (KPI>2)

13 { basic_Bonus=500;

14 extra_Bonus=salary/2.0;;

15 }

16 else

17 BASIC_BONUS=300; basic_Bonus=300;

18 total_Bonus=basic_Bonus+extra_Bonus;

19 cout<< Total Bonus is: "<<total_Bonus; cout<< “Total Bonus is: "<<total_Bonus;

Return 0; return 0;

}
EXAM 1 SPRING 2016 SOLUTION
[A] (5 Points) write a complete C++ program that:
Write a complete C++ program that reads time components (hours, minuets, second)
from the user as three integer values and then the program should compute the total
number seconds. Your program should give appropriate message in case of negative
input values or unrealistic values for time (i.e. hour> 24; minuets>60; second >60).

#include <iostream>
using namespace std;
int main()
{
// Declarations of all variables
int hours, minutes, seconds, totalSeconds; //[0.5]
//read hours, minutes and seconds values from user
cout<<"Please enter hours, minutes and seconds separated by space:"; //[0.5]
cin>>hours>>minutes>>seconds;

//Check that read values are not negative


if (hours<0 || minutes<0 || seconds<0) //[1]
{ cout<<"Sorry, negative values for time are not acceptable";
return 1;
}

//Check that hours, minutes, and seconds are valid (e.g hours within 0-23)
if (hours<24 && minutes<60 && seconds<60) //[1]
{ //calculate totalSeconds.
totalSeconds=hours*3600+minutes*60+seconds; //[1]

//Write the output


cout<<"Total seconds in "<<hours<<":"<<minutes<<":"<<seconds<<" is "
<<totalSeconds<<" seconds"; //[1]
}
else
cout<<"you have entered unrealistic values for time";

return 0;
}
EXAM 1 SPRING 2016 SOLUTION
Question 3 (10 Points)
[A] Trace output of the following code. (5 Points)
int temp;
char weather;

cout<<"Enter temp ";


cin>>temp;

if (temp < 5)
weather='C';
else if(temp>=5 && temp < 15)
weather='c';
else if(temp>=15 && temp<=28)
weather='M';
else
weather ='H';

cout<<fixed;

switch(weather)
{
case 'C':
case 'c': cout<<temp * 1.8 + 32<<"Celsius"<<endl;
break;
case 'M': cout<<setw(10)<<right<<setprecision(1)
<<temp * 1.8 + 32<<"\n\n";
break;
case 'H': cout<<setw(15)<<setprecision(3)<<temp * 1.8 + 32;

cout<<"Bye"<<endl;
return 0;
}

Write output when temp=5


4 1 . 0 0 0 0 0 0 C e l s i u S

Write output when temp=15


5 9 . 0

B y e

Write output when temp=30


8 6 . 0 0 0 B y e
EXAM 1 SPRING 2016 SOLUTION
[B] Translate the highlighted part of the program to flow chart diagram. (5 Points)

#include<iostream>
using namespace std;
int main()
{
int op;
cout<<"Enter code : " ;
cin>>op;

if(op>=1 && op<=3)


cout<<"Normal Operatoins"<<endl;
else if (op==4 || op==5)
cout<<"Maintenance Required"<<endl;
else
cout<<"Not a Valid code"<<endl;

return 0; }

Draw flow chart diagram here:

Read op

True/yes
if(op>=1 &&
op<=3) Print "Normal Operatoins"

False/no

True/yes
if (op==4 ||
Print "Maintenance Required” "
op==5)

False/no

Print "Not a Valid code”

Exit

You might also like