0% found this document useful (0 votes)
8 views11 pages

1036 Midterm 2013 S1 V1

Uploaded by

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

1036 Midterm 2013 S1 V1

Uploaded by

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

Sl # _______

WESTERN UNIVERSITY CANADA


FACULTY OF ENGINEERING

ES 1036: Programming Fundamentals for Engineers

Fall 2013 Midterm Examination


Saturday, October 26, 2013
SEB1200, 2200, 2202
Total Marks: 65
Total pages 11 (including the cover page) Please count

Time: 2 Hours (3pm – 5pm)

Student Name ____________________________________

Student Number __________________________________

Instructions (Please Read):

1. This book contains 26 questions.


2. This is a closed book examination. No calculator or smart electronic gadget is allowed in the
exam.
3. The last page contains supplementary information. You may detach this page and set it aside
to use as a reference.
4. Page number 10 contains questions 25 and 26; please detach this page and set it aside while
answering this question.
5. Read each question carefully and ration your time accordingly.
6. This examination constitutes 20% of your total grade for this course.
7. Please use the space provided beneath each question for your answer. For extra space use the
back side of each page.
8. Please write legibly.

"University policy states that cheating on an examination is a scholastic offense. The


commission of a scholastic offense is attended by academic penalties which might include
expulsion from the program. If you are caught cheating, there will be no second warning".
TOTAL: Part I ________/20 + Part II ________/17+ Part III ________/28 = ____________/65

Part I: Multiple choice: Identify the correct answer (1x20 = 20 Marks)

Part I Marks ________/20

Q1. How many bytes are used to store a character type variable in the memory?
(a) 1
(b) 2
(c) 4
(d) 8

Q2. Which of the following computer languages is known as binary language?


(a) High level language
(b) Machine language
(c) Assembly language
(d) Object oriented language

Q3. Which of the following hardware components is considered to be the secondary storage
device?
(a) the scanner
(b) the microphone
(c) the keyboard
(d) the USB memory stick
(e) the monitor

Q4. In C++ programming environment, the file myFile.cpp contains instructions in


(a) high-level language
(b) machine language
(c) assembly language
(d) none of the above

Q5. During which steps in the development process does a programmer write a pseudo code?
(a) Specifications
(b) Design
(c) Implementation
(d) Testing

Q6. What does the second C++ statement (below) mean?


int a(56), b(23);
a = b;
(a) The value of variable b has been assigned to variable a
(b) The value of variable a has been assigned to variable b
(c) Variable a and b are equivalent
(d) Variable a and b are located in the same address location
in the memory.

Q7. True or False: The following code may result in a logical error.
#include <iostream>
using namespace std;
int main()
{
int myNumber = 3;
if(myNumber = 4)
cout<<++myNumber<<endl;
else
cout<<myNumber--<<endl;
return 0;
}
(a) True
(b) False

ES1036 Midterm, Fall 2013, QMR 2/11


Q8. What will be the output of the following code segment?
#include <iostream>
using namespace std;
int main()
{
const int myNumber; int i(0), sum(0);
for(i=0; i<2; i++)
{
sum = sum + myNumber;
}
cout<<sum<<endl;
}

(a) 1
(b) 3
(c) The code will not compile
(d) The code will generate a runtime error

Q9. Assuming that all the required library files have been specified and all the variables have
been declared with appropriate initialization, identify the C++ statement which is
grammatically incorrect?

(a) int divide_by_2(0.2);


(b) cin>>k>>endl;
(c) if (a = b){cout<<a<<endl;}
(d) char a;
(e) None of the above is grammatically incorrect

Q10. True or False: The following two code segments will result in the same output for any value,
entered from the keyboard, for the variable ‘num’.

int num = 0; cin >> num; int num = 0; cin >> num;
if (num >= 0 && num <= 100) if (0 <= num <= 100)
cout << num <<endl; cout << num <<endl;

(a) True
(b) False

Q11. What will be the output of the following code segment?


#include <iostream>
using namespace std;
int main()
{
int a(5), b = 2;
double c = a/b*1.0;
cout<<c<<endl;
return 0;
}
(a) 2
(b) 2.5
(c) The code will not compile

Q12. To perform the following C++ operation logically correctly, which one of the following data
types should be chosen for the variable b?
cout << (5 / b + 2.5) <<endl;

(a) int
(b) char
(c) double
(d) Any one of the above

ES1036 Midterm, Fall 2013, QMR 3/11


