You are on page 1of 13

The Answer of the Mid-Term Sheet

1. Define the following:

Hardware : Refers to the parts of a computer that you can see and touch, including
the case and everything inside it.
Software : Refers to the instructions, or programs, that tell the hardware what to
do.
Data : Known facts in a form a computer can store and use.
(textual, numeric, etc)
Information : Processed data or any knowledge that can be communicated.
Algorithm : Instructions for solving a problem or sub-problem in a finite amount of
time using a finite amount of data.
Data type : The specification of how data or information is represented in the
computer.
Computer Data type specifications and instructions for carrying out operations
program : that are used by a computer to solve a problem.
Identifier : is a name used to identify a variable, function, class, module, or any
other user-defined item.
Computer : A programmable device that can store, retrieve, and process data.

2. Compare between RAM and ROM:

Comparison RAM ROM


Stands for Random Access Memory Read Only Memory
Usage Can be read from and written to Can only be read from and not
written to
Storage Temporary (volatile) Permanent (non volatile)
Speed of data Fast Slower than RAM
Types Static RAM and Dynamic RAM PROM, EPROM and EEPROM

3. Draw a block diagram of the basic components of a computer:

ALU control line


data line

Input Memory Output

Control
Unit
4. Draw a block diagram of the process of converting a program written in high level
language into machine code.

Library
Syntax Error

Source Compiler Linker Machine Code


code
.obj .exe

5.
C++ Program Flow Chart
#include<iostream> Start
using namespace std;
void main()
{ Read n
int n;
cout << "Enter a number:\n";
cin >> n; yes no
if (n % 2 == 0) n%2=0
{
if (n>0) yes no yes no
cout<< "It is even +ve"<< endl; n>0 n>0
else
cout<< "It is even -ve"<< endl;
} Print Print Print Print
else even +ve even -ve odd +ve odd -ve
{
if (n>0)
cout<< "It is odd +ve"<< endl;
else
cout<< "It is odd -ve"<< endl; End
}
} p
6.
C++ Program Flow Chart
#include<iostream>
Start
using namespace std;

void main() i= 1
{ int x;
for (int i=1; i<4 ; i++)
{ Read x
cout << "Enter a number:\n"; i=i+1
cin >> x; No I=1
if(x==7) No Yes
{ i=3 x=7
cout<< "You win"<<endl; Yes
break;
} //end of if Print you win
Print you lose
else if (i==3)
cout<< "you lose" <<endl;
} //end of for
} End

7.
C++ Program Flow Chart
#include<iostream> Start
using namespace std;

void main() i= 1, sum=0


{ int n ;
double avg, sum=0, x;
Read n
cout << "Enter number of digits: ";
cin >> n; I=1
Read x
for (int i=1; i<=n ; i++)
{ I=1
cout << "Enter a number: "; i=i+1
cin >> x; sum=sum + x
sum= sum + x;
}
i=n
avg= sum/n;

cout<< "The sum = "<<sum<<endl;


Average=sum/n
cout<< "The average = "<<avg<<endl;
}
Print average, sum

End
8.
C++ Program Flow Chart
#include<iostream> Start
using namespace std;

void main() Read n


{ int n, abs ;
cout << "Enter a number: "; I=1
cin >> n; Yes No
if (n>0) n>0
abs = n;
else
abs = -n; abs = n abs= -n
cout<< "The absolute = "<<abs<<endl;
}

Print abs

End

9.

C++ Program Flow Chart


#include<iostream> Start
using namespace std;
void main()
{ i= 1
int x, y, result;
char op;
bool b; Read x , y, op
for (int i=1; i<=3 ; i++) i=i+1
{ b = true; No I=1
cout<<"Enter x&y "; Yes
cin>>x>>y; i=3 print sum sum= x + y op= +
cout<<"Enter Operation";
cin>>op; Yes I=1 No
if(op=='+') result= x + y; print sub sub= x - y Yes
else if(op=='-') result= x - y; op= -
else if(op=='*') result= x * y; I=1
else if(op=='/') No
{ if(y==0) print mul mul= x * y Yes op= *
{ b=false;
cout<<"y can't be zero"<<endl;} I=1 No
else result=x / y; } No Yes
else div= x / y y=0 op= /
{ b=false;
cout<<"Wrong Operation"<<endl; } Yes
if (b) print div
Print y can't be 0 Print wrong op
cout<<"The result= "<<result<<endl;
I=1
} //end of for I=1 I=1
} End
10.

