CMPG 121 Exam - Example Theory Questions
CMPG 121 Exam - Example Theory Questions
1) Evaluate the below operator and provide the output results such that if:
A) 4 B) 5 C) 6 D) 0
A) 2 B) 4 C) 1 D) 0
A) 1 B) 20 C) 0 D) 2
1
4) Consider the below example operator and select the output results: if:
A) 3 B) 5 C) 0 D) 1
5) Complete this program, which declares and initializes a pointer and modifies the value it
points to:
Fill the correct answer in the ______ below (4 marks):
int x = 10;
int _____ptr = ______;
______ = 20;
cout << "Value of x: " << ______ << endl;
2
Section B Total = ___/46
1) In this program, we have a calculateDiameter function that takes the radius of the circle as a
parameter and returns the diameter. In the main function, we get the radius from the user.
Complete the program, call the calculateDiameter function to compute the diameter, and then
display the result using source code?
#include <iostream>
#include <cmath>
_______________________________________
}
int main()
{
double radius;
if (radius < 0)
{
cout << "Radius cannot be negative." << std::endl;
}
else
{
__________________________________________________________
___________________________________________________________
return 0;
}
3
2) Consider the below source code that declares and assign the element to the array x and the pointer
ptr points to the index element from the array list. When the below source code is compiled, it
generates the output results on the right, provide the output results:
*ptr = _______________
*(ptr+1) = _______________
*(ptr-1) = _______________
*(ptr+2) = _______________
*(ptr+3) = _______________
*ptr = _______________
*(ptr+1) = _______________
*(ptr-1) = _______________
*(ptr+2) = _______________
*(ptr+3) = _______________
*ptr = _______________
*(ptr+1) = _______________
*(ptr-1) = _______________
*(ptr+2) = _______________
*(ptr+3) = _______________
4
3) Consider the declaration of an array, suppose n is an array of 10 integers that output each array
element's value. When the below code is compiled and executed, it produces the results on the
right, provide the output of the source code:
4) Consider the below source code that declares an array balance of 5 elements, and a pointer p
which points to the array balance. When the below source code is compiled and executed; it
produces the output on the right: Provide the output results:
5
5) Below is the C++ source code that uses a struct to represent a person's information and displays
it. Complete the following source code and display the person's information. See output
example.
Fill the correct answer in the _____ below, and output section (5 marks):
int main() {
// Create an instance of the Person struct
Person person = {"John Doe",30, "123 Main Street"} ;
return 0;
}
6
6) Consider the below C++ source code that declares two integer variables, value1 and value2,
which will be used to store user-inputted values. Write the swapValues function that takes two
integer pointers as arguments and swaps the values stored at those memory locations. The
function effectively exchanges the values of value1 and value2.
Source code
#include <iostream>
using namespace std;
int main()
{
int value1, value2;
cout << "Enter value1: ";
cin >> value1;
cout << "Enter value2: ";
cin >> value2;
cout << "Calling swapValues " << value1;
cout << ", " << value2;
cout << "\n\n";
swapValues(&value1, &value2);
cout << "Value1 is now: " << value1;
cout << " and Value2 is now: " << value2;
return 0;
}
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
7
7) Consider the below source code and evaluate the logical operators:
Source Code
a) (1)__________
b) (2)__________
c) (3)__________
8) Consider the following source code and evaluate the value of x and y;
Source code
1) x= ______ y= ______