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

Cmpg121 Exam Notes

This document provides information about data types, naming conventions, operators, comments, and functions in C++. It discusses: - The main data types (string, integer, double, boolean) and examples of each. - How to name variables using camelCase and prefixing with the lowercase of the data type (e.g. sMyName for a string). - Common operators like && and ||. - Escape sequences like \n and \t. - How to write single and multi-line comments. - Many common math and string functions along with examples of how to call them.

Uploaded by

otheliamkhatshwa
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)
102 views

Cmpg121 Exam Notes

This document provides information about data types, naming conventions, operators, comments, and functions in C++. It discusses: - The main data types (string, integer, double, boolean) and examples of each. - How to name variables using camelCase and prefixing with the lowercase of the data type (e.g. sMyName for a string). - Common operators like && and ||. - Escape sequences like \n and \t. - How to write single and multi-line comments. - Many common math and string functions along with examples of how to call them.

Uploaded by

otheliamkhatshwa
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/ 30

CMPG 121

Anrich Kruger.
The Basics : Data types and naming conventions.
• String : Any textual data( singe character or sentences and some numbers) e.g “My text”, “Hello world”, “A”, ”B”, ”C” some
examples of where we use string with numbers are cellphone numbers “079 823 4324” and ID numbers
“0301045728235”, cellphone numbers and ID numbers are ALWAYS string.
• Integer : Any number from -2147483648 to 2147483648 ( not including fractions or decimals).
• Double : Any decimal numbers (decimals;fractions etc.). (Tip : If you think you might have decimal values rather use
double then integer!)
• Boolean : True(1) or False(0), 0-represents “false” and 1-represents “true”.

How I name my variables :


Camelcase : The use of a capital letter to begin the second word in a compound name or phrase when it is it not separated
from the first word with a space.
I also start the name of the variable with the lowercase of the data type.

• String : if the var is a string I would name it something like this sMyName.
• Integer : if the var is int I would name it something like this iSomeValue.
• Double : if the var is double I would name it something like this dSomeFloatVal.
• Boolean : if the var is bool I would name it something like this bFlag1.
Different operators:

Also we use && for AND and || for OR.

Escape sequences:
• \b : Backspace.
• \n : Newline.
• \t : Horizontal tab.
• \\ : The \ character.
• \’ : Single quotation mark.
• \” : Double quotation marks.

Comments and multiline comments:


• // : single line comment.
• /* */ : multi-line comment.
Math Functions:
REMEMBER TO #include <cmath>
• abs(x)-Returns the absolute value of x
• acos(x)-Returns the arccosine of x
• asin(x)-Returns the arcsine of x
• atan(x)-Returns the arctangent of x
• cbrt(x)-Returns the cube root of x
• ceil(x)-Returns the value of x rounded up to its nearest integer
• cos(x)-Returns the cosine of x
• cosh(x)-Returns the hyperbolic cosine of x
• exp(x)-Returns the value of Ex
• expm1(x)-Returns ex -1
• fabs(x)-Returns the absolute value of a floating x
• fdim(x, y)-Returns the positive difference between x and y
• floor(x)-Returns the value of x rounded down to its nearest integer
• hypot(x, y)-Returns sqrt(x2 +y2) without intermediate overflow or underflow
• fma(x, y, z)-Returns x*y+z without losing precision
• fmax(x, y)-Returns the highest value of a floating x and y
• fmin(x, y)-Returns the lowest value of a floating x and y
• fmod(x, y)-Returns the floating point remainder of x/y
• pow(x, y)-Returns the value of x to the power of y
• sin(x)-Returns the sine of x (x is in radians)
• sinh(x)-Returns the hyperbolic sine of a double value
• tan(x)-Returns the tangent of an angle
• tanh(x)-Returns the hyperbolic tangent of a double value
• max(x,y)-Returns the highest value of x and y
• min(x,y)-Returns the lowest value of x and y
• sqrt(x)-Returns the square root of x
• round(x)- Rounds the value of x
• log(x)-Returns the log value of x

Other functions we have used(From practicals):


