CSCI101 Mid-Term Exam
Please note: the style
and number of questions on the CSCI101 mid-term exam may vary from semester to semester.
Name:____________________
Please
write your answers IN INK.
For multiple choice questions, circle your selection for the correct
answer. Write your answers to the written questions in the space provided.
Note: Each of the first 25 questions is worth 2%
1) Which of the following are legal variable names in C++:
a) x
b) 3x
c) prog1.cpp
d) rate
e) %rate
f) percentage rate
g) percentage-rate
h) percentage_rate
i) prog1_cpp
ans: a) d) h) i)
2) What is the return type of the predefined character
function isupper(Char_exp)?
ans: boolean
3) Which of the following is an example of polymorphism:
a) encapsulation
b) overloading
c) iteration
d) type conversion
ans:b
4) Which of the following is NOT a C++ keyword
a) return
b) struct
c) close
d) void
ans:c
5) Which would be the most suitable loop statement to use to process arrays and why?
ans for loop
6) Suppose your program contains the following class definition (along with definitions of the member functions):
class YourClass
{
public:
YourClass(int
new_info, char more_new_inf0);
YourClass();
void
do_stuff();
private:
int information;
char
more_information;
};
Which of the following are NOT legal?
a) YourClass
an_object(42,'A');
b) YourClass
an_object(42,"A");
c) YourClass
an_object();
d) an_object =
YourClass(99,'B');
e) an_object =
YourClass(99,'B','C');
f) an_object =
YourClass();
g) an_object =
YourClass;
h) YourClass
an_object;
ans: b)
c) e) g)
7) Carefully study the following code. Which function or functions will be called after execution of the following program segment:
char answer = ‘Y’;
if (answer = ‘N’)
function1( );
else
function2( );
function3( );
ans: both function1( ) and function3( )
8) You cannot place the definition of one function within the body of another function definition.
a) True
b) False
ans T
9) What is the difference between a class and an object?
10) What is the difference between a class and a structure?
11) Carefully study the following code. What would be the output to the screen after running the code.
#include <iostream.h>
void main(void)
{
int sum = 0;
for (n = 1; n <= 10; n++);
sum = sum + n;
cout << sum;
}
ans: 11
12) What would be the output to the screen after running the following code:
#include <iostream.h>
#include <conio.h>
void do_calc(int n1, int & n2);
void main(void)
{
int num1 = 1;
int num2 = 2;
do_calc(num2,num1);
cout << num2 << " " << num1 << endl;
getch( );
}
void do_calc(int n1, int & n2)
{
n1 = n1 + 5;
n2 = n2 * 5;
cout << n2 << " " << n1 << endl;
}
ans: 5 7
2 5
13) quest13.txt contains the following:
1
12 4
6
80
10
(there is a new-line character at the end of each line)
What is printed to the screen after running the following code?
What are the contents of quest13out.txt after running the following code?
#include <fstream.h>
int main( )
{
ifstream infile;
ofstream outfile;
infile.open("quest13.txt");
outfile.open("quest13out.txt");
int c = 10, n;
while (infile >> n)
{
c--;
outfile << n*c << endl;
}
infile.close();
outfile << c;
outfile.close();
return 0;
}
ans nothing is printed to the
screen, and quest13out.txt contains:
9
96
28
36
400
40
4
14) Give an example of when you would use the eof function.
ans: to check for the end of a file
15) What is the value of the following logical expression?
!!(12 == 12)
ans: true
16) List the errors in the following program.
#include
<iostream.h>
struct employee
{
char lastname[20];
double monthly_salary;
}
void
get_data(employee person);
void main()
{
employee emp1;
get_data(emp1);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Employee's last
name :"
<<
emp1.lastname << "\n" ;
cout << "Employee's salary
:"
<<
emp1.monthly_salary << "\n" ;
getch();
return 0;
}
void
get_data(employee& person)
{
cout << "Please enter
employee's last name: ";
cin >> emp1.lastname;
cout << "Please enter this
employee's salary: " ;
cin >> emp1.monthly_salary;
}
ans: 1) no ; after structure def
2) function prototype
doesn't match header
3) getch() used without
conio.h
4) returning a 0 in main
when main is void
5) emp1 is used in
function when it should be person
17) Which operator is used to specify a member function of a class?
a) the scope resolution operator
b) the extraction operator
c) the insertion operator
d) the telecommunications operator
e) the dot operator
ans:a
18) What is the name of the object in the following statement: in_stream.open(“infile.dat”);
ans: in_stream
19) What is the output after running the following code:
#include
<conio.h>
#include
<iostream.h>
struct S1
{
int a;
int b;
int c;
};
void show(S1 &
s);
int main(void)
{
S1 s2 = {1,10};
cout << s2.a << endl
<< s2.b << endl << s2.c << endl;
show(s2);
cout << s2.a << endl
<< s2.b << endl << s2.c;
getch( );
return 0;
}
void show(S1 &
s)
{
s.a = s.a * 2;
s.b = s.b * s.b;
s.c = 99;
cout << s.c << endl <<
s.b << endl << s.a << endl;
}
ans:
1
10
0
99
100
2
2
100
99
20) return statements are not allowed in void functions
True/False
ans F
21) Call-by-value formal parameters are variables which are:
a) arguments
b) global
c) local
d) constants
ans c
22) Give an example of a C++ statement which will result in a compiler warning, but not a compiler error.
23) Give an example of when a global variable may be used. In what way should a global variable be written so that it is distinguishable from a local variable?
24) In a sentence, explain the purpose of precondition and postcondition comments.
25) Give an example of a syntax error, a run-time error and a logic error.
Sample written questions:
A. How would you define a class so that it is an abstract data type? (10%)
B. Write a program which reads any number of integers from an input file called infile.dat, sums the numbers, and writes the sum to the file outfile.dat. Include code to check for the successful opening of both files. (40%)
This page
is: mid101sample_ans.htm, edited
using: Word 2000
Last updated:
Sunday 4th July 2004, 12:58 PT by AHD