Example 2 Exam Questions:
- What is the output of the following program code:
int f1(int &a, int &b);
int main()
{
int x, y, z;
x=0;
y=0;
z=f1(y,x);
cout<”x is “< x <endl;
cout<”y is “< y <endl;
cout<”z is “< z <endl;
return 0;
}
int f1(int &a, int &b)
{
int temp;
temp = a*b;
b=temp;
a=0;
return(temp);
}
- Explain the difference between pass-by-value and pass-by-reference.
- What is the output of the following program:
#include <iostream.h>
int myfunction(float &a, float &b);
int main ()
{
float x=0.0,y=200.0, z;
z=myfunction(y,x);
cout<z<endl;
return 0;
}
int myfunction(float &b, int &a)
{
return 0;
}
- What is the output of the following program:
#include <iostream.h>
int f2(int x, int &y)
{
int temp;
temp = x;
y=x*x*x;
return(temp);
}
int main()
{
int a, b, c;
b=10;
a=25;
c=f2(a,b);
cout<c<endl;
return 0;
}
- What is the output of the following function when radius is 2 and price is 1.5?
void sliceprice(int radius, double price)
{
cout<”sq in/slice = “;
cout<3.14159*radius*radius/8<endl;
cout<”one slice: $”<price/8<endl;
cout<”$”<price/(3.14159*radius*radius);
cout<” per sq. inch”<endl;
}
- What is the output of the following function when item1 is 1.2, item2 is 1.1, and maxweight is 2.0?
void checkweight(double item1, double item2, double maxweight)
{
if(item1+item2>maxweight)
{
cout<”Items too heavy”<endl;
if(item1<maxweight)
cout<”First item is light enough by itself.”<endl;
else
cout<”First item is too heavy.”<endl;
if(item2<maxweight)
cout<”Second item is light enough by itself.”<endl;
else
cout<”Second item is too heavy.”<endl;
}
else
cout<”Both items can be carried.”<endl;
}
- Using global variables is better style than using local variables. True or False.
- Given the function definition
int mystery(float some)
{
if(someval>2.0)
return (3*some);
else
return 0;
}
what is the value of the expression Mystery (4)?
- 12
- 12.0
- 0
- 0.0
- nothing
9. Given the function definition
int mystery(float some)
{
if(someval>2.0)
return (3*some);
else
return 0;
}
what is the value of the expression Mystery (4,2)?
- 12
- 12.0
- 0
- 0.0
- nothing
- Given the function definition
int trans(int alpha, int beta)
{
if(alpha > beta)
return (alpha+10);
else
return (2*beta);
}
what is printed by the following code?
cout<Trans(5,Trans(9,4))<endl;
- 15
- 38
- 16
- 19
- 8
- Which of the following loops print 1 to 10 inclusively?
- for(I=0;I<=10;I=I+2)
cout<I<endl;
- for(I=1;I<=10;I++)
cout<I<endl;
- for(I=0;I<10;I++)
cout<I<endl;
- for(I=1;I<=10;I++)
cout<I<endl;
- for(I=0;I<=11;I++)
cout<I<endl;
- After the execution of the code fragment
int sum=0;
int i;
for(i=0;i<5;i++)
{
sum = i + 2;
if(i>=3)
sum = sum + 3;
}
what is the value of sum?
- 10
- 20
- 8
- 9
- 7
- In the C++ program fragment
count=1;
While(count<10)
count++;
cout<”Hello”;
the output statement that prints “Hello” is not part of the body of the loop. True/False
- What is the value of loopcount after the following program segment is executed?
loopcount = 1;
while(loopcount<=145)
{
alpha = alpha+7;
loopcount++;
}
- 1
- 144
- 145
- 146
- What is the value of some after the following program segment is executed?
int some = 273;
while(some>500)
some=some-3;
- 270d. 500
- 273e. None of the above – this is an infinite loop
- 497
- What is the output of the following code fragment?
n = 1;
while(n<=5)
cout<n<’’;
n++;
- 1 2 3 4 5
- 1 2 3 4
- 1 1 1 1 1 forever till the computer crashes
- 2 3 4 5
- Given the input data
25 10 6 –1
what is the output of the following code fragment?
(All variables are of type int.)
Sum = 0;
Cin>number;
While(number!=-1)
{
cin>number;
sum = sum + number;
}
cout<sum<endl;
- 15
- 41
- 40
- 16
- none of the above
- What is the value of length after the following code segment is executed?
(count and length are of type int.)
length = 5;
count = 4;
while(count<=6)
{
if(length>=100)
length=length-2;
else
length = count * length;
count++;
}
- 600
- 100
- 98
- 20
- none of the above.
- What is the output of the following code fragment?
int limit = 8;
cout<’H’;
for (int l=10;l<=limit;l++)
cout<’E’;
cout<’LP’;
- HLP
- HELP
- HEELP
- HEEELP
- None of the above
- What is the value of sum after execution of the following code?
int sum = 0;
for(int c=2;c<=5;c++)
sum=sum+2*c;
- 28
- 18
- 10
- 30
- none of the above.
- Take the following while loop and convert it to a for loop:
int p=10,c=0,pe=0;
while(p>=10)
{
pe=pe+p;
p=p-2;
c++;
}
- What is the output of the following code fragment?
int i=10;
for(i=0;i<5;i++)
{
cout<i*i<endl;
i++;
}
- What is the output of the following while loop?
int p=10,c=0,pe=0;
while(p>=10)
{
pe=pe+p;
p=p-2;
c++;
cout<c<p<pe<endl;
}
cout<c<p<pe<endl;
- Rewrite the following using a for loop.
int i=9;
while(i>0)
{
cout<i/2<endl;
i--;
}
- What is the output of the following program segment?
int sum = 0;
for(int c=1;c<=15;count=count+2)
sum=sum+count;
- Write a program that reads in the size of the side of a square and then prints a hollow square of that size out of asterisks and blanks. Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 5, it should print:
*****
* *
* *
* *
*****
- What are the 3 parts to a function?
- What are the 3 steps to a LCV?
- What is the difference between FILE and BLOCK SCOPE?
- Write a program that calculates and prints the average of several integers. Assume the last value read is the sentinel 9999. A typical input sequence might be
10 8 11 7 9 9999
- What are the 5 steps when dealing with file streams?
- Give an example of the 5 steps with an input stream called employee.