+ All Categories

ICT C++

Date post: 19-May-2015
Category:
Upload: karthikeyan-a-k
View: 2,535 times
Download: 0 times
Share this document with a friend
Description:
ICT training in Bannari Amman
Popular Tags:
144
C++
Transcript
Page 1: ICT C++

C++

Page 2: ICT C++

Explain the different forms of Polymorphism.

Question No : 1

Page 3: ICT C++

There are two types of polymorphism. One is

Compile time polymorphism and the other is run

time polymorphism. Compile time polymorphism

is method overloading. Runtime time

polymorphism is done using inheritance.

Distinct forms of polymorphism• Method overloading• Method overriding through inheritance

Answer

Page 4: ICT C++

What are the differences between Constructors

and Methods?

Question No : 2

Page 5: ICT C++

Constructors Methods

Purpose Create an instance of a class Group statements

Return

Type No return type, not even void void or a valid return type

Name

Same name as the class (first

letter is capitalized by

convention) -- usually a noun

Any name except the class.

Method names begin with a

lowercase letter by convention

-- usually the name of an action

Inheritanc

e Constructors are not inherited Methods are inherited

Answer

Page 6: ICT C++

All mammals have some common characteristics such as

having hair, being warm-blooded and nurturing their young.

Dog is a type of mammal and has some additional

characteristics such as slobbering and running in circles. The

object oriented concept that allows the dog to derive the

common characteristics of the mammal is called _____________

a. Polymorphism

b. Inheritance

c. Encapsulation

d. Prototyping

Question No : 3

Page 7: ICT C++

b. Inheritance

Inheritance is the mechanism by which one object

acquires the properties of another object.

Answer

Page 8: ICT C++

What are the major benefits of object oriented

programming?

a. Ease of maintenance

b. Reduced development time due to reusability

c. Consumes less resources during execution

d. Top-down approach

Question No : 4

Page 9: ICT C++

Ease of maintenance

Reduced development time due to reusability

Answer

Page 10: ICT C++

Consider the following inheritance diagram. The

Get_vehicle_type function is present in both the super class

(Four wheeler) and the derived class (Car). If the Car

receives a message for Get_vehicle_type, which method is

executed?

Question No : 5

Four wheeler

Member functionGet_vehicle_type

Car

Member functionGet_vehicle_type

Page 11: ICT C++

The method in the Car object is executed. A derived

class can override the base class member function

by supplying a new version of that function with the

same signature. When an object receives a

message, it first checks for the availability of the

method in this object. If it is not available, then it

consults its super-class. The first method found in

the hierarchy takes precedence and executed.

Answer

Page 12: ICT C++

Explain in simple terms why an object-oriented

language like C++ is different from a Classical

language like C.

Question No : 6

Page 13: ICT C++

In a classical language like C, data-structures and their procedures

tended to group logically, but it is the programmer's duty to devise and

enforce these groupings. In OOP, the language groups procedures with

their data type. This produces a decomposition grouped around the types

in a program. The programmer does not have to match up the right

procedure with the right data-type. Instead, variables know which

operations they implement.

OOP yields better decomposition and more opportunities for code reuse.

These strengths are especially important for writing large programs,

packaging up code into libraries for use by others, and programming in

teams.

Answer

Page 14: ICT C++

Consider the following statements:

Real world objects contain _________ and _________.

A software objects’ state is stored in ____________

A software object’s behaviour is exposed through _____________

Hiding internal data from the outside world and accessing it only

through publicly exposed methods is known as data ______________

A blue print for a software object is called as a ________

Pick and fill the right words from the following.

(Behaviour, Methods, Encapsulation, State, fields, Class)

Question No : 7

Page 15: ICT C++

State

Behaviour

Fields

Methods

Encapsulation

Class

Answer

Page 16: ICT C++

Scenario:

Consider a Stack data structure (Last-in, First-Out). You might have a

program that requires three types of stack. One stack is used for Integer

values one stack is used for floating-point values, and one for

characters. The algorithm that implements each stack is the same, even

