Question 1

Underline the line(s) which is incorrect (expected to produce error). State the type of error and then propose a solution by re-writing the line correctly, or any other line(s) associated to the incorrect line(s).

a.

#include<iostream>
using namespace std;
int main()
{
    int x,y;
    x = 7; y=8;
    if(x=y)
    {
        cout<<"They are equal\n";
    }
    else{
        cout<<"They are not equal";
        cout<<endl;
    }
}

b.

#include<iostream>
using namespace std;
int main()
{
    int x = 10;
    int y = 15;
    int ii = y%x
    cout<<n;
}
}

c.

#include<iostream>
using namespace std;
int main()
{
    int x = 4;
    int y = 0;
    int count = x/y;
    while(count<10)
    {
        cout<<"I'm a programmer\n";
    }
    cout<<count;
}

d.

#include<iostream>
using namespace std;
int Main()
{
    int a = 10;
    for(i, a >= 0; a = a-1)
    {
        cout << a;
        cout << " ";
    }
    return 0;
}

e.

/*A cpp program to print the word CPP three times */
#include<iostream>
using namespace std;
int main()
{
    for(int a = 0; a < 3; a++) ;
    {
        cout << "CPP";
        cout << endl;
    }
    return 0;
}

f.

/*A cpp program to print the sum of two numbers */
#include<iostream>
using namespace std;
int main()
{
    int a, b, c;
    a = 7;
    b = 8;
    a + b = c;
    cout << c;
    return 0;
}

Question 2

Consider that the following programs are error free program. What are the outputs of these programs?

a.

#include<iostream>
using namespace std;
int main()
{
    int a = 4;
    int b = 2;
    cout<<++a<<", "<<a;
    cout<<endl;
    cout<<b++<<", "<<b;
    cout<<"\n";
    cout<<-a<<", "<<a;
    cout<<"\n";
    cout<<b--<<", "<<b;
    return 0;
}

b.

#include<iostream>
using namespace std;
int main()
{
    int a = 4;
    int b = 2;
    int c = a + b;
    int d = a - b;
    int e = a * b;
    float f = a / b;
    int g = a % b;
    cout<<"The number: ";
    cout<<a<<b;
    cout<<c<<d<<e<<f<<g;
    return 0;
}

c.

#include<iostream>
using namespace std;
int main()
{
    int x = 2;
    cout<<(x != 4)<<endl;
    cout<<(x == 4)<<endl;
    cout<<(x > 3)<<endl;
    cout<<(x < 4)<<endl;
    cout<<(x = 0)<<endl;
    cout<<(x > 0)<<endl;
    cout<<(x && 4)<<endl;
    cout<<(x || 4)<<endl;
    cout<<(!x)<<endl;
    return 0;
}

d.

#include<iostream>
using namespace std;
int main()
{
    int x, y, *p, *q;
    x = 7; y = 8;
    p = &x; q = &y;
    cout<<*p<<*q<<endl;
    int *pq = p;
    p = q;
    q = pq;
    cout<<*p<<*q<<"\n";
    return 0;
}

e.

#include<iostream>
using namespace std;
int main()
{
    int x = 10;
    while(x > 0)
    {
        x = x - 2;
        cout<<x;
    }
    return 0;
}

f.

#include<iostream>
using namespace std;
int main()
{
    int i = 1;
    for(; i < 10; i++)
    {
        cout<<i;
        i = i + 1;
    }
    return 0;
}

Question 3

What is the output of the following piece of code?

//A program to know the operational of a while loop
#include<iostream>
using namespace std;
int main()
{
    //Variable initialization
    int sum = 0;
    
    //Data processing
    for(int i = 0; i<10; i++)
    {
        if(i%2 == 0)
        {
            sum = sum + i;
        }
    }
    //Data output
    cout<<"Jumla ya nanba: "<<sum;
    return 0;
}

Question 4

Use switch case technique to write a program to print the name of all east Africa countries. For example, to print Tanzania, a user should enter the character T which is the first character of the name Tanzania. Consider the countries like Tanzania, Uganda, Kenya, Rwanda, and Sudan.


Question 5

Use for loop, and array technique to write a program to capture, store and print the first name of your ten close relatives.


END OF EXAMINATION PAPER