0% found this document useful (0 votes)
17 views19 pages

Midsemester Examination Uscs22(s)

The document outlines the details for the Mid Semester Examination for the course 'Computer Programming II' at INTEC Education College, scheduled for October 10, 2024. It includes various sections with true/false questions, multiple choice questions, and short answer questions related to programming concepts, pointers, recursion, and file handling in C++. Additionally, it contains instructions for answering and examples of code segments to analyze.

Uploaded by

Dayana Zohari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views19 pages

Midsemester Examination Uscs22(s)

The document outlines the details for the Mid Semester Examination for the course 'Computer Programming II' at INTEC Education College, scheduled for October 10, 2024. It includes various sections with true/false questions, multiple choice questions, and short answer questions related to programming concepts, pointers, recursion, and file handling in C++. Additionally, it contains instructions for answering and examples of code segments to analyze.

Uploaded by

Dayana Zohari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

INTEC EDUCATION COLLEGE

MID OF SEMESTER EXAMINATION

E XAMINATION DETAILS MID OF SEMESTER EXAMINATION

Course code USCS22

Course name COMPUTER PROGRAMMING II

Date 10 OCTOBER 2024


Time 9.00 – 11.00 AM
Total no. of pages (incl. cover sheet) 19

ANSWER SCHEME
CONFIDENTIAL

SECTION A: TRUE OR FALSE QUESTIONS


Instruction: Circle the correct answer.

1. Refer to the code snippet below, it shows that j now points to b. (1 mark)

double b=70.5, c=29.99;


double *j=&c, *k=&b;
j = k

A. True

B. False

2. Declare the variable int i; double *dp = &i; are legal when initializing
the pointer. (1 mark)

A. True

B. False // You cannot assign the address of an int variable to a pointer to double without
type casting, as this would cause a type mismatch.

3. The function that can call itself and another function is recursion. (1 mark)

A. True

B. False
Recursion specifically refers to a function calling itself, not necessarily another function.

4. Recursion with void data type needs to return value when it is created. (1 mark)

A. True

B. False

5. The new operator is used to dynamically allocate memory on the stack in C++.

(1 mark)
A. True

B. False // on heap

2
CONFIDENTIAL

6. Pointer to constant means it cannot change the value pointed by variable but
can change pointer itself. (1 mark)

A. True

B. False

7. The contents of the second argument are copied to the memory location
specified by the first argument, including the null terminator is called strcat
function. (1 mark)

A. True

B. False

8. Referring to the code segment below, the output is: Number is 200000 96
3000. (1 mark)

char num1[ ] = “200000 96 3000”;


long int number = atol(num1);
cout<< “Number is ”<<number<<endl;

A. True

B. False

9. Sofea writes the code below using the structure in C++ program. Identify
that Sofea do the correct ways. (1 mark)

struct Animal
{
string color=”black”;
int year = 2;
double price;
}

A. True

B. False
3
CONFIDENTIAL

10. If a file stream object (ifstream, ofstream, or fstream) is not explicitly closed
using the .close() function, the file will remain open until the program
terminates, potentially leading to memory leaks. (1 mark)

A. True

B. False

Explanation: If a file stream object (such as ifstream, ofstream, or fstream) is not explicitly closed using the
.close() function, the file will automatically be closed when the stream object goes out of scope or when the
program terminates. This is because the file stream object’s destructor will handle the closing of the file.
While it is good practice to explicitly close the file using .close(), it will not cause memory leaks or leave the file
permanently open as the program will handle this cleanup. However, explicitly closing the file earlier is
sometimes important if further operations on the file or stream are required.

4
CONFIDENTIAL

SECTION B: MULTIPLE CHOICE QUESTIONS


Instruction: Circle the correct answer.

1. The W is a structure pointer and Q is a member. This expression accesses the Q


member of the structure pointed to by W. Write this expression. (1 mark)

A. W -> Q

B. *W -> Q
C. *(*W).Q

D. Q -> W

2. Refer to the code segment below. Identify what is the output. (1 mark)

struct Bag {
string brand;
string color;
int year;
};
int main(){
Bag a = {“Myvi”,”Green”,2020};
cout<<a.color<<endl;
return 0;
}

A. Myvi

B. Green
C. 2020
D. Toyota

3. Identify what is the correct code to mention recursion with a returning value.
(1 mark)

A. void display (num1,num2){}

B. int display (int num1,int num2){}


C. int main(){}
D. void number (int no, char s){}
5
CONFIDENTIAL

4. Identify the error(s) in the code below. (1 mark)

int main(){
int SIZE =50;
char name [SIZE] = "This is my first programming";
char name2 [] ="I am so happy to learn this subject";
stringcat (name;name2);
cout<<name<<endl;
return 0;
}

A. missing semicolon.
B. int size [] need to put const variable.
C. stringcat and semicolon in a parenthesis.
D. must call the name and name2 as an output.

