0% found this document useful (0 votes)
37 views

CMPG 121 Exam - Example Theory Questions

Example theory questions on c++

Uploaded by

koketsomolete880
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)
37 views

CMPG 121 Exam - Example Theory Questions

Example theory questions on c++

Uploaded by

koketsomolete880
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/ 8

CMPG 121 - Exam Example Questions

Section A Total = ___/22

1) Evaluate the below operator and provide the output results such that if:

Mark the correct answer with an X (2 marks):

A) 4 B) 5 C) 6 D) 0

2) Consider the below statement and provide the output:

Mark the correct answer with an X (2 marks):

A) 2 B) 4 C) 1 D) 0

3) Consider the below statement and select the output, if:

Mark the correct answer with an X (2 marks):

A) 1 B) 20 C) 0 D) 2

1
4) Consider the below example operator and select the output results: if:

Mark the correct answer with an X (2 marks):

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?

Fill the correct answer in the ______ below (5 marks):

#include <iostream>
#include <cmath>

// Function to calculate the diameter of a circle


double calculateDiameter(double radius)
{
_______________________________________

_______________________________________
}

int main()
{
double radius;

cout << "Enter the radius of the circle: ";


cin >> 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:

Fill the correct answer in the ______ below (6 marks):

Source code Output

*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:

Fill the correct answer in the output section (5 marks):

Source code Output

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:

Fill the correct answer in the output section (5 marks):

Source code Output

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):

Source code Output example


#include <iostream>
#include <string>

using namespace std;

// Define a struct to represent a person


struct Person
{
std::string name;
int age;
std::string address;
};

int main() {
// Create an instance of the Person struct
Person person = {"John Doe",30, "123 Main Street"} ;

// Display the person's information


_____________________________________________

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.

Fill the correct answer in the ______ below (5 marks):

Source code
#include <iostream>
using namespace std;

void swapValues(int*, int*);

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:

Fill the correct answer in the ______ below (6 marks):

Source Code

What is the output of the statements:

a) (1)__________
b) (2)__________
c) (3)__________

8) Consider the following source code and evaluate the value of x and y;

Fill the correct answer in the ______ below (4 marks):

Source code

1) x= ______ y= ______

You might also like