REMEMBER TO #include <string>
• stringVariable.size();-Returns the length of a string as an integer value
• Strcpy()-Copy string to another string.
• stringVariable.compare()- compares two string lexicographically and returns an integer representing the result.
REMEMBER TO #Include <cctype>
• isalnum(charVar)-Checks if a given character is alphanumeric
• isalpha(charVar)-Checks if the character is alphabetic
• isdigit(charVar)-Checks if the character is a digit
• tolower(charVar)-Converts character to lowercase
• toupper(charVar)-Converts character to uppercase
REMEMBER TO #include <cstdlib>
• rand()- Generates a random number
• srand()- Used to initialize random number generators
REMEMBER TO #include <string.h> | #include <cstring>
• strlen(charVar)-Gives the length of a string
• strcpy(charVar,Value to copy)-Copy’s a string value and saves it in a variable/array.

• strcat(string1,string2)-Concatenates two string values.
• strcmp(stringX,stringY)-Compares two string values with each-other.

Quick-breakdowns.
Quick type conversation breakdown:
(DataType)expression
Example
• (double)integerVar
Quick self defined Function breakdown:
Format : return data type/void FunctionName()
{
some code.
}
• Not all functions has parameters, if sed function has parameters
include them inside the braces, i.e. (string name,int age).
• If a function has a void type it does NOT return any value.
• If a function has a data type it returns a value based on the data type.
• Functions can be declared above the main() function (no calling is needed), should a function be declared under the main
function you need to call it before the main function to be able to use it(ill show this in an example).
• Tip, if you use a function with an array ALWAYS create/declare the function above the main function.

Quick array breakdown:


Format : dataType arrayName[size] = {value1,value2,value3…} (thats saying if the array is already populated)
• Array index always starts at 0.
• Arrays store multiple values of the same data type under a single variable.
• Arrays can have any dataType.

Quick breakdown of pointers:


What is a pointer ?
Pointers are variables that hold addresses of other variables. Not only can a pointer store the address of a single variable, it
can also store the address of cells of an array.
• Declaration : int *pointer or int* pointer (they are the same).
• The & is a reference operator, prefixing this will result in the address of sed variable.
Pointers with arrays:
So lets say for instance we are looking for the first index of an array, thus arrVariable[0] the pointer equivalent would be
*pointer = arrVariable, since *pointer points to the first address of an array. What if we want to get other values in the array?
Well we could then say *pointer + 1 and so forth depending on the index of the array we want. In a loop situation we would
use i, as the loop iterates and i increases the pointer will also point to the relevant array index.
Quick breakdown of file handling :
Files are permanent storage.
• “w” - Open a text file for write operations
• “a” - Open a text file for append operations
• “r” - Open a text file for read operations
Make sure the file is in the same folder as your main.cpp file!
It’s not necessary to check if a file exists when writing to a file.
There are a couple of ways on how to handle files, i prefer the #include <fstream> method:
• ifstream varName(“Filename”) - reads from the file.
• ofstream varName(“Filename”,ios::app) - appends to a file.
• ofstream varName(“Filename”) - writes to a file.
Note if you want to append to a file and forget the ios::app, it clears the text inside the file rewrites the file with the new text.
Basic format :

Reading from file:


Ifstream varName(“Filename”)
If file,is_open()
{
do code…
varName.close()
}
else
{
Show error message.
}

Writing/appending to file:
ofstream varName(“Filename”) (remember ios::app when appending!)
cout<<“Message you want to write”
varName.close()

Quick breakdown of Structs :


Structs are collections of different, related variables. A way to group several related variables into one place.
Format:
struct Human
{
int age;
int height;
};

