Introduction to Programming
Tutorial 2
Prerequisite
Read units I to III and attempt the questions before attending the tutorial
Unit I: Introduction to computers and Programming
1. Why must programs written in a high-level language be translated into machine language
before they can be run?
2. Why is it easier to write a program in a high-level language than in machine language?
3. Explain the difference between an object file and an executable file.
4. What is the difference between a syntax error and a logical error?
5. The job of the __________ is to fetch instructions, carry out the operations commanded by
the instructions, and produce some outcome or resultant information.
6. Internally, the CPU consists of the __________ and the __________.
7. A(n) __________ is an example of a secondary storage device.
8. The two general categories of software are __________ and __________.
9. A program is a set of __________.
10. Since computers can’t be programmed in natural human language, algorithms must be
written in a(n) __________ language.
11. _________ is the only language computers really process.
12. _________ languages are close to the level of humans in terms of readability.
13. _________ languages are close to the level of the computer.
14. A program’s ability to run on several different types of computer systems is called
__________.
15. Words that have special meaning in a programming language are called __________.
16. Words or names defined by the programmer are called __________.
17. _________ are characters or symbols that perform operations on one or more operands.
1
18. _________ characters or symbols mark the beginning or ending of programming
statements, or separate items in a list.
19. The rules that must be followed when constructing a program are called __________.
20. A(n) __________ is a named storage location.
21. A variable must be __________ before it can be used in a program.
22. The three primary activities of a program are __________, __________, and __________.
23. _________ is information a program gathers from the outside world.
24. _________ is information a program sends to the outside world.
25. A(n) __________ is a diagram that graphically illustrates the structure of a program.
2
Unit 2: Introduction to C++
1. How may the double variables temp , weight , and age be defined in one statement?
2. How may the int variables months , days , and years be defined in one statement, with
months initialized to 2 and years initialized to 3?
3. A variable must be defined before it can be used. (True / False)
4. Variable names may begin with a number. (True / False)
5. Variable names may be up to 31 characters long. (True / False)
6. A left brace in a C++ program should always be followed by a right brace later in the
program. (True / False)
7. Is the following comment written using single-line or multi-line comment symbols?
/* This program was written by an undergraduate*/
8. Is the following comment written using single-line or multi-line comment symbols?
// This program was written by an undergraduate
9. Modify the following program so it prints two blank lines between each line of text.
#include <iostream>
using namespace std;
int main()
{
cout << "Birds of the same";
cout << "feather";
cout << "Flock together";
cout << " – William Turner";
return 0;
}
10. What will the following programs print on the screen?
A)
#include <iostream>
using namespace std;
int main()
{
3
int freeze = 32, boil = 212;
freeze = 0;
boil = 100;
cout << freeze << endl << boil << endl;
return 0;
}
B)
#include <iostream>
using namespace std;
int main()
{
int x = 0, y = 2;
x = y * 4;
cout << x << endl << y << endl;
return 0;
}
B)
#include <iostream>
using namespace std;
int main()
{
cout << "I am the incredible";
cout << "computing\nmachine";
cout << "\nand I will\namaze\n";
cout << "you.";
return 0;
}
D)
#include <iostream>
using namespace std;
int main()
{
4
cout << "Be careful\n";
cout << "This might/n be a tricky ";
cout << "question\n";
return 0;
}
E)
#include <iostream>
using namespace std;
int main()
{
int a, x = 23;
a = x % 2;
cout << x << endl << a << endl;
return 0;
}
5
Unit III: Expressions and Interactivity
1. Assume that the following variables are defined:
int age;
double pay;
char section;
Write a single cin statement that will read input into each of these variables.
2. Assume a string object has been defined as follows:
string description;
A) Write a cin statement that reads in a one-word string.
B) Write a statement that reads in a string that can contain multiple words separated by
blanks.
3. What header files must be included in the following program?
int main()
{
double amount = 89.7;
cout << showpoint << fixed;
cout << setw(8) << amount << endl;
return 0;
}
4. Assume a program has the following variable definitions:
int a, b = 2;
float c = 4.2;
//and the following statement:
a = b * c;
What value will be stored in a ?
5. Assume that qty and salesReps are both integers. Use a type cast expression to rewrite the
following statement so it will no longer perform integer division.
unitsEach = qty / salesReps;
6. Complete the following table by writing statements with combined assignment operators
in the right-hand column. The statements should be equivalent to the statements in the
left-hand column.
Statements with Assignment Operator Statements with Combined Assignment
Operator
6
x = x + 5;
total = total + subtotal;
dist = dist / rep;
ppl = ppl * period;
inv = inv − shrinkage;
num = num % 2;
7. Write a multiple assignment statement that can be used instead of the following group of
assignment statements:
east = 1;
west = 1;
north = 1;
south = 1;
8. The __________ library function returns the value of a number raised to a power.
9. The __________ library function returns the square root of a number.
10. The __________ file must be included in a program that uses the mathematical functions.