Q13. What will be the output of the following code?
#include <iostream>
using namespace std;
int main()
{
int i(0);
do
{
}while(i++<2);
cout<<i<<endl;
}
(a) 0
(b) 1
(c) 2
(d) 3
(e) The loop will never end!

Q14. True or False: The following two code segments will result in the same output.
int i(2);
do for(int i = 2; i<3; i++)
{ {
cout<<i<<endl; cout<<i<<endl;
} while(++i<3); }

(a) True
(b) False

Q15. What will be the value of the variable a after the following code is executed?
#include <iostream>
using namespace std;
int main()
{
int a(3), b(5);
a = --a + b--;
}
(a) 7
(b) 8

Q16. True or False: The following two code segments will result in the same output.
int i = 3; int i = 3;
if('0') if(5)
{ {
--i; i--;
cout<<i<<endl; cout<<i<<endl;
} }

(c) True
(d) False

Q17. How many times will the cout statement in the following code segment be executed?
for (int k=3; k<=13; k=k+2)
{
cout<<"do nothing\n";
++k;
}
(a) 3
(b) 4
(c) 5
(d) 8

ES1036 Midterm, Fall 2013, QMR 4/11


Q18. What will be output of the following code segment if the user enters the string
“Hogwarts_School_of_Witchcraft_and_Wizardry” from the keyboard for line number x
below?
#include <iostream>
using namespace std;
#include <string>
int main()
{
string name;
cin>>name; //line number x
cout<<name<<endl;
}
(a) Hogwarts_School_of_Witchcraft_and_Wizardry
(b) Hogwarts
(c) name

Q19. Given the declaration float i(100), j(0.003); which of the following statements
will display the message: I’m in heaven!
(a) if (!i>j) cout << " I’m in heaven!";
(b) if (!(j<i)) cout << " I’m in heaven!";
(c) if ((j<i)&&(!i)) cout << " I’m in heaven!";
(d) if (!j) cout << "I’m in heaven!";
(e) None of the above

Q20. What will be printed after the following cout statement is executed?
cout<<(9*(1.0/3*3)+2)<<endl;
(a) 0
(b) 2
(c) 3
(d) 11

Part II: Write the output which is generated from each of the following program segments.
Note: There are no programming errors in this section. [Hint: Show the memory diagram to
receive partial marks in case the answer is incorrect]. [3 questions: 17 Marks]
Part II Marks ________/17

Q21. [3 Marks]
#include <iostream> Answer:
using namespace std;
int main()
{
for(int r = 0; r<3; r++)
{
for(int c = 0; c<=r; c++)
{
if(c == 1)
break;
cout<<'*';
}
cout<<endl;
}
return 0;
}
Memory Diagram (This is not Mandatory; this is for your rough work only):

ES1036 Midterm, Fall 2013, QMR 5/11


Q22. [8 Marks] Answer:
#include <iostream>
using namespace std;
int main()
{
int sum=1, i=0;
for (i=0; i<6; i++)
{
if (i%2==0)
sum=sum+i;
else
continue;
cout<<"Run "<<i<<": "<<"sum = "<<sum<<endl;
}
cout<<"i = "<<(++i+i)<<". "<<endl;
return 0;
}

Memory Diagram (This is not Mandatory; this is for your rough work only):

Q23. [6 Marks] [Watch out for the break statements!]


#include <iostream> Answer:
using namespace std;
int main()
{
int result(1), i = 1;
do
{
switch (i)
{
case 2:
result = result+3;
break;
case 3:
result = result/3;
default:
result = result + 2;
break;
}
cout << "i = " << i << ", "
<< "result = " << result << endl;
i = i + 1;
}while(i <= 3);
}
Memory Diagram (This is not Mandatory; this is for your rough work only):

ES1036 Midterm, Fall 2013, QMR 6/11


Part III: Code writing [3 questions, total 28 Marks]
Part III Marks ________/28

Q24. [6 Marks] Coding Fundamentals


(a) [1 Mark] Write a C++ statement that declares a character type data, and inilizes it with a
value Q.

(b) [1 Mark] State briefly, why the following code will NOT compile?
#include <iostream>
using namespace std;
int main(void)
{
double y = 3;
const int x(5);
while(++y<=6)
{
if(y%2==1)
cout<<(x-1)<<", ";
}
cout<<endl;
}

(c) [2 Marks] Write the expression for an if-statement that decides whether an integer
variable called number contains an even numbered value between -10 and +10
inclusive. Write your answer in the given space in if-header.

if(_____________________________________________________)
{
cout<<number<<endl;
}

