0% found this document useful (0 votes)
69 views28 pages

PF Assignment Shortquestions Answer

This document contains a programming assignment with 30 short questions and answers related to C++ fundamentals. The questions cover topics such as the definition of C++, classes, objects, encapsulation, inheritance, namespaces, templates, constants, variables, data types, operators, and more. For each question, there is a short 1-3 sentence answer explaining the key concept.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
69 views28 pages

PF Assignment Shortquestions Answer

This document contains a programming assignment with 30 short questions and answers related to C++ fundamentals. The questions cover topics such as the definition of C++, classes, objects, encapsulation, inheritance, namespaces, templates, constants, variables, data types, operators, and more. For each question, there is a short 1-3 sentence answer explaining the key concept.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 28

Programming

Fundamentals
Assignment
Name: SYED ZAKIR HUSSAIN
SHAH
ROLL NO: 2K21/SWEE/81

30 Short Questions & Answers


Q1:Define C++?

Answer: C++ is a computer programming language that is a


superset of C wherein additional features are made in the C
language.

Q2:Can we call C++ OOPS? and Why?

Answer: Yes, C++ can be called OOPS. The full form of OOPS is
an Object-Oriented Programming System, which means a
paradigm that provides an application of various concepts,
including data binding, polymorphism, inheritance, and
various others.

Q3:Define Class in C++?

Answer: Class is referred to as the designing of the user-


defined data type. It reflects the different entities, attributes,
and actions.

Q4:Define Object in C++?

Answer: Object is an instance of the class. An object can have


fields, methods, constructors, and related. For example, a bike
in real life is an object, but it has various features such as
brakes, color, size, design, and others, which are instances of
its class.

Q5:Define Encapsulation in C++?

Answer: Encapsulation is the process of binding together the


data and functions in a class. It is applied to prevent direct
access to the data for security reasons. The functions of a class
are applied for this purpose. For example, the customers' net
banking facility allows only the authorized person with the
required login id and password to get access. That is too only
for his/her part of the information in the bank data source.

Q6: What is an abstraction in C++?


Answer: An abstraction in C++ is hiding the internal
implementations and displaying only the required details. For
example, when you send an important message through email,
at that time, only writing and clicking the send option is used.
This outcome is just the success message that is displayed to
confirm that your email has been sent. However, the process
followed in transferring the data through email is not
displayed because it is of no use to you.

Q7: Briefly explain the concept of Inheritance in C++.

Answer: C++ allows classes to inherit some of the commonly


used state and behavior from other classes. This process is
known as inheritance.

Q8:Define a namespace?

Answer: A namespace is used for resolving the name conflict


of the identifier, which is accomplished by placing them under
various namespaces. This way, it helps in the logical division of
the different codes.

Q9:Define a class template?

Answer: A class template is a name given to the generic class.


The use of the keyword template is made for defining a class
template.

Q10:What is the function of the keyword “Volatile”?

Answer: "Volatile" is a function that helps in declaring that the


particular variable is volatile and thereby directs the compiler
to change the variable externally- this way, the compiler
optimization on the variable reference can be avoided.

Q11:Can we have a recursive inline function in C++?


Answer: Even though it is possible to call an inline function
from within itself in C++, the compiler may not generate the
inline code. This is so because the compiler won’t determine
the depth of the recursion at the compile time.

Nonetheless, a compiler with a good optimizer is able to inline


recursive calls until some depth is fixed at compile-time and
insert non-recursive calls at compile time for the cases when
the actual depth exceeds run time.

Q12: Explain ‘this’ pointer?

Answer: The ‘this’ pointer is a constant pointer, and it holds


the memory address of the current object. It passes as a
hidden argument to all the nonstatic member function calls.
Also, it is available as a local variable within the body of all the
nonstatic functions.

As static member functions can be called even without any


object, i.e., with the class name, the ‘this’ pointer is not
available for them.

Q13: What are the most important differences between C and


C++?

Answer:C++ supports references while C doesn’t

Features like friend functions, function overloading,


inheritance, templates, and virtual functions are inherent to
C++. These are not available in the C programming language.

In C, exception handling is taken care of in the traditional if-


