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; }}
Answer (Click to show)
Incorrect Line:if(x=y)
Type of Error: Logical Error (Assignment instead of comparison)
Corrected Line:
if(x == y)
b.
#include<iostream>using namespace std;int main(){ int x = 10; int y = 15; int ii = y%x cout<<n;}}
Answer (Click to show)
Incorrect Lines:
int ii = y%x (Missing semicolon)
cout<<n; (Variable n is not declared)
Extra closing brace }
Type of Errors: Syntax Error, Semantic Error
Corrected Lines:
int ii = y % x;cout << ii;
And remove the extra closing brace
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;}
Answer (Click to show)
Incorrect Line:int count = x/y;
Type of Error: Runtime Error (Division by zero)
Corrected Line: Initialize y to a non-zero value before division.
int y = 1; // or any other non-zero valueint count = x / y;
d.
#include<iostream>using namespace std;int Main(){ int a = 10; for(i, a >= 0; a = a-1) { cout << a; cout << " "; } return 0;}
Answer (Click to show)
Incorrect Lines:
int Main() (Should be main)
for(i, a >= 0; a = a-1) (Variable i not declared; incorrect for loop structure)
Type of Errors: Syntax Error, Semantic Error
Corrected Lines:
int main(){ int a = 10; for(; 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;}
Answer (Click to show)
Incorrect Line:for(int a = 0; a < 3; a++) ; (Extraneous semicolon)
Type of Error: Logical Error (Loop body is empty due to semicolon)
Corrected Line:
for(int a = 0; a < 3; a++)
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;}
Answer (Click to show)
Incorrect Line:a + b = c;
Type of Error: Semantic Error (Invalid assignment)
Corrected Line:
c = a + b;
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;}
Output (Click to show)
5, 52, 3-5, 53, 2
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;}
#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;}
Output (Click to show)
7887
e.
#include<iostream>using namespace std;int main(){ int x = 10; while(x > 0) { x = x - 2; cout<<x; } return 0;}
Output (Click to show)
86420
f.
#include<iostream>using namespace std;int main(){ int i = 1; for(; i < 10; i++) { cout<<i; i = i + 1; } return 0;}
Output (Click to show)
13579
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;}
Output (Click to show)
Jumla ya nanba: 20
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.
Answer (Click to show)
#include <iostream>using namespace std;int main() { char choice; cout << "Enter the first letter of an East African country (T, U, K, R, S): "; cin >> choice; switch(choice) { case 'T': case 't': cout << "Tanzania" << endl; break; case 'U': case 'u': cout << "Uganda" << endl; break; case 'K': case 'k': cout << "Kenya" << endl; break; case 'R': case 'r': cout << "Rwanda" << endl; break; case 'S': case 's': cout << "Sudan" << endl; break; default: cout << "Invalid choice! Please enter T, U, K, R, or S." << endl; } return 0;}
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.
Answer (Click to show)
#include <iostream>#include <string>using namespace std;int main() { const int NUM_RELATIVES = 10; string relatives[NUM_RELATIVES]; // Capture names cout << "Enter the first names of your 10 close relatives:" << endl; for(int i = 0; i < NUM_RELATIVES; i++) { cout << "Relative " << (i + 1) << ": "; cin >> relatives[i]; } // Print names cout << "\nNames of your close relatives:" << endl; for(int i = 0; i < NUM_RELATIVES; i++) { cout << relatives[i] << endl; } return 0;}