Calling a struct
Assign variables to the struct
Int main()
{
Human joe;
joe.age = 16;
joe.height = 179;
}
Structs and arrays:
Struct Dog
{
int age;
Char name[20];
};
Int main()
{
Dog myDog[5];
MyDog.age = 3;

Quick breakdown for binary files:


Binary files are files mainly used for storing records just as
we store record in a database. It makes it easier to access or modify the record easily.
• Not human readable.
• Binary files are faster. No conversion required as with text files.
• Takes less space than text files.
Binary file opening modes(Most used in our work):
• ios::binary | ios::in - Used to open a file in binary mode for reading. The file pointer is placed at the beginning of the file.
Default mode if no specification is made.
• ios::binary | ios::out - Used to open a file in binary mode for writing. If the file does NOT exist it WILL BE CREATED. If the
file already exists it will be OVERWRITTEN. File pointer is placed at beginning of file.
• ios::binary | ios::app - Used to open a file in binary mode for appending. If the file does NOT exist it WILL BE CREATED. If
the file already exists, all new data will be added to the END of the file.

Basic format:
Writing:
#Include <fstream> ———————Very important, DO NOT FORGET.
ofstream VarName(“Filename.dat”, ios::out | ios::binary);
If(!VarName)
{
cout<<“Cannot open file!”<<endl;
return 1;
}
VarName.write(reinterpret_cast<char*> Memory Adress, sizeof(Variable));
//.write takes two arguments, one character memory address and the size of bytes you need to write!
VarName.close(); //Remember to CLOSE the file!.

Reading:
#Include <fstream> ———————Very important, DO NOT FORGET.
Ifstream VarName(“Filename.dat”, ios::in | ios::binary);
VarName.read(reinterpret_cast<char*>(Variable),sizeof(Variable)); //Same go’s with regards to parameters as writing.
VarName.close(); //Remember to CLOSE the file!
How I interpret code snippets! Mock exam analysis and
explanation!
Section A:
1.

If we look at the two integer values x and y we can see that y has the higher value. Then we have a bool(true or false) variable
called isEqual which is equal to the evaluation test of x and y, “==“ is used to test if the values are the same, which they are
not because 8 is greater than 5. The output is the string expression and the bool value of the isEqual variable, which in this
case is false. We know that 0 represents false thus, 0 is the correct answer.

2.

If we look at the two variables x and y we can see that x has a higher value. Then we have a bool(true of false) variable called
isNotEqual which is equal to the evaluation test that x is not equal to y, “!=” is used to test if they are not equal, which they are
not because 10 is not equal to 8. The out is the string expression and the bool value of the isNotEqual variable, which in this
case is true. We know that 1 represents true thus, 1 is the correct answer.
3.

If we look at the two variables x and y we can see that y has a higher value. Then we have a bool(true or false) variable called
isLessThan which is equal to the evaluation test that tests if the value of x is less than that of y, “ < “ is used to test if one
variable is less than the other. In this case x = 4 is less than y = 8, thus the test will result in the correct answer of 1.

4.

If we look at the two variables x and y we can see that x has a higher value. Then we have a bool(true or false) variable called
isLessThanOrEqual which is equal to the evaluation test that tests if the value of x is less or equal to the value of y,”<=“ is
used to test if one variable is less or equal to the other, in this case x is not less or equal to y. We know the answer is false
because 50 is not less nor equal to 8 thus, the correct answer is 0.
5.

First let’s evaluate the question. We are asked to declare and initialize a pointer and modifies the value it points to. In the first
space we will be add the “*” to declare the pointer variable, in the same line we add the &x which gets the memory address of
the variable x and assigns it to the pointer. The third space we need to fill in we add the *ptr variable we just declared, this
piece of code accesses the value at the memory address stored in *ptr and modifies the value at that memory location. In the
output we add the *ptr variable once again. It will print : Value of x: 20

6.

The first function is a basic bubble sort, it swaps the values around so that the one value takes the place of the other using a
temporary variable to store the value until it is assigned its new position. Thus, the first blank we need to fill in we just fill in *a.
Then on the next line we will say *a = *b so that *a takes the value of *b. In the final line we will say *b = temp, which assigns
the origanl value of *a to *b since the value of *a is stored in the temp variable. This swaps the values of *a and *b around.
In the main function we just fill in &x,&y in the brackets. The “&” is used to pass the memory addresses of the values x and y.
7.

From the question we can read that the program passes an array to a function thus giving us the first answer in the bracket of
the modifyArray function so the first part of the bracket answer is int arr[]. The second part of the bracket is fairly simple, if we
look at the for loop there is a size variable that is not declared anywhere else thus the second part of the bracket is int size,
since the size variable is not declared anywhere else it should be an argument to the array. When looking inside the for loop
we can see the “*=“ operator, which is short hand for multiplication. The question isn’t very specific on what the modification of
the array should be so we can assume the second two blank spaces are arr[i] *= arr[i]. Inside the main function we can see
the incomplete function which we should just finish by adding (arr,5). This passes the original array and the size, which we get
by counting the values in the array since no size is specified. The output will be the squared version of each array value since
it is 1x1, 2x2, 3x3,…

A tip!
If you don’t know what the question is looking for, look at what is given and what is used. For instance there might be a
variable being used but it’s not declared anywhere, that could be an answer you are looking for. Also look at what is omitted,
there might be brackets or an “,” or an “;” left out. These could also be a mark so if those are not included add them for safety!
Should any assignment or comparison operators be left out look at the question and what they are looking for, maybe you
need to add a less or greater operator. This is all subjective to the question, the golden rule is, if something is left out but you
know it should be there add it for safety, but don’t go and edit parts of the code that does not include a space for an answer!
Section B:

The first part of the question can be done in 2 different ways, you can either immediately say return 2* radius; OR the method
I prefer is you can say radius = 2*radius; in the next line: return radius; Moving on the main function we can see an if-else
statement where the if clause is used for error testing. Thus in the else part we should initialize a
double diameter = calculateDiameter(radius); as our first answer, this assigns the return value of the function to a variable.
Following the variable assignment we can output the value for the final mark by saying
cout<<“The diameter of the circle is: ”<<diameter<<std::endl; NOTE: use std:: where they use it in there code if it is used!.
2.

For this question we only need to write down the output of pieces of code they ask for. But to write the outputs down we first
need to understand the code we are looking at. It starts off with the declaration of an array called x with a size of 5. Then a
pointer variable is declared. This variable is assigned to the address of the index element of the array, basically saying it is
pointing to the value in the array at index 2 which is 3(remember arrays start with the index 0) thus the array is pointing to the
value at index 2 in the array. The pointer is then given the value of 10 which modifies the value of 3 to 10. Now we can start
writing the answers down. The first answer is 10 since the *ptr is assigned the value 10. The next answer is 5, why? Because
the pointer is pointing to the value at index 2 so incrementing that value by one moves the pointer to the next value in the
array which is 5. Following the same logic the next answer is 2 since we are now decrementing the value. The next answer is
6 because we are increasing the index value by 2. The final answer is 0, since the array is of size 5 and we are already at the
second index, incrementing it by 3 means we are pointing to a value outside the range of the array which does not exist. The
rest of the answers follow the same logic and give the same answers except for the first answer by each which is the value of
the pointer which changes each time. Thus the rest of the answers are, 6,5,2,6,0 and 8,5,2,6,0 respectively.
3.

This question is not as hard as it seems, after you have looked at the code. So the question tells us that we have an array of
10 integers. This array is also an empty array since no values are predefined, which means we need to populate it before
being able to print whats inside the array. In the main function we can se the declaration of the array called n with a size of 10,
and two integer values i and j which have no values assigned. Following is the first for loop which assigns i the value of 0 and
says do this whilst i is less than 10, and increment i by one with each iteration. Inside the loop we can see that the n array at
index i (which is 0 thus the first index of the array) is assigned the value of i + 5. NOW is where the math comes in, i is 0, 0 +5
is 5 thus the first value of the array is 5. Now i increments by one with each iteration so the second value in the index is 1 +
5,thus the second value of the array is 6. This pattern continues 10 times to populate the array n of size 10. Now remember i
is 0 and the for loop runs whilst i(0)<10 thus it runs 10 times not 9 because i starts with the value 0. The second for loop just
prints the values of the array as output, which is also what we need to answer. Note the endl, thus the answers will print in a
new line each time. For the sake of space i’m going to be writing them next to each other.
5(0+5); 6(1+5); 7(2+5); 8(3+5); 9(4+5); 10(5+5); 11(6+5); 12(7+5); 13(8+5); 14(9+5).
4.

The question tells us that, there is an array of 5 elements and a pointer variable that points to the array. When looking at the
code we can see the declaration of the array which is populated, the declaration of the pointer variable and the declaration of
an unassigned variable i. Following the declaration, we can see the pointer being assigned to the array. Remember a pointer
unless otherwise specified points to the first index of an array. Following the assignment we have a for loop, but take note i is
assigned the value 4 and the test part of the loop is saying do this whilst i is greater or equal to 0, and then decrement i by
one. This indicates the loop works in reverse. Inside the loop we can see that it prints the output string and the value of the
pointer + i, basically saying print the value of the array equal to the pointer value + i. Now remember the pointer is pointing to
the value of the first index but in the for loop we are saying, ok but now point to the value at the first index + i. Thus for the first
iteration it is saying point to the 4th index value which is 50, and i decrements i with each iteration, thus we are printing the
array from the back. The answers are as follows:
*(p+4) = 50
*(p+3) = 17
*(p+2) = 45
*(p+1) = 5
*(p+0) = 3
5.

This question has 2 parts, We need to fill in the blanks as well as write down the expected output. From the question we know
we are working with a struct. We can see the struct has three members. Inside the main we can see the members of the struct
being assigned relevant values. Remember struct members can be assigned the way they did it in the given code, you don’t
need to use the = method. The way this works with curly braces is, the value gets assigned in the order the members are
declared. Thus the first value in the { } gets assigned to the first member of the struct. Thus “John Doe” gets assigned to the
name varibale inside the struct and so forth. The first part of the answer is inside the code, we need to write the code to
display the information, it will be as follows:

cout<<“Persons information:<<endl;
cout<<“Name: ”<<person.name<<endl;
cout<<“Age: ”<<person.age<<“ years old”<<endl;
cout<<“Address: ”<<person.address<<endl;

The second part of the answer is the printed output, which will be as follows:

Persons information
Name: John Doe
Age: 30 years old
Address: 123 Main Street
6.

Question 6 follows the same logic as Section A Question6, they are looking for a basic bubble sort. The function will be as
follows:
void swapValues(int* a,int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
7.

The Question is fairly basic. The first answer tests a and b, both included One is true and one is false thus the answer is
0(false). The second answer test’s either a or b, thus resulting in 1(true). The third answer test’s not a or reverses the value of
a which is true, thus resulting in the answer 0(false)
Answers in order:
a)0
b)1
c)0
8.