though the data being stored differs. In a non-Object Oriented language

like C, you would be required to create three different sets of stack

routines, with each set using different names. In OOP language you

specify a general set of stack routines that all share the same names.

Which feature of OOP allows you to implement the above?

Question No : 8

Page 17: ICT C++

Polymorphism based on the principle that of

‘One interface, multiple methods’

Answer

Page 18: ICT C++

Scenario:

Real-world objects share two characteristics: They all

have state and behavior. Identifying them is a great way

to begin thinking in terms of Object oriented

programming.

Give some examples of state and behavior for the

following

a. Animal ( say dog)

b. Vehicle( say bicycle)

Question No : 9

Page 19: ICT C++

Dogs have

state (name, color, breed, hungry)

behavior (barking, fetching, wagging tail)

Bicycles have

state (current gear, current pedal, current speed)

behavior (changing gear, changing pedal, applying

brakes)

Answer

Page 20: ICT C++

Scenario:

Imagine a programming environment where all the code is contained in a

single continuous block

The execution of flow statements are implemented through use of Goto

statements so that one can jump to a specified section of code. The source

code is notoriously difficult to read and debug. But being a scripting

language used for automating batch execution, the program typically runs

fast and does its job.

What kind of a programming environment are we referring to in

the above?

Is it object oriented, Structured or Unstructured?

Question No : 10

Page 21: ICT C++

Unstructured

Answer

Page 22: ICT C++

What is the output of the following code?

a. 14

b. 13

c. 12

d. 11

Question No : 11

#include<iostream.h>#include<string.h>void main(){

cout<<strlen(“Hello, World.\n” )<<” \n” ;}

Page 23: ICT C++

a. 14

Answer

Page 24: ICT C++

What is the output of the following code?

a. 123

b. Compile time error

c. 321

d. Run time error

Question No : 12

#include<iostream.h>void main(){

/* this is /* an example */ of nested comment */cout<<123<<endl;

}

Page 25: ICT C++

b. Compile time error

Answer

Page 26: ICT C++

What is the output of the following code?

a. 1

b. Compile time error

c. 0

d. Run time error

Question No : 13

#include<iostream.h>void main(){

cout<<;}

Page 27: ICT C++

b. Compile time error

(expected primary-expression before ';' token)

Answer

Page 28: ICT C++

What is the output of the following code?

a. 20, 20

b. 20, 21

c. 21, 22

d. 22, 22

Question No : 14

#include<iostream.h>void main(){

int a = 20;int &n = a;n=a++;a=n++;cout<<a <<”,”<<n<<endl;

}

Page 29: ICT C++

a. 20, 20

Answer

Page 30: ICT C++

What is the output of the following code?

a. 21, 21

b. 20, 21

c. 21, 22

d. Compile Time Error

Question No : 15

#include<iostream.h>void main(){

int a = 20,b=100;int &n = a;n=a++;n = &b;cout<<a <<” ,”<<n<<endl;

}

Page 31: ICT C++

a. Compile Time Error

Explanation: Invalid conversion from int* to int

Answer

Page 32: ICT C++

What is the output of the following code?

a. 10

b. False

c. 1

d. Error

Question No : 16

#include<iostream.h>void main(){

bool a=10;cout<<a<<endl;

}

Page 33: ICT C++

c. 1

Answer

Page 34: ICT C++

What is the output of the following code?

a. 101

b. 100

c. None

d. Error : Cannot use main as identifier

Question No : 17

#include<iostream.h>void main(){

int main;main = 100;cout<<main++<<endl;

}

Page 35: ICT C++

b. 100

Answer

Page 36: ICT C++

What is the output of the following code?

a. 0, 0, 0

b. 1, 0, 0

c. 2, 2, 2

d. 3, 2, 2

Question No : 18

#include<iostream.h>void main(){

int a=0,x;x = ++a * --a;cout<<++a<< “,“ << a++ << ”,” << x <<endl;

}

Page 37: ICT C++

b. 1, 0, 0

Answer

Page 38: ICT C++

What is the output of the following code?

a. 32

b. 0

c. Compile Time Error

d. Run Time Error

Question No : 19

#include<iostream.h>void main(){

a=32;cout<<a<<endl;int a;

}

Page 39: ICT C++

c. Compile Time Error

Answer

Page 40: ICT C++

What is wrong with the following program?

a. There is nothing wrong in the program.

b. Variable ‘b’ must not be initialized in the loop

c. Variable ‘b’ must declared before do-while the loop

d. The condition for while loop is not valid

Question No : 20

#include<iostream.h>void main(){

do{

int b=0;cout<<b;b++;

}while(b!=10);}

Page 41: ICT C++

c. Variable ‘b’ must declared before do-while the

loop

Answer

Page 42: ICT C++

What is the output of the following program?

a. 14, 14

b. 15, 14

c. 14, 15

d. 15, 15

Question No : 21

#include<iostream.h>void main(){

char p[]="This is a test";cout<<sizeof(p)<<","<<strlen(p);

}

Page 43: ICT C++

b. 15, 14

Answer

Page 44: ICT C++

What is wrong with the following program?

a. Array ‘a’ is not initialized properly

b. There is no problem

c. Redeclaration of variable ‘i’

d. There is a run time error

Question No : 22

#include<iostream.h>void main(){

int a[5] = {0};for(int i=0;i<2;i++)a[i]=i;for(int i=0;i<5;i++)cout<<a[i]<<endl;

}

Page 45: ICT C++

b. There is no problem

Answer

Page 46: ICT C++

What is the output of the following program?

a. 100, 2,3,22,400

b. 0,0,0,0,0

c. Error

d. 400,22,3,2,100

Question No : 23

#include<iostream.h>void main(){

int a[5] = {100,2,3,22,400};int b[5];b=a;for(int i=0;i<5;i++)cout<<b[i]<<endl;

}

Page 47: ICT C++

c. Error

C++ forbids assignment of arrays

Answer

Page 48: ICT C++

What is the output of the following program?

a. No output

b. 1 2 3 4 5

c. 1 2 3 0 0

d. There is a run time error

Question No : 24

#include<iostream.h>void main(){

int a[5] = {1,2,3};for(int i=0;i<5;i++)cout<<a[i]<<endl;

}

Page 49: ICT C++

c. 1 2 3 0 0

Answer

Page 50: ICT C++

What is the output of the following program?

a. 51, 42, 33, 24, 15,

b. 40, 30, 20, 10, 00,

c. 41, 32, 23, 14, 05,

d. 00, 10, 20, 30, 40,

Question No : 25

#include<iostream.h>void main(){

int i=5,j=0;while(i-- || j++){

cout<<i<<""<<j<<” ,” ;}

}

Page 51: ICT C++

b. 40, 30, 20, 10, 00,

Answer

Page 52: ICT C++

What is the output of the following program?

a. Error

b. 1 0

c. 0 1

d. 0 0

Question No : 26

#include<iostream.h>void main(){

int a;bool b;a = 12 > 100;b = 12 >= 100;cout<<a<<" "<<b<<endl;

}

Page 53: ICT C++

d. 0 0

Answer

Page 54: ICT C++

What is the output of the following program?

a. Error

b. 100, 200, 300, 100,

c. 300, 200, 100, 0,

d. 300, 200, 100, 1,

Question No : 27

#include<iostream.h>void main(){

int a = 100;{

int a = 200;{

int a = 300cout<<a<<",";

}cout<<a<<",";}cout<<a<<",";cout<<::a<<",";

}

Page 55: ICT C++

d. 300, 200, 100, 1,

Answer

Page 56: ICT C++

What is the output of the following program?

a. Error

b. 1000

c. 100

d. 0

Question No : 28

#include<iostream.h>void main(){

int x=10;(x<0)?(int a =100):(int a =1000);cout<<a;

}

Page 57: ICT C++

a. Error

Answer

Page 58: ICT C++

What is the output of the following program?

a. 0

b. 1

c. Compile Time Error

d. Run Time Error

Question No : 29