else style. On the other hand, C++ offers support for exception
handling at the language level.
Mainly used input and output in C are scanf() and printf(),
respectively. In C++, cin is the standard input stream while
cout serves as the standard output stream.

While C is a procedural programming language, C++ provides


support for both procedural and object-oriented programming
approaches.

Q14:What are the Comments in C++?

Answer: Comments in C++ are simply a piece of source code


ignored by the compiler. They are only helpful for a
programmer to add a description or additional information
about their source code.

In C++ there are two ways to add comments:

//single-line comment

/* block comment */

The first type will discard everything after the compiler


encounters “//”. In the second type, the compiler discards
everything between “/*” and “*/”.

Variables, Data Types, And Constants.

Q15: Difference between Declaration and Definition of a


variable.
Answer: The declaration of a variable is merely specifying the
data type of a variable and the variable name. As a result of the
declaration, we tell the compiler to reserve the space for a
variable in the memory according to the data type specified.

Example:

int Result;

char c;

int a,b,c;

All the above are valid declarations. Also, note that as a result
of the declaration, the value of the variable is undetermined.

Whereas, a definition is an implementation/instantiation of


the declared variable where we tie up appropriate value to the
declared variable so that the linker will be able to link
references to the appropriate entities.

From above Example,

Result = 10;

C = ‘A’;

These are valid definitions.

Q16:Comment on Local and Global scope of a variable.

Answer: The scope of a variable is defined as the extent of the


program code within which the variable remains active i.e. it
can be declared, defined or worked with.

There are two types of scope in C++:

Local Scope: A variable is said to have a local scope or is local


when it is declared inside a code block. The variable remains
active only inside the block and is not accessible outside the
code block.

Global Scope: A variable has a global scope when it is


accessible throughout the program. A global variable is
declared on top of the program before all the function
definitions.

Example:

#include <iostream.h>

Int globalResult=0; //global variable

int main()

Int localVar = 10; //local variable.

…..

Q17: What is the precedence when there are a Global variable


and a Local variable in the program with the same name?
Answer: Whenever there is a local variable with the same
name as that of a global variable, the compiler gives
precedence to the local variable.

Example:

#include <iostream.h>

int globalVar = 2;

int main()

int globalVar = 5;

cout<<globalVar<<endl;

The output of the above code is 5. This is because, although


both the variables have the same name, the compiler has given
preference to the local scope.

Q18: When there are a Global variable and Local variable with
the same name, how will you access the global variable?

Answer: When there are two variables with the same name but
different scope, i.e. one is a local variable and the other is a
global variable, the compiler will give preference to a local
variable.
In order to access the global variable, we make use of a “scope
resolution operator (::)”. Using this operator, we can access the
value of the global variable.

Example:

#include<iostream.h>

int x= 10;

int main()

int x= 2;

cout<<”Global Variable x = “<<::x;

cout<<”\nlocal Variable x= “<<x;

Output:

Global Variable x = 10

local Variable x= 2

Q18: How many ways are there to initialize an int with a


Constant?
Answer: There are two ways:

The first format uses traditional C notation.

int result = 10;

The second format uses the constructor notation.

int result (10);

Constants

Q19:What is a Constant? Explain with an example.

Answer: A constant is an expression that has a fixed value.


They can be divided into integer, decimal, floating-point,
character or string constants depending on their data type.

Apart from the decimal, C++ also supports two more constants
i.e. octal (to the base 8) and hexadecimal (to the base 16)
constants.

Examples of Constants:

75 //integer (decimal)

0113 //octal

0x4b //hexadecimal
3.142 //floating point

‘c’ //character constant

“Hello, World” //string constant

Note: When we have to represent a single character, we use


single quotes and when we want to define a constant with
more than one character, we use double-quotes.

Q20: How do you define/declare constants in C++?

Answer: In C++, we can define our own constants using the


#define preprocessor directive.

#define Identifier value

Example:

#include<iostream.h>

#define PI 3.142

int main ()

float radius =5, area;


area = PI * r * r;

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

Output: Area of a Circle = 78.55

As shown in the above example, once we define a constant


using #define directive, we can use it throughout the program
and substitute its value.

We can declare constants in C++ using the “const” keyword.


This way is similar to that of declaring a variable, but with a
const prefix.

Examples of declaring a constant

const int pi = 3.142;

const char c = “sth”;

const zipcode = 411014;

In the above examples, whenever the type of a constant is not


specified, the C++ compiler defaults it to an integer type.
Operators

Q21:Comment on Assignment Operator in C++.

Answer: Assignment operator in C++ is used to assign a value


to another variable.

a = 5;

This line of code assigns the integer value 5 to variable a.

The part at the left of the =operator is known as an lvalue (left


value) and the right as rvalue (right value). Lvalue must always
be a variable whereas the right side can be a constant, a
variable, the result of an operation or any combination of
them.

The assignment operation always takes place from the right to


left and never at the inverse.

One property which C++ has over the other programming


languages is that the assignment operator can be used as the
rvalue (or part of an rvalue) for another assignment.

Example:
a = 2 + (b = 5);

is equivalent to:

b = 5;

a = 2 + b;

Which means, first assign 5 to variable b and then assign to a,


the value 2 plus the result of the previous expression of b(that
is 5), leaves a with a final value of 7.

Thus, the following expression is also valid in C++:

a = b = c = 5;

assign 5 to variables a, b and c.

Q22: What is the difference between equal to (==) and


Assignment Operator (=)?

Answer: In C++, equal to (==) and assignment operator (=) are


two completely different operators.
Equal to (==) is an equality relational operator that evaluates
two expressions to see if they are equal and returns true if they
are equal and false if they are not.

The assignment operator (=) is used to assign a value to a


variable. Hence, we can have a complex assignment operation
inside the equality relational operator for evaluation.

Q23:What are the various Arithmetic Operators in C++?

Answer: C++ supports the following arithmetic operators:

+ addition

– subtraction

* multiplication

/ division

% module

Let’s demonstrate the various arithmetic operators with the


following piece of code.

Example:

#include <iostream.h>
int main ()

int a=5, b=3;

cout<<”a + b = “<<a+b;

cout<”\na – b =”<<a-b;

cout<<”\na * b =”<<a*b;

cout<<”\na / b =”<<a/b;

cout<<”\na % b =“<<a%b;

return 0;

Output:

a+b=8

a – b =2

a * b =15

a / b =2

a % b=1

As shown above, all the other operations are straightforward


and the same as actual arithmetic operations, except the
modulo operator which is quite different. Modulo operator
divides a and b and the result of the operation is the
remainder of the division.

Q24: What are the various Compound Assignment Operators in


C++?

Answer: Following are the Compound assignation operators in


C++:

+=, -=, *=, /=, %=, >>=, <<=, &=, ^=,|=

Compound assignation operator is one of the most important


features of C++ language which allow us to change the value of
a variable with one of the basic operators:

Example:

value += increase; is equivalent to value = value + increase;

if base_salary is a variable of type int.

int base_salary = 1000;

base_salary += 1000; #base_salary = base_salary + 1000

base_salary *= 5; #base_salary = base_salary * 5;


Q25:State the difference between Pre and Post
Increment/Decrement Operations.

Answer: C++ allows two operators i.e ++ (increment) and –


(decrement), that allow you to add 1 to the existing value of a
variable and subtract 1 from the variable respectively. These
operators are in turn, called increment (++) and decrement
(–).

Example:

a=5;

a++;

The second statement, a++, will cause 1 to be added to the


value of a. Thus a++ is equivalent to

a = a+1; or

a += 1;

A unique feature of these operators is that we can prefix or


suffix these operators with the variable. Hence, if a is a
variable and we prefix the increment operator it will be
++a;

This is called Pre-increment. Similarly, we have pre-decrement


as well.

If we prefix the variable a with an increment operator, we will


have,

a++;

This is the post-increment. Likewise, we have post-decrement


too.

The difference between the meaning of pre and post depends


upon how the expression is evaluated and the result is stored.

In the case of the pre-increment/decrement operator, the


increment/decrement operation is carried out first and then
the result passed to an lvalue. Whereas for post-
increment/decrement operations, the lvalue is evaluated first
and then increment/decrement is performed accordingly.