The question ask’s for the answers to x and y after some basic calculations have been done. For the first one we are saying x
+ y, which is 5 + 3, thus the answer to x is 8. The second one is saying y times 2, thus 3*2 which is 6.
Answers in order:
1.) x = 8
2.) y = 6.
Exam Example

Sec�on A Total = ___/22

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

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

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

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

A) 1 B) 20 C) 0 D) 2
4) Consider the below example operator and select the output results: if:

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

A) 3 B) 5 C) 0 D) 1
X

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

*
&x
*
tr
*
tr

6) Complete this program, which declares and initializes a pointer and modifies the
value it points to:
Fill the correct answer in the ______ below (5 mark):

*
a

mp
a

&x , &y
7) Complete this program, which passes an array to a function and modifies its
elements:
Fill the correct answer in the ______ below (5 mark):

int arr int Size

arr[i] arr[i]

Carr ;
sl
Sec�on B Total = ___/46

1) In this program, we have a calculateDiameter func�on that takes the radius of the circle as a
parameter and returns the diameter. In the main func�on, we get the radius from the user.
Complete the program, call the calculateDiameter func�on to compute the diameter, and then
display the result using source code?

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

radius =

2
*
radius ;

return radius ;

double diameter =

calculate Diameter Cradius) ;

Cout "The diameter of the circle is : " diameter endl ;


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