5. Ignore the errors in the question 4. Identify what is the possibility of the output.
(1 mark)

A. This is my first programmingI am so happy to learn this subject


B. This is my first programming I am so happy to learn this subject
C. I am so happy to learn this subject
D. I am so happy to learn this subject This is my first programming

6. Identify which of the following statements is TRUE. (1 mark)

A. Recursion is always better than iteration.

B. Recursion uses more memory compared to iteration.


C. Recursion uses less memory compared to iteration.

D. Iteration is always better and simpler than recursion.

6
CONFIDENTIAL

7. State which functions below can convert a floating-point number to a string in


C++. (1 mark)

A. stod()

B. to_string()
C. atoi()
D. atof()

8. Consider the following statements:

int *p; int i, k; i = 142; k = i; p = &i;

Identify which of the following statements changes the value of i to 143.


(1 mark)
A. k = 143;

B. *k = 143;
C. *p = 143;
D. p = 143;

9. Consider the following code below. Identify what will be the output. (1 mark)

int main(){
int a =0;
int *b =&a;
 *b = 30;
cout<<a<<endl;
cout<<b<<endl;
return 0;
}

A. 30 and 0

B. 0 and 30
C. 0 and address
D. 30 and address

7
CONFIDENTIAL

10. In C++, identify which of the following operations will fail if a file is opened in
read mode using an ifstream object and the file does not exist. (1 mark)

A. The file stream will be set to a failed state.

B. The program will create a new empty file.


C. The file pointer will be positioned at the end of the file.

D. The program will automatically switch to write mode to create the file.

8
CONFIDENTIAL

SECTION C: SHORT ANSWER QUESTIONS


Instruction: Answer all the questions in the space provided.

QUESTION 1

(A) Consider the following lines of code:

int digit1 [10] = {50, 55, 60, 65, 70, 75, 80, 85, 90, 95};

int *digit2;

digit2 = digit1;

index 0 1 2 3 4 5 6 7 8 9

digit1 50 55 60 65 70 75 80 85 90 95

address 0xx 1xx 2xx 3xx 4xx 5xx 6xx 7xx 8xx 9xx

(i) Identify what is the value of (digit1+5). (1 mark)

Answer:

5xx

(ii) Identify what is the value of digit2[6]. (1 mark)

Answer:

80

9
CONFIDENTIAL

(iii) If we add the following line of code,

digit2 += 4;

determine the value of &(( digit2 – 2 )[ 4 ] ) (2 marks)

Answer:

6xx

(B) Write the output of the following program segment. Draw the memory diagram on
the Stack/Heap diagram provided. You may assign random memory address to
your variables/values. (5 marks)

int *p1, *p2, *p3; STACK HEAP


p1 = new int;
*p1 = 10;
cout<< *p1<<endl;
p2 = new int;
*p2 = 20;
cout<< *p2<<endl;
p3 = p2;
*p3 = *p3**p2**p1;
cout<< *p3<<endl;

//1m for stack, 1m for heap

Output:

10
20
4000
//1m for correct value

10
CONFIDENTIAL

QUESTION 2

(A) In the C++ program, we can read and write files directly with the fstream
library. Consider the code below and draw the output. (3 marks)

#include <iostream>
#include <fstream>
using namespace std;

int main(){
ofstream fileCode;
fileCode.open("Programming.txt",ios::out);
fileCode<< "Happy MERDEKA Day to all my Nation
\nAppreciate our freedom! ";
fileCode.close();
return 0;
}

Answer:

//1m for correct draw in text file, 2m for content

11
CONFIDENTIAL

(B) Write a C++ program that reads value from the user using getline()function.
Your program should use appropriate numeric conversion function to solve the
problem. Below is an example of output: (6 marks)

Please enter the price of the t-shirt:


RM 250.40
Please enter the quantity of the book:
10
The total price you should pay is:RM 2500

Answer:

#include <iostream>
#include <string>
#include <cstring> //1m
#include <cstdlib>
using namespace std;
int main() {
char price [5],quantity [5]; //1m
double total;

cout<<"Please enter the price of the t-shirt: \nRM ";


//1m
cin.getline(price,50);
book
cout<<"Please enter the quantity of the t-shirt: \n";
cin.getline(quantity,50); //1m

total = atoi(price)*atoi(quantity);
cout<<"The total price you should pay is:RM "<<total<<endl; //2m

return 0;
}

12
CONFIDENTIAL

QUESTION 3

(A) Refer to the code segment below, and determine which code is illegal or legal
based on const and pointer. Explain your answer. (4 marks)

