Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Functions

Topic function arrays

Uploaded by

Abdul Rafay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Functions

Topic function arrays

Uploaded by

Abdul Rafay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to Programming

Objective Part
Q 1. Consider the following C++ function:

int mystery(int num)


{
int y = 1;
if (num == 0)
return 1;
else if (num < 0)
return -1;
else
for (int count = 1; count < num; count++)
y = y * (num - count);
return y;
}
What is the output of the following statements?

a. cout << mystery(6) << endl;


b. cout << mystery(0) << endl;
c. cout << mystery(-5) << endl;
d. cout << mystery(10) << endl;

Q 2. What is the output of the following program?

#include <iostream>
using namespace std;
int x;
void summer(int&, int);
void fall(int, int&);
int main() {
int intNum1 = 2;
int intNum2 = 5;
x = 6;
summer(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
fall(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
return 0;
}
void summer(int& a, int b) {
int intNum1;
intNum1 = b + 12;
a = 2 * b + 5;
b = intNum1 + 4;
}
Introduction to Programming
void fall(int u, int& v) {
int intNum2;
intNum2= x;
v = intNum2 * 4;
x = u - v;
}

Q 3. What is the output of the following program?

#include <iostream>
using namespace std;
void tryMe(int& v);
int main()
{
int x = 8;
for (int count = 1; count < 5; count++)
tryMe(x);
return 0;
}
void tryMe(int& v)
{
static int num = 2;
if (v % 2 == 0) {
num++;
v = v + 3;
}
else {
num--;
v = v + 5;
}
cout << v << ", " << num << endl;
}

Q 4. Consider the following function prototype:


void funcDefaultParam(double x = 7.3, int y = 4, char z =
'*');
Which of the following function calls is correct?
a. funcDefaultParam();
b. funcDefaultParam(2.8);
c. funcDefaultParam(3.2, 0, 'h');
d. funcDefaultParam(9.2, '*');
e. funcDefaultParam(7, 3);
Introduction to Programming
Q 5. Consider following function definition

void defaultParam(int num1, int num2 = 7, double z = 2.5) {


int num3;
num1 = num1 + static_cast<int>(z);
z = num2 + num1 * z;
num3 = num2 - num1;
cout << "num3 = " << num3 << endl;
}

What is the output of the above mentioned function calls by providing input as:
a. defaultParam(7);
b. defaultParam(8, 2);
c. defaultParam(0, 1, 7.5);
d. defaultParam(1, 2, 3.0);

Q 6. Describe scope rule in given table for following mentioned program

#include <iostream>
using namespace std;
const double RATE = 10.50;
int z;
double t;
void one(int x, char y);
void two(int a, int b, char x);
void three(int one, double y, int z);
int main(){
int num, first;
double x, y, z;
char name, last;
.
.
.
return 0;
}
void one(int x, char y){
:
:
}
int w;
void two(int a, int b, char x) {
int count;
:
}
void three(int one, double y, int z) {
Introduction to Programming
char ch;
int a;
.
.
.
//Block four
{
int x;
char a;
.
.
}//end Block four
.
.
.
}

Identifier Visibility in Visibility in Visibility in Visibility in block Visibility in


function one function two function three four main
RATE (before main)
z (before main)
t (before main)
main
local variables of main
one (function name)
x (one’s formal
parameter)
y (one’s formal
parameter)
w (before function two)
two (function name)
a (two’s formal
parameter)
b (two’s formal
parameter)
x (two’s formal
parameter)
local variables of two
three (function name)
one (three’s formal
parameter)
y (three’s formal
parameter)
Introduction to Programming
z (three’s formal
parameter)
ch (three’s local variable)
a (three’s local variable)
x (block four’s local
variable)
a (block four’s local
variable)

Q 7. Name various parts of the function in given boxes.

Q 8. Select right answer


a. A variable for which memory is allocated at block entry and deallocated at block exit is called:
i. Static variable
ii. Automatic variable
b. A variable for which memory remains allocated as long as the program executes is called a
i. Static Variable
ii. Automatic Variable
c. Global variables are __________ variables, and by default, variables declared within a block
are ___________ variables.
i. Static
ii. Automatic
iii. By reference
Q 9. Mark the following statements as true or false:
a. Value-returning function returns only one value.
i. True
ii. False
b. Parameters allow you to use different values each time the function is called.
i. True
ii. False
c. When a return statement executes in a user-defined function, the function immediately exits.
i. True
Introduction to Programming
ii. False
d. If a C++ function does not use parameters, parentheses around the empty parameter list are
still required.
i. True
ii. False
e. In C++, function definitions can be nested; that is, the definition of one function can be
enclosed in the body of another function.
i. True
ii. False
Q 10. Consider the following function:

int secret(int m, int n)


{
int temp = 0;
for (int i = 1; i < abs(n); i++)
temp = temp + i * m;
return temp;
}

What is the output if function is provided with following input?


i. cout << secret(3, 6) << endl;
ii. cout << secret(5, -4) << endl;
Introduction to Programming

Subjective Part

Q 1. Write the definition of a void function that takes as input two decimal numbers. If the first
number is nonzero, it outputs the second number divided by the first number; otherwise, it outputs
a message indicating that the second number cannot be divided by the first number because the
first number is 0.
Q 2. Write a program that uses the function isPalindrome. The function return true if value is
Palindrome, false otherwise.
a. Mention proper function declaration/prototype, and function call from main body.
b. Implement isPalindrome(char[] value, int size) function and specify the return type.
Q 3. Write a program that implements isVowel that returns the value true if a given character is a
vowel and otherwise returns false. Mention proper function declaration/prototype, and function call
from main body.
Q 4. Write a function, reverseDigit that takes an integer as a parameter and returns the number with
its digits reversed. For example, the value of reverseDigit(12345) is 54321; the value of
reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532)
is -235.
Q 5. During winter when it is very cold, typically, everyone would like to know the windchill factor,
especially, before going out. Meteorologists use the following formula to compute the windchill
factor, W:
W = 35.74 + 0.6215 * T - 35.75 * V 0.16 + 0.4275 * T * V0.16

Where V is the wind speed in miles per hour and T is the temperature in degrees Fahrenheit. Write a
program that prompts the user to input the wind speed, in miles per hour, and the temperature in
degrees Fahrenheit. The program then outputs the windchill factor. Your program must contain at least
two functions: one to get the user input and the other to determine the windchill factor.

Q 6. Write a function that takes as a parameter an integer (as a long value) and returns the number
of odd, even, and zero digits. Also write a program to test your function.
Q 7. Write a program that defines the named constant p, i.e. const double p = 3.1419 which stores
the value of p.

The program shall contain following functions:

I. surfaceArea which takes radius:r as input and return the value of 4pr2 , which is the surface
area of the sphere.
II. volume which takes radius:r as input and return the value of (4/3)pr3, which is the volume of the
sphere.

You might also like