C++ Program Flow Chart


#include<iostream> Start
using namespace std;

void main() i=1, fact=1


{ int n, fact=1;

cout << "Enter a number:\n"; Read n


cin >> n;
I=1
for (int i=1; i<=n ; i++) fact=fact * i
{ i=i+1
fact = fact * i; No
} i=n
Yes
cout<< "The factorial = "<<fact<<endl;
}
Print fact

End

11.

C++ Program Flow Chart


#include<iostream> Start
using namespace std;
void main()
{ int n, m, sum=0; i=1, sum=0

cout << "Enter a number:\n";


cin >> n; Read n

for (int i=1; i<=n ; i++) I=1


{ i=i+1 m=i*i
m = i * i; sum= sum +m
sum = sum + m;
}
no
i=n
cout<< "The sum = "<<sum <<endl;
} yes
Print sum

End
12.

C++ Program Flow Chart


#include<iostream> Start
using namespace std;
void main()
{ int n, m, sum=0; i=1, sum=0
cout << "Enter a number:\n";
cin >> n;
for (int i=1; i<=n ; i++) Read n
{
if(i%3==0) I=1 No
{ i%3=0
m = i * i; yes
sum = sum + m;
} m=i*i
} i=i+1
sum= sum +m
cout<< "The sum = "<<sum <<endl;
}
no
i=n
yes
Print sum

End

13. Answer the following questions:

a) C is Uppercase char
b) 5 is a Digit
c) If we put any special character (as , . ; : etc.)
d) No we can't , because there is a comparison in if else statement

14. Identify and correct the error(s):


a) if ( age >= 80 ) ;
cout << "Age is greater than or equal to 80"<endle;
else
cout << "Age is less than 80" << endl" ;

b) Ffor ( x = 100,; x >= 1,; x++ x- - )


cout << x << endl;

c) Wwhile ( x <= 100 )


{
total += x;
x++;
}
d) #include <isostream>
using namespace sdtstd;
void main( )
{
char iIndex_of_Name;
cout <<"'Enter First Char of Student name:'";
cin >> index_of_Name ;
switch on ( index_of_Name )
{
case "'A'":
cout<<" Ali kammel,20 Year old, third stage" << endlL;
case "'S'":
cout<<" Salem kammel,18 Year old, third stage" << endlL;
case "'H'":
cout<<" Hussien Ahmmed,21 Year old, third stage" << endlL;
defuault:
cout<<"You Not have saved names in this index" << endlL;
}
return 0;
}

15. What is the function of each program? What is the output?

a) It's only adding the odd numbers from 20


to 300.

b) It displays the even numbers in one


column from 2 to 99.

c) It determines the entered value whether


it's even or odd.
16. What is the output of the following fragment codes?

a) 8

b) 8

c) 8

d) 8

e) 8

17. Given the following declarations:

a) (count == 0) && (limit > 12) FALSE


b) (limit > 20) || (count < 5) TRUE
c) !(count == 12) TRUE
d) x == 3 TRUE
e) x < y%2 FALSE
f) x >= (y%3)++ TRUE
g) x != ++y - 2 TRUE
h) x < 10 TRUE
i) x >= 0 && x < 10 TRUE
j) x >= 0 && x < 2 FALSE
k) x < 0 || x > 10 FALSE
l) ! (c1 && !c2) FALSE
18. Answer the questions below concerning the following fragment of code:

int n;
cout << "Enter an integer: ";
cin >> n;
if (n < 10)
cout << "less than 10" << endl;
else if (n > 5)
cout << "greater than 5" << endl;
else
cout << "not interesting" << endl;

a) less than 10
b) greater than 5
c) greater than 5
d) no values
e)

19. Choose the correct answer:


1) What is the only function all C++ programs must contain?

a) start()
b) system()
c) main()
d) program()

2) What punctuation ends most lines of C++ code?

a) . (dot)
b) ; (semi-colon)
c) : (colon)
d) ' (single quote)
3) Which of the following is a correct comment?

a) */ Comments */
b) **Comment**
c) /* Comment */
d) // Comment
e) { Comment }
f) Both c , d
g) None of the above

4) The file iostream includes:

a) The declarations of the basic standard input-output library.


b) The streams of includes and outputs of program effect.
c) Both of these.
d) None of these.
5) To increase the value of c by 3 which of the following statement is wrong?

