1036 Midterm 2013 S1 V1
1036 Midterm 2013 S1 V1
Q1. How many bytes are used to store a character type variable in the memory?
(a) 1
(b) 2
(c) 4
(d) 8
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
Q5. During which steps in the development process does a programmer write a pseudo code?
(a) Specifications
(b) Design
(c) Implementation
(d) Testing
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
(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?
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
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
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
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):
Memory Diagram (This is not Mandatory; this is for your rough work only):
(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.
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
---
---
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);