Example 2 Exam Questions:

  1. 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);

}

  1. Explain the difference between pass-by-value and pass-by-reference.
  2. 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;

}

  1. 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;

}

  1. 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;

}

  1. 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;

}

  1. Using global variables is better style than using local variables. True or False.
  2. 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)?

  1. 12
  2. 12.0
  3. 0
  4. 0.0
  5. 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)?

  1. 12
  2. 12.0
  3. 0
  4. 0.0
  5. nothing
  1. 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;

  1. 15
  2. 38
  3. 16
  4. 19
  5. 8
  1. Which of the following loops print 1 to 10 inclusively?
  1. for(I=0;I<=10;I=I+2)

cout<I<endl;

  1. for(I=1;I<=10;I++)

cout<I<endl;

  1. for(I=0;I<10;I++)

cout<I<endl;

  1. for(I=1;I<=10;I++)

cout<I<endl;

  1. for(I=0;I<=11;I++)

cout<I<endl;

  1. 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?

  1. 10
  2. 20
  3. 8
  4. 9
  5. 7
  1. 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

  1. What is the value of loopcount after the following program segment is executed?

loopcount = 1;

while(loopcount<=145)

{

alpha = alpha+7;

loopcount++;

}

  1. 1
  2. 144
  3. 145
  4. 146
  1. What is the value of some after the following program segment is executed?

int some = 273;

while(some>500)

some=some-3;

  1. 270d. 500
  2. 273e. None of the above – this is an infinite loop
  3. 497
  1. What is the output of the following code fragment?

n = 1;

while(n<=5)

cout<n<’’;

n++;

  1. 1 2 3 4 5
  2. 1 2 3 4
  3. 1 1 1 1 1 forever till the computer crashes
  4. 2 3 4 5
  1. 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;

  1. 15
  2. 41
  3. 40
  4. 16
  5. none of the above
  1. 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++;

}

  1. 600
  2. 100
  3. 98
  4. 20
  5. none of the above.
  1. 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’;

  1. HLP
  2. HELP
  3. HEELP
  4. HEEELP
  5. None of the above
  1. 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;

  1. 28
  2. 18
  3. 10
  4. 30
  5. none of the above.
  1. 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++;

}

  1. What is the output of the following code fragment?

int i=10;

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

{

cout<i*i<endl;

i++;

}

  1. 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;

  1. Rewrite the following using a for loop.

int i=9;

while(i>0)

{

cout<i/2<endl;

i--;

}

  1. What is the output of the following program segment?

int sum = 0;

for(int c=1;c<=15;count=count+2)

sum=sum+count;

  1. 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:

*****

* *

* *

* *

*****

  1. What are the 3 parts to a function?
  2. What are the 3 steps to a LCV?
  3. What is the difference between FILE and BLOCK SCOPE?
  4. 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

  1. What are the 5 steps when dealing with file streams?
  2. Give an example of the 5 steps with an input stream called employee.