Final Exam Question Paper FSPK0022 Foc July 2023-2024
Final Exam Question Paper FSPK0022 Foc July 2023-2024
DURATION : 2 HOURS
INSTRUCTION TO CANDIDATES:
1. This question paper consists of three parts:
STUDENT NAME:
MATRIC NO:
SECTION:
LECTURER NAME:
WARNING!
Students caught copying/cheating during the examination will be liable for disciplinary actions and
SPACE may recommend the student to be expelled from the study.
This examination question consists of (14) printed pages only including this page.
PART A: MULTIPLE CHOICE QUESTIONS (20 MARKS)
Answer ALL the questions and write the correct answer the answer booklet.
A. C
B. Basic
C. UNIX
D. Fortran
A. Operators
B. Preprocessors
C. Punctuation marks
D. Reserved words
A. Object
B. Source
C. Library
D. Executable
2
4. What is the output given by the following code fragment?
15 int i = 2;
16 int j = 10;
17 int k = 25;
18 int temp = 0;
19
20 cout << (( i != j ) && (( i > 5) || ( j < 15)) && temp );
A. 0
B. 1
C. True
D. False
5. Given the following output, the first line presents the column for ease of reference.
Which of the following statement will instruct the compiler to display Hello at the
columns as demonstrated above?
6. What is the name of the header file that contains mathematical functions?
A. ctype
B. cmath
C. iomanip
D. iostream
3
7. The possible value for variable response is ‘Y’ or ‘N’. What is the data type that is
suitable for variable response?
A. int
B. char
C. bool
D. String
A. int
B. char
C. three
D. double
A. void
B. double
C. int
D. bool
4
10. How many types of looping are there in C++?
A. 1
B. 2
C. 3
D. 4
A. For
B. While
C. Do While
A. for loop
B. while loop
C. do-while loop
D. switch
5
13. How many minimum numbers of functions should be represent in a C++ program for its
execution?
A. 0
B. 1
C. 2
D. 3
#include <iostream>
using namespace std;
int main() {
if (0) {
cout << "Hello";
}
else
{
cout << "Good Bye";
}
return 0;
}
A. Hello
B. HelloGood by
C. Good Bye
D. Compilation Error
6
15. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int n ;
for (n = 5; n < 0; n--)
{
cout << n;
if (n == 3)
break;
}
return 0;
}
A. 543
B. 54
C. 5432
D. 53
7
18. Which of the following statement is FALSE about local and global variables?
A. Local variables can only be used inside the function or code block in which they were
declared.
B. Variable declared outside of all functions are called global variables.
C. Global variables can be accessed within any function defined for the program.
D. Only main function can update or change the value of the global variable.
19. Which of the following is the default return value of functions in C++?
A. int
B. char
C. float
D. void
A. 10
B. 0
C. Error
D. Segmentation fault
-----------------------------------------------------------------------------------------------------------
8
PART B: STRUCTURED QUESTIONS (50 MARKS)
Answer ALL the questions and write the answers on the answer booklet provided.
Use a new page for each question.
2. Given the code fragment below, what is the output for the following value of uniYear?
(2 marks)
20 switch (uniYear)
21 {
22 case 1: cout<<”Freshman.”<<end1;
23 case 2: cout<<”Sophomore..”<<end1; break;
24 case 3: cout<<”Junior..”<<end1;
25 case 4: cout<<”Senior.”<<end1; break;
26 Default: cout<<”Unrecognized.”;
27 }
9
QUESTION 2 (10 MARKS)
1. Convert the following coding fragment into while loop structure. (8 marks)
Without a loop
18 cout << “ Enter a salary: “;
19 cin >> salary;
20 total = total + salary;
21
22 cout << “ Enter a salary: “;
23 cin >> salary;
24 total = total + salary;
25
26 cout << “ Enter a salary: “;
27 cin >> salary;
28 total = total + salary;
29
30 cout << “ Enter a salary: “;
31 cin >> salary;
32 total = total + salary;
33
34 cout << “ Enter a salary: “;
35 cin >> salary;
36 total = total + salary;
37
38 avgSal = total / 5;
39
40 cout << “\nThe average salary is: “;
41 cout << avgSal;
2. What is the output for the coding below and the correct value for variable cnt after
execution? (2 marks)
16 int cnt, total = 0;
17
18 cnt = 1;
19 while (cnt <= 5)
20 {
21 total += cnt;
22 cnt++;
23 }
24 cout << total << endl;
10
QUESTION 3 (10 MARKS)
1. Given the following function definition, complete the missing coding statement.
(8 marks)
16 // Average( )
17 // Accepts three floating point grades as parameters
18 //calculates and returns the average
19
20 float (a)________((b)_______ (c)________, (d)_______ test2, float (e)_________)
21 {
22 (f)________________; // declare avg and the type is float
23 avg = (g)__________________________; // calculate average of 3 tests
24 return (h)____________; //return average
25 }
2. Given the following function definition, write the function calling. (2 marks)
Function Definition Funtion Call
a) 13 float getSqrt (float a)
14 {
15 return sqrt (a);
16 }
11
QUESTION 4 (10 MARKS)
3. Write a C++ program fragment that converts the currency amount from Malaysian
Ringgit to Indonession Rupiah, Singapore Dollar, Thailand Baht, Brunei Dollar and
Philippine Peso. (10 marks)
a) Function ringgitToRupiah( ) that receives the amount in ringgit as parameter
and returns the equivalent value in rupiah. (Note: 1 ringgit = 3345.40 Rupiah)
12
QUESTION 5 (10 MARKS)
1. Differentiate and give example between local variable and global variable (6 marks)
2. Refer to the C++ code segment below: (4 marks)
#include <iostream>
using namespace std;
int g = 0;
void fun ()
{
int a = 5;
g = g + a;
cout << g << endl;
}
int main ()
{
g = 15;
fun ();
g++;
cout << g << endl;
}
13
PART C: FULL PROGRAMMING QUESTIONS (20 MARKS)
Instruction: Write a complete C++ Program
Write a complete C++ program that calculate the payment for various fuels at a petrol
stations. This program contains the following:
1) The Function Prototype (3 Marks)
2) The Function Definitions (9 Marks)
(a) Function calcPetrol( ) that receives the amount in liters. Calculate and return the
total costs of petrol.
(b) Function calcDiesel( ) that receives the amount in liters. Calculate and return the
total costs of diesel.
(c) Function calcGas( ) that receives the amount in liters. Calculate and return the total
costs of gas.
3) The Main Function (8 Marks)
A main program that uses the above functions. The user will enter the amount of fuel in
liters and selects the desired fuel type from the following Table 1:
CHOICE FUEL TYPE
1 Petrol
2 Diesel
3 Gas
4 Exit
Table 1: Choice and Fuel Type
The program will compute and display the total cost of fuel purchased. Display the
appropriate message when the user enters any invalid choice. (Note: - 1 liter of petrol:
RM2.85, 1 liter of diesel: RM2.60, 1 liter of gas: RM0.50)
14