Sie sind auf Seite 1von 7

INTRODUCTION TO OBJECT ORIENTED PROGRAMMING PAST PAPER Question 1 a.

With examples, explain how a while loop differs from a do while loop. In while loop, the syntax is While (condition) { Statements } Example #include<iostream.h> int main () { Int x=10; While (x>0){ cout<<x<<endl; } } Do while loop The syntax is; do { Statements; } while (condition); Example #include<iostream.h> int main () { int x=10; do {

cout<<x<<endl; x-=1; }while (x>0); } b. With an example, define an infinite loop. An infinite loop is a loop whose termination test never evaluates to false. Or is a sequence of instructions in a computer program that is repeated over and over without end, due to a mistake in the programming. Study the following code #include<iostream.h> int main () { int i=200; while (i>100) { cout<<i<<endl; i=i-10; } } (i). what will be the output of the above code. The above code output the following integers 200, 190, 180, 170, 160, 150,140,130,120, and 110. (ii). Rewrite the above program using do-while loop and for loop producing the same output.

Using do-while loop #include<iostream.h> int main () { int i=200;

do{ cout<<i<<endl; i=i-10; }while (i>100); } Question 2 (a). Go through this source code and fine out all the possible errors. Isolate each line with an error, state the error and underline exactly where the error is, then rewrite the correct version of each line from which you have isolated the error. #include<iostream.h> int main () { int 4theman; 4theman=10; float interes Rate=200.4; float case=10.4; cout<<case+4theman<<endl; return 0; Rewriting the correct program, we have; #include<iostream.h> int main () { int a=4, b=10; float c=200.4,d=10.4; cout<<d+b<<endl; return 0; } (b). Write down the output of the following code fragment.

For (int sum=10;sum<=10;sum+=11) { if ((sum==21))//(sum==51)) {continue;} cout<<sum<<endl; } } The above program output an integer 10. (c). Write a C++ program that captures any three integers, computes and outputs their sum, product, difference and average. #include<iostream.h> int main() { int x; int y; int z; int sum; int product; int difference; float avg; { cout << " Enter x y z: "; cin >> x >> y >> z; sum = x + y + z; product = x*y*z; difference =x-y-z; avg = sum/3;

cout<< " SUM = "<< sum <<endl; cout<<"PRODUCT ="<<product<<endl; cout<<"DIFFERENCE ="<<difference<<endl; cout<<" AVG = " << avg <<endl; } return 0; } (d). Write a C++ program that will output the following as it appears: Politics is for all of us You and me

are political animals. #include<iostream.h> int main () { cout<<"'politics is for all of us'"<<endl; cout<<"You and me"<<endl;

cout<<"are like political animals"<<endl; return 0; } Question 3 (a). Study the code below and explain it line by line. //my first C++ program #include<iostream.h> int main () { Cout<<Hello, This is my first test in C++ programming\n);

The code //my first C++ program is a comment line which has no effect on the behavior of the program. The code #include<iostream.h> is the directive for the processor which includes the iostream standard file. The code int main () is the beginning of the main function where all C++ programs start their execution, independent of its location within the source code. The code cout<<Hello, This is my first test in C++ programming\n); this statement performs the only action that generates a visible effect in our first program.

b). Exp=ain with examples the difference between a block comment and a line comment. A block comment is a comment is a comment which discard everything between the /* characters and the first appearance of the */ character with the possibility of including more than one line. E.g. /* my second program in C++ with more comments */. While Line comment is a comment which discards everything from where the pair of slash signs (//) is found up to the end of that same line. E.g. // prints Hello World! C). State whether the following statements will evaluate to false or true, given that: int a=2, b=5, c=3; i. ii. iii. iv. d). Given the code (++b>a*c) ((b=3)===a) (b++==5)&&(c>a) ++b==5 &&c!=b||3>6

int s=0,t; for int(r=1; r<=7; r++){ t=r*r s=s+t } State any error in the code. The errors in the code are;

The unidentified symbol r in the main function. For statement missing in the main function. Statement missing in the main function. Compound statement missing} in function main.

Question 4

a). What is a pointer? Is a variable that hold the memory address of another variables.

5fb). Study the following code fragment.

Question 5. a). Write down two different examples of each of the following. i. ii. iii. iv. Programming statement Variable declaration One dimension Array declaration Two dimension Array declaration

v.

Pointer declaration

char *message; /* Declares a pointer variable named message */ struct list *next, *previous; /* Uses the tag for list */

vi.

Data type

int string float char

b). state four rules of naming identifier

The first character should not start with a digit (0-9) The variable name must not be a keyword/reserve word. The first character must be an alphabet or an underscore. No special characters or punctuation symbols are allowed except the underscore No two successive underscores are allowed Identifiers can consist of the capital letter A-Z, a-z, the digit 0-9 and the underscore character.

Das könnte Ihnen auch gefallen