10

S
%
0

8
S

2
3) Consider the declara�on 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 sec�on (5 mark):

Element [0] =
3

Element [1] =
6

Element [2] =
>

Element [3] =
8

Element [4] =
9

Element [3] =

10

Element [6] =

11

Element [7] =
12

Element [8] =
13

Element [9] =

14

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 sec�on (5 mark):

*
(p 4)+
=
50

*
(p 3)+
= 17

*
(p 2)+
=
45

*
(p +

1) = 3

*
(p 0)+
=
3
5) Below is the C++ source code that uses a struct to represent a person's informa�on and
displays it. Complete the following source code and display the person's informa�on. See
output example.

Fill the correct answer in the _____ below, and output sec�on (5 mark):

Person's information :

Name :
John doe

Age years old


:

30

Address
:

123 Main Street

Cout "Person's information : "endl ,


Cout "Name :

"person name
. endl ,

cont "Age "personage"


:
years old "endl ;

cout" Address : "Person adress


. endl
;
6) Consider the below C++ source code that declares two integer variables, value1 and value2,
which will be used to store user-inputed values. Write the swapValues func�on that takes
two integer pointers as arguments and swaps the values stored at those memory loca�ons.
The func�on effec�vely exchanges the values of value1 and value2.

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

Void SwapValues (int*a


*
int bl
,

E
*
int temp =
a
;

b
* *
a =

;
*
b = temp ;


7) Consider the below source code and evaluate the logical operators:

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

