0% found this document useful (0 votes)
3 views5 pages

Assignment 5

The document is an assignment for MET 111 Computer and Programming, focusing on functions in C++. It includes multiple-choice questions about function characteristics, code output questions, and programming tasks that require writing functions for various operations. The assignment aims to assess understanding of function usage, variable scope, and basic arithmetic operations in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Assignment 5

The document is an assignment for MET 111 Computer and Programming, focusing on functions in C++. It includes multiple-choice questions about function characteristics, code output questions, and programming tasks that require writing functions for various operations. The assignment aims to assess understanding of function usage, variable scope, and basic arithmetic operations in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MET 111 Computer and Programing Dr.

Abdelhady Mostafa

MET 111 Assignment #5 Functions

1. Chose the right answer:


What will you use if you are not intended to get a return value?
a) static
b) const
c) volatile
d) void

Where does the return statement returns the execution of the program?
a) main function
b) caller function in the main function
c) same function
d) block function

Which of the following is important in a function?


a) Return type
b) Function name
c) Both return type and function name
d) The return type, function name and parameter list

From which function the execution of a C++ program starts?


a) start() function
b) main() function
c) new() function
d) end() function

What is the scope of the variable declared in the user defined function?
A. Whole program
B. Only inside the {} block
C. The main function
D. None of the above

How are many minimum numbers of functions need to be presented in c++?


A. 0
B. 1
C. 2
D. 3

2. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
void copy (int& a, int& b, int& c) a) 2 5 10
{ b) 2 4 5
a *= 2; c) 2 6 14
b *= 2; d) 2 4 9
c *= 2;
}
MET 111 Computer and Programing Dr. Abdelhady Mostafa

int main ()
{
int x = 1, y = 3, z = 7;
copy (x, y, z);
cout << "x =" << x << ", y =" << y << ", z =" <<
z;
return 0;
}
What will be the new value of x in the following C++ code?
#include <iostream>
using namespace std;
void fun(int &x)
{
x = 20; a) 10
} b) 20
int main() c) 15
{ d) 36
int x = 10;
fun(x);
cout << "New value of x is " << x;
return 0;
}
#include <iostream>
using namespace std;
int add(int a, int b);
int main()
{
int i = 5, j = 6; a) 11
cout << add(i, j) << endl; b) 12
return 0; c) 13
} d) compiler
int add(int a, int b ) time error
{
int sum = a + b;
a = 7;
return a + b;
}
#include <iostream>
using namespace std;
void Sum(int a, int b, int & c) a) 2 3
{ b) 6 9
a = b + c; c) 2 15
b = a + c; d) compile
c = a + b; time error
}
int main()
MET 111 Computer and Programing Dr. Abdelhady Mostafa

{
int x = 2, y =3;
Sum(x, y, y);
cout << x << " " << y;
return 0;
}

#include <iostream>
using namespace std;
int max(int a, int b )
{
return ( a > b ? a : b );
}
a) 5
b) 7
int main()
c) either 5 or
{
7
int i = 5; d) 13
int j = 7;
cout << max(i, j );
return 0;
}

#include<iostream>
using namespace std;

int fun(int x = 0, int y = 0, int z)


{
a) 10
return (x + y + z);
b) 0
}
c) Error

int main()
{
cout << fun(10);
return 0;
}
#include <iostream>
using namespace std;
int mult (int x, int y)
{
a) 20
int result;
b) 25
result = 0;
c) 30
while (y != 0)
d) 35
{
result = result + x;
y = y - 1;
}
MET 111 Computer and Programing Dr. Abdelhady Mostafa

return(result);
}
int main ()
{
int x = 5, y = 5;
cout << mult(x, y) ;
return(0);
}
#include <iostream>
using namespace std;
int gcd (int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b; a) 15
b = temp; b) 25
} c) 375
return(a); d) 5
}
int main ()
{
int x = 15, y = 25;
cout << gcd(x, y);
return(0);
}

#include <iostream>
using namespace std;

int fun(int=0, int = 0);

int main() a) -5
{ b) 0
cout << fun(5); c) 10
return 0; d) 5
}
int fun(int x, int y)
{
return (x+y);
}
MET 111 Computer and Programing Dr. Abdelhady Mostafa

3. Write a program that ask for two numbers, compare them and show the
maximun. Declare a function called max_two that compares the numbers and
returns the maximum.
4.
5. Write a program that performs arithmetic division. The program will use two
integers, a and b (obtained by the user) and will perform the division a/b, store
the result in another integer c and show the result of the division using cout.
• In a similar way, extend the program to add, subtract, multiply, do modulo and
power using integers a and b.
• Modify your program so that when it starts, it asks the user which type of
calculation it should do, then asks for the 2 integers, then runs the user selected
calculation and outputs the result in a user friendly formatted manner.

Sample run:

Enter two numbers: 3 12


The sum is 15.

6. Basically, the same as previous exercises, but this time, the function that adds
the numbers should be void, and takes a third, pass by reference parameter; then
puts the sum in that.

You might also like