Programming In C++ Long Questions and AnswersHere in this section of Programming In C++ Long Questions and Answers,We have listed out some of the important Long Questions with Answers on Programming Basics which will help students to answer it correctly in their University Written Exam.

 

Lists of Long Descriptive type Questions that may be asked in this format in Written Exams.

  • 1. What is C++? Explain Structure of C++ Program.
  • 2. Explain following terms , Namespace, std, Keywords, Identifiers, Constants
  • 3. Explain various Data types used in C++
  • 4. Describe various operators used in C++
  • 5. Explain Memory Management Operators of C++ with example
  • 6. What is reference variable? What is its major use?
  • 7. Explain use of scope resolution operator (::) by giving example.
  • 8. Explain member dereferencing operators in short
  • 9. Explain implicit and explicit type conversion in short.

 

Question – 1. What is C++? Explain Structure of C++ Program.

  • C++ is an object oriented programming language.
  • It is a superset of C language and also called as extended version of C language.
  • It was developed by Bjarne Stroustrup at AT&T Bell lab in New Jersey, USA in the early 1980’s.
  • Structure of C++ program is as follow
    1. Include Files
    2. Class Declaration or Definition
  • Member functions definitions
  1. Main function

 

In any program first write header files like as iostream.h, conio.h, etc..as per requirement of program.

  • After header file write class declaration or definition as per your planning.
  • After class, define all member functions which are not define but declare inside the class.
  • In last write main function without main function program execution is not possible.

Example:

#include <iostream> //Header File

using namespace std;

class trial //Class

{

    int a;

    public:

    void getdata()

    {

    a=10;

    }

    void putdata();

};



void trial::putdata() //Member Function outside class definition

{

    cout<<"The value of a = "<<a;

}



int main() //Main Function

{

    trial T;

    T.getdata();

    T.putdata();

}

Output:

The value of a = 10

 

 

Question – 2. Explain following terms , Namespace, std, Keywords, Identifiers, Constants

Namespace:

  • It defines a scope for the identifiers that are used in a program.
  • For using identifiers defined in namespace using directive is used as follows:

using namespace std;

  • std is the namespace where ANSI C++ standard class libraries are defined.
  • All ANSI C++ programs must include this directive.
  • This will bring all the identifiers defined in std to the current global scope.

 

Keywords:

  • They are explicitly reserved identifiers and cannot be used as names for the program variables or other user-defined program elements.

Ex: int, class, void etc.

 

Identifiers:

  • They refer to the names of variables, functions, arrays, classes etc., created by the programmer.
  • Each language has its own rules for naming these identifiers.
  • Following are common rules for both C and C++:
    1. Only alphabetic characters, digits and underscores are permitted.
    2. The name cannot start with a digit.
    3. Uppercase and lowercase letters are distinct.
    4. A declared keyword cannot be used as a variable name.

 

Constants:

  • Like variables, constants are data storage locations. But variables can vary, constants do not change.
  • You must initialize a constant when you create it, and you can not assign new value later, after constant is initialized.

Defining constant using #define:

 

  • #define is a preprocessor directive that declares symbolic constant.

Example syntax:

     #define PI 3.14

  • Every time the preprocessor sees the word PI, it puts 3.14 in the text.

 

Example:

#include<iostream>

using namespace std;

#define PI 3.14

int main()

{

int r,area;

cout<<”Enter Radius :”;

cin>>r;

area=PI*r*r;

cout<<”Area of Circle = ”<<area;

return 0;

}

Output:

Enter Radius :5

Area of Circle = 78.5

 

Defining constant using const keyword:

  • const keyword is used to declare constant variable of any type.
  • We cannot change its value during execution of program.

Syntax: const DataType Variable_Name=value;

Ex: const int a=2;

  • Now ‘a’ is a integer type constant.

 

Question – 3. Explain various Data types used in C++

 

C++ provides following data types.

We can divide data types into three parts

  1. Primary data type
  2. Derived data type
  3. User defined data type

 

C++ Data Types

  1. Primary data type
  • int
  • float
  • char
  • void

 

  1. Secondary data type
  2. Derived data type
    • Array
    • Pointer
    • Function
    • Reference

 

  1. User defined data type
    • Class
    • Structure
    • Union
    • enum

 

Primary/Inbuilt Data types:

The primary data type of C++ is as follow.

 