What is the output of the statements:

a) (1)__________
8

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

1) x= ______
8

2) y= ______
6
Quiz theory: Practical theory questions are omitted
Quiz 1 :
1. Which one is correct representing the precision of floating point types from lowest to highest? float, double, long double
2. Which statement describes the purpose of the linker correctly? Combines the object files and the required library functions
into an executable file.
3. Which one of the following statements is correct? #include <iostream>
4. Which statement is NOT true about ASCII codes? The ASCII values 0 to 31 represent printable letters.
5. The following data : 72; ‘A’; “Hello world”; 2.8712 are all examples of: Literal constants.
6. The name IDE we are using is: Code::Blocks
7. Which one of the following is an example of a header in c++? Iostream
8. What is the actual value represented by the following floating point number? 0.456000E02 : 45.6
9. Which arithmetic operator is found in c++ but NOT in Python? x+
10. Which one of the following is referred to as a directive? #include
11. Which statement is TRUE about a floating point numbers? The modulus operator cannot be used with floating point
numbers.

Quiz 2 :
1. An relational operator compares the values of two expressions.
2. An decision operator can execute a set of statements only under certain circumstances.
3. Each time a loop executes its statement or statements, we say the loop is performing an iteration.
4. The conditional operator evaluates to one of two expressions, depending on whether a logical expression evaluates true
or false.
5. The while loop is known as a pretest loop | pre-test loop | pretest , which means it tests its condition before performing
each iteration.
6. An if statement executes a statement or a block of statements depending on the value of a condition expression.
7. The logical OR operator, covers the situation in which you want to check for any of two or more conditions being true.
8. The process of checking input for errors before it is processed is called input validation.
9. The logical NOT operator, is a unary operator, as it applies to just one operand, and it reverses the value of a logical
expression.
10. The logical AND operator, is a binary operator that combines two logical expressions.
11. A switch statement can be used with which of the following types of variable? Int,char
12. Which of the following logical AND operators are NOT represented in c++ as ||
13. The if-block statement always need to be associated with an else-block. False
14. The “else” part of an “if-else” statement is optional; you can have an “if” statement without an “else” part. False
Quiz 3:
1. Which statement is false? The data types of the arguments must be included in the functions call statement
2. The header file that must be included at the beginning of a program to use a library function cos() is math.h
3. Recursion means : Function calling same function
4. Which is more effective while calling the function? Call by reference
5. True or false? A function can receive more than one parameter True
6. Which statement is true about functions? A function can return only one value/data item
7. Which statement is true about functions? A function can be called from a statement such as a cout-statement
8. Based on arguments and return types functions are classified into 4 types
9. A function can return only one value
10. Which of the following is a function call? calcTotal();
11. Which one of the following function/types of function cannot have default parameters? Main()
12. A function can be called in a program Any number of times
13. True or false? All functions start with the keyword void False
14. The parameters of the called function (function definition) are called formal parameters
15. Constant functions in c++ can be declared as void display() const
16. A function can return more than one value. True or false? False
Quiz 4:
1. What is the last valid subscript(index value) for the following array? arrTemps[6]; 5
2. Which declaration of an array that contains integer values is correct? Int x[4] = {8,7,6,4}
3. What is the size of the following array? Int arrNum = {1,1,1,1,1,1,}. 6
4. What is the index value in the following statement? A[4] = 2; 2
5. The subscript (index) of an array can be? An integer value
6. The array called arrValues is filled up with the following values : 12, -15, 18, 13, 19, 21, -17, 24, 27, 21 Which statement is
the correct declaration for this array? Int arrValues[10];
7. How many values can the following array hold? Int arrValues[6] : 6
8. Which one is a valid declaration for an array? Int arrNumbers[10]
Quiz 5:
1. Which one of the following cannot be a structure member? Function
2. Union can store Only 1 number of values at a time.
3. In the expression p -> value, p is a Pointer
4. Which one of the following is a memory location that is shared by two or more different types of variables? Union
5. Bit filed can only be declared as part of a structure: true
6. ‘C’ provides a facility for the user defined data type using Structure concept.
7. A string variable contains all collection of characters surrounded by double quotes.
8. Declared functions are not executed immediately. They are “saved for later use”, and will be executed later when they are
called.
9. A function is a block of code which only runs when its called.
10. To call a function, write the functions name followed by two parentheses() and a semicolon;
11. An array is a variable that represent a sequence of memory locations, each can store an item of the same data type. Eg
double temperatures[366]. True
12. You can create an array of integers or an array of characters-in fact an array of any type of data, and there can be as
many as the memory allows. True
Quiz 6:
1. The operator exclusively used with pointer to structure is? ->
2. A pointer is? A variable that stores address of other variable
3. What does the declaration int (*p)[5]; p is a pointer to a 5 element integer array
4. The operator -> is same as the combination of the operators : * and .
5. In C++ language, a function can not return a pointer, True or false? False
6. Putting the & operator in front of a variable is how to get the address of the variable in memory, True or false? True
7. In the expression p-> value, p is a Pointer.
8. Multiple indirection operator is **
9. Pointer variable is declared using preceding with * sign.
10. Int* pnumber; and int *pnumber; This is exactly the same when declare a pointer you can use either notation.
11. A pointer is a variable whose value is the address of another variable, and in general form of a pointer variable declaration
is: datatype *varName;
12. If you want to initialize your variable pnumber with the address of a variable you’ve already declared, you use the address
of operator, int *pnumber = &number;
13. Consider *pnumber += 25; This statement add the value of the variable pnumber by 25. The * indicates you’re accessing
the content to which the variable is pointing.
14. You can use the const keyword when you declare a pointer to indicate that the value pointed to must not be changed.
15. A pointer is a variable that can store an address. The address stored can be the address of a variable or the address of a
function. True
Quiz 7:
1. fputs adds newline character. False
2. In fseek() function, the position value 2 indicates? EOF(end of file)
3. The mode used for opening an existing file for reading and writing a text stream is r+.
4. What are the C functions used to read or write a file in Binary Mode? fread() and fwrite()
5. FILE reserved word is it is a type name definition in stdio.h.
6. If the function fopen() fails, it returns NULL.
7. fseek(fptr,0,0) is equivalent to rewind.
8. The function used for writing a character to a file is putc().
9. In C, file processing function fseek() takes 3 arguments.
10. What is the need for closing a file in C? fclose() closes a file to release the memory used in opening a file | Closing a file
clears Buffer contents from Ram or memory | Unclosed files occupy memory and PC hangs when on low memory.
11. The variable you have created can store only a single data item of the specified type - an integer, a floating-point value, a
character, or a bool value. An array stores several data items of the same type.
12. Every variable and function in your program is located somewhere in memory so they each have a unique address that
identifies where they are stored.
13. Applying the indirection operator, to a pointer accesses the contents of the memory location to which it points. The
operator is sometimes called the deference operator, and the process of accessing the data in the memory location
pointed to by a pointer is termed dereferencing the pointer.
14. There are three types of stream objects one for working with files : istream object represents a file input stream so you
can only read it, ofstream object represents a file output stream that you can only write to it, and fstream is a file that you
can read or write.
15. The fstream header defines the ofstream type as well as the ifstream, so including this header into a source file provides
for creating stream objects for all combinations of file read and write operations.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy