top of page

Basics: C plus plus Class Notes

Updated: Oct 22, 2022

Mobiprep has created last-minute notes for all topics of C++ to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on C++.

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.


Basics


Question 1) Define keyword, identifier, and token.

Answer) Token: A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as follows:

  1. Keywords

  2. Identifiers

  3. Constants

  4. Strings

  5. Special Symbols

  6. Operators

Keywords: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying to assign a new meaning to the keyword which is not allowed. You cannot redefine keywords.


Identifiers: Identifiers are used as the general terminology for the naming of variables, functions and arrays. These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved for special use. Once declared, you can use the identifier in later program statements to refer to the associated value.


 

Question 2) What do you mean by operator precedence?

Answer) Operator precedence specifies the order of operations in expressions that contain more than one operator. Operator associativity specifies whether, in an expression that contains multiple operators with the same precedence, an operand is grouped with the one on its left or the one on its right.


 

Question 3) What is type conversion in C++?

Answer) A type cast is basically a conversion from one type to another. There are two types of type conversion:


Implicit Type Conversion Also known as ‘automatic type conversion’.

a. Done by the compiler on its own, without any external trigger from the user.

b. Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.

c. All the data types of the variables are upgraded to the data type of the variable with largest data type.


bool -> char -> short int -> int ->


unsigned int -> long -> unsigned ->


long long -> float -> double -> long double


It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).


Example of Type Implicit Conversion:



// An example of implicit conversion

#include <iostream>
using namespace std;

int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;

// x is implicitly converted to float
float z = x + 1.0;

cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;

return 0;
}

Output:



x = 107
y = a
z = 108


Explicit Type Conversion: This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type.


In C++, it can be done by two ways:

Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting.


Syntax:

(type) expression


where type indicates the data type to which the final result is converted.


Example:

// C++ program to demonstrate
// explicit type casting

#include <iostream>
using namespace std;

int main()
{
double x = 1.2;

// Explicit conversion from double to int
int sum = (int)x + 1;

cout << "Sum = " << sum;

return 0;
}

Output:

Sum = 2

 

Question 4) Differentiate between nested if-else and switch statement.

Answer) If-else” and “switch” both are selection statements. The selection statements, transfer the flow of the program to the particular block of statements based upon whether the condition is “true” or “false”.

The fundamental difference between if-else and switch statements is that the if-else statement “selects the execution of the statements based upon the evaluation of the expression in if statements”. The switch statements “selects the execution of the statement often according to a keyboard command”.


 


Question 5) What is the need of array. Discuss different types of arrays.

Answer) "Arrays:- When there is a need to use many variables then There is a big problem because we will Conflict with name of variables So that in this Situation where we wants to Operate on many numbers then we can use array . The Number of Variables also increases the complexity of the Program. So that we uses Arrays.

Arrays are Set of Elements having same data type or we can Say that Arrays are Collection of Elements having same name and same data type But Always Remember Arrays are Always Start From its index value and the index of array is start From 0 to n-1.

Suppose we wants to Access 5th Element of array then we will use 4th Element Because Arrays are Start From 0 and arrays are always stored in Continuous Memory Locations The Number of Elements and Types of array are Identified by Subscript of array Elements. The Various types of Array those are provided by c as Follows:-


1. Single Dimensional Array

2. Two Dimensional Array

3. Three Dimensional array

4. Character Array or Strings.


A Single dimensional is used representing the elements of the array for example

int a[5]

The [] is used for dimensional or the sub-script of the array that is generally used for declaring the elements of the array For Accessing the Element from the array we can use the Subscript of the Array like this

a[3]=100

This will set the value of 4th element of array

So there is only the single bracket then it called the Single Dimensional Array

This is also called as the Single Dimensional Array


2) Two Dimensional Array or the Matrix


The Two Dimensional array is used for representing the elements of the array in the form of the rows and columns and these are used for representing the Matrix A Two Dimensional Array uses the two subscripts for declaring the elements of the Array


Like this

int a[3][3]

So This is the Example of the Two Dimensional Array In this first 3 represents the total number of Rows and the Second Elements Represents the Total number of Columns The Total Number of elements are judge by Multiplying the Numbers of Rows * Number of Columns in The Array in the above array the Total Number of elements are 9


3) Multidimensional or the Three Dimensional Array :The Multidimensional Array are used for Representing the Total Number of Tables of Matrix A Three dimensional Array is used when we wants to make the two or more tables of the Matrix Elements for Declaring the Array Elements we can use the way like this


int a[3][3][3]

In this first 3 represents the total number of Tables and the second 3 represents the total number of rows in the each table and the third 3 represents the total number of Columns in the Tables


So this makes the 3 Tables having the three rows and the three columns

The Main and very important thing about the array that the elements are stored always in the Contiguous in the memory of the Computer


4) Character Array of String: -Like an integer characters are also be in the Array The Array of Characters are called as the Strings They are Generally used for representing the Strings Always Remember that a String is Terminated with the \0 or Null Character


There are the built in string Operation those are provided by the C Language in the String.h Header file Like

  1. strLen: For Getting the Length or Total Numbers of Characters in String

  2. strconcat: This is Used for Joining the two Strings or This function is used for Concatenating the two Strings.

  3. strRev: This Function is used for obtaining the Reverse of the String

  4. strcmp: This Function is used for Comparing the Two Strings and it gives us the Result as follows after comparing the Two Strings


it Returns us + value

if String1 is Greater than String2


it Returns us the - Value

if String1 is Less than String2


it Returns us the 0

if string1 is Equals to String2


Like The array elements of Integer Types The Character array also are the Single Dimensional or The Two Dimensional Array



1) Single Dimensional Array: The Single Dimensional array are used for creating the Number of characters like

char name[10]

in this we can use the 10 characters on the name variable Means we can give the name as 10 characters long



2) Two Dimensional array :-When we talk about the Two Dimensional of the Character array The first Subscript of the array if used for representing the Total Numbers of Strings and the second Subscript is used for defining the length of the array Characters in the String


like This

char name[5][10]

It declares the 5 names and each having the characters up to 10 So the First Subscript is used for defining the total number of the Strings and the Second is used for Length of the Characters




31 views0 comments

Recent Posts

See All

Comments


Commenting has been turned off.
bottom of page