Size (bytes) Range
char 1 -128 to 127
unsigned char 1 0 to 255
short or int 2 -32,768 to 32,767
unsigned int 2 0 to 65535
long 4 -2147483648 to 2147483647
unsigned long 4 0 to 4294967295
float 4 3.4e-38 to 3.4e+308
double 8 1.7e-308 to 1.7e+308
long double 10 3.4e-4932 to 1.1e+4932

 

Derived Data types:

  • Following derived data types.
  1. Arrays
  2. Function
  3. Pointers
  • We cannot use the derived data type without use of primary data type.
  • Array: An array is a fixed-size sequenced collection of elements of the same data type.
  • Pointer: Pointer is a special variable which contains address of another variable.
  • Function: A Group of statements combined in one block for some special purpose.

 

User Defined Data types:

  • We have following type of user defined data type in C++ language.
  • The user defined data type is defined by programmer as per his/her requirement.
  • Structure: Structure is a collection of logically related data items of different data types grouped together and known by a single name.
  • Union: Union is like a structure, except that each element shares the common memory.
  • Class: A class is a template that specifies the fields and methods of things or objects. A class is a prototype from which objects are created.
  • enum: Enum is a user-defined type consisting of a set of named constants called enumerator.
    • In other words enum is also used to assign numeric constants to strings
    • Syntax of enumeration: enum enum_tag {list of variables};
    • Example of enumeration: enum day-of-week {mon=1,tue,wed,thu,fri,sat,sun};

 



 

Question – 4. Describe various operators used in C++

An operator is a symbol that tells the compiler to perform certain mathematical or logical operation.

  1. Arithmetic Operators

Arithmetic operators are used for mathematical calculation. C++ supports following arithmetic operators

 

+ Addition or unary plus
Subtraction or unary minus
* Multiplication
/ Division
% Modulo division

 

 

  1. Relational Operators

Relational operators are used to compare two numbers and taking decisions based on their relation.

Relational expressions are used in decision statements such as if, for, while, etc…

 

< less than
<= less than or equal to
> greater than
>= greater than or equal to
== is equal to
!= is not equal to

 

  1. Logical Operators

Logical operators are used to test more than one condition and make decisions

 

&& logical AND (Both non zero then true, either is zero then

false)

|| logical OR (Both zero then false, either is non zero then true)
! logical NOT (non zero then false, zero then true)

 

 

  1. Assignment Operators

Assignment operators are used to assign the result of an expression to a variable. C++ also supports

shorthand assignment operators which simplify operation with assignment

 

= Assigns value of right side to left side
+= a += 1 is same as a = a + 1
-= a -= 1 is same as a = a – 1
*= a *= 1 is same as a = a * 1
/= a /= 1 is same as a = a / 1
%= a %= 1 is same as a = a % 1

 

 

  1. Increment and Decrement Operators

These are special operators in C++ which are generally not found in other languages.

 

++ Increments value by 1

a++ is postfix, the expression is evaluated first and then the value is incremented.

Ex. a=10; b=a++; after this statement, a= 11, b = 10.

++a is prefix, the value is incremented first and then the expression is evaluated.

Ex. a=10; b=++a; after this statement, a= 11, b = 11.

Decrements value by 1

a– is postfix, the expression is evaluated first and then the value is decremented.

Ex. a=10; b=a–; after this statement, a= 9, b = 10.

–a is prefix, the value is decremented first and then the expression is evaluated.

Ex. a=10; b=–a; after this statement, a= 9, b = 9

 

 

  1. Conditional Operator

A ternary operator is known as Conditional Operator.

       exp1?exp2:exp3 if exp1 is true then execute exp2 otherwise exp3

Ex: x = (a>b)?a:b; which is same as

if(a>b)

x=a;

else

x=b;

 

  1. Bitwise Operators

Bitwise operators are used to perform operation bit by bit. Bitwise operators may not be applied to float or double

 

& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left ( shift left means multiply by 2)
>> shift right ( shift right means divide by 2)

 

  1. Special Operators

 & Address operator – it is used to determine address of the variable.

  * Pointer operator – it is used to declare pointer variable and to get value from it.

   , Comma operator – It is used to link the related expressions together.

sizeof  – It returns the number of bytes the operand occupies.

. member selection operator –  used in structure.

-> member selection operator – used in pointer to structure.

 

& Address operator, it is used to determine address of the variable.
* Pointer operator, it is used to declare pointer variable and to get value from it.
, Comma operator. It is used to link the related expressions together.
. member selection operator, used in structure.
-> member selection operator, used in pointer to structure
sizeof It returns the number of bytes the operand occupies

 

 

  1. Extraction operator (>>)

Extraction operator (>>) is used with cin to input data from keyboard.

 

  1. Insertion operator (<<)

