INTRODUCTION TO PROGRAMMING Exam Questions
INTRODUCTION TO PROGRAMMING Exam Questions
INTRODUCTION TO PROGRAMMING Exam Questions
CODE : CPRG1201
DURATION: 3 HOURS
INSTRUCTIONS:
1. THIS EXAMINATION PAPER CONSISTS OF TWO (2) SECTIONS: (A) AND (B)
SECTION A
Instructions: On the computerized answer sheet provided, shade the letter that corresponds
with the correct response for each of the following:
1. Which of the following loop correctly prints the elements of the array?
A. do
{
Console.WriteLine((char) i);
}
while (int i = 0; i <X i++);
D. foreach (int i in X)
{
Console.WriteLine((char) i);
}
A. Int
B. Long
C. Short
D. Byte
4. Which of the following are the correct way to initialise the variables i and j to a value 4?
i. int i = 4; int j = 4;
ii. int i, j;
i = 4 : j = 4;
iii. int i, j = 4;
iv. int i = 4, j = 4;
A. i, iii
B. ii, iv
C. i, iv
D. ii, iii
A. X AND Y
B. X OR Y
C. NOT X
D. NOT X AND Y
6. Which of the following will be the CORRECT output for the C# program given below?
using System;
namespace ConsoleApplication1
{
structTest
{
public inti;
}
class Program
{
static void Main(string[] args)
{
Test a = new Test();
a.i = 15;
Practice(a);
Console.Write(a.i + " ");
Console.ReadKey();
}
static void Practice(Test b)
{
b.i = 30;
Console.Write(b.i + " ");
}
}
}
A. 15 30
B. 30 15
C. 15 15
D. 30 30
A. i only
B. ii only
C. ii, iii only
D. i, ii, iii
8. If a procedure mark() is to receive an integer, and a double then which of the following is
correct way of defining this procedure?
A. i and ii
B. i and iii
C. iii and ii
D. i, ii, iii
A. It is not possible to extend the if statement to handle multiple conditions using the
else-if arrangement.
B. The switch statement can include any number of case instances with two case
statements having the same value.
C. The if statement selects a statement for execution based on the value of a boolean
expression.
D. A jump statement such as a break is required after each case block.
11. The C# Switch statement works only with which of the following data types:
i. Int
ii. Float
iii. String
iv. Char
A. i and iii
B. i and ii
C. ii and iv
D. i, iii and iv
12. Which of the following is the CORRECT way to rewrite the C# code below using a
definite loop?
inta = 0;
do
{
Console.WriteLine(a);
a++;
}
while (a<= 5);
A. int a;
for (a = 0; a <= 5; a++)
{
Console.WriteLine(a);
}
B. inta = 0;
do
{
Console.WriteLine(a);
}
until (a<= 5);
C. inta = 0;
while (a<= 11)
{
Console.WriteLine(a);
a += 1;
}
D. int a;
for (a = 1; a= 4; a+ 1)
{
Console.WriteLine(a);
}
A. local variable
B. global variable
C. parameter
D. procedure
A.
B.
C.
D.
A. 3, -3
B. 3, 3
C. 4, -3
D. 4, -4
A. Welcome to
C# Programming!
B. Welcome to C# Programming!
C. Welcome to
C#
Programming!
D. Welcome
to
C# Programming
17. Which of the following statements are correct about the C# code given?
i. The condition num<17 will be evaluated only if age > 22is True
ii. The statement A = 55 will get executed if any one condition is True.
iii. The condition num< 17 will be evaluated only if age > 22is False.
iv. The statement A = 55 will get executed if both the conditions are True.
A. i, iv
B. i, iii
C. ii, iv
D. i, ii, iv
READ Grade
WHILE (Grade <> 0 DO
Grade = Grade + 10
PRINT “Grade: ”, Grade
READ Grade
ENDWHILE
18. How many types of flowchart symbols are needed to complete the flowchart
representation of the above pseudocode?
A. 4
B. 3
C. 2
D. 5
19. Select the choice that most accurately describes the error below:
The algorithm below attempts to print the contents of an array of size 20.
FOR count = 1 TO 20 DO
PRINT array[0]
ENDFOR
A. Logic error
B. Syntax error
C. Runtime error
D. Desk checking error
A. i only
B. ii only
C. iii only
D. i, ii, iii
i. string s = "678";
int i;
i = (int)s;
ii. string s = "678";
int i;
i = int.Parse(s);
iii. string s = "678";
int i;
i = Int32.Parse(s);
iv. string s = "678";
int i;
I = Convert.ToInt32(s);
A. i, ii
B. i, ii, iii
C. ii,iii,iv
D. ii, iii
line 1. intTestGrade;
line 2. string drivingGrade;
line 3. if (TestGrde>= 50 AND TestGrade<= 100)
line 4. {
line 5. drivingGrade = Pass;
line 6. }
A. 1
B. 2, 6
C. 1, 3
D. 3, 5
23. Which of the following is the correct way to define a variable of the type structEmployee
declared below in C#?
struct Employee
{
private string name;
private intidNum;
private doublesal;
}
A. i, iii
B. ii,iv
C. i, iv
D. ii, iii
24. What is the method structure for passing a variable that holds a reference to an array of
strings?
Which of the following GPA values satisfies at least one of the given conditions?
A. 3.80
B. 3.69
C. 2.69
D. 1.70
END OF SECTION A
SECTION B
Question 1
Begin
set i = 0
set sum = 0
while i 10
get x
sum = sum + x
add 1 to i
avg = sum / 10.0
print avg
End (10 marks)
(Total 25 marks)
Get
price
F
Price <>
-1
T
F T
Total >
10000
Get payment
Question 2
Write a C# program that creates a two dimensional (2D) array with 5 rows and 5 columns. The
array represents scores in three exams for five students. The scores should be entered from the
keyboard and stored in the first three columns of the array, the total of the three scores should be
calculated and stored in the fourth column of the array, and the average of the three scores
calculated and stored in the fifth column of the array. The array is printed to the screen after it is
populated. An example of the structure of the array is shown below.
45 78 63 186 62
90 45 67 202 67
83 98 76 257 85
40 62 59 161 53
80 75 63 218 72
(Total 25 marks)
Question 3
A. List THREE (3) advantages of assembly language over a high level language. (3 marks)
B. Outline THREE (3) reasons why you might want to use an interpreter over a complier.
(6 marks)
C. Define the term “pseudocode”. (2 marks)
D. Given the pseudocode below identify a line or statement of the pseudocode that shows
the input, processing, and output. (6 marks)
i. Start
ii. float price, subbill, discPrice, finalPrice
iii. int qty
iv. disc = 0.10
v. tax = 0.25
vi. Print “Enter the quantity of items purchased”
vii. Read qty
viii. Print “Enter the price of the item”
ix. Read price
x. subbill = qty * price
xi. discPrice = price – ( price * disc)
xii. finalPrice = discPrice + (price * tax)
xiii. Print “Total bill is: ”, finalPrice
xiv. Stop
Question 4
B. Write a pseudocode for a program that will use a FOR repetition structure to calculate the
bonuses for its employees. Assume a company pays an annual bonus to its employees. The
bonus is based on the number of years the employee has been with the company. Employees
working at the company for less than 5 years receive a bonus equal to 1% percent of their
salary; all others receive a bonus equal to 2% of their salary.
Print an output message that mimics the format of the following message, where <bonus-
value> represents the numeric value for the bonus and <employee name > represents the
name of the employees inputted. The total pay of the employee should also be outputted in
the format shown below:
C. Outline the stages of the Program Development Life Cycle. (10 marks)
(Total 25 marks)
Question 5
A. Write a psuedocode using a modular structure that will compute the pay for 20 employees.
Assume that the employee name, National Insurance Number (NIS), hours worked and
hourly rate are to be provided as input. The output will be employee name, National
Insurance Number (NIS) and pay for each employee. Regular pay will be computed as
hours (up through 40) times rate, and overtime pay will be computed at time and a half (1.5
times hours times rate) for all hours worked over 40. (12 marks)
B. Write a C# program that will compare the following strings and determined if they are
equal. If the strings are equal an appropriate message should be outputted.
D. Create a structure called MyStruct with public integer members’ num1 and num2.
(3 marks)
END OF EXAMINATION
CODE : (CPRG1201)
DURATION: 3 HOURS
SOLUTIONS
SECTION A
1 D 6 B 11 D 16 B 21 C
2 A 7 C 12 A 17 A 22 D
3 D 8 A 13 A 18 B 23 C
4 C 9 A 14 B 19 A 24 A
5 C 10 C 15 C 20 B 25 A
SECTION B
QUESTION 1
Begin
set i = 0
set sum = 0
while i 10
get x
sum = sum + x
add 1 to i
avg = sum / 10.0
print avg
End
(10 Marks)
Mark scheme: 1 Mark for each correct flowchart symbol with correct statement,
2 Marks for correct arrow flow.
Answer:
1(a).
Answer:
Call by value means that the value contained by a variable that is passed as the parameter is
copied to the variables of the method, and if inside the method these values are changed or
modified, the change is not reflected in the actual passed variable. However, for call by reference
when a parameter is passed to a method by reference, instead of passing value contained by a
variable, the reference of the variable is passed to method. The method operates on the
references of the variables passed in the parameters rather than operating on their values. This
results in the modification of variables in the calling function when they are modified in the
called function.
Mark scheme: 1 mark for clearly explaining call by reference, 1 mark for call by value
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
//procedure
static void Square(int num1)
{
}
}
Start
Get price
F
Price <> -1
F T
Total > 10000
Get payment
End
The Council of Community Colleges of Jamaica Page 19
CPRG1201
Answer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
double total=0;
double price;
double gct;
double discount;
double finalPrice;
double payment;
double change;
if (total>10000)
{
discount = total * 0.2;
}
Console.WriteLine("enter payment");
payment = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
}
Mark scheme: 1 mark declare variables, 2 marks for loop, ½ mark for each calculation
(gct, finalprice, payment, change), 2 marks for if statement.
Total = 7 Marks
QUESTION 2
Write a C# program that creates a two dimensional (2D) array with 5 rows and 5 columns. The
array represents scores in three exams for five students. The scores should be entered from the
keyboard and stored in the first three columns of the array, the total of the three scores should
be calculated and stored in the fourth column of the array, and the average of the three scores
calculated and stored in the fifth column of the array. The array is printed to the screen after it is
populated. An example of the structure of the array is shown below.
Answer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int i, j;
int stu1avg = 0;
int stu2avg = 0;
int stu3avg = 0;
int stu4avg = 0;
int stu5avg = 0;
{
if (j < 3)
{
Console.WriteLine("Enter grade for student {0}", i + 1);
gradeArr[i, j] = Convert.ToInt32(Console.ReadLine()); //fill the
first row
if (i == 0)
{
else if (i == 1)
{
stu2avg += gradeArr[i, j];
}
else if (i == 2)
{
stu3avg += gradeArr[i, j];
}
if (i == 3)
{
stu4avg += gradeArr[i, j];
}
if (i == 4)
{
stu5avg += gradeArr[i, j];
}
}
else if (j==3)
{
if (i == 0)
{
gradeArr[i, j] = stu1avg;
}
else if (i == 1)
{
gradeArr[i, j] = stu2avg;
}
else if (i == 2)
{
gradeArr[i, j] = stu3avg;
}
if (i == 3)
{
gradeArr[i, j] = stu4avg;
}
if (i == 4)
{
gradeArr[i, j] = stu5avg;
}
}
else if (j == 4)
{
if (i == 0)
{
else if (i == 1)
{
int avg1 = (int)(stu2avg / 3);
gradeArr[i, j] = avg1;
}
else if (i == 2)
{
int avg1 = (int)(stu3avg / 3);
gradeArr[i, j] = avg1;
}
if (i == 3)
{
int avg1 = (int)(stu4avg / 3);
gradeArr[i, j] = avg1;
}
if (i == 4)
{
int avg1 = (int)(stu5avg / 3);
gradeArr[i, j] = avg1;
}
}
}
Console.WriteLine();
Console.ReadLine();
}
}
Mark scheme: 2 marks for correct header statements, 1 mark for main, 2 marks for array
declaration, 5 marks for reading grades from keyboard, 5 marks calculating total and
storing in array, 5 marks for calculating average and storing in array, 5 marks for final
array output
QUESTION 3
a) List three (3) advantages of assembly language over a high level language. (3 Marks)
Answer:
b) Outline three (3) reasons why you might want to use an interpreter over a complier. (6 Marks)
Answer:
1. The main advantage of an interpreter over a compiler is portability. The binary code
produced by the compiler is tailored specifically to a target computer architecture.
The interpreter, on the other hand, processes the source code directly.
3. An interpreter does not translate all the source code before running it, therefore the
time necessary to test the modifications in a program is significantly shorter.
Answer
(d) Given the pseudocode below identify a line or statement of the pseudocode that shows the
input, processing, and output. (6 Marks)
1. Start
2. float price, subbill, discPrice, finalPrice
3. int qty
4. disc = 0.10
5. tax = 0.25
Answer:
input – lines 7 or 9
Output – lines 6, 8, 13
Processing – lines 10, 11, 12
Students allowed to write any one line number or the complete statement. Only one response is
required for each element. Mark Scheme [2 * 3 = 6 Marks]
Answer:
Variables are data items whose values may change, or vary, during processing. We create
variable names to represent, or refer to, these data items.
QUESTION 4
(b) Write a pseudocode for a program that will use a FOR repetition structure to calculate the
bonuses for its employees. Assume a company pays an annual bonus to its employees. The bonus is based
on the number of years the employee has been with the company. Employees working at the company for
less than 5 years receive a bonus equal to 1% percent of their salary; all others receive a bonus equal to 2% of
their salary.
Print an output message that mimics the format of the following message, where <bonus-value> represents
the numeric value for the bonus and <employee name > represents the name of the employees inputted. The
total pay of the employee should also be outputted in the format shown below:
Answer:
Start
Integer NumEmp, i
float Bonus, Salary, NumYears
String EmpName
For i = 1 to NumEmp DO
Print “Enter Employee name: ”
Read EmpName
Print “Enter the number of years the employee is employed to the company”
Read NumYears
Print “Enter Employee Salary: ”
Read Salary
(c) Outline the stages of the Program Development Life Cycle. (10 Marks)
(d) Identify three (3) needs for computer programming. (3 Marks)
Total 25 Marks
QUESTION 5
(a) Write a pseudocode using a modular structure that will compute the pay for 20 employees.
Assume that the employee name, National Insurance Number (NIS), hours worked and hourly
rate are to be provided as input. The output will be employee name, National Insurance Number
(NIS) and pay for each employee. Regular pay will be computed as hours (up through 40) times
rate, and overtime pay will be computed at time and a half (1.5 times hours times rate) for all
hours worked over 40. (12 Marks)
Else
Pay = hrs * rate
Endif
Print “Customer name:”, name
Print “NIS:”, NIS
Print “Pay is:”, Pay
Return
(b) Write a C# program that will compare the following strings and determined if they are equal.
If the strings are equal an appropriate message should be outputted.
(8 Marks)
Mark Scheme [System Directive(s) – 1 Mark
Declaration of strings – 1 Mark,
If statements – 2 Marks
Correct use of the string compare function – 3 Marks
Output appropriate message – 1 Mark]
Answer:
using System;
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str1 = "This is test";
string str2 = "This is text";
if (String.Compare(str1, str2) == 0)
{
Console.WriteLine(str1 + " and " + str2 + " are equal.");
}
else
{
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
}
Console.ReadKey() ;
}
}
}
Answer:
Structure scope means all the members that are a part of the body of a structure. A structure can
contain constructors, constants, fields, methods, properties, indexers, operators, events, and
nested types.
(d) Create a structure called MyStruct with public integer members’ num1 and num2.