Tutorial Letter 203/1/2017: Introduction To Programming I
Tutorial Letter 203/1/2017: Introduction To Programming I
Introduction to Programming I
COS1511
Semesters 1
School of Computing
This tutorial letter contains the discussion of the solutions to assignment 3 semester 1.
DISCUSSION OF ASSIGNMENT 3
Dear Student
Please take the trouble to compare your answers to those given in this tutorial letter. Try to understand why your answer
is wrong if it differs from ours. This will help you in acquiring the skill (or art) of programming. The process of becoming a
good programmer, involves building new skills and new knowledge and new insights on top of others. This means that it is
a growing process and all the building blocks are important.
Question 1
With arrays, the data type of the index is always a(n) _____.
1. int
2. float
3. char
4. string
Answer: 1
Question 2
1. 0
2. 1
3. either a or b
4. none of the above
Answer: 1
Question 3
Which of the following statements declares a one-dimensional array containing two integer values?
Answer: 3
2
COS1511/203
Question 4
Which of the following statements will store the user’s entry into the second element of the numbers array?
1. numbers[1] = 0;
2. numbers[2] = 0;
3. cin >> numbers[1];
4. cin >> numbers[2];
Answer: 3
Question 5
1. by value
2. by reference
3. by actual parameters
4. by forma parameters
Answer: 2
Question 6
When passing an array to a function, you code _____ after the formal parameter name of the array in the function header
to indicate it is an array.
1. &
2. []
3. {}
4. ||
Answer: 2
Question 7
Passing an array _____ allows the computer to pass the address of only the first array element.
1. by value
2. by reference
3. by address
4. by scalar
Answer: 2
Question 8
Which of the following statements declares a one-dimensional array called letters to contain the first five lowercase
letters of the alphabet.
Answer: 1
3
Question 9
What happens when an array declaration statement does not provide an initial value for each of the elements in a
numeric array?
1. The compiler automatically initialises the elements, so the array elements contain 0.
2. The compiler does not automatically initialise the elements, but as it is a numeric array, all array elements contain a
numeric value.
3. The compiler does not automatically initialise the elements, so the array elements may contain garbage.
4. The compiler automatically initialises the elements with 0.
Answer: 3
Question 10
Answer: 4
Question 11
Given the array declaration below, which assignment statement will place the number 5 into the fifth array element?
1. numbers[4] = 5;
2. numbers[5] = 5;
3. numbers[1+3] = 5;
4. numbers[1+4] = 5;
Answer: 1
Question 12
Given the array declaration below and user prompt, which statement will place the number 1 into the first array element
using the extraction operator.
Answer: 4
4
COS1511/203
Question 13
Variables located in the first column in a two-dimensional array are assigned a column subscript of _____.
1. 0
2. 1
3. -1
4. NULL
Answer: 1
Question 14
Variables located in the third row in a two-dimensional array are assigned a row subscript of _____.
1. 2
2. 3
3. 4
4. -3
Answer: 1
Question 15
How many elements will a two-dimensional array with 4 rows and 5 columns contain?
1. 9
2. 12
3. 18
4. 20
Answer: 4
Question 16
Which of the following statements will declare a two-dimensional array called scores to be of the int data type and
contain 3 rows and 4 columns, while also initializing all elements to 0?
Answer: 3
Question 17
When referencing an element in a two-dimensional array, the _____ is coded before the _____.
Answer: 2
5
Question 18
How many loops are necessary to access the contents of a two-dimensional array efficiently?
1. 0
2. 1
3. 2
4. more than 2
Answer: 3
Question 19
How many loops are necessary to access the contents of a one-dimensional array efficiently?
1. 0
2. 1
3. 2
4. more than 2
Answer: 2
Question 20
Given the following array declaration, what is the value stored in the scores[2][3] element?
1. 0
2. 5
3. 10
4. 25
Answer: 1
Question 21
Given the following array declaration, what is the value stored in the scores[1][1] element?
1. 0
2. 5
3. 10
4. 25
5. Answer: 1
6
COS1511/203
Question 22
Given the following array declaration, what is the value stored in the scores[1][1] element?
1. 1
2. 4
3. 7
4. none of the above
Answer: 4
Question 23
Given the following array declaration, what is the value stored in the scores[2][2] element?
1. 0
2. 1
3. 2
4. 3
Answer: 1
Question 24
You would use _____ to reference the variable located in the first row, first column of the scores two-dimensional array.
1. scores[0][0]
2. scores[1][1]
3. scores[first][first]
4. scores[1,1]
Answer: 1
Question 25
Which statement will create a two-dimensional array called orders of type int with 2 rows and 3 columns and intialises
each element to 0?
1. int orders[2][3];
2. int orders[][] = {0};
3. int orders[3][2] = {0};
4. int orders[2][3] = {0};
Answer: 4
7
Question 26
Which statement will create a 2x2 char array called letterswith and initialses each element 'x'?
Answer: 2
Question 27
Which statement will create a two-dimensional integer array called scores with 3 rows and 2 columns where each
element has the initial value of 100?
Answer: 1
Question 28
Given the declaration below, which statement assigns the element in the first row and first column a value of 99?
1. scores[1][1] = 99;
2. scores[0][0] = '99';
3. scores[0][0] = 99;
4. scores[1][1] = "99";
Answer: 3
Question 29
Given the declaration below, which statement assigns the element in the last row and last column a value of 99?
1. scores[4][5] = 99;
2. scores[6][7] = 99;
3. scores[5][6] = 99;
4. scores[5][6] = '99';
Answer: 3
8
COS1511/203
Question 30
Given the declaration below, which statement assigns the element in the first row and first column the value entered by
the user through the prompt below.
Answer: 2
Question 31
Given the declarations below, which nested loop sums all of the elements in the array below.
int scores[3][3] = { {92, 87, 91}, {88, 72, 93}, {100, 94, 97} };
int sum = 0;
Answer: 2
Question 32
#include<iostream.h>
void main()
{
int n=1;
cout<<endl<<"The numbers are;"<<endl;
do
{
cout <<n<<"\t";
n++;
} while (n<=100);
cout <<endl;
}
9
1. print natural numbers 0 to 99
2. print natural numbers 1 to 99
3. print natural numbers 0 to 100
4. print natural numbers 1 to 100
Answer: 4
Question 33
................ allows that a section of a program is compiled only if the defined constant that is specified as the parameter
has been defined, independently of its value.
1. #ifdef
2. #if
3. #define
4. #ifd
Answer: 1
Question 34
1. int
2. char
3. float
4. string
Answer: 4
Question 35
The statement ____ declares and initializes a string variable named zipCode
Answer: 1
Question 36
1. 0
2. 1
3. 2
4. 3
Answer: 4
10
COS1511/203
Question 37
1. 0
2. 1
3. 2
4. 3
Answer: 3
Question 38
1. 0
2. 1
3. 2
4. 3
Answer: 3
Question 39
1. 0
2. 1
3. 2
4. 3
Answer: 3
Question 40
You can use the string class’s _____ function to remove one or more characters located anywhere in a string variable.
1. remove
2. delete
3. truncate
4. none of the above
Answer: 4
Question 41
You can use the string class’s _____ function for inserting characters within a string variable.
1. add
2. addition
3. append
4. insert
Answer: 4
11
Question 42
Given the following, which statement stores the characters entered by the user, up until the newline character, in the
name variable; and consume the newline character.
1. getline(cin, name);
2. getline( name, cin);
3. readline(cin, name);
4. cin >> (cin, name);
Answer: 4
Question 43
Which C++ statements determine the length of the name string defined below and place the value into the number
variable?
int number = 0;
string name = "Hello";
1. number = name.len();
2. number = name.size();
3. number = size(name);
4. number == name.size();
Answer: 3
Question 44
Given the declarations below, using the substr function, which statements will assign "First" to the name1 variable
and "Last" to the name2 variable.
Answer: 3
Question 45
Given the declarations below, using the find function, write the statements to search the number string variable to
determine if the sequence "123" exists. The location should be placed in the position variable.
12
COS1511/203
int position = 0;
string number = "1234567890";
1. position = number.find(123);
2. find("123", 0);
3. position = number.find("123", 0);
4. find(number("123", 0), position);
Answer: 3
Question 46
Which one of the following options represents the output of the program below?
struct pixel
{
int c, r;
};
void display(pixel p)
{
cout << "col "<< p.c << " row " << p.r << endl;
}
int main()
{
pixel x = {40,50}, y, z;
z = x;
x.c += 10;
y = z;
y.c += 10;
y.r += 20;
z.c -= 15;
display(x);
display(y);
display(z);
return 0;
}
1. Col 50 Row 50
Col 50 Row 70
Col 25 Row 50
2. Col 40 Row 40
Col 40 Row 70
Col 25 Row 50
3. Col 60 Row 50
Col 60 Row 80
Col 25 Row 50
4. Col 50 Row 70
Col 50 Row 55
Col 25 Row 50
Answer: 1
13
Question 47
Which one of the following options represents the output of the program below?
struct play
{
int score, bonus;
};
int main()
{
play pl = {10, 15};
calculate(pl, 5);
cout << pl.score << ":" << pl.bonus << endl;
calculate(pl);
cout << pl.score << ":" << pl.bonus << endl;
calculate(pl, 15);
cout << pl.score << ":" << pl.bonus << endl;
return 0;
}
1. 11:20
12:30
13:45
2. 16:10
17:40
18:55
3. 10:20
11:30
12:45
4. 11:10
12:20
13:35
Answer: 1
14
COS1511/203
Question 48
Which one of the following options represents the output of the program below?
struct myBox
{
int length, breadth, height;
};
int main ()
{
myBox B1 = {10, 15, 5}, B2, B3;
++B1.height;
dimension(B1);
B3 = B1;
++B3.length;
B3.breadth++;
dimension(B3);
B2 = B3;
B2.height += 5;
B2.length--;
dimension(B2);
return 0;
}
1. 10x15x5
11x16x6
10x16x11
2. 10x5x6
11x16x6
10x16x11
3. 10x15x6
11x16x6
10x5x11
4. 10x15x6
11x16x6
10x16x11
Answer: 4
15
Question 49
#include <iostream>
using namespace std;
return 0;
}
1. #a#E#E@rTH
2. @a@E@E#rtH
3. @a@E@E#rTH
4. #a#E#E#rtH
Answer: 3
Question 50
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "CoMPutER";
for (int x = 0; x < name.size(); x++)
if (islower(name [x]))
name [x] = toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] = tolower(name[x]);
else
name[x] = name[x-1];
cout << name;
return 0;
}
1. CoMPutEr
2. cOmpUTeR
16
COS1511/203
3. coMMutEE
4. cOmmUTee
Answer: 4
Question 51
#include <iostream>
using namespace std;
int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;
return 0;
}
1. 4, 4
2. 18, 4
3. 18, 18
4. 4, 18
Answer: 1
Question 52
#include <iostream>
using namespace std;
int main()
{
int value = 7;
func (value, value);
cout << value << ", " << value << '\n';
return 0;
}
1. 7, 70
2. 7, 0
3. 0, 0
17
4. 0, 70
Answer: 3
Question 53
#include <iostream>
using namespace std;
int i = 100;
void abc()
{
int i = 8;
cout << "first = " << i++ << endl;
}
int main()
{
int i = 2;
abc();
cout << "second = " << i << endl;
abc();
return 0;
}
1. first = 8
second = 2
first = 8
2. first = 8
second = 2
3. second = 2
first = 8
4. first = 8
second = 2
first = 9
Answer: 1
Question 54
#include <iostream>
using namespace std;
18
COS1511/203
int main()
{
int p = 20, q = 23;
q = func(p, q);
cout << p << " " << " " << q << endl;
p = func (q);
cout << p << " " << " " << q << endl;
q = func (p);
cout << p << " " << " " << q << endl;
return 0;
}
1. 20 22
10 23
11 11
2. 20 23
10 23
11 11
3. 20 23
9 23
11 11
4. 20 23
10 23
10 11
Answer: 2
Question 55
**********
**********
**********
**********
1. int i, j; 2. int i, j;
for(i = 1; i <= 4; i++) for(i = 1; i < 4; i++)
{ {
for(j = 1; j <= 10; j++) for(j = 1; j <= 10; j++)
cout << '*' << endl; cout << '*';
} cout << endl;
}
3. int i, j; 4. int i, j;
for(i = 1; i <= 4; i++) for(i = 0; i <= 4; i++)
{ {
for(j = 1; j <= 10; j++) for(j = 1; j < 10; j++)
cout << '*'; cout << '*';
cout << endl; cout << endl;
} }
19
The following four questions refer to the person structure.
Question 56
Which of the following declarations define a structure named person which has three members: name, age and
salary.
1. struct
{
string name;
int age;
float salary;
} person;
2. struct person
string name;
int age;
float salary;
3. struct person
{
string name;
int age;
float salary;
};
4. person
{
string name;
int age;
float salary;
};
Answer: 3
Question 57
1. person bill;
2. bill person;
3. person bill[];
4. person[] bill;
Answer: 1
Question 58
Which statement accesses the age member of structure variable bill and assign the value 50 to it.
1. bill[].age = 50;
2. bill[age] = 50;
3. bill(age) = 50;
4. bill.age = 50;
Answer: 4
20
COS1511/203
Question 59
Which one of the following statements declares an array of size 10 with elements of type person?
1. person bill[10];
2. person[] bill;
3. person bill[9];
4. person[10] bill;
Answer: 1
Question 60
#include <iostream>
int func()
{
return var;
}
int main()
{
int var = 5;
cout << func() << "\n";
return 0;
}
1. 0
2. 15
3. 10
4. 5
Answer: 3
Question 61
Which of the following function header lines is valid for a function called findMax that finds and returns the maximum
value stored in an array of integers that is passed in as a parameter?
Answer: 2
Question 62
21
1. for (i = 0; i < NUMROWS; i++)
{
for (j = 0; j < NUMCOLS; j++)
cout << setw(4) << val[i][j];
cout << endl;
}
Answer: 1
Question 63
Which of the following function header correctly includes a two dimensional array as its parameter?
Answer: 2
Question 64
#include <iostream>
using namespace std;
int main ()
{
int array[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0; n < 8; n++)
{
result += array[n];
}
cout << result;
return 0;
}
1. 24
2. 25
3. 26
4. 27
Answer: 4
22
COS1511/203
Question 65
#include <iostream>
using namespace std;
int main()
{
char str[5] = "ABC";
cout << str[3];
cout << str;
return 0;
}
a. ABC
b. ABCD
c. AB
d. C
Answer: 1
Question 66
Suppose that gamma is an array of 50 elements of type int and j is an int variable. Which of the following for loops
sets the subscript of gamma out-of-range?
Answer: 3
Question 67
1. struct studentType
{
int ID;
};
2. struct studentType
{
string name;
int ID;
float gpa;
}
23
4. struct studentType
{
int ID = 1;
};
Answer: 1
Question 68
struct rectangleData
{
float length;
float width;
float area;
float perimeter;
};
1. rectangle rectangleData;
2. struct rectangleData();
3. rectangleData myRectangle;
4. rectangleData rectangle = new rectangleData();
Answer: 3
Question 69
struct temp
{
int b;
};
temp s[50];
The correct syntax to access the member of the ith structure in the array of structures is?
1. s.b.[i];
2. s.[i].b;
3. s.b[i];
4. s[i].b;
Answer: 3
Question 70
1. .memberName
2. *memberName
3. [memberName]
4. $memberName
Answer: 1
24
COS1511/203
Question 71
struct rectangleData
{
float length;
float width;
float area;
float perimeter;
};
rectangleData bigRect;
Which of the following statements correctly initializes the element length of bigRect?
1. bigRect = {10};
2. bigRect.length = 10;
3. length[0]= 10;
4. bigRect[0]= 10
Answer: 2
Question 72
struct rectangleData
{
float length;
float width;
float area;
float perimeter;
};
rectangleData bigRect;
Answer: 2
Question 73
struct personalInfo
{
string name;
int age;
float height;
float weight;
};
25
struct commonInfo
{
string name;
int age;
};
personalInfo person1, person2;
commonInfo person3, person4;
1. person1 = person3;
2. person2 = person1;
3. person2 = person3;
4. person2 = person4;
Answer: 2
Question 74
struct studentType1
{
string name;
int ID;
float gpa;
};
struct studentType2
{
string name;
int ID;
float gpa;
};
studentType1 student1, student2;
studentType2 student3, student4;
1. student2 = student3;
2. student1 = student4;
3. student2.ID = ID;
4. student1.ID = student3.ID;
Answer: 4
26
COS1511/203
Question 75
struct rectangleData
{
float length;
float width;
float area;
float perimeter;
};
rectangleData bigRect;
rectangleData smallRect;
1. if (bigRect == smallRect)
2. if (bigRect != smallRect)
3. if (bigRect.length == width)
4. if (bigRect.length == smallRect.width)
Answer: 4
Question 76
struct circleData
{
float radius;
float area;
float circumference;
};
circleData circle;
Answer: 4
Question 77
struct rectangleData
{
float length;
float width;
float area;
float perimeter;
};
27
rectangleData bigRect;
Answer: 2
Question 78
struct supplierType
{
string name;
int supplierID;
};
struct applianceType
{
supplierType supplier;
string modelNo;
float cost;
};
applianceType applianceList[25];
1. It is a multidimensional array.
2. It is a struct.
3. It is an array of structs.
4. It is a struct of arrays.
Answer: 3
Question 79
struct supplierType
{
string name;
int supplierID;
};
struct applianceType
{
supplierType supplier;
string modelNo;
float cost;
};
applianceType applianceList[25];
Which of the following statements correctly initializes the cost of each appliance to 0?
28
COS1511/203
1. applianceList.cost = 0;
2. applianceList.cost[25] = 0;
3. for (int j = 1; j < 25; j++)
applianceList.cost[j] = 0;
Answer: 4
Question 80
#include <iostream>
#include <string>
using namespace std;
struct student
{
int num;
char name[25];
};
int main()
{
student stu;
stu.num = 123;
strcpy(stu.name, "John");
cout << stu.num << endl;
cout << stu.name << endl;
return 0;
}
1. 123
John
2. John
John
3. 123
123
4. compile time error
Answer: 1
©
UNISA
2017
29