Insertion operator (<<) is used with cout to output data from keyboard.

 

  1. Scope resolution operator (::)

Scope resolution operator (::) is used to define the already declared member functions of the class.

 

 

Question – 5. Explain Memory Management Operators of C++ with example

  • For dynamic memory management, C++ provides two unary operator ‘new’ and ‘delete’.
  • An object can be created by using new, and destroy by using delete, as and when required.
  • Dynamic allocation of memory using new
  • Syntax of new :

pointer_variable = new data_type;

  • Here pointer_variable is a pointer of any data type.
  • The new operator allocates sufficient memory to hold a data object.
  • The pointer_variable holds the address of the memory space allocated.

For example:

p=new int;

q=new float;

  • Type of ‘p’ is integer and type of ‘q’ is float.
  • We can combine declaration and initialization.

int *p=new int;

float *q=new float;

  • We can dynamic allocate space for array, structure and classes by new.

int *p=new int[10];

  • Allocates a memory space for an array of size 10.
  • p[0] will refer location of p[1] and p[1] will refer location of [2] and so on.
  • Release memory using delete
  • When a data object is no longer needed, it is destroyed to release the memory space for reuse.
  • Syntax of delete: delete pointer_variable;
  • The pointer_variable is the pointer that points to a data object created with new.
  • For example:

delete p;

delete q;

  • To free a dynamically allocated array

delete [size] pointer_variable;

delete [10]p;

 

 

Question – 6. What is reference variable? What is its major use?

  • A reference variable provides an alias (alternative name) for a previously defined variable.
  • This mechanism is useful in object oriented programming because it permits the manipulation of objects by
  • reference, and eliminates the copying of object parameters back and forth.
  • It is also important to note that references can be created not only for built-in data types but also for userdefined data types.
  • Syntax: Data_type & reference_name = variable_name

For example :

int a=100;

int &b=a; //Now both a and b will give same value.

 

Question – 7. Explain use of scope resolution operator (::) by giving example.

  • The scope resolution operator is used to resolve or extend the scope of variable.
  • C++ is block structured language. We know that the same variable name can be used to have different meaning in different block.
  • The scope resolution operator will refer value of global variable from anywhere (also from inner block)
  • Without scope resolution operator all variable will refer local value.

 

Example:

#include <iostream.h>

int m=10;

int main()

{

int m=20;

{

int k=m;

int m=30;

cout<<"we are in inner block\n";

cout<<"k="<<k<<"\n";

cout<<"m="<<m<<"\n";

cout<<"::m="<<::m<<"\n";

}

cout<<"we are in outer block\n";

cout<<"m="<<m<<"\n";

cout<<"::m="<<::m<<"\n";

return 0;

}

Output:

we are in inner block

k=20

m=30

we are in outer block

m=20

::m=20

 

Question – 8. Explain member dereferencing operators in short

C++ provides three pointers to member operators to access the class member through pointer.

 

Operators Function

 

::* To declare a pointer to a member of a class.
.* To access a member using object name and a pointer to that member.
->* To access a member using a pointer to the object and a pointer to that member.

 

 

Question – 9. Explain implicit and explicit type conversion in short.

 Implicit Conversion:

  • Wheneer data types are mixed in an expression, C++ performs the conversions automatically. This process is known as implicit or automatic conversion.

Example: m= 5+2.5;

  • For a binary operator, if the operands type differs, the compiler converts one of them to match with the other.
  • Using the rule that the smaller type is converted to wider type.
  • For example if one operand is integer and another is float then integer is converted to float because float is wider than integer.
  • In above example answer means m, will be in float.

 

Explicit conversion:

  • C++ permits explicit type conversion of variables or expressions using the type cast operator.
  • Syntax: type_name (expression)
  • Example: average = sum/float(i);
  • Alternatively we can use typedef to create an identifier of the required type and use it in the functional notation.

Example:

typedef int * int_ptr;

p = int_ptr(q);

 

Example:

#include<iostream>

using namespace std;

int main()

{

int intvar=5;

float floatvar=3.5;

cout<<"intvar = "<<intvar;

cout<<"\nfloatvar = "<<floatvar;

cout<<"\nfloat(intvar) = "<<float(intvar);

cout<<"\nint(floatvar) = "<<int(floatvar);

}

Output:

intvar = 5

floatvar = 3.5

float(intvar) = 5

int(floatvar) = 3

 
You may be interested in:
Programming In C MCQs
Programming In C++ MCQs
Programming In C Tutorials