You are on page 1of 11

The American University in Cairo

Computer Science & Engineering Department


Fall 2020 - CSCE 1001

Review Questions

[1] Show the output of the following:


#include <iostream>
using namespace std;

int main()
{
int num = 57;
cout << "\n\n Display the operation of pre and post increment and decrement :\n";

cout << "--------------------------------------------------------------------\n";


cout <<" The number is : " << num << endl;
num++;
cout <<" After post increment by 1 the number is : " << num << endl;
++num;

cout <<" After pre increment by 1 the number is : " << num << endl;
num = num + 1;
cout <<" After increasing by 1 the number is : " << num << endl; // 79
num--;
cout <<" After post decrement by 1 the number is : " << num << endl;

--num;
cout <<" After pre decrement by 1 the number is : " << num << endl;
num = num - 1;
cout <<" After decreasing by 1 the number is : " << num << endl;
cout << endl;
return 0;

[2]Show the output of the following program:

Show the output of the following program:

int main()

cout << "\n\n Print the result of some specific operation :\n";

cout << "--------------------------------------------------\n";

cout << " Result of 1st expression is : "<< (-1+4*6) <<"\n" ;

cout << " Result of 2nd expression is : "<< ((35+5)%7) <<"\n" ;

cout << " Result of 3rd expression is : "<< (14+-4*6/11) <<"\n" ;

cout << " Result of 4th expression is : "<< (2+15/6*1-7%2) <<"\n\n" ;

[3]Show the output of the following program:


Assume num1=25 and num2=39

int main()

cout << "\n\n Swap two numbers :\n";

cout << "-----------------------\n";

int num1, num2, temp;

cout << " Input 1st number : ";

cin >> num1 ;

cout << " Input 2nd number : ";


cin >> num2;

temp=num2;

num2=num1;

num1=temp;

cout << " After swapping the 1st number is : "<< num1 <<"\n" ;

cout << " After swapping the 2nd number is : "<< num2 <<"\n\n" ;

[4]Show the output of the following program:


int main()

int x;

cout<<" Input the number : ";

cin>> x;

cout<<" "<<x<<x<<x<<x<<endl;

cout<<" "<<x<<" "<<" "<<x<<endl;

cout<<" "<<x<<" "<<" "<<x<<endl;

cout<<" "<<x<<" "<<" "<<x<<endl;

cout<<" "<<x<<" "<<" "<<x<<endl;

cout<<" "<<x<<x<<x<<x<<endl;

cout << endl;

return 0;

[5] Show the output of the following program:


int main()

cout << "\n\n Print a mystery series:\n";


cout << "-------------------------\n";

cout << " The series are: \n";

int nm1 = 1;

while (true)

++nm1;

if ((nm1 % 3) == 0)

continue;

if (nm1 == 50)

break;

if ((nm1 % 2) == 0)

nm1 += 3;

else

nm1 -= 3;

cout << nm1 << " ";

cout << endl;

return 0;

[6] Write a C++ program (include a function in your answer) to add all the numbers from 1 to a given
number.

Add 1 to 4: 10

Add 1 to 100: 5050


[7] Write a C++ program that accepts sales unit price and sales quantity of various items and compute
total sales amount and the average sales quantity. All input values must greater than or equal to 0 and
less than or equal to 1,000, and the number of pairs of sales unit and sales quantity does not exceed
100. If a fraction occurs in the average of the sales quantity, round the first decimal place

[8] Write a program that reads in ten whole numbers and that outputs the sum of all the numbers
greater than zero, the sum of all the numbers less than zero (which will be a negative or zero), and the
sum of all the numbers, whether positive, negative, or zero.

[9] Write a program that will ask the user to input n positive numbers. The program will terminate if
one of those number is not positive.

[10] Write a program that determines a student’s grade. The program will read three types of scores
(quiz, mid-term, and final scores) and determine the grade based on the following rules:

-if the average score =90% =>grade=A

-if the average score >= 70% and <90% => grade=B

-if the average score>=50% and <70% =>grade=C

-if the average score<50% =>grade=F

Using Switch Statement

(no answer;please refer to the notes)

[11] Write a program that asks the user for an integer number and find the sum of all natural numbers
up to that number.

[12] Write a program that calculates 6^5. Declare your own function to do this.
[13] The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the
user to input two integer vales for the calculation. See the sample below.

MENU

1. Add

2. Subtract

3. Multiply

4. Divide

5. Modulus

Enter your choice: 1

Enter your two numbers: 12 15

Result: 27

Continue? y

The program also asks the user to decide whether he/she wants to continue the operation. If he/she
input ‘y’, the program will prompt the user to choose the operation gain. Otherwise, the program will
terminate.

[14]Show the output of the following program:


num1=1234

#include <iostream>
using namespace std;
int main()
{
int num1, num2, r, sum=0;

cin >> num1;


num2 = num1;
while (num1 > 0)
{
r = num1 % 10;
num1 = num1 / 10;
sum = sum + r;
}
cout << num2 << sum << endl;
}

[15]Show the output of the following program:

#include <iostream>

void figureMeOut(int& x, int y, int& z);

int main()

using namespace std;

int a, b, c;

a = 10;

b = 20;

c = 30;

figureMeOut(a, b, c);

cout << a << " " << b << " " << c;

return 0;

void figureMeOut(int& x, int y, int& z)

using namespace std;

cout << x << " " << y << " " << z << endl;

x = 1;

y = 2;

z = 3;

cout << x << " " << y << " " << z << endl;

}
[16]Show the output of the following program:

void mysterious(int n1, int n2, int n3);

int main()

using namespace std;

int num1, num2, num3;

cout << "Enter three integers: ";

cin >> num1 >> num2 >> num3;

mysterious(num1, num2, num3);

return 0;

void mysterious(int n1, int n2, int n3)

using namespace std;

cout << (n1 * n2 * n3) << endl;

[17] write an “overloaded” function called absolute that converts a negative number of either int or
float to an absolute positive number

[18]Show the output of the following program:


void mysterious(int arr[])

int i, first, second, third;

third = first = second = -32767;

for (i = 0; i < 8 ; i ++)


{

if (arr[i] > first)

third = second;

second = first;

first = arr[i];

else if (arr[i] > second)

third = second;

second = arr[i];

else if (arr[i] > third)

third = arr[i];

cout << <<first <<", "<< second <<", "<< third;

int main()

int nums[] = {7, 12, 9, 15, 19, 32, 56, 70};

for (int i=0; i < n; i++)

cout << nums[i] <<" ";

mysterious(nums);

return 0;}

[19] Show the output of the following program:

void mysterious(int nums[])


{

int max_count = 0;

for (int i=0; i<size; i++)

int count=1;

for (int j=i+1;j<size;j++)

if (nums[i]==nums[j])

count++;

if (count>max_count)

max_count = count;

for (int i=0;i<size;i++)

int count=1;

for (int j=i+1;j<size;j++)

if (nums[i]==nums[j])

count++;

if (count==max_count)

cout << nums[i] << endl;

int main()

int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};

cout << "Original array: ";

for (int i=0; i < n; i++)

cout << nums[i] <<" ";


mysterious(nums);

[20] Write a C++ program to find the largest element of a given array of integers.
(You can assume any number you want)

You might also like