-
Without using global variables, what is the proper way to
get information {\em into} a function from the caller?
-
Using the return type.
-
Using the return value.
-
Using a parameter.
-
Without using global variables, what are the proper two ways
to get information {\em out of} a function to the caller?
-
Using the return type or return value.
-
Using a parameter type or return type.
-
Using a parameter value or return value.
-
What is produced by the following program?
#include <iostream.h>
void three(void) { cout << "3 "; }
void two(void) { cout << "2 "; three(); }
void one(void) { cout << "1 "; two(); }
void main(void) { cout << "Counting "; one(); }
-
The program will produce a compilation error.
-
3 2 1 Counting
-
Counting 1 2 3
-
What is produced by the following program?
#include <iostream.h>
void Jack(void);
void John(void) { cout << "John "; Jack(); }
void Jane(void) { cout << "Jane "; }
void Jill(void) { cout << "Jill "; John(); }
void Jack(void) { cout << "Jack "; Jane(); }
void main(void) { cout << "Hi "; Jill(); cout << "!";}
-
John Jack Jane Jill John Jack Jane Hi Jill!
-
John Jane Jill Jack Hi !
-
Hi Jack Jill Jane John !
-
Hi Jill John Jack Jane !
-
Why is the Jack definition done twice in the above program,
and John, Jane, and Jill are not?
-
The instructor is an idiot.
-
This is necessary since Jack is the last function
before the main function.
-
Are you an idiot? Jack is not defined twice!
-
What happens if you call the following function:
void hi(void) {
cout << "Hello\n" ;
hi();
}
-
It comes running.
-
It will produce a compilation error since recursion of
functions is not allowed.
-
It will keep writing Hello lines to the screen.
-
What is produced by:
#include <iostream.h>
void set(void) {
int a;
a=1;
}
void main(void) {
int a;
a=0;
set();
cout << a;
}
-
It will produce a compilation error since we did not put
void between the parentheses when a was called
inside main.
-
0
-
1
-
What is produced by:
#include <iostream.h>
int a;
void set(void) {
a=1;
}
void main(void) {
a=0;
set();
cout << a;
}
-
It will produce a compilation error since declarations must
be immediately behind the opening curly brackets of functions.
-
0
-
1
-
Is the following code legal in C++? What does it produce?
#include <iostream.h>
void out(int houses, int horses) {
cout << houses << " " << horses;
}
void main(void) {
int boats=10, sheep=2;
out(boats, sheep);
}
-
No, this is a syntax error.
-
No, you must put void before out in main.
-
No, you must put int before boats and
sheep in the call to out in main.
-
Yes, but it is lousy coding. It produces houses horses
-
Yes, but it is lousy coding. It produces boats sheep
-
Yes, but it is lousy coding. It produces 10 2
-
What is produced by the following program?
#include <iostream.h>
void show(float a) {
cout << a << endl;
}
void main(void) {
float a=1, b=2;
show(b);
}
-
This will produce a compilation error. The argument must be
a, not b
-
1
-
2
-
What is produced by:
#include <iostream.h>
void out(int a, int b) {
cout << a << " " << b << endl;
}
void main(void) {
int a=1, b=2;
out(b,a);
}
-
This will produce a compilation error. The arguments are
reversed.
-
1 2
-
2 1
-
What is produced by:
#include <iostream.h>
int out(int a, int b) {
cout << a << " " << b << " ";
return a+b;
}
void main(void) {
cout << "(" << out(1,2) << ")" << endl;
}
-
This will produce a compilation error. You cannot use a
function in a cout statement.
-
(1 2)
-
(3)
-
(1 2 3)
-
(1 2 ) 3
-
(a b ) 3
-
What is produced by:
#include <iostream.h>
int fun(int i) {
if (i==1) return 1;
else return i*fun(i-1);
}
void main(void) {
cout << fun(1) << " " << fun(2) << " " << fun(3) << " "
<< fun(4) << " " << fun(5) << endl;
}
-
0 0 0 0 0
-
1 2 3 4 5
-
1 2 6 8 10
-
1 2 6 24 120
-
What does the following code produce:
#include <iostream.h>
int ratio(int a, int b) {
return a/b;
}
void main(void) {
int a=1, b=2;
cout << ratio(a,b) << " " << ratio(b,a);
}
-
ratio(a,b) ratio(b,a)
-
a/b a/b
-
0 0
-
0.5 0.5
-
0.5 2
-
0 2
-
2 2
-
What happens for:
#include <iostream.h>
void out(int i) {
cout << i << endl;
}
void main(void) {
out(1.5);
}
-
This will produce a compilation error. The argument must be
an integer.
-
It will print 1.5.
-
The computer will stream the I/O to an h.
-
What is produced by:
#include <iostream.h>
void swap(int a, int b) {
int tmp;
tmp=a;
a=b;
b=tmp;
}
void main(void) {
int a=1, b=2;
cout << a << " " << b << " --- ";
swap(a,b);
cout << a << " " << b << endl;
}
-
1 2 --- 1 2
-
1 2 --- 2 1
-
1 2 --- b tmp
-
What is produced by:
#include <iostream.h>
void swap(int &a, int &b) {
int tmp;
tmp=a;
a=b;
b=tmp;
}
void main(void) {
int a=1, b=2;
cout << a << " " << b << endl;
swap(a,b);
cout << a << " " << b << endl;
}
-
This will produce a compilation error. There is an &
before the a and b.
-
This will produce a compilation error. a and b
must be reference variables in main.
-
This will produce a compilation error. a and b
must be aliases in main.
-
1 2 --- 1 2
-
1 2 --- 2 1
-
1 2 --- b tmp