Spring 2014 CSCI 111 Final exam � of �1 6
1. (2 points) Flip over this test. On the back of this test write your name in the upper, left-hand
corner.
2. (2 points) What are the four parts of the compiling process (just give me 4 words, not a
paragraph).
3. (4 points) Which of the four steps of the compiling process occurs only once, regardless of
the number of source files your application has?
4. (4 points) Write a line of code that causes the preprocessor to generate an error.
5. (4 points) Write a line of code that causes the compiler to generate an error.
6. (5 points) Describe how you could incorrectly compile the joust project to cause the linker to
generate an error.
7. (5 points) Given:
1 float* fp;
2 //...
3 float pi;
4 pi=*(314 + fp);
Rewrite line 4 using array subscript notation.
Spring 2014 CSCI 111 Final exam � of �2 6
8. (5 points) Given:
1 float arr[100];
2 for(int x=0; x<100; ++x)
3 arr[x]=100-x;
What does the following expression print out?
cout << *arr << endl;
9. (14 points) Given:
int a=0;
int b=6;
int x=0;
Circle each if-expression that evaluates to true:
A) if(b)
B) if(x)
C) if(a=b==6)
D) if(a=b==5)
E) if(a=b=5)
F) if(a=x=0)
G) if(a=x==0)
Spring 2014 CSCI 111 Final exam � of �3 6
10. (10 points) Given:
1 #include<iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x;
7 cout << "Enter a number greater than 10" << endl;
8 while ( x < 10 )
9 {
10 cin >> x;
11 }
12 return 0;
13 }
This program compiles just fine, and sometimes it runs as expected. But sometimes when you
run it, it exits immediately after printing "Enter a number greater than 10". That is, the program
doesn't pause for you to enter a number. Why are you getting this inconsistent behavior?
11. (4 points) What is the output of the following:
int x=4;
int y=3;
A) cout << x / y << endl;
B) cout << x % y << endl;
C) cout << x << "%" << y << endl;
D) cout << "x" << '%' << 'y' << endl;
Spring 2014 CSCI 111 Final exam � of �4 6
12. (16 points) What is the type of the expression. That is, what is the kind of thing that each
expression evaluates to. For example:
3 + 4 integer
You may assume that the variable a has been declared as an integer.
A. a + 4
B. a = 4
C. 3.14 + 4.49
D. 3 + 3.14
E. 'a'
F. cout << a
G. new float[30]
H. new float
Spring 2014 CSCI 111 Final exam � of �5 6
13. (5 points) Write a for-loop that prints out the numbers between 1 and 100 that are evenly
divisible by three.
14. (5 points) Write a while-loop that prints out the numbers between 1 and 100 that are evenly
divisible by three.
15. (5 points) Write a do-while-loop that prints out the numbers between 1 and 100 that are
evenly divisible by three.
Spring 2014 CSCI 111 Final exam � of �6 6
16. (10 points) Given:
1 #include<iostream>
2
3 class Willow {
4 publi.
Introduction to ArtificiaI Intelligence in Higher Education
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
1. Spring 2014 CSCI 111 Final exam � of �1 6
1. (2 points) Flip over this test. On the back of this test write
your name in the upper, left-hand
corner.
2. (2 points) What are the four parts of the compiling process
(just give me 4 words, not a
paragraph).
3. (4 points) Which of the four steps of the compiling process
occurs only once, regardless of
the number of source files your application has?
4. (4 points) Write a line of code that causes the preprocessor to
generate an error.
5. (4 points) Write a line of code that causes the compiler to
generate an error.
6. (5 points) Describe how you could incorrectly compile the
joust project to cause the linker to
generate an error.
7. (5 points) Given:
1 float* fp;
2 //...
3 float pi;
4 pi=*(314 + fp);
Rewrite line 4 using array subscript notation.
2. Spring 2014 CSCI 111 Final exam � of �2 6
8. (5 points) Given:
1 float arr[100];
2 for(int x=0; x<100; ++x)
3 arr[x]=100-x;
What does the following expression print out?
cout << *arr << endl;
9. (14 points) Given:
int a=0;
int b=6;
int x=0;
Circle each if-expression that evaluates to true:
A) if(b)
B) if(x)
C) if(a=b==6)
D) if(a=b==5)
E) if(a=b=5)
F) if(a=x=0)
G) if(a=x==0)
3. Spring 2014 CSCI 111 Final exam � of �3 6
10. (10 points) Given:
1 #include<iostream>
2 using namespace std;
3
4 int main()
5 {
6 int x;
7 cout << "Enter a number greater than 10" << endl;
8 while ( x < 10 )
9 {
10 cin >> x;
11 }
12 return 0;
13 }
This program compiles just fine, and sometimes it runs as
expected. But sometimes when you
run it, it exits immediately after printing "Enter a number
greater than 10". That is, the program
doesn't pause for you to enter a number. Why are you getting
this inconsistent behavior?
11. (4 points) What is the output of the following:
int x=4;
int y=3;
A) cout << x / y << endl;
B) cout << x % y << endl;
C) cout << x << "%" << y << endl;
4. D) cout << "x" << '%' << 'y' << endl;
Spring 2014 CSCI 111 Final exam � of �4 6
12. (16 points) What is the type of the expression. That is, what
is the kind of thing that each
expression evaluates to. For example:
3 + 4 integer
You may assume that the variable a has been declared as an
integer.
A. a + 4
B. a = 4
C. 3.14 + 4.49
D. 3 + 3.14
E. 'a'
F. cout << a
G. new float[30]
H. new float
Spring 2014 CSCI 111 Final exam � of �5 6
13. (5 points) Write a for-loop that prints out the numbers
5. between 1 and 100 that are evenly
divisible by three.
14. (5 points) Write a while-loop that prints out the numbers
between 1 and 100 that are evenly
divisible by three.
15. (5 points) Write a do-while-loop that prints out the numbers
between 1 and 100 that are
evenly divisible by three.
Spring 2014 CSCI 111 Final exam � of �6 6
16. (10 points) Given:
1 #include<iostream>
2
3 class Willow {
4 public:
5 Willow(int g);
6 private:
7 float f;
8 };
9
10 Willow::Willow(int g) : f(g)
11 {
12 }
13
14 void display(int g)
15 {
16 std::cout << g << std::endl;
17 }
Write a main-function that:
A. Creates a Willow object
6. B. Calls the display function
CSCI 111, Fall 2013, Final Exam
You do NOT have to write complete programs to answer the
questions below,
unless the question asks explicitly that you do so.
1. (2 points) Write your name on the backside of the last page.
2. (8 points) Given the function:
void printMessage(string message);
Answer the following questions
What is the function's return type?
List the formal parameter(s) of the function. Include the data
type and name
of each formal parameter.
Write a call/invocation of the function printMessage with the
7. string "This is
so easy!" as the argument.
Is the function declaration or function definition shown above?
3. (5 points) Given the statement:
float* fp=new float[3];
To release this memory:
a) delete fp;
b) delete [] fp;
c) for(int i=0; i<3; ++i) delete fp[i];
d) delete fp[3]
e) delete float[3];
4.(5 points) Given:
8. a=3;
b=4;
expression 1: a -= b;
expression 2: a =- b;
What's the difference between the first and second expressions
a) They are equivalent
b) First is positive and second is negative
c) Second is negative and first is positive
d) The second one doesn't compile
e) None of the above
5.(10 points) Consider the program below in the test.cpp file
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
9. 5. do {
6. cout << “give me an int: “;
7. int i;
8. cin >> i;
9. } while (i < 0 || i > 10);
10. cout << “Thanks, i is “ << i << endl;
11. return 0;
12. }
This program has a bug. It will not compile and gives the
compiler error of
test.cpp:10:28 error: ’i’ was not declared in this scope
Identify what line number the bug is on and propose how to
eliminate it.
You’re welcome to write on or alongside the code itself.
10. 6.(10 points) Given the statement
string* airplane[25];
Describe what is created when this statement is executed. Be
explicit in your
answer.
7. (10 points) Given an array of integers:
int myArray[20];
open a text file for writing called “arraycontents.txt” and then
write a loop
that will print all of the contents of the array to the file.
11. 8. (10 points) Carefully distinguish between the meaning and
use of the dot
operator “.” and the scope resolution operator “::”
9. (15 points) Suppose your program contains the following
class, contained in
automobile.h,
class Automobile
{
public:
Automobile(double prc, double prft); //sets private data
members
void setPrice(double prc);
void setProfit(double prft);
double getPrice( );
private:
12. double price;
double profit;
double getProfit( );
};
and suppose the main function of your program contains the
following
declarations
Automobile hyundai (3999.99, 299.50);
Automobile jaguar (38999.99, 3200.00);
Which of the following statements will compile in the main
function of your
program? Write YES or NO before each statement.
hyundai.price = 4999.99;
jaguar.setPrice(30000.97);
double aPrice, aProfit;
aPrice = jaguar.getPrice( );
aProfit = jaguar.getProfit( );
13. aProfit = hyundai.getProfit( );
hyundai = jaguar;
10. (25 points) Write the implementation file (automobile.cpp)
for the class
contained in automobile.h, which is described in the previous
question.
CSCI 111, Spring 2013, Final Exam
1. (1 point) Write your name on the backside of the last page.
2. (1 point) Every C++ program begins at the function
____________.
3. (4 point) What is the difference between these 2 include
statements.
#include <iostream>
#include “iostream”
14. 4. (2 point) What is the expected output of the following piece
of code?
int x = 5.7;
int y = 3.2;
cout << x-y;
5. (3 point) Assuming we have three C++ file named main.cpp,
test.h, and test.cpp, what
command could we use to compile our code into an executable?
6. (4 point) Name the 4 steps that occur when a program is
compiled
1.
2.
3.
4.
7. (10 point) Write the implementation file (person.cpp) for the
following person.h file. The
file must be complete and should compile.
15. #include <string>
using namespace std;
class Person {
public:
Person(string, int); //should set private data members
string getName();
int getAge();
private:
string name;
int age;
};
8. (5 point) What is the output of the following program?
#include <iostream>
using namespace std;
void figureMeOut(int& x, int y, int& z);
int main( ) {
int a, b, c;
a = 10;
b = 20;
c = 30;
figureMeOut(a, b, c);
cout << a << " " << b << " " << c << endl;
return 0;
}
16. void figureMeOut(int& x, int y, int& z)
{
cout << x << " " << y << " " << z << endl;
x = 1;
y = 2;
z = 3;
cout << x << " " << y << " " << z << endl;
}
9. (4 point) What is the purpose of a constructor? When does it
run?
10. (3 point) Why do you use #ifndef, #define, and #endif in a
header file?
17. 11. (2 point) Describe the action of the new operator
12. (3 point) Suppose you are writing a program the uses a
stream called fin, which will be
connected to an input file called “stuff1.txt” and a stream called
fout, which will be
connected to an output file called “stuff2.txt”. How do you
declare fin and fout? What
include directive, if any, do you need to place in the program
file?
13. (2 point) What is wrong with the following piece of code?
int sampleArray[10];
for (int index = 1; index <= 10; index++)
sampleArray[index] = 3*index;