(d) [2 Marks] Write the corresponding C++ expression for the arithmetic equation given
below. You can assume that all the varaibles have been declared as double type variable
and the required header files have been included. Also note that the % sign in the
following expression represents modulus operator.
bd + a 32
y = a+ −d
a + 5%c

Answer:

Answer Question numbers 25 and 26 on the next pages. These questions have been presented
on page number 10 so that you can detach this page from the exam-paper and set it aside while
answering these questions. For extra space, you can use the back side of the previous page.

ES1036 Midterm, Fall 2013, QMR 7/11


This page has been left empty for extra space

ES1036 Midterm, Fall 2013, QMR 8/11


This page has been left empty for extra space

ES1036 Midterm, Fall 2013, QMR 9/11


Q25. [10 Marks] Write a C++ program to numerically calculate the value of ‘x to the power
y’ ( x y ) WITHOUT using the <cmath> library functions. No mark will be awarded if
<cmath> library function is used to solve this problem. (Note: this problem was solved
on the board during the lecture hours)
Program requirements:
(a) Prompt the user to enter the value x. The variable x should be able to accommodate any
real number. No validation is required. [1 Mark]
(b) Prompt the user to enter the value of y. You can assume that the user will enter any
positive INTEGER value including zero. No validation is required. Note: your code
should be able to print 1 if the user enters zero for the variable y. [1 Mark]
(c) Calculate x y , and display the result. [4 Marks]
(d) After displaying the result of this calculation, the program should ask the user whether
s/he wants to continue or not. If the user types y or Y as her/his choice, the above steps
will be repeated otherwise the code will be terminated. [4 Marks]

Sample output:
Enter x value: 2
Enter y value: 0
The result is: 1
More power calculation? (press 'n' to terminate): y
Enter x value: 2.5
Enter y value: 2
The result is: 6.25
More power calculation? (press 'n' to terminate): y
Enter x value: 4
Enter y value: 2
The result is: 16
More power calculation? (press 'n' to terminate): n
Good bye!

Q26. [12 Marks] Write a C++ program with the following specifications:
(a) Prompt the user to enter an integer value n greater than 5. You can assume that the user
will enter an integer number greater than 5; no validation is required. [1 Mark]
(b) Now, based on the input from (a), ask the user to enter these n different integer numbers
from the keyboard and do the following:
i. Validate that each of the entered numbers is in between 4 and 500 inclusive (no mark
will be awarded if the validation is not done inside a loop-structure). You’re only
required to validate the range of the entered numbers. [3 Marks]
ii. Calculate the average of the entered numbers which are divisible either by 3 or 5 but
not divisible by 15. [5 Marks]
iii. Print this average. Note that if there is no entered number divisible either by 3 or 5 but
not divisible by 15, the program should display a message such as “No expected
number was entered”. [3 Marks]

Note: you can use the back side of the previous page for extra space
Sample output:
Enter an integer greater than 5: 66
Enter number (between 4 and 500 inclusive) 1: 72
Enter number (between 4 and 500 inclusive) 2: 2
Invalid number!
Enter number (between 4 and 500 inclusive) 2: 57
Enter number (between 4 and 500 inclusive) 3: 89
Enter number (between 4 and 500 inclusive) 4: 689
Invalid number!
Enter number (between 4 and 500 inclusive) 4: 6
Enter number (between 4 and 500 inclusive) 5: 34
---
---

Enter number (between 4 and 500 inclusive) 66: 23

Total entered numbers divisible by 3 or 5 but not by 15, is: 31


Average of these numbers is: 45.34.
Press any key to continue . . .

ES1036 Midterm, Fall 2013, QMR 10/11


Appendix: Supplementary Information

Operator Precedence: U Are the best CLAss in the Universe.


Math Functions (#include <cmath>) if-else statement
double sqrt(double v) gives the square root of v
double pow(double x, double y) gives xy if(expression)
{
statements;
Relational, equality, and logical expressions }
< Less than == Equal else
> Greater than != Not equal {
statements;
<= Less or equal && And
}
>= Greater or equal || Or
! not

Switch statement
switch (integer_expression)
{
case integer_constant:
statements;
break;
etc…
default:
statements;
}
Note: “integer expression” is an expression
that evaluates to an integer and may be a
simple integer variable or char variable.

For loop
for(expr1 ; expr2; expr3)
{
Statements;
}

While loop
while (expression)
{
Statements;
}

Do loop
do
{
Statements;
} while (expression);

ES1036 Midterm, Fall 2013, QMR 11/11

You might also like