Example:
a = 5; b=6;

++a; #a=6

b–; #b=6

–a; #a=5

b++; #6

I/O through Console

Q26: What are the Extraction and Insertion operators in C++?


Explain with examples.

Answer: In the iostream.h library of C++, cin, and cout are the
two data streams that are used for input and output
respectively. Cout is normally directed to the screen and cin is
assigned to the keyboard.

“cin” (extraction operator): By using overloaded operator >>


with cin stream, C++ handles the standard input.

int age;

cin>>age;

As shown in the above example, an integer variable ‘age’ is


declared and then it waits for cin (keyboard) to enter the data.
“cin” processes the input only when the RETURN key is
pressed.

“cout” (insertion operator): This is used in conjunction with


the overloaded << operator. It directs the data that followed it
into the cout stream.

Example:

cout<<”Hello, World!”;

cout<<123;

Q27: What is the difference between while and do while loop?


Explain with examples.

Answer: The format of while loop in C++ is:

While (expression)

{statements;}

The statement block under while is executed as long as the


condition in the given expression is true.

Example:

#include <iostream.h>

int main()

int n;

cout<<”Enter the number : “;

cin>>n;

while(n>0)

cout<<” “<<n;

--n;

cout<<”While loop complete”;

In the above code, the loop will directly exit if n is 0. Thus in


the while loop, the terminating condition is at the beginning of
the loop and if it’s fulfilled, no iterations of the loop are
executed.
Next, we consider the do-while loop.

The general format of do-while is:

do {statement;} while(condition);

Example:

#include<iostream.h>

int main()

int n;

cout<<”Enter the number : “;

cin>>n;

do {

cout<<n<<”,”;

--n;

}while(n>0);

cout<<”do-while complete”;

In the above code, we can see that the statement inside the
loop is executed at least once as the loop condition is at the
end. These are the main differences between the while and do-
while.

In case of the while loop, we can directly exit the loop at the
beginning, if the condition is not met whereas in the do-while
loop we execute the loop statements at least once.

Q28: What do you mean by ‘void’ return type?

Answer: All functions should return a value as per the general


syntax.

However, in case, if we don’t want a function to return any


value, we use “void” to indicate that. This means that we use
“void” to indicate that the function has no return value or it
returns “void”.

Example:

void myfunc()

Cout<<”Hello,This is my function!!”;

int main()

{
myfunc();

return 0;

Q29: Explain Pass by Value and Pass by Reference.

Answer: While passing parameters to the function using “Pass


by Value”, we pass a copy of the parameters to the function.

Hence, whatever modifications are made to the parameters in


the called function are not passed back to the calling function.
Thus the variables in the calling function remain unchanged.

Example:

void printFunc(int a,int b,int c)

a *=2;

b *=2;

c *=2;

int main()
{

int x = 1,y=3,z=4;

printFunc(x,y,z);

cout<<”x = “<<x<<”\ny = “<<y<<”\nz = “<<z;

Output:

x=1

y=3

z=4

As seen above, although the parameters were changed in the


called function, their values were not reflected in the calling
function as they were passed by value.

However, if we want to get the changed values from the


function back to the calling function, then we use the “Pass by
Reference” technique.

To demonstrate this we modify the above program as follows:


void printFunc(int& a,int& b,int& c)

a *=2;

b *=2;

c *=2;

int main()

int x = 1,y=3,z=4;

printFunc(x,y,z);

cout<<”x = “<<x<<”\ny = “<<y<<”\nz = “<<z;

Output:

x=2

y=6

z=8

As shown above, the modifications done to the parameters in


the called functions are passed to the calling function when we
use the “Pass by reference” technique. This is because using
this technique we do not pass a copy of the parameters but we
actually pass the variable’s reference itself.
Q30: What is an Inline function in C++?

Answer: Inline function is a function that is compiled by the


compiler as the point of calling the function and the code is
substituted at that point. This makes compiling faster. This
function is defined by prefixing the function prototype with
the keyword “inline”.

Such functions are advantageous only when the code of the


inline function is small and simple. Although a function is
defined as Inline, it is completely compiler dependent to
evaluate it as inline or not.

You might also like