int main() {
char j = 'J', k= 'K';
const char *ptr = &j;
char *const ptr1 = &j;
const char *const ptr2 = &j;
//Code 1
*ptr= k;
cout << "Value pointer to by ptr1" <<*ptr << endl;
//Code 2
*ptr1= k;
cout << "Address ptr1 is" <<ptr << endl;
//Code 3
ptr2= &k;
cout << "Value ptr2 is" <<ptr2;
//Code 4
cout << "Address ptr2 is" <<ptr2;
return 0;
}

Answer:
Code 1:illegal //cannot change value pointed by ptr1, can change the pointer itself. ptr=&k
Code 2:legal // cannot change the pointer, but can change the value pointed by ptr
Code 3:illegal //read only variable ptr2
Code 4:legal// you can neither change the value pointed by ptr nor the pointer ptr .

13
CONFIDENTIAL

(B) In the C++ language, there is a precise distinction made between a declaration
of a function and a definition of a function. Briefly describe the distinction.
(4 marks)

Answer:

In C++, a function declaration gives the function name, result type, and number and types of
parameters. This is the function prototype, and it may be repeated or included in as many separate
source files as necessary. (2m)

A function definition is the actual function, containing the code that makes up the body of the
function. It must appear exactly once in the program.(2m)

14
CONFIDENTIAL

(C) (i) Referring to the C++ code below, write the output. (3 marks)

#include <iostream>
#include <cstring>
using namespace std;
int main() {
const int SIZE = 100;
char str1[SIZE] = "Happy holidaysguys!!";
char str2[SIZE] = "Happy back to class";
int result = strcmp(str1, str2);

if (result == 0) {
cout << "The strings are equal." << endl;
} else if (result < 0) {
cout << "The first string is less than the second
string." << endl;
} else {
cout << "The first string is greater than the
second string." << endl;
}
strcpy(str2, str1);
cout << "Copied String: " << str2 << endl;
return 0;
}

Answer:

The first string is greater than the second string.


Copied String: Happy holidaysguys!!

(ii) Refer to the code in C(i), add the code to display the length of the str1. (1 mark)

Answer:

cout << "Length of first string: " << strlen(str1) << endl;

15
CONFIDENTIAL

QUESTION 4

(A) Write a C++ program that can read the factorial of the non-negative number using
recursion. Your program should apply ONE (1) function to return value to solve the
problem. The input for the number is to be obtained from the user. (6 marks)

Please enter a number to be calculate: 10


Sum of your number 10 is = 55

Answer:

// Factorial of n = 1*2*3*...*n

#include <iostream>
using namespace std; //0.5m

int sum(int n); //0.5m

int main() {
int n, result; //0.5m
//1m
cout << "Please enter a number to be calculate: ";
cin >> n;

result = sum(n); //0.5m


cout << "Sum of your number " << n << " is = " << result; //1m
return 0;
}
//2m
int sum(int n) {
if (n != 0) {
return n + sum(n - 1);
} else {
return 0;
}
}

16
CONFIDENTIAL

(B) List TWO (2) advantages and TWO (2) disadvantages of using C++ recursion.
(4 marks)
Answer:

//2points – advantages, 2points - disadvantages

17
CONFIDENTIAL

SECTION D: LONG STRUCTURED QUESTION


Instruction: Write the full C++ code.

Illustrate and write a full C++ program to access all members of a structured

Stationery. Your program must use the member access operator (.). You would use

the struct keyword to define variables of structure type. Implement at least TWO (2)

different ways to access the members of the Stationery objects using pointers. Your

program needs to calculate the total price of the Stationery and display the output.

(15 marks)

Sample Output:

Enter the name of the stationery: Ruler


Enter the price of the item: RM 1.50
Enter the quantity of the item: 10

Stationery Item Details:


Name: Ruler
Price: RM1.50
Quantity: 10
Total Price: RM15

18
CONFIDENTIAL

Answer:

#include <iostream>
#include <string> 1m mark include main() and close
using namespace std;

// Define the Stationery structure


struct Stationery {
string name; 2m
float* price; // Pointer for price
int quantity;
};

int main() {
// Create an instance of Stationery //0.5m
Stationery item;

// Dynamically allocate memory for price


float priceValue;
item.price = &priceValue; // Assign pointer to priceValue 1.5m
Stationery *ptr = &item;
// Get user input
cout << "Enter the name of the stationery item: ";
getline(cin, item.name); // Use getline for string input
cout << "Enter the price of the item:RM "; 4.5m
cin >> *(*ptr).price; // Input price through pointer
cout << "Enter the quantity of the item: ";
cin >> ptr->quantity;

// Calculate total price


float totalPrice = *(item.price) * item.quantity; //1m

// Output the details


cout << "\nStationery Item Details:" << endl;
cout << "Name: " << item.name<< endl;
cout << "Price: RM" << *(*ptr).price << endl; // Accessing price via pointer 4.5m
cout << "Quantity: " << ptr->quantity << endl;
cout << "Total Price: RM" << totalPrice << endl;

return 0;
}

END OF QUESTION PAPER

19

You might also like