#include<iostream.h>void main(){

int a = 0;cout<<(a = 10/a);

}

Page 59: ICT C++

d. Run Time Error

Answer

Page 60: ICT C++

What is the output of the following program?

a. 1 2 3 4 5

b. 2 4 6 8 10

c. Compile Time Error

d. Run Time Error

Question No : 30

#include<iostream.h>void main(){

int x=0;while(x++<5){

static x;x+=2;cout<<x<<" ";

}}

Page 61: ICT C++

c. Compile Time Error

C++ forbids declaration of `x' with no type

Answer

Page 62: ICT C++

What is the output of the following program?

a. Both the strings are same

b. Both the strings are not same

c. Compile Time Error

d. Run Time Error

Question No : 31

#include<iostream.h>void main(){

char str1[]=” India” , str2[]=” India”;if(str1==str2)cout<<”Both the strings are same”;elsecout<<”Both the strings are not same”;

}

Page 63: ICT C++

b. Both the strings are not same

Answer

Page 64: ICT C++

What is the output of the following code if user

enters “This is a test”?

a. This is a test

b. This is a

c. This

d. Error

Question No : 32

#include<iostream.h>#include<string.h>void main(){

char str[8];cin>>str;cout<<str;

}

Page 65: ICT C++

c. This

Answer

Page 66: ICT C++

What is the output of the following code?

a. 10 20

b. 10 10

c. 20 20

d. 20 10

Question No : 33

#include<iostream.h>void main(){

int arr[] = {10,20,30,40,50};int *ptr = arr;cout<< *ptr++<<" "<<*ptr;

}

Page 67: ICT C++

a. 10 20

Answer

Page 68: ICT C++

What is the output of the following code?

a. 6

b. 3

c. Compile Time Error

d. 0

Question No : 34

#include<iostream.h>void main(){

int arr[] = {10,20,30,40,50};int x,*ptr1 = arr, *ptr2=&arr[3];x = ptr2 - ptr1;cout<<x;

}

Page 69: ICT C++

b. 3

Answer

Page 70: ICT C++

What is the output of the following code?

a. 55 33

b. 33 55

c. 11 55

d. 11 33

Question No : 35

#include<iostream.h>void main(){

int arr[][3]={0,11,22,33,44,55};int *a = &arr[0][0];cout<<arr[1][2]<<" "<<*(a+3);

}

Page 71: ICT C++

a. 55 33

Answer

Page 72: ICT C++

What is the output of the following code?

a. 10

b. 3

c. 0

d. 11

Question No : 36

#include<iostream.h>void main(){

int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};cout<<(*(*(*arr+1)+2)+0)+7;

}

Page 73: ICT C++

a. 10

Answer

Page 74: ICT C++

What is the output of the following code?

a. 16

b. 7

c. 11

d. 10

Question No : 37

#include<iostream.h>void main(){

int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};cout<<**(*arr+1)+2+7;

}

Page 75: ICT C++

a. 16

Answer

Page 76: ICT C++

What is the namespace?

Question No : 38

Page 77: ICT C++

Namespaces allow to group entities like classes,

objects and functions under a name. This way the

global scope can be divided in "sub-scopes", each

one with its own name

The format of namespaces is:

Answer

namespace identifier{

entities}

Page 78: ICT C++

What is the Basic nature of "cin" and "cout" and

what concept or principle we are using on those

two?

Question No : 39

Page 79: ICT C++

Basically "cin and cout" are INSTANCES of istream

and ostream classes respectively, and the

concept which is used on cin and cout is operator

overloading. Extraction and Insertion operators

are overloaded for input and output operations.

Answer

Page 80: ICT C++

What is scope resolution operator?

Question No : 40

Page 81: ICT C++

The scope resolutions operator is also used to

find the value of a variable out of the scope of the

variable.

Example:

::i refers to the value just before the scope (i.e. 10)

Answer

int i=10;main(){

int i=5;cout<<::i;cout<<i;

}

Page 82: ICT C++

Can main() be overridden?

Question No : 41

Page 83: ICT C++

In any application, there can be only one main

function. In c++, main is not a member of any

class. There is no chance of overriding.

Answer

Page 84: ICT C++

In what way macros are different from template?

Question No : 42

Page 85: ICT C++

In case of macros, there is no way for the compiler to verify that the macro

parameters are of compatible types. The macro is expanded without any

special type checking.

If macro parameter has a post incremented variable (like c++), the increment

is performed two times.

Because macros are expanded by the preprocessor, compiler error messages

will refer to the expanded macro, rather than the macro definition itself. Also,

the macro will show up in expanded form during debugging.

Example: Macro:

Answer

#define min(i, j) (i < j ? i : j)template:template<class T> T min (T i, T j) { 

return i < j ? i : j;}

Page 86: ICT C++

The design of classes in a way that hides the

details of implementation from the user is known

as:

a. Encapsulation

b. Information Hiding

c. Data abstraction

d. Inheritance

Question No : 43

Page 87: ICT C++

a. Encapsulation

Answer

Page 88: ICT C++

Which of the following keywords do you think can

be used when declaring static members in a

class? Encapsulation

(i) Public (ii) Private (iii) Protected

a. i, ii and iii

b. i and ii

c. Only i

d. i and iii

Question No : 44

Page 89: ICT C++

c. Only i

Answer

Page 90: ICT C++

I want a nonmember function to have access to

the private members of a class. The class must

declare that function:

a. friend

b. inline

c. static

d. virtual

Question No : 45

Page 91: ICT C++

a. friend

Answer

Page 92: ICT C++

What is a base class?

a. An abstract class that is at the top of the inheritance

hierarchy

b. A class with a pure virtual function in it

c. A class that inherits from another class

d. A class that is inherited by another class, and thus is

included in that class

Question No : 46

Page 93: ICT C++

b. A class with a pure virtual function in it

Answer

Page 94: ICT C++

When do preprocessor directives execute?

a. Before the compiler compiles the program

b. After the compiler compiles the program

c. At the same time as the compiler compiles the

program

d. Never executes

Question No : 47

Page 95: ICT C++

a. Before the compiler compiles the program

Answer

Page 96: ICT C++

An address is a ________ while a pointer is a

_________.

a. Variable, Location

b. Variable, Constant

c. Constant, Variable

d. Constant, Location

Question No : 48

Page 97: ICT C++

a. Variable, Location

Answer

Page 98: ICT C++

What is the difference between declaration and

definition?

Question No : 49

Page 99: ICT C++

The definition is the one that actually allocates space, and

provides an initialization value, if any.

There can be many declarations, but there must be exactly

one definition.

A definition tells the compiler to set aside storage for the

variable.

A declaration makes the variable known to parts of these

program that may wish to use it.

A variable might be defined and declared in the same

statement.

Answer

Page 100: ICT C++

What is Object?

Question No : 50

Page 101: ICT C++

An Object is an instance of a class.

Example:

Ferrari and maruthi are objects of class car.

An Object is basic runtime entities of its class

Answer

Page 102: ICT C++

We can overload assignment operator as a

normal function. But we cannot overload

assignment operator as friend function why?

Question No : 51

Page 103: ICT C++

Because, Friend functions do not have a "this"

pointer

Answer

Page 104: ICT C++

What is an inline function?

Question No : 52

Page 105: ICT C++

Inline functions are like macros in c language. The functions

are expanded in the line where it is invoked at the time of

compilation. These functions should not include any loops or

static members inside it. And also it should not be a recursive

one. Inline functions should return a value.

Keyword ‘inline’ should be included in the function definition.

Answer

inline int add(int a,int b){return a+b;}

void main(){int x=5;int y=10;cout<<?The sum is : ?<<add(x,y);}

Page 106: ICT C++

What is the difference between deep copy and

shallow copy?

Question No : 53

Page 107: ICT C++

A Shallow copy of an object copies all of the member field

values. If there are fields that point to dynamically allocated

memory, Shallow copy copies the pointer but the memory it

points to will not be copied. The field in both the original

object and the copy will then point to the same dynamically

allocated memory.

Whereas Deep copy copies all the fields and makes copies of

dynamically allocated memory pointed to by the fields. To

make a deep copy, one needs to overwrite the copy

constructor and assignment operator

Answer

Page 108: ICT C++

Can we make any program in c++ without using

any header file?

Question No : 54

Page 109: ICT C++

No

Answer

Page 110: ICT C++

Can we have more than one constructor in a

class?

Question No : 55

Page 111: ICT C++

Yes

Answer

Page 112: ICT C++

What is public, private and protected?

Question No : 56

Page 113: ICT C++

Public, protected and private are three access

specifiers in C++.

Public data members and member functions are

accessible outside the class.

Protected data members and member functions

are only available to derived classes.

Private data members and member functions

cannot be accessed outside the class.

Answer

Page 114: ICT C++

What are pointers?

Question No : 57

Page 115: ICT C++

A pointer is an address location of another variable. It

is a value that designates the address or memory

location of some other value (usually value of a

variable).

The value of a variable can be accessed by a pointer

which points to that variable.

To do so, the reference operator (&) is pre-appended

to the variable that holds the value.

A pointer can hold any data type, including functions.

Answer

Page 116: ICT C++

What is NULL pointer?

Question No : 58

Page 117: ICT C++

A pointer that points to a no valid location is known as

null pointer. Null pointers are useful to indicate

special cases.

Example:

No next node pointer in case of a linked list, which is an

indication of errors that pointer returned from functions.

Answer

Page 118: ICT C++

What is reference variable in C++?

Question No : 59

Page 119: ICT C++

A reference variable is just like pointer with few differences.

It is declared using & operator.

A reference variable must always be initialized.

The reference variable once defined to refer to a variable

cannot be changed to point to other variable.

You cannot create an array of references the way it is

possible with pointer.

Answer

Page 120: ICT C++

What is the output of the following program?

Question No : 60

#include <iostream.h>class myclass{int val;

Public:myclass(int I){ val = I; cout<<”constructing \n”;}~myclass( ) { cout<<"destructing\n";}int getval( ) { return val;}

};void display(myclass ob){

cout<<ob.getval()<<'\n';}main(){

myclass a(10);display(a);return 0;

}

Page 121: ICT C++

constructing

10

destructing

When a copy of an object is created to be used as an

argument to a function, the “constructor" function is not

called, but "copy constructor " is called. However, when a

copy is destroyed (usually by going out of scope when

the function returns), the destructor function is called.

Answer

Page 122: ICT C++

What is the output of the following program?

Question No : 61

#include "iostream.h"class base {

public:base(){ cout << "constructing base \n";}~base(){ cout << "destructing base \n";}

};

class derived: public base{public:derived() {cout<<"constructing derived \n";}~derived() {cout<<"destructing derived \n";}

};

main(){

derived ob;return 0;

}

Page 123: ICT C++

constructing base

constructing derived

destructing derived

destructing base

Constructors are called in the order of derivation and

destructors are called in the reverse order.

Answer

Page 124: ICT C++

Observe the following piece of code and reduce

the amount of duplicate code.

Question No : 62

if(book::title=new char[256] == 0){ cerr <<"Error allocating memory\n"; exit(0);}

if(book::author=new char[64] ==0){ cerr<<"Error allocating memory\n"; exit(0);}

if(book::publisher=new char[128] ==0){ cerr<<"Error allocating memory\n"; exit(0);}

Page 125: ICT C++

The following code snippet is appearing 3 times

in the original piece of code.

We can have it just one time. Remaining two

times it is redundant

Answer

book::title=new char[256] ;book::author=new char[64] ;book::publisher=new char[128];if((book::title&&book::author&&book::publisher) ==0){

cerr<<"Error allocating memory\n";exit(0);

}

cerr<<"Error allocating memory\n";exit(0);

Page 126: ICT C++

When the interface exposed by C++ component

is not compatible to the client, what needs to be

done?

Question No : 63

Page 127: ICT C++

Client has to use Adapter design pattern to make

interface compatible. Adapter publicly

implements the interface to be used by the client

Answer

Page 128: ICT C++

What is the output of following program? Why?

Question No : 64

#include <iostream.h> class ConstFromConst { private : int num1; int num2; public: ConstFromConst(){num1=100;num2=101;ConstFromConst(109);}; ConstFromConst(int i1){num1 = 109;}; ConstFromConst(int i1,int i2){}; void print() { cout << num1<<endl; cout << num2<<endl; } virtual ~ConstFromConst(){}; }; void main() { //your code ConstFromConst a; a.print(); }

Page 129: ICT C++

100

101

num1 and num2 will be 100 and 101 and not 109 and

101.

So when you call another constructor, another object is

created and then destroyed with in the scope of the

constructor that calls another constructor.

So calling another constructor is pretty useless in the

body of a constructor.

Answer

Page 130: ICT C++

Instead of char* what type can be used in pure

C++ to describe the String?

Question No : 65

Page 131: ICT C++

basic_string is the class defined for this

purpose, string is typedefed as

basic_string<char>

Answer

Page 132: ICT C++

What happens if there is no catch block suitable

to the throw type?

Question No : 66

Page 133: ICT C++

Application crashes due to unhandled exception

Answer

Page 134: ICT C++

Is the program given below a valid one?

Question No : 67

#include<iostream.h>int func(int i);double func(int i);void main(void){

cout<<func(10);cout<<func(10.201);

}int func(int i){

return i;}double func(int i){

return i;}

Page 135: ICT C++

No, because you cannot overload functions if they

differ only in terms of the data type they return.

They should vary with arguments they take in,

that are called as function overloading.

Answer

Page 136: ICT C++

Determine the output for the following program.

Question No : 68

#include <iostream>using namespace std;class arith {public:void calc(int num1){

cout<<"Square of a given number: " <<num1*num1 <<endl;}void calc(int num1, int num2 ){

cout<<"Product of two whole numbers: " <<num1*num2 <<endl;}};int main() //begin of main function{

arith a;a.calc(5);a.calc(6,7);

}

Page 137: ICT C++

Square of a given number: 25

Product of two whole numbers: 42

Answer

Page 138: ICT C++

Determine the output for the following program.

Question No : 69

#include <iostream>using namespace std;class arith {public:void calc(int num1){

cout<<"Square of a given number: " <<num1*num1 <<endl;}void calc(int num1, int num2 ){

cout<<"Product of two whole numbers: " <<num1*num2 <<endl;}};int main() //begin of main function{

arith a;a.calc(5);a.calc(6,7);

}

Page 139: ICT C++

Square of a given number: 25

Product of two whole numbers: 42

Answer

Page 140: ICT C++

How does the C++ compiler process inline

functions included in a header file?

Question No : 69

Page 141: ICT C++

The compiler, when it encounters a definition of an inline function as a

header, puts the function type (the signature combined with the return

value) and the function body in its symbol table. When the function is used

somewhere in the program, the compiler not only checks to ensure that

the call is correct but the return value is also used correctly. It then

substitutes the function body for the function call, thereby eliminating the

overhead. An inline function in a header file has a special status, since one

must include the header file containing the function and its definition in

every file where the function is used, and doesn’t end up with multiple

definition errors (however, the definition must be identical in all places

where the inline function is included).

Answer

Page 142: ICT C++

What is a template?

Question No : 70

Page 143: ICT C++

Templates allow creating generic functions that admit any

data type as parameters and return values without having

to overload the function with all the possible data types.

Its prototype is any of the two following ones:

The only difference between both prototypes is the use of

keyword class or typename, its use is indistinct since both

expressions have exactly the same meaning and behave

exactly the same way

Answer

template <class indetifier> function_declaration;template <typename indetifier> function_declaration;

Page 144: ICT C++

We want to swap int, char , float.

We need not define 3 functions; instead we can implement

it by using templete

Answer

function definitiontemplate <class T>T fn_name(argument name)


Recommended