a) c=c+3
b) c+=3
c) c+++
d) c =+3
e) both c , d
f) none of the above

6) Which of the following is known as insertion operator?

a) ^
b)
c) <<
d) >>

7) Which of the following statements is NOT legal?

a) char ch ='b';
b) char ch ='0';
c) char ch ="cc";

8) What is the correct way to write the condition y < x < z?

a) (y < x < z)
b) ((y < x) && z)
c) ((y > x) || (y < z))
d) ((y < x) && (x < z))
9) Given the following code:
#include<iostream>
using namespace std;
void main ( )
{
int score;
cout << "Enter your test score: ";
cin >> score;
switch (score/10)
{
case 10:
case 9: cout << "Your grade is A" << endl; break;
case 8: cout << "Your grade is B" << endl; break;
case 7: cout << "Your grade is C" << endl; break;
case 6:
case 5: cout << "Your grade is D" << endl; break;
case 4:
case 3:
case 2:
case 1:
case 0: cout << "Your grade is F" << endl; break;
default: cout << "Error: score is out of range.\n";
}
cout << "Goodbye" << endl;
}

According to the given code, choose the correct answer:

i. If the user enters score by 95, the output will be:

a) "Your grade is B"


b) "Your grade is A"
c) "Error: score is out of range"
ii. If the user enters score by 45, the output will be:

a) "Your grade is D"


b) "Your grade is F"
c) "Error: score is out of range"
d) All of the above
e) both b , c

20. Answer the following questions:


a) The conditions for naming a valid identifier:
1) Doesn't start with a number.
2) Doesn't have any special character except underscore ( _ ).
3) Can't be a keyword.
b) Which of the following legal identifiers and which is illegal and why?

Modern Academy Illegal Has a special character


Integer Legal
int Illegal A keyword
Modern_academy Legal
Const Legal
1section Illegal Begins with a number
Group1 Legal
Modern-Academy Illegal Has a special character
Main Legal
student@gmail Illegal Has a special character
_modern Legal
Constant Legal
dOuble Legal
float Illegal A keyword
Not_done! Illegal Has a special character
MaxTemp Legal

21. Put (F) for the false statement and correct it, otherwise put (T) :
1) The body of the while loop may never execute. T
2) The opposite of (x >3 || x < 10) is (x < 3 && x > 10). F (x >= 3 && x <= 10)
3) Loops are used when we need our program to make a choice between two or more things.
F Conditional Statements
4) It is legal to declare more than one variable in a single statement. T
5) The opposite of less than or equal is greater than or equal. F is only greater than
6) All switch statements can be converted into nested if-else statements. T
7) All nested if-else statements can be converted into switch statements. F some not all
8) A break statement in a switch stops your program. F stops the switch case
9) The compiler ignores comments and does not translate them into machine code. T
10) A data type of a value determines how this value is represented in computer memory. T
11) When you create a variable, you reserve some space in memory. T
12) The name of a variable can be composed of letters, digits, and special characters.
F and underscore only
13) An if statement must be followed by else statement, which executes when the Boolean
expression is false. F can be optional followed by else statement
14) The "do...while loop" repeats a statement or group of statements while a given condition is
true. It tests the condition before executing the loop body. F while loop
15) Program comments are explanatory statements that you can include in the C++ code that
you write and helps anyone reading it's source code. T
16) y = x++ means that we first increase x by 1 then assign the new value of x into y. F y=++x
17) y = --x is called post-decrement operation. F pre-decrement
18) if x = 5 then x / 2 = 2.5 . T
19) if y = 6 and x = 2 then x % y = 0 . F x%y=2 (there's 0 remaining of 2 in 6 + 2 remaining)
20) The % operator can be used with float and integer operands. F only with integer

22. Evaluate each of the following expressions, assuming in each case that m has the value 25
and n has the value 7:

a) m - 8 - n (ans. 10)
b) m = n = 3 (ans. m=3 and n=3)
c) m % n (ans. 4)
d) m % n++ (ans. 4)
e) m % ++n (ans. 1)
f) ++m - n-- (ans. 19)

23. In each of the following statements, assume that m has the value 5 and n has the value 2
before the statement executes. What will be the values of m and n will be after each of the
following statements executes?

a) m *= n++; (ans. 10)


b) m += --n; (ans. 6)

You might also like