669
WWW.VIRTUALINSPIRE.COM Cs 201 2 Feb 2012 Final Term Long Question Q1: Given is the code for a class named Coordinate.(10) class Coordinate { private: int X, Y; public: Coordinate() { X = 0; Y = 0; } class Coordinate { friend void Displaycord(); private: int X, Y; public: Coordinate() { X = 0; Y = 0; } }; Q2: Write a program that defines a Template function named Square () which finds the square of a number and then return it. Define one variable of type int in main function and then call Square function on that. (10) Program output should look like this: Enter an integer value to find its Square: 6 Square of integer number is: 36 Q3: Why is it necessary to initialize a static object as the time of creation and how it is initialized ?(5)

api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMCs 2012 Feb 2012 Final Term

Long Question 

Q1: Given is the code for a class named Coordinate.(10)

class Coordinate{

private:

int X, Y;

public:

Coordinate(){X = 0;Y = 0;} 

class Coordinate{friend void Displaycord();private:

int X, Y;

public:

Coordinate(){X = 0;Y = 0;} 

};

Q2: Write a program that defines a Template function named Square () which finds the square of a number and then return it. Define one variable of type int in main function and then call Square function on that. (10)

Program output should look like this:

Enter an integer value to find its Square: 6Square of integer number is: 36

Q3: Why is it necessary to initialize a static object as the time of creation and how it is initialized ?(5)

Q4:Transform the following If-else statement into switch statement.(5)

if (x == 5)...else if (x == 10 || x == 11)...else if (x == 8)...else if (x == 9)...else

Page 2: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM...

Q5:Read the given code and explain code functionality. (3)

Matrix :: Matrix ( const Matrix & m ){numRows = m.numRows ;numCols = m.numCols ;elements = new ( double * ) [ numRows ] ;for ( int i = 0 ; i < numRows ; i ++ ){elements [ i ] = new double [ numCols ] ;for ( int j = 0 ; j < numCols ; j ++ )elements [ i ] [ j ] = m.elements [ i ] [ j ] ;}}

Hint : This function belong to a matrix class, having Number of Rows = numRowsNumber of Columns = numCols

Q6: In the following code;(3)

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

double j; 

getche();

}

Is double j; an example of global variable or local variable ? Why ? 

Q7: Identify and correct the error in the given code segment.(3)main(){int val = 30;int &ref = val; 

&ref = &ref++; 

}

Q8: Determine the output of the given program code.(2)

for (int I = 1; i<5; i++){if ( I == 3)continue; cout I endl ;}

Q9: What is the output of following code snippet.(2)

// class templates#include <iostream>using namespace std;

template <class T>class mypair {T a, b;public:

Page 3: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmypair (T first, T second){a=first; b=second;}T getmax ();};

template <class T>T mypair<T>::getmax (){T retval;retval = a>b? a : b;return retval;}int main () {mypair <int> myobject (100, 75);cout myobject.getmax();system("pause");}

Q10: How can we declare and initialize static data member of a class?(2)

Q11: When memory is allocated dynamically using new operator within the constructor of class then what is an appropriate place to de-allocate the memory?(2)

Q11: Write down the output of the following code segment? (2)

int x[10] = {0,1, 2, 3, 4, 5, 6, 7, 8, 9};int *xptr;xptr = &x[5];cout *(xptr++) + 1;

MCQSequence of event(s) when allocating memory using new operator is ____________.

Assignment operator is ________ associative

To avoid dangling reference, always return _________.Which value is returned by the destructor of a class?The functions used for dynamic memory allocation return pointer of type ______.The function free() returns back the allocated memory got through calloc and malloc to _____ .At the time of linking, library functions are available in the ________form.Which of the following is NOT a preprocessor directive?When we define an array of objects then ___________.A constructor will create a new object with a full copy of the other object, this type of copy is known as ___________.When an object of a class is defined inside another class then ______________.Every stream has _________._________in the following options is not a method of output stream.To perform manipulation with input/output, we have to include ________ header fileA class whose object is contained as const object, must have ____________.By using objects as class members, _________ is achievedThe operator function will be implemented as _____, if both objects (obj1, obj2) are passed as arguments to - operator in the statement given below. obj3 = obj1 - obj2;

ANSI stands for _________.A template function must have at least _______________ generic data type.

Which of the following is true about operator overloading?1-The operator works with only objects of the class in which it is overloaded.2-The operator works with objects and primitive data types regardless of parameters.3-The operator works with both objects and primitive data types depending on parameters.4-The operator must have a constant and an object as parameters.Operator overloading can be performed through__________________.The pointer returned by the new operator points to ___________ of memory chunks allocated by the new operator.

The return type of the operator function for operator is __________.

Page 4: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWhat will be the output of following statement?

cout setfill(‘0’) setw(7) 128 ;

The operator used for casting in C is standard ________ operator.

Consider the following code segment.

struct intorDouble{Int ival;Double charvar;}; 

main(){ intorDouble VAZ;int size ;size = sizeof(VAZ);}

What will be the value of variable size if integer occupies 4 bytes and double occupies 8 bytes?

The file-open mode __________ is used to open a file for output and move to the end of the file.

In the statement int &x = y; compiler ______.Which header file must be included to use the functions tolower() and toupper()?Which one of the following functions is included in stdlib.h header file?For one byte, there are _____ combinations of values which can be stored in computer.Which of the following statement is best regarding declaration of friend function?Answer- It can be declared anywhere in class as these are not affected by the public and private keywords....

Long Question

Q1: Given is the code for a class named Coordinate.(10)

class Coordinate{

private:

int X, Y;

public:

Coordinate(){X = 0;Y = 0;

Page 5: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM}

class Coordinate{friend void Displaycord();private:

int X, Y;

public:

Coordinate(){X = 0;Y = 0;}

};

Q2: Write a program that defines a Template function named Square () which finds the square of a number and then return it. Define one variable of type int in main function and then call Square function on that. (10)

Program output should look like this:

Enter an integer value to find its Square: 6Square of integer number is: 36

Q3: Why is it necessary to initialize a static object as the time of creation and how it is initialized ?(5)

Q4:Transform the following If-else statement into switch statement.(5)

if (x == 5)...else if (x == 10 || x == 11)...else if (x == 8)

Page 6: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM...else if (x == 9)...else...

Q5:Read the given code and explain code functionality. (3)

Matrix :: Matrix ( const Matrix & m ){numRows = m.numRows ;numCols = m.numCols ;elements = new ( double * ) [ numRows ] ;for ( int i = 0 ; i < numRows ; i ++ ){elements [ i ] = new double [ numCols ] ;for ( int j = 0 ; j < numCols ; j ++ )elements [ i ] [ j ] = m.elements [ i ] [ j ] ;}}

Hint : This function belong to a matrix class, having Number of Rows = numRowsNumber of Columns = numCols

Q6: In the following code;(3)

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

double j;

getche();

}

Is double j; an example of global variable or local variable ? Why ?

Q7: Identify and correct the error in the given code segment.(3)

Page 7: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmain(){int val = 30;int &ref = val;

&ref = &ref++;

}

Q8: Determine the output of the given program code.(2)

for (int I = 1; i<5; i++){if ( I == 3)continue; cout I endl ;}

Q9: What is the output of following code snippet.(2)

// class templates#include <iostream>using namespace std;

template <class T>class mypair {T a, b;public:mypair (T first, T second){a=first; b=second;}T getmax ();};

template <class T>T mypair<T>::getmax (){T retval;retval = a>b? a : b;return retval;}int main () {mypair <int> myobject (100, 75);cout myobject.getmax();system("pause");}

Page 8: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Q10: How can we declare and initialize static data member of a class?(2)

Q11: When memory is allocated dynamically using new operator within the constructor of class then what is an appropriate place to de-allocate the memory?(2)

Q11: Write down the output of the following code segment? (2)

int x[10] = {0,1, 2, 3, 4, 5, 6, 7, 8, 9};int *xptr;xptr = &x[5];cout *(xptr++) + 1;

MCQSequence of event(s) when allocating memory using new operator is ____________.

Assignment operator is ________ associative

To avoid dangling reference, always return _________.Which value is returned by the destructor of a class?The functions used for dynamic memory allocation return pointer of type ______.The function free() returns back the allocated memory got through calloc and malloc to _____ .At the time of linking, library functions are available in the ________form.Which of the following is NOT a preprocessor directive?When we define an array of objects then ___________.A constructor will create a new object with a full copy of the other object, this type of copy is known as ___________.When an object of a class is defined inside another class then ______________.Every stream has _________._________in the following options is not a method of output stream.To perform manipulation with input/output, we have to include ________ header fileA class whose object is contained as const object, must have ____________.By using objects as class members, _________ is achievedThe operator function will be implemented as _____, if both objects (obj1, obj2) are passed as arguments to - operator in the statement given below. obj3 = obj1 - obj2;

ANSI stands for _________.A template function must have at least _______________ generic data type.

Which of the following is true about operator overloading?1-The operator works with only objects of the class in which it is overloaded.2-The operator works with objects and primitive data types regardless of parameters.

Page 9: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM3-The operator works with both objects and primitive data types depending on parameters.4-The operator must have a constant and an object as parameters.Operator overloading can be performed through__________________.The pointer returned by the new operator points to ___________ of memory chunks allocated by the new operator.

The return type of the operator function for operator is __________.

What will be the output of following statement?

cout setfill(‘0’) setw(7) 128 ;

The operator used for casting in C is standard ________ operator.

Consider the following code segment.

struct intorDouble{Int ival;Double charvar;};

main(){ intorDouble VAZ;int size ;size = sizeof(VAZ);}

What will be the value of variable size if integer occupies 4 bytes and double occupies 8 bytes?

The file-open mode __________ is used to open a file for output and move to the end of the file.

In the statement int &x = y; compiler ______.Which header file must be included to use the functions tolower() and toupper()?Which one of the following functions is included in stdlib.h header file?For one byte, there are _____ combinations of values which can be stored in computer.Which of the following statement is best regarding declaration of friend function?Answer- It can be declared anywhere in class as these are not affected by the public and private keywords.The operator used for dereferencing the elements of an array using a pointer is _________.The __________ statement forces the immediate next iteration of the loop.

Page 10: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMA function is a block of statements (code) that can be __________.Which of the following values are used in C/C++ to represent true and false?Ans- 1 and 0The dynamic memory allocation uses memory from the ____________.In C/C++, null character is represented as ________________.

Another paper

what is stack un winding?

what is templet?

what is relation ship between person and teacher?? diagaram thi?

what is iterator?

what is types of iterator?

long Question horse is a good pet animal describe by oop that about horse?

give C++ code to simplyfiy that oop is case senstive

(please friends dua lazmi karna mery liay mera paper normal howa hai so ap ki dua ki b zaroort hai)

give c++ code and define a templete fuction

c++ code that eror corect karna tha .....templete ka tha??

eror handling ki type this??

aur mcqs half past say half new thay...

Solved By: Hamid Khan

BS IT (3rd Semester)

[email protected]

Question No: 1- There are mainly -------------------- types of software

Page 11: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Two

► Three

► Four

► Five

Question No: 2- seekg() and write() are functionally _________________ .

► Different

► Identical

► Two names of same function

► None of the above

Question No: 3- When a pointer is incremented, it actually jumps the number of memory addresses

► According to data type

► 1 byte exactly

► 1 bit exactly

► A pointer variable can not be incremented

Question No: 4- setw is a parameterized manipulator.

► True

► False

Question No: 5- eof( ), bad( ), good( ), clear( ) all are manipulators.

► True

► False

Question No: 6- In functions that return reference, use __________variables.

► Local

► Global

► Global or static

► None of the given option

Question No: 7- The declarator of Plus (+) member operator function is

► Class-Name operator + (Class-Name rhs)

► operator Class-Name + ( )

► operator Class-Name + ( rhs)

Page 12: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Class-Name operator + ( )

Question No: 8- The compiler does not provide a copy constructor if we do not provide it.

► True

► False

Question No: 9- What is the functionality of the following syntax to delete an array of 5 objects named arr allocated using new operator?

delete arr ;

► Deletes all the objects of array

► Deletes one object of array

► Do not delete any object

► Results into syntax error

Question No: 10- What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

► Memory is allocated first before calling constructor

► Constructor is called first before allocating memory

Question No: 11- What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

► Destructor is called first before deallocating memory

Question No: 12- new and delete operators cannot be overloaded as member functions.

► True

► False

Question No: 13- The operator function of << and >> operators are always the member function of a class.

► True

► False

Question No: 14 - A template function must have at least ---------- generic data type

Page 13: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Zero

► One

► Two

► Three

Question No: 15 - If we do not mention any return_value_type with a function, it will return an _____ value.

► int

► void

► double

► float

Such function which do not return any value are called:

► int

► void

► double

► float

Question No: 16 - Suppose a program contains an array declared as int arr[100]; what will be the size of array?

► 0

► 99

► 100

► 101

Question No: 17 - The name of an array represents address of first location of array element.

► True

► False

Question No: 18 - Reusing the variables in program helps to save the memory

► True

► False

Question No: 19 - Which of the following option is true about new operator to dynamically allocate memory to an object?

► The new operator determines the size of an object

Page 14: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Allocates memory to object and returns pointer of valid type

► Creates an object and calls the constructor to initialize the object

► All of the given options

Question No: 20 - new and delete are _____ whereas malloc and free are _____.

► Functions, operators

► Classes, operators

► Operators, functions

► Operators, classes

Question No: 21 - Like member functions, ______ can also access the private data members of a class.

► Non-member functions

► Friend functions

► Any function outside class

► None of the given options

Question No: 22 - Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition.

► It can be declared anywhere in class as these are not affected by the

public and private keywords.

Question No: 23 - The operator function overloaded for an Assignment operator (=) must be

► Non-member function of class

► Member function of class

► Friend function of class

► None of the given options

Question No: 24 - For non-member operator function, object on left side of the operator may be

► Object of operator class

► Object of different class

Page 15: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Built-in data type

► All of the given options

Question No: 25 - The operator function will be implemented as _____, if obj1 drive the - operator whereas obj2 is passed as arguments to - operator in the statement given below.

obj3 = obj1 - obj2;

► Member function

► Non-member function

► Friend function

► None of the given options

Question No: 26 - Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?

► Class-name operator +() ;

► Class-name operator +(int) ;

► Class-name operator ++() ;

► Class-name operator ++(int) ;

Question No: 27 - The static data members of a class are initialized _______

► at file scope

► within class definition

► within member function

► within main function

Question No: 28 - Class is a user defined___________.

► data type

► memory referee

► value

► none of the given options.

Question No: 29 - We can also define a user-defines manipulators.

► True

► False

Question No: 30 - Automatic variable are created on ________.

► Heap

Page 16: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Free store

► static storage

► stack

Question No: 31- How do we provide the default values of function parameters?

The default value of a function parameter is provided inside the function prototype or function definition. For example, we could declare the default function arguments for a function while declaring or defining it. Question No: 32

Why do java consider pointer as dangerous

The concept of pointers is very important but quite limited to C and C++. The modern languages, for example JAVA, describe pointers as dangerous. We can go anywhere in the memory and can change a value. There is another problem with pointers, which is that these could be pointing to nowhere. Question No: 33

What is memory leak?

Answer: size. When there is no memory on heap, the computer will stop running and there may be a system crash. This situation is called a memory leak. Question No: 34

What does optimization of code means?

Header file is a nice mechanism to put function prototypes and define constants (global constants) in a single file. That file can be included simply with a single line of code.

Question No: 35

What is the difference between structure and class? In structures, some data variables are gathered, grouped and named as a single entity. Class and structure are very closely related. In classes, we group some data variables and functions. These functions normally manipulate these variables. Before going ahead, it is better to understand what a class is: “A class includes both data members as well as functions to manipulate that data” Question No: 36

See the following code segment.

template <class T>

class myclass {

private:

T x;

public:

Page 17: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmyclass (T a) {

x = a;

}

};

Write the main function which creates two objects of class for int and double data types.

Question No: 37

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Solution:

No, it is impossible to define two functions as in the main function the way to call both functions is same. How does the compiler know that which functions is being called? There is no way for the compiler to find out. Therefore there is an ambiguity and that is not allowed. The only thing to realize is the side effect. Side effects are critical to take care of whenever you are doing call by reference. Question No: 38

Write a program using getline() member function to inputs a string up to delimiter character comma (,) and then display the string on the screen.

Question No: 39( Marks: 5 )

Do you think that friend functions violate encapsulation? Justify your answer.

The friend functions of a class have access to the private data members of class. Despite being a good thing, there is possibility of vulnerability. We are opening our thoughts, inside view for somebody else. Without having 100% trust, it will be risky to make our thoughts and feelings public. We want that our private data is accessible to someone outside, not public for everybody. Otherwise, the data encapsulation and data-hiding concept will be violated. We keep the data members private and declare some specific functions that are not member of the class but friend of the class. As friends, they have access to the inside data structure of the class despite not being members.Question No: 1- To access the data members of structure _______ is used.

► dot operator (.)

► * operator

► à operator

► None of given.

Question No: 2- What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?

Page 18: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► 1

► 2

► 3

► 4

Question No: 3- If constructor contains a return statement in its body then compiler will give __________

► No error

► Syntax error

► Logical error

► Run time error

Question No: 4- We can use New keyword inside of Class Constructor.

►True

►False

Question No: 5- When an operator function is define as member function for a Unary operator then the number of argument it take is/are,

►Zero

►One

►Two

►N arguments

Question No: 6- The declarator of Plus (+) member operator function is

►Class-Name operator + (Class-Name rhs)

►operator Class-Name + ( )

►operator Class-Name + ( rhs)

►Class-Name operator + ( )

Question No: 7- Friend function of a class is ______________ .

►Member function

►Non-member function

►Private function

►Public function

Question No: 8- We can also create an array of user define data type.

Page 19: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ►True

►False

Question No: 9- What is the sequence of event(s) when deallocating memory using delete operator?

►Only block of memory is deallocated for objects

►Only destructor is called for objects

►Memory is deallocated first before calling destructor

►Destructor is called first before deallocating memory

Question No: 10- Deleting an array of objects without specifying [] brackets may lead to memory leak

►True

►False

Question No: 11- Which of the following data type(s) can operate on modulus operator ‘%’?

► float, int

► float, double

► int

► char

Question No: 12- Array is passed by value to a function by default.

►True

►False

Question No: 13- With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.

►True

►False

Question No: 14- What will be the correct syntax to initialize all elements of two-dimensional array to value 0?

►int arr[2][3] = {0,0} ;

►int arr[2][3] = {{0},{0}} ;

►int arr[2][3] = {0},{0} ;

►int arr[2][3] = {0} ;

Page 20: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 15- When an operator function is define as member function then operand on the left side of operator must be an object.

►True

►False

Question No: 16- break statement can be used outside a loop or switch statement.

►True

►False

Question No: 17- The keyword_______ is used to return some value from a function.

►return

►break

►continue

►goto

Question No: 18- Every data member or function inside the structure is ___ by default whereas everything declared inside a class is ____ by default.

►private, public

►public, private

►private, protected

►public, protected

Question No: 19- Which of the following is true for the C++ statement given below?

int &ref = val ;

►It creates a synonym for variable ‘val’

►It creates an alias for variable ‘val’

►It’s a new name for variable ‘val’

►All of the given options

Question No: 20- If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below?

obj3 = obj1 + obj2 ;

►obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator

►obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator

Page 21: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ►Both objects (obj1, obj2) will be passed as arguments to the +

operator

►Any of the objects (obj1, obj2) can drive the + operator

Question No: 21- Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.

►One, zero

►Zero, one

►One, two

►Two, one

Question No: 22- The input/output streams cin and cout are ________ therefore have _______.

► Structures, function

► Objects, member functions

► Functions, objects

► None of the given options

Question No: 23- If a symbolic constant has been defined, it will be an error to define it again.

► True

► False

Question No: 24- Every class contains _______________.

► Constructor

► Destructor

► Both a constructor and a destructor (not sure)

► None of the given options

Question No: 25- new and delete keywords are _____________ in C++ language.

► Built-in- Function

► Operators

► Memory Allocation Function

► None of the given options

Question No: 26- Consider the following code segment.

class M {

Page 22: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMpublic:

M &operator+(const M &);

...

};

p + q

...

Let assume if p and q are class objects then function is implemented as _______

►Member function

►Non-member function

►Friend function

►None of the given options

Question No: 27- Assignment operator is -------------------------associative.

►right

►left

►binary

►unary

Question No: 28- Static variable which is defined in a function is initialized __________.

► Only once during its life time

► Every time the function call

► Compile time of the program

► None of the above

Question No: 29- We can not define a variable of user-defined data type in the class.

►True

►False

Question No: 30- A constructor that will create a new object with a full copy of the other object, is copy is known as ___________

►deep copy

►shallow copy

►constructor copy

Page 23: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ►none of the options

Question No: 31

What is the use of reference data type?

A reference data type is a variable that can contain an address. The reference data types in Java are arrays, classes and interfaces. You'll hear often say that Java does not have pointers. Yet, you could consider a reference data type to be a pointer

Question No: 32

What are the main types of operators in terms of number of arguments they take?

The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.

Question No: 33

What is the this pointer? Give an example of its use

In a C++ program, if you create object A of class X, you can then obtain the address of A by using the "this" pointer. The address is available as a local variable in the non-static member functions of X, and its type is const X*. The "this" pointer works because C++ creates instances of its data members, and it keeps one copy of each member function.

Question No: 34

What are manipulators? Give one example.

Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of displayed endl manipulator. This manipulator has the same functionality as the ‘\n’ newline character.

Question No: 35

What will be the output of following functions if we call these functions three times?

1)void func1(){

int x = 0;

x++;

cout << x << endl;

}

Output will be:

Page 24: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM111

2)void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Output will be:

123

Question No: 36

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

malloc returns a void pointer to the allocated space or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item.

By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when malloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason.

Question No: 38

Write down the disadvantages of the templates.

The disadvantages of templates are:

• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Page 25: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 39

The following code segment has errors. Locate as many as you can and explain briefly.

class Circle // no need to enter colon here , so I removed it

{

private : //colon missing

double centerX;

double centerY;

double radius;

public: //colon missing

void setCenter(double, double);

void setRadius(int);

};//semi colon missing

Question No: 40

Write a program which consists of two classes, Date and Person.

Date class should contain three data members day, month, year and setter and getter function for these data members. Date class should also contain showdate() member function to display date.

Person class should contain three data members Name, Address, and Bday, where Name and Address are char pointer while Bday(Date of birth) is of type Date, Person class should further contain two member functions Display() and setdate().

In main program Create an object of Class person and call the member functions with it.

Please find the Answer in following Box

#include <stdio.h>#include <iostream>#include <cstring>using namespace std;

class Date{public:

int day;int month;int year;

public:Date()

{day=0;month=0;

Page 26: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMyear=0;

}

void setDay(int);void setMonth (int);void setYear(int);

int getDay();int getMonth();int getYear();

void showDate();};void Date: :setDay(int d){

if{d<1 | | d>31)cout<<"Invalid month Renter it";cin>>d;

}day=d;

}

void Date: :setMonth (int m){

if(m<1 | | m>12){

cout<<"Invalid month Renter it";cin>>m;

}month=m;

}void Date: :setYear (int y)

{year=y;

int Date: :getDay(){

return day;}

int Date: :getMonth(){

return month:}

int Date: :getYear(){

return year;}

void Date: :showDate(){

cout<<day<<"-"<<month<<"-"<<year<<end1;}Class Person{public:

Page 27: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMchar *Name;

char *AddressDate Bday;

public:Student(){Name=new char[20];Address=new char[10];cin.getline(Name,20);cout<<"Enter Address:";cin.getline(Address,10);}

void setDate(){

cout<<"Enter Day:";cin>>Ad_date.day;cout<<"Enter month:";cin>>Ad_date.month;cout<<"Enter Year:";cin>>Ad_date.year;}

void Display(){cout<<"Name: "<<end1;cout<<"Address: "<<Address<<end1;cout<<"Date of Birth: ";Ad-date.showDate();}

};void main(){

Person object;object.setDate();

object.Display();

system("pause");}

Question No: 41

Write a C++ program that contains a class ‘myClass’ having two data members of type int.

The class must have

· A default constructor which must initialize all the data members to their meaningful values.

· A destructor with no implementation.· Setter member functions to set all data members of class· Getter member functions to get all data members of class

In main function of the program

Page 28: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM5. Prompt the user to enter the number of objects to be created.6. Dynamically allocate memory to objects according to the size entered by user.7. De-allocate memory that was allocated to objects

Find solution in following box:

#include <stdio.h>#include <iostream>#include <cstring>using namespace std;

class myclass

{

public:int a;int b;int *iptr, *sptr;

construct{int,int.int}

void seta(int);void setb(int);void setc(int);

int geta();int getb();int getc();

};

void Person: :seta(int aa){

a=aa;}

void Person: :setb (int bb){

b=bb;}

void Person: :setc (int cc){

c=cc;}

main()

{

int num;

Page 29: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMcout<<"Enter the number of objects to be created";

cin>>num;

for (int i =1;i==num;i++){

Person i_

}

}

Question No: 1- Operator overloading can be performed through_________.

       ► Classes

       ► Functions

       ► Operators

       ► ReferenceQuestion No: 2- When a value is referred by a normal variable then it is known as,

       ► Direct Reference

       ► Indirect Reference

       ► Partial Reference

       ► Proper Reference

When a value is referred by a normal variable is known as direct reference

Question No: 3- Which of the following function is used to increase the size of already allocated memory chunk?

        ► malloC

► calloc

       ► realloc

       ► free

Question No: 4- Which of the following is NOT a preprocessor directive?

       ► #error

       ► #define

       ► #line

       ► #ndefinelist of preprocessors

Page 30: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM• #include • #include “filename” • #define • #undef • #ifdef • #ifndef • #if • #else • #elif • #endif • #error • #line • #pragma • #assert

Question No: 5- The stream objects cin and cout are included in which header file?

       ► iostream.h

       ► fstream.h

       ► istream.h

       ► ostream.h

Question No: 6- Overloaded delete operator function takes the same parameter as an argument returned by new operator function.

       ► True

       ► False

The same pointer that is returned by the new operator, is passed as an argument to the delete operator. These rules apply to both, if operators (new and delete) are overloaded as member or non-member operators (as global operators).

Question No: 7- When an array of object is created dynamically then there is no way to provide parameterized constructors for array of objects.

       ► True

       ► False

if we are allocating an array of objects, there is no way to pass arguments to objects’ constructors. Therefore it is required that the objects that are stored in such an array have a no-argument constructor.

Question No: 8- C is widely known as development language of _______ operating system.

       ► Linux

       ► Windows

       ► Unix

   ► Mac OS

In the start C became widely known as the development language of the UNIX operating system, and the UNIX operating system was written by using this C language. The C language is so powerful that the compiler of C and other various operating systems are written in C.

Question No: 9- Computer can understand only machine language code.

       ► True 

       ► False

Page 31: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 10- We can not define a function as a friend of a Template class.

       ► True

       ► False 

Class templates can have friends. A class or class template, function, or function template can be a friend to a template class. Friends can also be specializations of a class template or function template, but not partial specializations.

Question No: 11-What will be the value of ‘a’ and ‘b’ after executing the following statements?

a = 3;

b = a++;

       ► 3, 4

       ► 4, 4

       ► 3, 3

       ► 4, 3

Question No: 12     

Consider the following code segment. What will be the output of following code?

int addValue (int *a){

int b = (*a) + 2;

return b ;

}  

main () {

int x =6 ;

cout <<  x << “,” ;

cout << addValue(&x) << “,” ;

cout <<  x ;

}

       ► 6,8,6        ► 6,6,8

       ► 6,8,8

       ► 6,6,6

Page 32: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 13- _______ is used to trace the logic of the program and correct the logical errors.

       ► Compiler

       ► Editor

       ► Linker

       ► Debugger

Question No: 14-  new and delete are _____ whereas malloc and free are _____.

       ► Functions, operators

       ► Classes, operators

       ► Operators, functions

       ► Operators, classes

Hence, we can call new and delete operators, P# 342we have allocated a memory space for our use by malloc function. P# 285Question No: 15- Like member functions, ______ can also access the private data members of a class.

       ► Non-member functions

       ► Friend functions

       ► Any function outside class

       ► None of the given options

Question No: 16- Which situation would require the use of a non-member overloaded operator?

       ► The overloaded operator is an Assignment operator.

       ► The left most operand is an object of a class.        ► The left operand is built-in data type.

       ► The operator returns a reference.When an operator function is implemented as a non-member function, the left-most operand may be an object of the operator’s class, an object of a different class, or a built-in type

Question No: 17-  The stream insertion and stream extraction operators are already overloaded for ______.

       ► User-defined data types

       ► Built-in data types

       ► User-defined and built-in data types

       ► None of the given options

Page 33: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 18-  If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.

       ► can not be replaced

       ► None of the given options

       ► Remain constant.

       ► can be changed by some operation

Question No: 19- Assignment operator is -------------------------associative.

       ► right

       ► left

       ► binary

       ► unary

You can assign values to several variables in a single statement. For example, the following code sets the contents of apples and oranges to the same value:

apples = oranges = 10;

The assignment operator is right associative, so this statement executes by first storing the value 10 in oranges and then storing the value in oranges in apples, so it is effectively

apples = (oranges = 10);

Question No: 20-  When ever dynamic memory allocation is made in C/C++, it is freed_____________.

       ► Explicitly

       ► Implicitly

       ► Both explicitly and implicitly

       ► None of the given options

Question No: 21-  The appropriate data type to store the number of rows and colums of the matrix is____________.

       ► float

       ► int

       ► char

       ► none of the given options.

Question No: 22- Which of the following function do NOT initialize the chunk of memory to all zero?

       ► calloc() function 

Page 34: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Both malloc() and calloc()

       ► None of the above 

       ► malloc() function  The malloc function differs from calloc in the way that the space allocated by malloc is not initialized and contains any values initially.

Question No: 23-  The function free() returns back the allocated memory got thorough calloc and malloc to _____ .

       ► stack

       ► heap

       ► stack and heap

       ► None of the given options

Question No: 24-  width() is member function of _____________

       ► cin object

       ► cout object

       ► Both cin and cout object

       ► None of the given option 

Question No: 25- Templates are not type safe.

       ► true

       ► false

Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

Question No: 26-  A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

       ► Write a separate class to handle each

       ► Use templates

       ► Use strings to store all types

       ► None of the given options

A Matrix can be composed of ints, floats or doubles as their elements. Instead of handling these data types separately, we can write Matrix class as a template class and write code once for all native data types. While writing this template class, the better approach to write will be, to go with a simple data type (e.g. double) first to write a Matrix class and then extend it to a template class later.Question No: 27   

Page 35: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMGive the general syntax of class template.

template

class myclass { ---} ;

 

Question No: 28

What is a truth Table?

There are some areas where the decision structures become very complicated. Sometimes, we find it difficult to evaluate a complicated logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax statement in any language. It becomes complicated to determine what will be evaluated in what way. We know the concept of truth table. The truth tables are very important. These are still a tool available for analyzing logical expressions. We will read logic design in future, which is actually to do with chips and gates. How we put these things together.

Question No: 29

What will be the output of following code, if user input a number 123?

int input ;

cin >> oct >> input;

cout << hex << input ;

53

Rational: it will take 123 as octal and print it in hex form which is 53. 

Question No: 30

What is principle of friendship in the context of functions and classes?

Class can declare a friend function and someone from outside the class cannot declare itself friend of a class.

A friend function can access the private variables of class just like a member function

Question No: 31

What are the limitations of the friendship relation between classes?

Class can declare a friend class from inside and someone from outside the class cannot declare itself friend of a class.

Question No: 33

Define static variable. Also explain life time of static variable?

When you declare a static variable (native data type or object) inside a function, it is created and initialized only once during the lifetime of the program 

Page 36: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 34

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

#include

#include

main () {

double a = 12.12345;

double b = 13.123456;

double c = 14.1234567;

cout << setprecision (5) << a << endl;

cout << setprecision (2) << a << endl;

cout << setprecision (3) << a << endl;

}

Question No: 35

Let we have a class,

class String

{

private:

char  buf[25];

};

Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment 

Answer:

void String::operator = ( const String &other )

 { int length ;

 length = other.length();

delete buf;

 buf = new char [length + 1];

 strcpy( buf, other.buf ); }

Page 37: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMSolved By: Hamid Khan

BS IT (3rd Semester)

[email protected]

Question No: 1- *.doc is _____________ by type.

► Sequential File

► Random Access File

► Data File

► Record File

Question No: 2- Which of the following is NOT a preprocessor directive?

► #error

► #define (wrong)

► #line

► #ndefine (correct)

Question No: 3- The return type of operator function must always be void.

► True

► False

Question No: 4- What does (*this) represents?

► The current function of the class

► The current pointer of the class

► The current object of the class

► A value of the data memberQuestion No: 5 The statement cin.get (); is used to,

► Read a string from keyboard

► Read a character from keyboard

► Read a string from file

► Read a character from file

Question No: 6 When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor.

► True

Page 38: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► False

Question No: 7 Overloaded new operator function takes parameter of type size_t and returns

► void (nothing)

► void pointer

► object pointer

► int pointer

Question No: 8 The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

► False

Question No: 9 C++ is a case-sensitive language

► True

► False

Question No: 10 To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

► include <iostream.h>

► #include <iostream.h>

Question No: 11 A template function must have only generic data types.

► True

► False

Question No: 12 Template class can not have static variables.

► True

► False

Question No: 13 What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?

► *ptr = arr ; ► ptr = arr ; ► *ptr = arr[5] ; ► ptr = arr[5] ; Question No: 14 What will be the correct syntax to access the value of fourth element of an array using pointer ptr?

Page 39: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► ptr[3]

► (ptr+3)

► *(ptr+3)

► Both 1and 3

Question No: 15 If most significant bit of un-signed number is 1 then it represents a positive number.

► True

► False

Question No: 16 If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____.

► Address of variable, reference variable

► Reference variable, value of variable

► Reference variable, address of variable

► Address of variable, value of variable

Question No: 17 We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 18 The default value of a parameter can be provided inside the ________

► function prototype

► function definition

► both function prototype or function definition

► none of the given options.

Question No: 19 Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Question No: 20 What purpose do classes serve?

► Data encapsulation

► Providing a convenient way of modeling real-world objects

Page 40: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Simplifying code reuse

► All of the given options

Question No: 21 Every class contains _______________.

► Constructor

► Destructor

► Both a constructor and a destructor

► None of the given options

Question No: 22 new operator is used to allocate memory from the free store during

► Compile Time

► Run Time

► Link Time

► None of the given options

Question No: 23 When an object of a class is defined inside another class then,

► Destructor of enclosing class will be called first

► Destructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 24 It is possible to define a class within another class.

► True

► FalseQuestion No: 25 New and Delete are also used with _________ and data types as well.

► Class, Objects

► Structures, Pointers

► Both Class and structures

► None of above

Question No: 26 With New keyword, data types and class members are initialized with meaningful values instead of garbage.

► True

► False

Question No: 1In if structure the block of statements is executed only,

Page 41: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► When the condition is false

       ► When it contain arithmetic operators

       ► When it contain logical operators

       ► When the condition is true 

Question No: 2 Header file: fstream.h includes the definition of the stream classes __________.

       ► ifstream, fstream, cout

       ► ifstream, fstream, ofstream

       ► fstream, cin, cout

       ► None of the above

Question No: 3 To access the data members of structure _______ is used.

       ► dot operator (.)

       ► * operator

       ►  operatorà

       ► None of given. 

Question No: 4 eof( ), bad( ), good( ), clear( ) all are manipulators.

       ► True

       ► False 

Question No: 5 Which kind of functions can access private member variables of a class?

       ► Friend functions of the class

       ► Private member functions of the class

       ► Public member functions of the class

       ► Friend, private and public functions

Question No: 6 The return type of operator function must always be void.

       ► True

       ► False

Question No: 7 Friend function of a class is ______________ .

       ► Member function

       ► Non-member function             

       ► Private function

Page 42: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Public function

Question No: 8 Function implementation of friend function must be defined outside the class.

       ► True

       ► False (any where in the class)

Question No: 9 The normal source of cin object is,

       ► File

       ► Disk

       ► Keyboard

       ► RAM

Question No: 10 Which of the following is correct way to initialize a variable x of int type with value 10?

       ► int x ; x = 10;

       ► int x = 10;

       ► int x, x = 10;

       ► x = 10; http://groups.google.com/group/vuZs/

Question No: 11 Consider the following code segment. What will be the output of the following program?

int func(int) ;

int num = 10 ;

int main(){

int num ;

num = 5 ;

cout << num ;

cout << func(num) ;

}

int func(int x){

return num ;

}

       ► 5, 5  

       ► 10, 5

Page 43: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► 5, 10 

       ► 10, 10

Question No: 12 With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.

       ► True

       ► False

Question No: 13 What will be the correct syntax to declare two-dimensional array of float data type?

       ► float arr{2}{2} ;

       ► float arr[2][2] ;

       ► float arr[2,2] ;

       ► float[2][2] arr ;   

Question No: 14 The first parameter of operator function for << operator,

       ► Must be passed by value

       ► Must be passed by reference

       ► Can be passed by value or reference

       ► Must be object of class 

Question No: 15 Heap is constantly changing in size.

       ► True

       ► False

Question No: 16 While calling function, the arguments are assigned to the parameters from

       ► left to right.

       ► right to left 

       ► no specific order is followed

       ► none of the given options.

Question No: 17 Classes defined inside other classes are called ________ classes

       ► looped

       ► nested

       ► overloaded

       ► none of the given options.

Page 44: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 18 If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________

       ► can not be replace

       ► None of the given options

       ► Remain constant.

       ► can be changed by some operation 

Question No: 19 Which value is returned by the destructor of a class?

       ► A pointer to the class.

       ► An object of the class.

       ► A status code determining whether the class was destructed correctl

       ► Destructors do not return a value.

Question No: 20 Every class contains _______________.

       ► Constructor

       ► Destructor

       ► Both a constructor and a destructor

       ► None of the given options

Question No: A template function must have

       ► One or more than one arguments

       ► Only one argument

       ► Zero argument

       ► None of the given options 

Question No: 22 Structured Query Language is used for ______________

       ► Databases Management

       ► Networks

       ► Writing Operating System

       ► none of the given options

Question No: 23 When a call to a user-defined function finishes, the variable defined inside the function is still in existence.

       ► True

       ► False

Page 45: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 24 The precedence of an operator can be changed through operator overloading.

       ► True

       ► False

Question No: 25 A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

       ► Write a separate class to handle each

       ► Use templates

       ► Use strings to store all types

       ► None of the given options

Question No: 26 "delete" operator is used to return memory to free store, which is allocated by the "new" operator.

       ► True

       ► False

Question No: 1 Operator overloading can be performed through__________________.

       ► Classes

       ► Functions

       ► Operators

       ► Reference

Question No: 2 When a value is referred by a normal variable then it is known as,

       ► Direct Reference

       ► Indirect Reference

       ► Partial Reference

       ► Proper Reference

Question No: 3 Which of the following function is used to increase the size of already allocated memory chunk?

       ► malloc

       ► calloc

       ► realloc

       ► free

Question No: 4 Which of the following is NOT a preprocessor directive?

       ► #error

Page 46: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► #define

       ► #line

► #ndefine

Question No: 5 The stream objects cin and cout are included in which header file?

       ► iostream.h

       ► fstream.h

       ► istream.h

       ► ostream.h

Question No: 6 Overloaded delete operator function takes the same parameter as an argument returned by new operator function.

       ► True

       ► False

Question No: 7 When an array of object is created dynamically then there is no way to provide parameterized constructors for array of objects.

       ► True

► False

Question No: 8 C is widely known as development language of _______ operating system.

       ► Linux

       ► Windows

       ► Unix

   ► Mac OS

Question No: 9 Computer can understand only machine language code.

       ► True

       ► False

Question No: 10 We can not define a function as a friend of a Template class.

       ► True

       ► False

 Question No: 11 What will be the value of ‘a’ and ‘b’ after executing the following statements?

a = 3;

b = a++;

Page 47: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► 3, 4

       ► 4, 4

       ► 3, 3

       ► 4, 3

Question No: 12      ( Marks: 1 ) - Please choose one

Consider the following code segment. What will be the output of following code?

int addValue (int *a){

int b = (*a) + 2;

return b ;

}  

main () {

int x =6 ;

cout <<  x << “,” ;

cout << addValue(&x) << “,” ;

cout <<  x ;

}

       ► 6,8,6

       ► 6,6,8

       ► 6,8,8

       ► 6,6,6

Question No: 13  _______ is used to trace the logic of the program and correct the logical errors.

       ► Compiler

       ► Editor

       ► Linker

       ► Debugger

Question No: 14 new and delete are _____ whereas malloc and free are _____.

       ► Functions, operators

       ► Classes, operators

       ► Operators, functions

Page 48: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Operators, classes

Question No: 15 Like member functions, ______ can also access the private data members of a class.

       ► Non-member functions

       ► Friend functions

       ► Any function outside class

       ► None of the given options

Question No: 16      ( Marks: 1 ) - Please choose one

Which situation would require the use of a non-member overloaded operator?

       ► The overloaded operator is an Assignment operator.

► The left most operand is an object of a class.

       ► The left operand is built-in data type.

       ► The operator returns a reference.

Question No: 17 The stream insertion and stream extraction operators are already overloaded for ______.

       ► User-defined data types

       ► Built-in data types

       ► User-defined and built-in data types

       ► None of the given options

Question No: 18 If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.

       ► can not be replaced

       ► None of the given options

       ► Remain constant.

       ► can be changed by some operation

Question No: 19 Assignment operator is -------------------------associative.

       ► right

       ► left

       ► binary

       ► unary

Page 49: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 20 When ever dynamic memory allocation is made in C/C++, it is freed_____________.

       ► Explicitly

       ► Implicitly

       ► Both explicitly and implicitly

       ► None of the given options

Question No: 21 The appropriate data type to store the number of rows and colums of the matrix is____________.

       ► float

       ► int

       ► char

       ► none of the given options.Question No: 22 Which of the following function do NOT initialize the chunk of memory to all zero?

       ► calloc() function 

       ► Both malloc() and calloc()

       ► None of the above 

       ► malloc() function 

Question No: 23 The function free() returns back the allocated memory got thorough calloc and malloc to _____ .

       ► stack

       ► heap

       ► stack and heap

       ► None of the given options

Question No: 24 width() is member function of _____________

       ► cin object

       ► cout object

       ► Both cin and cout object

       ► None of the given option

Question No: 25 Templates are not type safe.

       ► true

Page 50: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► false

Question No: 26 A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

       ► Write a separate class to handle each

       ► Use templates

       ► Use strings to store all types

       ► None of the given options

Question No: 27 How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

Ans: Unary operator takes only one aurgument like i++ or i— (Post increment or post decrement operators for intergers) or ++i,--i (Pre increment or pre decrement operators for intergers) ,we can not make Unary operator as binary or binary as Unary operator.

Question No: 28 Which arithmetic operators cannot have a floating point operand?

Ans: Modulus operator. This operator can only be used with integer operands ONLY

Question No: 29 What are manipulators? Give one example.

Ans: The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14 . This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

Some manipulators are parameter less. We simply use the name of the manipulator that works. For example, we have been using endl, which is actually a manipulator, not data. When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it manipulates the output stream.

Question No: 30 Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;

Ans:

int matrix [3] [3] ;

matrix [0] [0] = 0;

matrix [0] [1] = 0;

matrix [0] [2] = 0;

matrix [1] [0] = 0;

matrix [1] [2] = 0;

Page 51: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmatrix [1] [2] = 0;

matrix [2] [0] = 0;

matrix [2] [1] = 0;

matrix [2] [2] = 0;

Question No: 31 Which one (copy constructor or assignment operator) will be called in each of the following code segment?

1) Matrix m1 (m2);

2) Matrix m1, m2;

m1 = m2;

3) Matrix m1 = m2;

Ans:

1) Matrix m1 (m2); copy constructor

2) Matrix m1, m2;

m1 = m2; assignment operator

3) Matrix m1 = m2; assignment operator

Question No: 32 What will be the output of following function if we call this function by passing int 5?

template <class T>T reciprocal(T x) {return (1/x); }

Ans: 1/5

Question No: 33 Identify the errors in the following member operator function and also correct them.

math * operator(math m);

math * operator (math m)

{

math temp;

temp.number= number * number;

return number;

}

Page 52: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMANS:

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator(math *m);

math *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

Question No: 34 Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

Ans:

#include <iostream>

#include <iomanip>

int main ()

{

double x1 = 12345624.72345

double x2 = 987654.12345

double x3 = 1985.23456

cout << setprecision (3) << x1<< endl;

cout << setprecision (4) << x2 << endl;

cout << setprecision (5) << x3<< endl;

return 0;

}

Question No: 35 What are the advantages and disadvantages of using templates?

Ans:

Page 53: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMMany thing can be possible without using templates but it do offer several clear advantages not offered by any other techniques:

Advanatages:

• Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

• Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

• Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.

• It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

Disadvantages:

On the other hand, if misused

• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 36 Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

math obj1, obj2;

obj2= 10 + obj1 ;

Ans:

#include <iostream.h>

math

{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

{

mth operator + (obj1,obj2)

Page 54: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmth operator + (obj1,obj2)

}

}

Solved By: Hamid KhanBS IT (3rd Semester)[email protected]

Question No: 1 && is -------------------- operator.       ► An arithmetic       ► Logical       ► Relational       ► Unary Question No: 2 Which of the following function(s) is/are included in ctype.h header file?       ► isdigit(int c)       ► isxdigit(int c )       ► tolower(int c)       ► All of the above Question No: 3 Disks are _______________ devices having access time of _________ milliseconds.       ► Electro-physical, 6       ► Electro-Mechanical, 4       ► Electro-physical, 5       ► Electro-Mechanical, 7Question No: 4 All preprocessor directives are started with the symbol______.       ► *       ► ++       ► @       ► # Question No: 5 When we use manipulators in our program then which header file should be included?       ► iostream.h       ► stdlib.h       ► stdio.h       ► iomanip.h Question No: 6 setprecision is a parameter less manipulator.       ► True       ► FalseQuestion No: 7 Which of the following is NOT a preprocessor directive?       ► #error       ► #define       ► #line       ► #ndefine Question No: 8 We can do arithmetic with references like pointers.

Page 55: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► True       ► False Question No: 9 What does (*this) represents?       ► The current function of the class       ► The current pointer of the class       ► The current object of the class       ► A value of the data memberQuestion No: 10 Friend function of a class is ______________ .       ► Member function       ► Non-member function       ► Private function       ► Public function Question No: 11 A function can declare itself a friend of a class.       ► True       ► False Question No: 12 Overloaded delete operator function takes the same parameter as an argument returned by new operator function.       ► True       ► FalseQuestion No: 13 The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.       ► True       ► False Question No: 14 A template function must have at least ---------- generic data type       ► Zero       ► One       ► Two       ► Three Question No: 15 A template function must have only generic data types.       ► True       ► FalseQuestion No: 16 We can not make a member function of a class as template function.       ► True       ► False Question No: 17 Which character is inserted at the end of string to indicate the end of string?       ► new line       ► tab       ► null       ► carriage return Question No: 18 Assignment operator is used for ___________.       ► calculation       ► reading       ► assigning value to variables       ► None of the given options.Question No: 19 The object _______________may be used both for file input and file

Page 56: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMoutput       ► fstream,       ► ifstream,       ► ofstream,       ► none of the given options. Question No: 20 Which of the following function calling mechanism is true for the function prototype given below?float func(float &);       ► Call by value       ► Call by reference using pointer       ► Call by reference using reference variable       ► None of the given options Question No: 21  The programs, in which we allocate static memory, run essentially on ________       ► Heap       ► System Cache       ► None of the given options       ► Stack Question No: 22 Overloaded delete operator function takes parameter of void pointer and returns ________.       ► void       ► void pointer       ► pointer to an object       ► pointer of type int Question No: 23 What should be the return type of the constructor?       ► void pointer       ► int       ► same as object type       ► constructors do not return any thing Question No: 24 It is a way of reusing the code when we contain objects of our already written classes into a new class,       ► True       ► False Question No: 25 Templates are not type safe.       ► true       ► false Question No: 26 The functions used for dynamic memory allocation return pointer of type ______         ► int       ► float       ► void       ► doubleQuestion No: 1 Pointer is a variable which store,       ► Data       ► Memory Address

Page 57: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Data Type       ► ValuesQuestion No: 2 All preprocessor directives are started with the symbol______.       ► *       ► +       ► @       ► #Question No: 3 Within the statement obj1=obj2;   obj1 will call the assignment operator function and obj2 will be passed as an argument to function.       ► True       ► FalseQuestion No: 4 What is the sequence of event(s) when deallocating memory using delete operator?       ► Only block of memory is deallocated for objects       ► Only destructor is called for objects       ► Memory is deallocated first before calling destructor       ► Destructor is called first before deallocating memoryQuestion No: 5 The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators. http://vustudents.ning.com       ► True       ► FalseQuestion No: 6 To include code from the library in the program, such as iostream, a directive would be called up using this command.       ► #include “iostream.h”       ► include <iostream.h>       ► include <iostream.h>       ► #include <iostream.h>Question No: 7 The number 544.53 must be stored in _____ data type.       ► int       ► short       ► float       ► charQuestion No: 8 A template function can have different type of arguments.       ► True       ► FalseQuestion No: 9 For which values of the integer _value will the following code becomes an infinite loop?int number=1;while (true) {            cout << number;            if (number == 3) break;            number +=  integer_value; }      ► any number other than 1 or 2       ► only 0       ► only 1       ► only 2

Page 58: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 10 Template class cannot have static variables.       ► True       ► FalseQuestion No: 11 Which of the following is used with bit manipulation?       ► Signed integer       ► Un-signed integer       ► Signed double       ► Un-signed doubleQuestion No: 12 Structure is a collection of ______________ under a single name.       ► Only Functions       ► Only Variables       ► Both Functions and Variables       ► None of the given optionsQuestion No: 13 Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?       ► new int(10) ;       ► new int[10] ;       ► int new(10) ;       ► int new[10];Question No: 14 Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.       ► One, zero       ► Zero, one       ► One, two       ► Two, oneQuestion No: 15 The first parameter of overloaded stream insertion operator is _________ where second parameter is _______       ► input stream, object of class       ► object of class, output stream       ► output stream, object of class       ► object of class, input streamQuestion No: 16 We can also do conditional compilation with preprocessor directives.       ► True       ► FalseQuestion No: 17 If a symbolic constant has been defined, it will be an error to define it again.       ► True       ► FalseQuestion No: 18 While calling function, the arguments are assigned to the parameters from _____________.       ► left to right.       ► right to left       ► no specific order is followed       ► none of the given options.Question No: 19 Classes defined inside other classes are called ________ classes       ► looped

Page 59: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► nested       ► overloaded       ► none of the given options.Question No: 20  If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.       ► cannot be replaced       ► None of the given options       ► Remain constant.       ► can be changed by some operationQuestion No: 21 Assignment operator is -------------------------associative       ► right       ► left       ► binary       ► unaryQuestion No: 22 If text is a pointer of class String then what is meant by the following statement?text = new String [5];       ► Creates an array of 5 string objects statically       ► Creates an array of 5 string objects dynamically       ► Creates an array of pointers to string       ► Creates a string ObjectQuestion No: 23 The return type of the operator function for << operator is __________.       ► class for which we overload operator       ► reference of ostream class (ostream&)       ► reference of istream class (istream&)       ► voidQuestion No: 24 The code is written to __________ the program.       ► implement       ► design       ► analysis       ► none of the given options.Question No: 25 Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the memory.       ► System       ► Programmer       ► User of program       ► None of given optionsQuestion No: 26  Templates are not type safe.       ► true       ► falseQuestion No: 1 If it is required to copy an array to another array then,       ► Both arrays must be of the same size and data type       ► Both arrays may be of different size       ► Both arrays may be of different data type       ► Both arrays may be of different size and type

Page 60: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 2 Dealing with structures and functions passing by reference is the most economical method        ► True       ► FalseQuestion No: 3 eof( ), bad( ), good( ), clear( ) all are manipulators.       ► True        ► FalseQuestion No: 4 Overloaded new operator function takes parameter of type size_t and returns       ► void (nothing)       ► void pointer       ► object pointer       ► int pointerQuestion No: 5 When new operator is overloaded at global level then corresponding built-in new operator will not be visible to whole of the program.       ► True        ► FalseQuestion No: 6 If there is more than one statement in the block of a for loop, which of the following must be placed at the beginning and the ending of the loop block?       ► parentheses ( )       ► braces { }        ► brackets [ ]         ► arrows < > Question No: 7  The return type of a function that do not return any value must be ________       ► float       ► int       ► void        ► doubleQuestion No: 8  UNIX has been developed in ________ language.       ► JAVA       ► B       ► C       ► FORTRANQuestion No: 9 Like member functions, ______ can also access the private data members of a class.       ► Non-member functions       ► Friend functions       ► Any function outside class       ► None of the given optionsQuestion No: 10 Which of the following statement is best regarding declaration of friend function?        ► Friend function must be declared after public keyword.       ► Friend function must be declared after private keyword.       ► Friend function must be declared at the top within class definition.       ► It can be declared anywhere in class as these are not affected by the public

Page 61: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMandprivate keywords.

Question No: 11 The operator function overloaded for an Assignment operator (=) must be        ► Non-member function of class        ► Member function of class       ► Friend function of class       ► None of the given optionsQuestion No: 12 The endl and flush are _______       ► Functions       ► Operators       ► Manipulators       ► ObjectsQuestion No: 13 If a symbolic constant has been defined, it will be an error to define it again.       ► True       ► FalseQuestion No: 14 The operator used for casting, in C, is standard ________ operator.       ► none of the given options.        ► cast        ► cost       ► constQuestion No: 15 Constructors can not be overloaded like ordinary functions.        ► True       ► FalseQuestion No: 16 Which of the following function call is correct for the function prototype?defaultParameters ( int a, int b = 7, char z = ‘*’ );       ► defaultParameters (5);        ► defaultParameters (5, ‘8’);        ► defaultParameters (6, ‘#’);       ► defaultParameters (0, 0, ‘*’, 0);Question No: 17  When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are.        ► Zero       ► One        ► Two       ► N argumentsQuestion No: 18 We can not define a variable of user-defined data type in the class.       ► True       ► FalseQuestion No: 19 When an object of a class is defined inside an other class then,       ► Constructor of enclosing class will be called first       ► Constructor of inner object will be called first       ► Constructor and Destructor will be called simultaneously       ► None of the given options

Page 62: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 20 The appropriate data type to store the number of rows and colums of the matrix is____________.        ► float       ► int        ► char        ► none of the given options.Question No: 21 Class is a user defined___________.        ► data type        ► memory referee        ► value        ► none of the given options.Question No: 22 A pointer variable can be,        ► Decremented        ► Incremented         ► Multiplied         ► Both Decremented and Incremented Question No: 23 NULL value has been defined in ______ and _________ header files.       ► strings.h and iostream.h        ► ctype.h and conio.c        ► conio.c and conio.h        ► stdlib.h and stddef.hQuestion No: 24 A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this, _______________        ► Write a separate class to handle each        ► Use templates        ► Use strings to store all types        ► None of the given optionsQuestion No: 25 "setprecision" manipulator will set        ► The number of digits after the decimal point       ► The number of digits before the decimal point       ► The number of digits in a number       ► None of the given optionsQuestion No: 26 Which of the following option will be true, if we overload "-=" operator?         ► only - operator needs to be overloaded (not sure)        ► Minus (-) and = operators need to be overloaded        ► the -= operator need to be overloaded explicitly        ► the - and = operators need to be overloaded implicitly Question No: 1 When we define an array of objects then,

Destructor will call once for whole array Destructor will call for each object of the array Destructor will never call Depends on the size of array 

Question No: 2  We can also create an array of user define data type True False

Page 63: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 3 What is the sequence of event(s) when allocating memory using new operator?

Only block of memory is allocated for objects Only constructor is called for objects Memory is allocated first before calling constructor Constructor is called first before allocating memory

Question No: 4  We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally

True False 

Question No: 5 The declarator of Plus (+) member operator function is Class-Name  operator + (Class-Name  rhs) Operator Class-Name + ( ) Operator Class-Name + ( rhs) Class-Name  operator + ( ) 

Question No: 6 The  second parameter of  operator  functions for  << and >> are objects of the class for which we are overloading these operators

True (not sure) False

Question No: 7 Which  of  the  following  is  correct  way  to  initialize  a  variable  x  of int  type  with value 10?

int x ; x = 10 ; int x = 10 ; int x, x = 10; x = 10 ; 

Question No: 8  Default mechanism of function calling in case of array is and in case of variable is _                          

Call by value, call by reference Call by referene, call by reference Call by reference, call by value Call by value, call by value 

Question No: 9  What does STL stand for? Source template library Standard template library Stream template library Standard temporary library

Question No: 10  Skill(s) that is/are needed by programmers Paying attention to detail Think about the reusability Think about user interface All of the given options

Question No: 11  For which array, the size of the array should be one more than the number of elements in an array?

int double float

Page 64: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM char

Question No: 12 new and delete are                                                     whereas malloc and free are Functions, operators Classes, operators Operators, functions Operators, classes

Question No: 13  The prototype of friend functions must be written                                                       the class and its definition must be written                                              

inside, inside the class inside, outside the class outside, inside the class outside, outside the class

 Question No: 14  Friend function of a class are                                                                 of a class. Non-member functions not sure Friend functions Any function outside class None of the given options

Question No: 15  If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below?obj3 = obj1 + obj2 ;

obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator

obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator Both objects (obj1, obj2) will be passed as arguments to the + operator Any of the objects (obj1, obj2) can drive the + operator

Question No: 16  Which  one  of  the  following  is  the  declaration  of  overloaded pre-increment operator implemented as member function?

Class-name operator +() ; Class-name operator +(int) ; Class-name operator ++() ; Class-name operator ++(int) ;

Question No: 17  For cin, the source is normally a                                                                     and destination can be                            

File, native data type Disk, user-define type Keyboard, variable File, user-define type

Question No: 18  We can do condition compilation with pre processor directives. True False.

Question No: 19  The programs, in which we allocate static memory, run essentially on                                                                          

Heap System Cache None of the given options

Page 65: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Stack

Question No: 20  A template function must have at least ---------- or more arguments Zero One Two Three

Question No: 21  The default value of a parameter can be provided inside the function prototype function definition both function prototype or function definition none of the given options

Question No: 22  While calling function, the arguments are assigned to the parameters from                                                      

left to right right to left no specific order is followed none of the given options

Question No: 23  When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are  

Zero One Two N arguments 

Question No: 24  new operator allocates memory from free store and return A pointer A reference An integer A float

Question No: 25  With user-defined data type variables (Objects), self assignment can produce              

Syntax error not sure Logical error Link error Non of the given options 

Question No: 26 Assignment operator is used to initialize a newly declared object from existing object

True False

Question No: 27  When an object of a class is defined inside an other class then, Constructor of enclosing class will be called first Constructor of inner object will be called first Constructor and Destructor will be called simultaneously None of the given options

Question No: 28 In the member initializer list, the data members are initialized, From left to right From right to left

Page 66: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM In the order in which they are defined within class None of the given options

Question No: 29 "new" and "delete" keywords are                                                                                         in C++ language

Built-in- Function Operators Memory Allocation Function None of the given options

Question No: 1 There are mainly -------------------- types of software  ► Two  ► Three  ► Four  ► FiveQuestion No: 2 When x = 7; then the expression x%= 2; will calculate the value of x as,  ► 1  ► 3  ► 7  ► 2Question No: 3 A pointer variable can be,  ► Decremented only  ► Incremented only  ► Multiplied only  ► Both 1 and 2Question No: 4 setprecision is a parameter less manipulator.  ► True  ►FalseQuestion No: 5  We can change a Unary operator to Binary operator through operator overloading. ►False  ► TrueQuestion No: 6 delete operator is used to return memory to free store which is allocated by the new operator  ► True  ►False.Question No: 7 When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor.  ► True  ►FalseQuestion No: 8 What is the functionality of the following statement?String str[5] = {String(“Programming”), String(“CS201”)};  ► Default constructor will call for all objects of array  ► Parameterized constructor will call for all objects of array  ►   Parameterized constructor will call for first 2 objects and default constructor for remaining objects

Page 67: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM  ► Default constructor will call for first 3 objects and Parameterized constructor for remaining objectsQuestion No: 9 What is the sequence of event(s) when allocating memory using new operator?  ► Only block of memory is allocated for objects  ► Only constructor is called for objects  ► Memory is allocated first before calling constructor  ► Constructor is called first before allocating memoryQuestion No: 10 Deleting an array of objects without specifying [] brackets may lead to memory leak  ► True  ►FalseQuestion No: 11 Which of the following data type will be assumed if no data type is specified with constant?  ► short  ► float  ► int  ► doubleQuestion No: 12  There is an array of characters having name ‘course’ that has to be initialized by string ‘programming’ which of the following is the correct way to do this,i.  course[] = {‘p’, ’r’, ’o’, ’g’, ’r’, ’a’, ’m’, ’m’, ’i’, ’n’, ’g’};ii.course[] = ‘programming’ ;iii.    course[12] = “programming” ;iv.    course = “programming” ;Choose the correct options.  ► (i) and (ii) only  ► (i) and (iv) only  ► (i) and (iii) only  ► (ii) and (iii) onlyQuestion No: 13 What will be the correct syntax of the following statement?ptr is a constant pointer to integer.  ► const int *ptr ;  ► const *int ptr ;  ► int const *ptr ;  ► int *const ptr ; Question No: 14 Overloaded member operator function is always called by _______  ► Class  ► Object  ► Compiler  ► Primitive data typeQuestion No: 15 Loader loads the executable code from hard disk to main memory.  ► True  ►FalseQuestion No: 16 Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

Page 68: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM  ► new int(10) ;  ► new int[10] ;  ► int new(10) ;  ► int new[10];Question No: 17 The prototype of friend functions must be written ____ the class and its definition must be written ____   ► inside, inside the class  ► inside, outside the class  ► outside, inside the class  ► outside, outside the classQuestion No: 18 Like member functions, ______ can also access the private data members of a class.   ► Non-member functions  ► Friend functions  ► Any function outside class  ► None of the given options Question No: 19 To perform manipulation with input/output, we have to include _____ header file.   ► iostream.h  ► stdlib.h  ► iomanip.h  ► fstream.hQuestion No: 20 The endl and flush are _______   ► Functions  ► Operators  ► Manipulators  ► ObjectsQuestion No: 21 If we want to use stream insertion and extraction operators with _______ then we have to overload these operators.  ► int, float, double  ► objects of class  ► int, float, object  ► int, char, floatQuestion No: 22  The static data members of a class can be accessed by ________  ► only class  ► only objects (not sure ) .....   ► both class and objects  ► none of given options Question No: 23  Classes defined inside other classes are called ________ classes  ► looped  ► nested  ► overloaded  ► none of the given options.Question No: 24 Which value is returned by the destructor of a class?  ► A pointer to the class.  ► An object of the class.

Page 69: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM  ► A status code determining whether the class was destructed correctly  ► Destructors do not return a value.Question No: 25 Consider the following code segmentclass M { friend int operator!(const M &);...};!s // code of line implies that operator!(s)...Let assume if s is an object of the class then function is implemented as ___________  ► Member function  ► Non-member function  ► Binary operator function  ► None of the given options Question No: 26 When the compiler overloads the assignment (=) operator by default then __________  ►  compiler does member wise assignment.   ► compiler does not allow default overload of assignment (=) operator  ► member of the class are not assigned properly  ► None of the given optionsQuestion No: 27 If text is a pointer of class String then what is meant by the following statement?text = new String [5]; ► Creates an array of 5 string objects statically  ► Creates an array of 5 string objects dynamically  ► Creates an array of pointers to string  ► Creates a string ObjectQuestion No: 28  Static variable which is defined in a function is initialized ________  ► Only once during its life time  ► Every time the function call  ► Compile time of the program  ► None of the aboveQuestion No: 29  The appropriate data type to store the number of rows and is________  ► floatcolums of the matrix  ► int  ► char  ► none of the given options.Question No: 30 Copy constructor becomes necessary while dealing with _______allocation in the class.  ► Dynamic memory  ► Static memory  ► Both Dynamic and Static memory  ► None of the given optionsSolved By: Hamid Khan

BS IT (3rd Semester)

Page 70: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

[email protected]

Question No: 1 If we write a statement like s2 = s1; ___ will be the calling object and ____ will be passed to the = operator as an argument.

       ► s1, s1

       ► s1, s2

       ► s2, s1

       ► s2, s2

Question No: 2 If we write a statement like s2 = s1; s2 will be the calling object and s1 will be passed to the = operator as an argument.

cout << setfill(‘0’) << setw(7) << 128 ;

       ► 0000128

       ► 0128128

       ► 1280000

       ► 0012800

Question No: 3 The stream insertion and extraction operators are not already overloaded for _______

       ► Built-in data types

       ► User-defined data types

       ► Both built-in and user-defined types

       ► None of the given optionsQuestion No: 4 Constructors can not be overloaded like ordinary functions.

       ► True

       ► False

Question No: 5 Overloaded new operator function takes parameter of type size_t and returns

       ► void (nothing)

       ► void pointer

       ► object pointer

       ► int pointer 

 Question No: 6 Which of the following is the correct way to declare a variable x of integer type?

Page 71: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► x int ;

       ► integer x ;

       ► int x;

       ► x integer

Question No: 7 Reserve words cannot be used as a variable name.

       ► True

       ► False

Question No: 8 A template function must have at least ---------- generic data type 

       ► Zero

       ► One

       ► Two

       ► Three

 Question No: 9 Template functions can also be overloaded

       ► True

       ► False

 Question No: 10 We can not make a member function of a class as template function.

       ► True

       ► False not sure

Question No: 11 When break statement is encountered in switch statement, it

       ► Stops the entire program

       ► Stops the execution of current statement

       ► Exits from switch statement

       ► None of the given optionsQuestion No: 12 We can also define a variable of user define data type (object) as static.

       ► True

       ► FalseQuestion No: 13 The declarator of Plus (+) member operator function is

       ► Class-Name  operator + (Class-Name  rhs)

       ► operator Class-Name + ( )

       ► operator Class-Name + ( rhs)

Page 72: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Class-Name  operator + ( )Question No: 14  Let suppose

    int a, b, c, d, e;

  a = b = c = d = e = 42;

This can be interpreted by the complier as:

       ► a = (b = (c = (d = (e = 42))));

       ► (a = b = (c = (d = (e = 42))));

       ► a = b = (c = (d = (e = 42)));

       ► (a = b) = (c = d) = (e = 42);  

Question No: 15 What will be the range of numbers generated by function rand () % 9?

       ► 0 to 9

       ► 1 to 9

       ► 0 to 8

       ► 1 to 8

Question No: 16 When 6 divides any number, the remainder will always be less than 6. Then result will be between therefore we will add 1. 1 + rand ( ) % 6;

Which of the following is the correct function call having array named student of 10 elements as a parameter.

       ► addRecord(student[]) ;

       ► addRecord(student) ;

       ► addRecord(student[10]) ;

       ► addRecord(*student) ;  

Question No: 17 Declaring structures does not mean that memory is allocated.

       ► True

       ► False 

Question No: 18 Identifier is a name that can be given to variables, labels and functions.

       ► True

       ► False

Question No: 19 If a class A declares itself a friend of class B and a class B declares itself a friend of class C then

       ► Class A is also a friend of class C.

Page 73: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Class B is also a friend of class A.

       ► Class A is also a friend of class C if A declares C as its friend.

       ► Class A is also a friend of class C if C declares A as its friend.

Question No: 20 Which of the following statement is best regarding declaration of friend function?  

       ► Friend function must be declared after public keyword.

       ► Friend function must be declared after private keyword.

       ► Friend function must be declared at the top within class definition.

       ► It can be declared anywhere in class as these are not affected by the public

and private keywords. 

Question No: 21 A pointer is a special type of variable that contain ___________

       ► Memory Address

       ► Data values

       ► Both Values and Memory

       ► None of given of options

Question No: 22 When memory for a program is allocated at run time then it is called ___

       ► static memory allocation 

       ► dynamic memory allocation 

       ► stack memory allocation

       ► virtual memory allocation 

Question No: 23 What purpose do classes serve?

       ► Data encapsulation

       ► Providing a convenient way of modeling real-world objects

       ► Simplifying code reuse

       ► All of the given options

Question No: 24 Which of the following function cannot be overloaded?

       ► Member functions

       ► Utility functions

       ► Constructor

       ► Destructor

Page 74: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 25 The following prototype of unary operator function indicates that it is __

       ► Member functions of post increment operator

       ► Member functions of  pre increment operator

       ► Non-member functions of post increment operator

       ► Non-member functions of  pre increment operator

Question No: 26 Static variable which is defined in a function is initialized __________.

       ► Only once during its life time

       ► Every time the function call

       ► Compile time of the program

       ► None of the above

Question No: 27 In the member initialize list, the data members are initialized,

       ► From left to right

       ► From right to left

       ► In the order in which they are defined within class

       ► None of the given options

Question No: 28 If we do not indent the code properly it will __________________

       ► Be a syntax error

       ► Be a logical error

       ► Not be an error at all

       ► None of the given options

Question No: 29 Truth tables are used for analyzing ___________.

       ► logical expressions

       ► arithmetic expressions

       ► both logical and arithmetic expressions

       ► none of the given options.

Question No: 30 Static memory allocation is also known as ____________

       ► Dynamic allocation

       ► Compile time allocation

       ► Run time allocation

       ► None of the given options

Page 75: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COMQuestion No: 1 When we define an array of objects then,

Destructor will call once for whole array Destructor will call for each object of the array Destructor will never call Depends on the size of array

Question No: 2 We can also create an array of user define data type

True False

Question No: 3 What is the sequence of event(s) when allocating memory using new operator?

Only block of memory is allocated for objects Only constructor is called for objects Memory is allocated first before calling constructor Constructor is called first before allocating memory

Question No: 4 We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally

True False

Question No: 5 The declarator of Plus (+) member operator function is

Class-Name operator + (Class-Name rhs) Operator Class-Name + ( ) Operator Class-Name + ( rhs) Class-Name operator + ( )

Question No: 6 The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators

True False

Question No: 7 Which of the following is correct way to initialize a variable x of int type with value 10?

int x ; x = 10 ; int x = 10 ; int x, x = 10; x = 10 ;

Question No. 8 Default mechanism of function calling in case of array is ……. and in case of variable is ……..

Call by value, call by reference Call by referene, call by reference Call by reference, call by value Call by value, call by value

Question No: 9 What does STL stand for?

Page 76: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Source template library Standard template library Stream template library Standard temporary library

Question No: 10 Skill(s) that is/are needed by programmers …………

Paying attention to detail Think about the reusability Think about user interface All of the given options

Question No: 11 For which array, the size of the array should be one more than the number of elements in an array?

int double float char

Question No: 12 new and delete are whereas malloc and free are

Functions, operators Classes, operatorsOperators, functions Operators, classes

Question No: 13 The prototype of friend functions must be written the class and its definition

must be written

inside, inside the class inside, outside the class outside, inside the class outside, outside the class

Question No: 14 Friend function of a class are of a class.

Non-member functions Friend functions Any function outside class None of the given options

Question No: 15 If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below? obj3 = obj1 + obj2 ;

obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator

obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator Both objects (obj1, obj2) will be passed as arguments to the + operator Any of the objects (obj1, obj2) can drive the + operator

Question No: 16 Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?

Page 77: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Class-name operator +() ; Class-name operator +(int) ; Class-name operator ++() ; Class-name operator ++(int) ;

Question No: 17 For cin, the source is normally a and destination can be

File, native data type Disk, user-define type Keyboard, variable File, user-define type

Question No: 18 We can do condition compilation with pre processor directives.

True False

Question No: 19 The programs, in which we allocate static memory, run essentially on ………..

Heap System Cache None of the given options Stack

Question No: 20 A template function must have at least ------ or more arguments

Zero One Two Three

Question No: 21 The default value of a parameter can be provided inside the …

Function prototype Function definition Both function prototype or function definition None of the given options

Question No: 22 While calling function, the arguments are assigned to the parameters from …

left to right right to left no specific order is followed none of the given options

Question No: 23 When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are

Zero One Two N arguments

Question No: 24 new operator allocates memory from free store and return …

A pointer

Page 78: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM A reference An integer A float

Question No: 25 With user-defined data type variables (Objects), self assignment can produce

Syntax error Logical error Link error Non of the given options

Question No: 26 Assignment operator is used to initialize a newly declared object from existing object

True False

Question No: 27 When an object of a class is defined inside an other class then,

Constructor of enclosing class will be called first Constructor of inner object will be called first Constructor and Destructor will be called simultaneously None of the given options

Question No: 28 In the member initializer list, the data members are initialized,

From left to right From right to left In the order in which they are defined within class None of the given options

Question No: 29 "new" and "delete" keywords are in C++ language

Built-in- Function Operators Memory Allocation Function None of the given options

Question No: 1 There are mainly -------------------- types of software

► Two

► Three

► Four

► Five

Question No: 2 When x = 7; then the expression x%= 2; will calculate the value of x as,

► 1

► 3

► 7

► 2

Page 79: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 3 A pointer variable can be,

► Decremented only

► Incremented only

► Multiplied only

► Both 1 and 2

Question No: 4 setprecision is a parameter less manipulator.

► True

► False

Question No: 5 We can change a Unary operator to Binary operator through operator overloading.

► False

► True

Question No: 6 delete operator is used to return memory to free store which is allocated by the new operator

► True

► False

Question No: 7 When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor

► True

► False

Question No: 8 What is the functionality of the following statement?

String str[5] = {String(“Programming”), String(“CS201”)};

► Default constructor will call for all objects of array

► Parameterized constructor will call for all objects of array

► Parameterized constructor will call for first 2 objects and default constructor for

remaining objects

► Default constructor will call for first 3 objects and Parameterized constructor for

remaining objects

Question No: 9 What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

Page 80: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Only constructor is called for objects

► Memory is allocated first before calling constructor

► Constructor is called first before allocating memory

Question No: 10 Deleting an array of objects without specifying [] brackets may lead to memory leak

► True

► False

Question No: 11 Which of the following data type will be assumed if no data type is specified with constant?

► short

► float

► int

► double

Question No: 12 There is an array of characters having name ‘course’ that has to be initialized by string ‘programming’ which of the following is the correct way to do this,

i. course[] = {‘p’, ’r’, ’o’, ’g’, ’r’, ’a’, ’m’, ’m’, ’i’, ’n’, ’g’};ii. course[] = ‘programming’ ;iii. course[12] = “programming” ;iv. course = “programming” ;

Choose the correct options.

► (i) and (ii) only

► (i) and (iv) only

► (i) and (iii) only

► (ii) and (iii) only

Question No: 13 What will be the correct syntax of the following statement?

ptr is a constant pointer to integer.

► const int *ptr ;

► const *int ptr ;

► int const *ptr ;

► int *const ptr ;

Question No: 14 Overloaded member operator function is always called by _______

► Class

Page 81: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Object

► Compiler

► Primitive data type

Question No: 15 Loader loads the executable code from hard disk to main memory.

► True

► False

Question No: 16 Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

► new int(10) ;

► new int[10] ;

► int new(10) ;

► int new[10];

Question No: 17 The prototype of friend functions must be written ____ the class and its definition must be written ____

► inside, inside the class

► inside, outside the class

► outside, inside the class

► outside, outside the class

Question No: 18 Like member functions, __ can also access the private data members of a class

► Non-member functions

► Friend functions

► Any function outside class

► None of the given options

Question No: 19 To perform manipulation with input/output, we have to include ___ header file

► iostream.h

► stdlib.h

► iomanip.h

► fstream.h

Question No: 20 The endl and flush are ______

Page 82: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Functions

► Operators

► Manipulators

► Objects

Question No: 21 If we want to use stream insertion and extraction operators with _______ then we have to overload these operators.

► int, float, double

► objects of class

► int, float, object

► int, char, float

Question No: 22 The static data members of a class can be accessed by ________

► only class

► only objects

► both class and objects

► none of given options

Question No: 23 Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Question No: 24 Which value is returned by the destructor of a class?

► A pointer to the class.

► An object of the class.

► A status code determining whether the class was destructed correctly

► Destructors do not return a value.

Question No: 25 Consider the following code segment

class M {

friend int operator!(const M &);

...

};

Page 83: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

!s // code of line implies that operator!(s)

...

Let assume if s is an object of the class then function is implemented as ___________

► Member function

► Non-member function

► Binary operator function

► None of the given options

Question No: 26 When the compiler overloads the assignment (=) operator by default then __________

► compiler does member wise assignment.

► compiler does not allow default overload of assignment (=) operator

► member of the class are not assigned properly

► None of the given options

Question No: 27 If text is a pointer of class String then what is meant by the following statement?

text = new String [5];

► Creates an array of 5 string objects statically

► Creates an array of 5 string objects dynamically

► Creates an array of pointers to string

► Creates a string Object

Question No: 28 Static variable which is defined in a function is initialized __________.

► Only once during its life time

► Every time the function call

► Compile time of the program

► None of the above

Question No: 29 The appropriate data type to store the number of rows and colums of the matrix is____________.

► float

► int

► char

Page 84: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► none of the given options.

Question No: 30 Copy constructor becomes necessary while dealing with _______allocation in the class.

► Dynamic memory

► Static memory

► Both Dynamic and Static memory

► None of the given options

Solved By: Hamid Khan

BS IT (3rd Semester)

[email protected]

Question No: 1 If we write a statement like s2 = s1; ___ will be the calling object and ____ will be passed to the = operator as an argument.

       ► s1, s1

       ► s1, s2

       ► s2, s1

       ► s2, s2

Question No: 2 If we write a statement like s2 = s1; s2 will be the calling object and s1 will be passed to the = operator as an argument.

cout << setfill(‘0’) << setw(7) << 128 ;

       ► 0000128

       ► 0128128

       ► 1280000

       ► 0012800

Question No: 3 The stream insertion and extraction operators are not already overloaded for _______

       ► Built-in data types

       ► User-defined data types

       ► Both built-in and user-defined types

Page 85: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► None of the given optionsQuestion No: 4 Constructors can not be overloaded like ordinary functions.

       ► True

       ► False

Question No: 5 Overloaded new operator function takes parameter of type size_t and returns

       ► void (nothing)

       ► void pointer

       ► object pointer

       ► int pointer 

 Question No: 6 Which of the following is the correct way to declare a variable x of integer type?

       ► x int ;

       ► integer x ;

       ► int x;

       ► x integer

Question No: 7 Reserve words cannot be used as a variable name.

       ► True

       ► False

Question No: 8 A template function must have at least ---------- generic data type 

       ► Zero

       ► One

       ► Two

       ► Three

 Question No: 9 Template functions can also be overloaded

       ► True

       ► False

 Question No: 10 We can not make a member function of a class as template function.

       ► True

       ► False not sure

Question No: 11 When break statement is encountered in switch statement, it

Page 86: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Stops the entire program

       ► Stops the execution of current statement

       ► Exits from switch statement

       ► None of the given optionsQuestion No: 12 We can also define a variable of user define data type (object) as static.

       ► True

       ► FalseQuestion No: 13 The declarator of Plus (+) member operator function is

       ► Class-Name  operator + (Class-Name  rhs)

       ► operator Class-Name + ( )

       ► operator Class-Name + ( rhs)

       ► Class-Name  operator + ( )Question No: 14  Let suppose

    int a, b, c, d, e;

  a = b = c = d = e = 42;

This can be interpreted by the complier as:

       ► a = (b = (c = (d = (e = 42))));

       ► (a = b = (c = (d = (e = 42))));

       ► a = b = (c = (d = (e = 42)));

       ► (a = b) = (c = d) = (e = 42);  

Question No: 15 What will be the range of numbers generated by function rand () % 9?

       ► 0 to 9

       ► 1 to 9

       ► 0 to 8

       ► 1 to 8

Question No: 16 When 6 divides any number, the remainder will always be less than 6. Then result will be between therefore we will add 1. 1 + rand ( ) % 6;

Which of the following is the correct function call having array named student of 10 elements as a parameter.

       ► addRecord(student[]) ;

       ► addRecord(student) ;

Page 87: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► addRecord(student[10]) ;

       ► addRecord(*student) ;  

Question No: 17 Declaring structures does not mean that memory is allocated.

       ► True

       ► False 

Question No: 18 Identifier is a name that can be given to variables, labels and functions.

       ► True

       ► False

Question No: 19 If a class A declares itself a friend of class B and a class B declares itself a friend of class C then

       ► Class A is also a friend of class C.

       ► Class B is also a friend of class A.

       ► Class A is also a friend of class C if A declares C as its friend.

       ► Class A is also a friend of class C if C declares A as its friend.

Question No: 20 Which of the following statement is best regarding declaration of friend function?  

       ► Friend function must be declared after public keyword.

       ► Friend function must be declared after private keyword.

       ► Friend function must be declared at the top within class definition.

       ► It can be declared anywhere in class as these are not affected by the public

and private keywords. 

Question No: 21 A pointer is a special type of variable that contain ___________

       ► Memory Address

       ► Data values

       ► Both Values and Memory

       ► None of given of options

Question No: 22 When memory for a program is allocated at run time then it is called ___

       ► static memory allocation 

       ► dynamic memory allocation 

       ► stack memory allocation

Page 88: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► virtual memory allocation 

Question No: 23 What purpose do classes serve?

       ► Data encapsulation

       ► Providing a convenient way of modeling real-world objects

       ► Simplifying code reuse

       ► All of the given options

Question No: 24 Which of the following function cannot be overloaded?

       ► Member functions

       ► Utility functions

       ► Constructor

       ► Destructor

Question No: 25 The following prototype of unary operator function indicates that it is __

       ► Member functions of post increment operator

       ► Member functions of  pre increment operator

       ► Non-member functions of post increment operator

       ► Non-member functions of  pre increment operator

Question No: 26 Static variable which is defined in a function is initialized __________.

       ► Only once during its life time

       ► Every time the function call

       ► Compile time of the program

       ► None of the above

Question No: 27 In the member initialize list, the data members are initialized,

       ► From left to right

       ► From right to left

       ► In the order in which they are defined within class

       ► None of the given options

Question No: 28 If we do not indent the code properly it will __________________

       ► Be a syntax error

       ► Be a logical error

       ► Not be an error at all

Page 89: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► None of the given options

Question No: 29 Truth tables are used for analyzing ___________.

       ► logical expressions

       ► arithmetic expressions

       ► both logical and arithmetic expressions

       ► none of the given options.

Question No: 30 Static memory allocation is also known as ____________

       ► Dynamic allocation

       ► Compile time allocation

       ► Run time allocation

       ► None of the given options

Question No: 1 When we define an array of objects then,

Destructor will call once for whole array Destructor will call for each object of the array Destructor will never call Depends on the size of array

Question No: 2 We can also create an array of user define data type

True False

Question No: 3 What is the sequence of event(s) when allocating memory using new operator?

Only block of memory is allocated for objects Only constructor is called for objects Memory is allocated first before calling constructor Constructor is called first before allocating memory

Question No: 4 We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally

True False

Question No: 5 The declarator of Plus (+) member operator function is

Class-Name operator + (Class-Name rhs) Operator Class-Name + ( ) Operator Class-Name + ( rhs) Class-Name operator + ( )

Question No: 6 The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators

Page 90: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

rWWW.VIRTUALINSPIRE.COM True False

Question No: 7 Which of the following is correct way to initialize a variable x of int type with value 10?

int x ; x = 10 ; int x = 10 ; int x, x = 10; x = 10 ;

Question No. 8 Default mechanism of function calling in case of array is ……. and in case of variable is ……..

Call by value, call by reference Call by referene, call by reference Call by reference, call by value Call by value, call by value

Question No: 9 What does STL stand for?

Source template library Standard template library Stream template library Standard temporary library

Question No: 10 Skill(s) that is/are needed by programmers …………

Paying attention to detail Think about the reusability Think about user interface All of the given options

Question No: 11 For which array, the size of the array should be one more than the number of elements in an array?

int double float char

Question No: 12 new and delete are whereas malloc and free are

Functions, operators Classes, operatorsOperators, functions Operators, classes

Question No: 13 The prototype of friend functions must be written the class and its definition

must be written

inside, inside the class inside, outside the class outside, inside the class

Page 91: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM outside, outside the class

Question No: 14 Friend function of a class are of a class.

Non-member functions Friend functions Any function outside class None of the given options

Question No: 15 If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below? obj3 = obj1 + obj2 ;

obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator

obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator Both objects (obj1, obj2) will be passed as arguments to the + operator Any of the objects (obj1, obj2) can drive the + operator

Question No: 16 Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?

Class-name operator +() ; Class-name operator +(int) ; Class-name operator ++() ; Class-name operator ++(int) ;

Question No: 17 For cin, the source is normally a and destination can be

File, native data type Disk, user-define type Keyboard, variable File, user-define type

Question No: 18 We can do condition compilation with pre processor directives.

True False

Question No: 19 The programs, in which we allocate static memory, run essentially on ………..

Heap System Cache None of the given options Stack

Question No: 20 A template function must have at least ------ or more arguments

Zero One Two Three

Question No: 21 The default value of a parameter can be provided inside the …

Function prototype Function definition

Page 92: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Both function prototype or function definition None of the given options

Question No: 22 While calling function, the arguments are assigned to the parameters from …

left to right right to left no specific order is followed none of the given options

Question No: 23 When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are

Zero One Two N arguments

Question No: 24 new operator allocates memory from free store and return …

A pointer A reference An integer A float

Question No: 25 With user-defined data type variables (Objects), self assignment can produce

Syntax error Logical error Link error Non of the given options

Question No: 26 Assignment operator is used to initialize a newly declared object from existing object

True False

Question No: 27 When an object of a class is defined inside an other class then,

Constructor of enclosing class will be called first Constructor of inner object will be called first Constructor and Destructor will be called simultaneously None of the given options

Question No: 28 In the member initializer list, the data members are initialized,

From left to right From right to left In the order in which they are defined within class None of the given options

Question No: 29 "new" and "delete" keywords are in C++ language

Built-in- Function Operators

Page 93: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Memory Allocation Function None of the given options

Question No: 1 There are mainly -------------------- types of software

► Two

► Three

► Four

► Five

Question No: 2 When x = 7; then the expression x%= 2; will calculate the value of x as,

► 1

► 3

► 7

► 2

Question No: 3 A pointer variable can be,

► Decremented only

► Incremented only

► Multiplied only

► Both 1 and 2

Question No: 4 setprecision is a parameter less manipulator.

► True

► False

Question No: 5 We can change a Unary operator to Binary operator through operator overloading.

► False

► True

Question No: 6 delete operator is used to return memory to free store which is allocated by the new operator

► True

► False

Question No: 7 When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor

► True

Page 94: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► False

Question No: 8 What is the functionality of the following statement?

String str[5] = {String(“Programming”), String(“CS201”)};

► Default constructor will call for all objects of array

► Parameterized constructor will call for all objects of array

► Parameterized constructor will call for first 2 objects and default constructor for

remaining objects

► Default constructor will call for first 3 objects and Parameterized constructor for

remaining objects

Question No: 9 What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

► Memory is allocated first before calling constructor

► Constructor is called first before allocating memory

Question No: 10 Deleting an array of objects without specifying [] brackets may lead to memory leak

► True

► False

Question No: 11 Which of the following data type will be assumed if no data type is specified with constant?

► short

► float

► int

► double

Question No: 12 There is an array of characters having name ‘course’ that has to be initialized by string ‘programming’ which of the following is the correct way to do this,

v. course[] = {‘p’, ’r’, ’o’, ’g’, ’r’, ’a’, ’m’, ’m’, ’i’, ’n’, ’g’};vi. course[] = ‘programming’ ;vii.course[12] = “programming” ;viii. course = “programming” ;

Choose the correct options.

► (i) and (ii) only

Page 95: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► (i) and (iv) only

► (i) and (iii) only

► (ii) and (iii) only

Question No: 13 What will be the correct syntax of the following statement?

ptr is a constant pointer to integer.

► const int *ptr ;

► const *int ptr ;

► int const *ptr ;

► int *const ptr ;

Question No: 14 Overloaded member operator function is always called by _______

► Class

► Object

► Compiler

► Primitive data type

Question No: 15 Loader loads the executable code from hard disk to main memory.

► True

► False

Question No: 16 Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

► new int(10) ;

► new int[10] ;

► int new(10) ;

► int new[10];

Question No: 17 The prototype of friend functions must be written ____ the class and its definition must be written ____

► inside, inside the class

► inside, outside the class

► outside, inside the class

► outside, outside the class

Page 96: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 18 Like member functions, __ can also access the private data members of a class

► Non-member functions

► Friend functions

► Any function outside class

► None of the given options

Question No: 19 To perform manipulation with input/output, we have to include ___ header file

► iostream.h

► stdlib.h

► iomanip.h

► fstream.h

Question No: 20 The endl and flush are ______

► Functions

► Operators

► Manipulators

► Objects

Question No: 21 If we want to use stream insertion and extraction operators with _______ then we have to overload these operators.

► int, float, double

► objects of class

► int, float, object

► int, char, float

Question No: 22 The static data members of a class can be accessed by ________

► only class

► only objects

► both class and objects

► none of given options

Question No: 23 Classes defined inside other classes are called ________ classes

► looped

► nested

Page 97: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► overloaded

► none of the given options.

Question No: 24 Which value is returned by the destructor of a class?

► A pointer to the class.

► An object of the class.

► A status code determining whether the class was destructed correctly

► Destructors do not return a value.

Question No: 25 Consider the following code segment

class M {

friend int operator!(const M &);

...

};

!s // code of line implies that operator!(s)

...

Let assume if s is an object of the class then function is implemented as ___________

► Member function

► Non-member function

► Binary operator function

► None of the given options

Question No: 26 When the compiler overloads the assignment (=) operator by default then __________

► compiler does member wise assignment.

► compiler does not allow default overload of assignment (=) operator

► member of the class are not assigned properly

► None of the given options

Question No: 27 If text is a pointer of class String then what is meant by the following statement?

text = new String [5];

► Creates an array of 5 string objects statically

Page 98: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Creates an array of 5 string objects dynamically

► Creates an array of pointers to string

► Creates a string Object

Question No: 28 Static variable which is defined in a function is initialized __________.

► Only once during its life time

► Every time the function call

► Compile time of the program

► None of the above

Question No: 29 The appropriate data type to store the number of rows and colums of the matrix is____________.

► float

► int

► char

► none of the given options.

Question No: 30 Copy constructor becomes necessary while dealing with _______allocation in the class.

► Dynamic memory

► Static memory

► Both Dynamic and Static memory

► None of the given options

Cs201 final paper 07\02\2012

Mcqs were easy and from past papers.

1. Write down the syntax in two different ways to initialize character array with string 'programming'. 2

2. Write the C++ code for the declaration of overloaded stream insertion(>>) and stream extraction(<<) operator for the object d of type Date. 2 marks

3. What happens when an object is assigned to another object without overloading an assignment operator? 2 marks

Page 99: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM4.5. Suppose there is a template function 'func' having argument of type U and return type T. What

will be the C++ syntax to call this function, when we pass a variable 'x' of type double and it is returning an int type? 2 marks

6. If the requested memory is not available in the system then what does calloc(), malloc() and new operator return? 3 marks

7. Which one of the given two methods is better to read from a file and Why ? 3 marks

Using stream insertion operator >>

Using get() function

8. Write a function named Sum which takes an integer as its argument and uses a static integer variable to keep track of the sum of all values passed to it during the running of a program. 3 marks

9. Given is the code for nested classes.

class First{ public: class Second { public: Second() { fValue = 20; } void Display(); private:

int fValue; }; };

Write the code to define Display() member function of class inner class outside the body of outer class. The function Display() will display the value of data member fValue of class First. 3marks

10. Briefly explain the logic of given code. Also determine what is the value of 'counter' variable if the code below is executed ? 5 marks

main(){ int num = 5; int counter=0; while(num != 0) { if ( num & 01)

Page 100: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM counter++; num>>=1; } return 0;}

11. Using Template (code given) have to write a function square() that takes “x” as double but return its square as integer. 5marks

12. One question was from the phone example in the handouts. Have to write part of missing code . 5marks

13. Using manipulation operator show Virtual--------university (8 – character between them) 5 marks

Cs201 final paper 07\02\2012

Mcqs were easy and from past papers.

14. Write down the syntax in two different ways to initialize character array with string 'programming'. 2

15. Write the C++ code for the declaration of overloaded stream insertion(>>) and stream extraction(<<) operator for the object d of type Date. 2 marks

16. What happens when an object is assigned to another object without overloading an assignment operator? 2 marks

17.18. Suppose there is a template function 'func' having argument of type U and return type T. What

will be the C++ syntax to call this function, when we pass a variable 'x' of type double and it is returning an int type? 2 marks

19. If the requested memory is not available in the system then what does calloc(), malloc() and new operator return? 3 marks

20. Which one of the given two methods is better to read from a file and Why ? 3 marks

Using stream insertion operator >>

Using get() function

21. Write a function named Sum which takes an integer as its argument and uses a static integer variable to keep track of the sum of all values passed to it during the running of a program. 3 marks

Page 101: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM22. Given is the code for nested classes.

class First{ public: class Second { public: Second() { fValue = 20; } void Display(); private:

int fValue; }; };

Write the code to define Display() member function of class inner class outside the body of outer class. The function Display() will display the value of data member fValue of class First. 3marks

23. Briefly explain the logic of given code. Also determine what is the value of 'counter' variable if the code below is executed ? 5 marks

main(){ int num = 5; int counter=0; while(num != 0) { if ( num & 01) counter++; num>>=1; } return 0;}

24. Using Template (code given) have to write a function square() that takes “x” as double but return its square as integer. 5marks

25. One question was from the phone example in the handouts. Have to write part of missing code . 5marks

26. Using manipulation operator show Virtual--------university (8 – character between them) 5 marks

Page 102: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMFINALTERM EXAMINATION

Spring 2009 CS201- Introduction to Programming

BY ღ( Cuŧє~Mαħϊ )♥ ♥ ღsubjective

Question No: 30 ( Marks: 2 )What are the two types of conversion for user-defined data types?

There are two types of conversion: implicit and explicit.

Question No: 31 ( Marks: 2 )Give the general syntax of class template.

The syntax of the template class istemplate

class class-name() { definition of class };

[B]Question No: 32 ( Marks: 2 ) - What is a constructor in class?

The name of this function is same as the name of the class,having no return type. This function is called constructor.

Question No: 33 ( Marks: 2 ) Is there a way to increase the size of already allocated memory chunk ? Can the same chunk be

increased or not?

Can the same chunk be increased or not? The answer is yes. we can reallocate the same memory with a new size according to our requirement. The function that reallocates the memory is realloc.

Question No: 34 ( Marks: 3 ) - What is the difference between structure and class?.

The ONLY DIFFERENCES between classes and structures are 1) classes DEFAULT to having private members. Structures DEFAULT to having public members. These

defaults can be changed so classes can be made to work like structures and vice versa.

2) classes DEFAULT to inheriting privately from base classes. Structures DEFAULT to inheriting public from base classes. These defaults can be changed so classes can be made to work like structures and

vice versa.

Page 103: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 35 ( Marks: 3 )

How many arguments does binary member operator function and binary non- member operator function take?

When an operator function is defined as member function for a binary Plus (+)operator then the number of argument it take is/are

Question No: 36 ( Marks: 2 ) - Write ProgramFind the error in the given code for ( int i=0; imRows; i++)

{for(int j=0; jumCols; j++)

{elements[i , j] = m.elements[i][j];

}}

Question No: 39 ( Marks: 10 ) -What is Standard Template Library (STL) also describe its advantages?

The standard template library is the collection of functions of very common use. Their every day use is so important that two researchers wrote a whole library of these functions. This library is a part of the

official standard of C++. It is called STL i.e. Standard Template Library. As a library, it is a tested code base. Some one has written, tested and compiled for the ultimate use of programmers. We can use

these templates and can implement different concepts for our own data types. Equally is true about the use of the array data type. Our code will become very small with the use of this tested facility. Similarly,

there is no bug or error in it. Thus, if we have a tested and tried code base, we should try our best to write programs by using it. STL is a lot of important code, pre-developed for us. It is available as a library.

We can write programs by using it. Thus our programs will be small and error free

Question No: 37 ( Marks: 5 ) - Write ProgramWrite the C++ syntax for making a class friend of other class

Question No: 38 ( Marks: 5 ) - Write ProgramWhat is a template function? Give the general syntax of writing a template function

FINALTERM EXAMINATION

CS201- Introduction to Programming

Question No: 1 ( Marks: 1 ) - Please choose one

There are mainly -------------------- types of software

Page 104: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Two

► Three

► Four

► Five

Question No: 2 ( Marks: 1 ) - Please choose one

Structures help to define program-specific ___________ .

► functions

► datatypes

► Arithmetic operations

► None of the given options.

Question No: 3 ( Marks: 1 ) - Please choose one

Page 105: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

A special name which is substituted in code by its definition and as a result we get an expanded code is called,

► include

► Directive

► Macro

► Define

Question No: 4 ( Marks: 1 ) - Please choose one

If constructor contains a return statement in its body then compiler will give __________

► No error

► Syntax error

Page 106: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Logical error

► Run time error

Question No: 5 ( Marks: 1 ) - Please choose one

eof( ), bad( ), good( ), clear( ) all are manipulators.

► True

► False

Question No: 6 ( Marks: 1 ) - Please choose one

If a friend function outside the class declare itself friend of the class, this will neglect concept of ___________.

► encapsulation and data accessing

Page 107: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► encapsulation and data hiding

► friend member functions of the class

► Interface of the class

Question No: 7 ( Marks: 1 ) - Please choose one

Which of the following is the only operator that the compiler overloads for user define data type?

► Plus (+)

► Minus (-)

► Equal (= =)

► Assignment (=)

Page 108: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 8 ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ .

► Member function

► Non-member function

► Private function

► Public function

Question No: 9 ( Marks: 1 ) - Please choose one

We can also create an array of user define data type.

Page 109: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► True

► False

Question No: 10 ( Marks: 1 ) - Please choose one

The default scope for members of structures is public whereas the default visibility for class members is private.

► True

► False

Question No: 11 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

Page 110: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Memory is deallocated first before calling destructor

► Destructor is called first before deallocating memory

Question No: 12 ( Marks: 1 ) - Please choose one

Overloaded delete operator function takes the same parameter as an argument returned by new operator function.

► True

► False

Question No: 13 ( Marks: 1 ) - Please choose one

In a group of nested loops, which loop is executed the most number of times?

► the outermost loop

► the innermost loop

Page 111: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► all loops are executed the same number of times

► cannot be determined without knowing the size of the loops

Question No: 14 ( Marks: 1 ) - Please choose one

Which of the following syntax will be used to initialize an array of size 5 of int data type to value 0?

► arr[5] = {0} ;

► int arr[5]= 0 ;

► int arr[5] = {0} ;

► int arr[] = 0 ;

Question No: 15 ( Marks: 1 ) - Please choose one

Page 112: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWhat will be the correct syntax of the following statement?ptr is a constant pointer to integer.

► const int *ptr ;

► const *int ptr ;

► int const *ptr ;

► int *const ptr ;

Question No: 16 ( Marks: 1 ) - Please choose one

We want to access array in random order which approach is better?

► Pointers

► Array index

► Both pointers and array index are better

► None of the given options.

Page 113: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 17 ( Marks: 1 ) - Please choose one

What is the output of the following statement?

int i = 2.5; do { cout i * 2; } while (i > 3 && i < 10);

► 510

► 5

► 48

► error

Question No: 18 ( Marks: 1 ) - Please choose one

Which statement about operator overloading is false?

► New operators can never be created

► Certain overloaded operators can change the number of arguments they take.

Page 114: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► The precedence of an operator cannot be changed by overloading.

► Overloading cannot change how an operator works on built-in types.

Question No: 19 ( Marks: 1 ) - Please choose one

The stream insertion and stream extraction operators are already overloaded for ______.

► User-defined data types

► Built-in data types

► User-defined and built-in data types

► None of the given options

Question No: 20 ( Marks: 1 ) - Please choose one

Page 115: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe stream insertion and extraction operators are not already overloaded for _______

► Built-in data types

► User-defined data types

► Both built-in and user-defined types

► None of the given options

Question No: 21 ( Marks: 1 ) - Please choose one

The programs, in which we allocate static memory, run essentially on ________

► Heap

► System Cache

► None of the given options

► Stack

Question No: 22 ( Marks: 1 ) - Please choose one

Page 116: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

The dynamic memory allocation uses memory from the ____________.

► Stack

► Heap

► System Cache

► None of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

The default value of a parameter can be provided inside the ________________

► function prototype

► function definition

► both function prototype or function definition

► none of the given options.

Question No: 24 ( Marks: 1 ) - Please choose one

Page 117: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Consider the following code segment

class M {

friend int operator!(const M &);

...

};

!s // code of line implies that operator!(s)

...

Let assume if s is an object of the class then function is implemented as ___________

► Member function

► Non-member function

► Binary operator function

► None of the given options

None of the given options

Page 118: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 25 ( Marks: 1 ) - Please choose one

The following prototype of unary operator function indicates that it is ____________ .

Date operator++(int )

► Member functions of post increment operator

► Member functions of pre increment operator

► Non-member functions of post increment operator

► Non-member functions of pre increment operator

Question No: 26 ( Marks: 1 ) - Please choose one

Page 119: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe second parameter of operator function for >> operator must always be passed

► By reference

► Function takes no argument

► By value

► None of the given options

Question No: 27 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside another class then,

► Destructor of enclosing class will be called first

Page 120: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Destructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 28 ( Marks: 1 ) - Please choose one

When ever dynamic memory allocation is made in C/C++, it is freed_____________.

► Explicitly

► Implicitly

► Both explicitly and implicitly

► None of the given options

Question No: 29 ( Marks: 1 ) - Please choose one

The prototype of friend functions is written:

Page 121: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Top of the class definition.

► As Private member functions.

► Anywhere inside the class definition

► None of the given options

Question No: 30 ( Marks: 1 ) - Please choose one

What should be the return type of the constructor?

► void pointer

► int

► same as object type

► constructors do not return any thing

Page 122: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 31 ( Marks: 1 )

Is there any type checking on the parameters of macros?

Question No: 32 ( Marks: 1 )

What does an arity of operator represent?

Question No: 33 ( Marks: 2 )

Are the methods of a class reproduced for every object?

Question No: 34 ( Marks: 2 )

Can we overload new and delete operators?

Question No: 35 ( Marks: 3 )

What will be the output of following function if we call this function by passing int 5?

Page 123: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

template <class T>T reciprocal(T x) {return (1/x); }

Question No: 36 ( Marks: 3 )

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

Question No: 37 ( Marks: 3 )

If we want to send the data by reference and don’t want that original data should be affected then what can we do to prevent any change?

Question No: 38 ( Marks: 5 )

Why the first parameter of operator function for << operator must be passed by reference?

Question No: 39 ( Marks: 5 )

Page 124: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What will be the output of the following program?

int addValue (int &);

main () {

int x, y;

x = 23;

cout << "Value of x before calling addValue ():\t" << x << endl;

y = addValue (x);

cout << "Value of x after calling addValue ():\t" << x <<endl;

cout << "Value of y:\t" << y;

}

int addValue (int &a){

a = a + 2;

return a;

}

Question No: 40 ( Marks: 10 )

While doing dynamic memory allocation in class what are the important things that should be implemented in class.

Question No: 41 ( Marks: 10 )

Page 125: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What is difference between using a square(x) macro and square(x) function?

15th July SPRING 2011CS201 – Introduction to Programming

Which of the following is NOT a preprocessor directive?

► #error

► #define

► #line

► #ndefine

Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

What purpose do classes serve?

► Data encapsulation

► Providing a convenient way of modeling real-world objects

► Simplifying code reuse

► All of the given options

New and Delete are also used with ___________ and data types as well.

► Class, Objects

► Structures, Pointers

► Both Class and structures

► None of above

Page 126: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe statement cin.get (); is used to,

► Read a string from keyboard

► Read a character from keyboard

► Read a string from file

► Read a character from file

Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition.

► It can be declared anywhere in class as these are not affected by the public and private keywords.

A preprocessor directive is identified by _________ symbol

1) : #

2) : $

3) : %

4) : ##

The function call to a default constructor

1) : looks like any function call, except there is no return value

2) : is made automatically when an object is created

3) : creates but cannot initialize an object

4) : never takes any arguments The function call to a default constructor

Which of the following is the correct C++ syntax to allocate space dynamically for an array

of 10 int?

1) : new int(10) ;

2) : new int[10] ;

3) : int new(10) ;

4) : int new[10];

Page 127: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition.

► It can be declared anywhere in class as these are not affected by the public and private keywords.

A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

► Write a separate class to handle each

► Use templates

► Use strings to store all types

► None of the given options

What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

► Memory is allocated first before calling constructor

► Constructor is called first before allocating memory

What will be the correct syntax of the following statement?

ptr is a constant pointer to integer.

► const int *ptr ;

► const *int ptr ;

► int const *ptr ;

► int *const ptr ;

The programs, in which we allocate static memory, run essentially on ________

       ► Heap

       ► System Cache

Page 128: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► None of the given options

       ► Stack

Like member functions, ______ can also access the private data members of a class.

► Non-member functions

► Friend functions

► Any function outside class

► None of the given options

Copy constructor becomes necessary while dealing with _______allocation in the class.

► Dynamic memory

► Static memory

► Both Dynamic and Static memory

► None of the given options

Friend function of a class is ______________ .

► Member function

► Non-member function

► Private function

► Public function

Like member functions, ______ can also access the private data members of a class.

► Non-member functions

► Friend functions

► Any function outside class

► None of the given option

The operator used for casting, in C, is standard ________ operator.

► none of the given options.

► cast

► cost

► const

Page 129: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion:- (Marks = 2)

What is conversation construct?

Question:- (Marks = 2)

What does code optimization mean?

Question:- (Marks = 5)

What do you mean by garbage collection and how it works in JAVA and C++ ?

Question:- (Marks = 3)

Identify the errors in the following member operator function and also correct them.

math * operator(math m);

math * operator (math m)

{

math temp;

temp.number= number * number;

return number;

}

Question:- (Marks = 3)

Suppose we have the following class.

class Matrix

{

private:

int Elements[3][3];

};

Question:- (Marks = 2)

What are the first parameters of stream insertion“<<” and stream extraction “>>” for operator function

FINALTERM  EXAMINATION

Page 130: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMSpring 2010

CS201- Introduction to Programming

Time: 90 min

Marks: 58

By Cute~MahiQuestion No: 1      ( Marks: 1 ) - Please choose one

&& is -------------------- operator.

       ► An arithmetic

       ► Logical

       ► Relational

       ► Unary

 

Question No: 2      ( Marks: 1 ) - Please choose one

Which of the following function(s) is/are included in ctype.h header file?

       ► isdigit(int c)

       ► isxdigit(int c )

       ► tolower(int c)

► All of the above

Page 131: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Question No: 3      ( Marks: 1 ) - Please choose one

Disks are _______________ devices having access time of _________ milliseconds.

       ► Electro-physical, 6

       ► Electro-Mechanical, 4

       ► Electro-physical, 5

► Electro-Mechanical, 7

Question No: 4      ( Marks: 1 ) - Please choose one

All preprocessor directives are started with the symbol______.

       ► *

       ► ++

       ► @

► #

 

Question No: 5      ( Marks: 1 ) - Please choose one

When we use manipulators in our program then which header file should be included?

       ► iostream.h

Page 132: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► stdlib.h

       ► stdio.h

► iomanip.h

 

Question No: 6      ( Marks: 1 ) - Please choose one

setprecision is a parameter less manipulator.

       ► True

► False

Question No: 7      ( Marks: 1 ) - Please choose one

Which of the following is NOT a preprocessor directive?

       ► #error

       ► #define

       ► #line

► #ndefine

 

Question No: 8      ( Marks: 1 ) - Please choose one

We can do arithmetic with references like pointers.

Page 133: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► True

► False

 

Question No: 9      ( Marks: 1 ) - Please choose one

What does (*this) represents?

       ► The current function of the class

       ► The current pointer of the class

► The current object of the class

       ► A value of the data member

Question No: 10      ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ .

       ► Member function

► Non-member function

       ► Private function

       ► Public function

 

Question No: 11      ( Marks: 1 ) - Please choose one

A function can declare itself a friend of a class.

Page 134: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► True

► False

 

Question No: 12      ( Marks: 1 ) - Please choose one

Overloaded delete operator function takes the same parameter as an argument returned by new operator function.

► True

       ► False

Question No: 13      ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

       ► False

 

Question No: 14      ( Marks: 1 ) - Please choose one

A template function must have at least ---------- generic data type

       ► Zero

► One

Page 135: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Two

       ► Three

 

Question No: 15      ( Marks: 1 ) - Please choose one

A template function must have only generic data types.

       ► True

► False

Question No: 16      ( Marks: 1 ) - Please choose one

We can not make a member function of a class as template function.

       ► True

► False

 

Question No: 17      ( Marks: 1 ) - Please choose one

Which character is inserted at the end of string to indicate the end of string?

       ► new line

       ► tab

► null

Page 136: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► carriage return

 

Question No: 18      ( Marks: 1 ) - Please choose one

Assignment operator is used for ___________.

       ► calculation

       ► reading

► assigning value to variables

       ► None of the given options.

Question No: 19      ( Marks: 1 ) - Please choose one vuzs

The object _______________may be used both for file input and file output

► fstream,

       ► ifstream,

       ► ofstream,

       ► none of the given options.

 

Question No: 20      ( Marks: 1 ) - Please choose one

Which of the following function calling mechanism is true for the function prototype given below?

Page 137: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMfloat func(float &);

       ► Call by value

       ► Call by reference using pointer

► Call by reference using reference variable

       ► None of the given options

 

Question No: 21      ( Marks: 1 ) - Please choose one

The programs, in which we allocate static memory, run essentially on ________

       ► Heap

       ► System Cache

       ► None of the given options

► Stack

 

Question No: 22      ( Marks: 1 ) - Please choose one

Overloaded delete operator function takes parameter of void pointer and returns ________.

       ► void

       ► void pointer

       ► pointer to an object

Page 138: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► pointer of type int

 

Question No: 23      ( Marks: 1 ) - Please choose one

What should be the return type of the constructor?

       ► void pointer

       ► int

       ► same as object type

► constructors do not return any thing

 

Question No: 24      ( Marks: 1 ) - Please choose one

It is a way of reusing the code when we contain objects of our already written classes into a new class,

► True

       ► False

 

Question No: 25      ( Marks: 1 ) - Please choose one vuzs

Templates are not type safe.

       ► true

Page 139: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► false

 

Question No: 26      ( Marks: 1 ) - Please choose one

The functions used for dynamic memory allocation return pointer of type ______

  

       ► int

       ► float

► void

       ► double

 

Question No: 27      ( Marks: 2 )

Write a declaration statement for an array of 10 elements of type float. Include an initialization statement of the first four elements to 1.0, 2.0, 3.0 and 4.0.

Answer:

float  floatArry[10] = {1.0,2.0,3.0,4.0};

Question No: 28      ( Marks: 2 )

Write the general syntax for the declaration of pre-increment and post-increment member operator function.

Page 140: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMClassname operator ++(); ---- pre increment

Classname operator ++(int)  ---- post increment

Question No: 29      ( Marks: 2 )

What is difference between endl and \n? 

Question No: 30      ( Marks: 2 )

What does code optimization mean?

Question No: 31      ( Marks: 3 )

How is the following cout statement interpreted by compiler?szuv

cout << a << b << c ;

Question No: 32      ( Marks: 3 )

Suppose an object of class A is declared as data member of class B.

(i) The constructor of which class will be called first?  Answer : A

(ii) The destructor of which class will be called first?  Answer : B

Question No: 33      ( Marks: 3 )

Define static variable. Also explain life time of static variable?

Page 141: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 34      ( Marks: 5 )

What is difference between Unary and binary operators and how they can be overloaded?

Question No: 35      ( Marks: 5 )

What steps we must follow to design good program?

 Question No: 36      ( Marks: 5 ) 

 Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.

Note: Display all data with in a particular width and the empty space should be filled with character x

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

xxxxx20000

xxxxx30000

xxxxx60000

FINALTERM EXAMINATION

Spring 2010

CS201- Introduction to Programming

Page 142: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

BY Cute~MahiRef No: 1557656

Time: 90 min

Marks: 58

Student Info

Student ID: Dc100200028

Center: OPKST

Exam Date: 12/8/10

For Teacher's Use Only

Q No. 1 2 3 4 5 6 7 8 Total

Marks

Q No. 9 10 11 12 13 14 15 16

Marks

Q No. 17 18 19 20 21 22 23 24

Marks

Q No. 25 26 27 28 29 30 31 32

Marks

Q No. 33 34 35 36

Marks

Page 143: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 1 ( Marks: 1 ) - Please choose one

Pointer is a variable which store,

► Data

► Memory Address

► Data Type

► Values

Question No: 2 ( Marks: 1 ) - Please choose one

All preprocessor directives are started with the symbol______.

► *

► ++

► @

► #

Page 144: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 3 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

Within the statement obj1=obj2; obj1 will call the assignment operator function and obj2 will be passed as an argument to function.

► True

► False

Question No: 4 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

Page 145: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Destructor is called first before deallocating memory

Question No: 5 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators. http://vustudents.ning.com

► True

► False

Question No: 6 ( Marks: 1 ) - Please choose one

To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

Page 146: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► include <iostream.h>

► #include <iostream.h>

Question No: 7 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

The number 544.53 must be stored in _____ data type.

► int

► short

► float

► char

Question No: 8 ( Marks: 1 ) - Please choose one

A template function can have different type of arguments.

► True

► False

Page 147: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 9 ( Marks: 1 ) - Please choose one

For which values of the integer _value will the following code becomes an infinite loop?

int number=1;

while (true) {

cout << number;

if (number == 3) break;

number += integer_value; }

► any number other than 1 or 2

► only 0

► only 1

► only 2

Question No: 10 ( Marks: 1 ) - Please choose one

Page 148: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMTemplate class cannot have static variables. http://vustudents.ning.com

► True

► False

Question No: 11 ( Marks: 1 ) - Please choose one

Which of the following is used with bit manipulation?

► Signed integer

► Un-signed integer

► Signed double

► Un-signed double

Question No: 12 ( Marks: 1 ) - Please choose one

Structure is a collection of ______________ under a single name.

Page 149: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Only Functions

► Only Variables

► Both Functions and Variables

► None of the given options

Question No: 13 ( Marks: 1 ) - Please choose one

Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

► new int(10) ;

► new int[10] ;

Page 150: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► int new(10) ;

► int new[10];

Question No: 14 ( Marks: 1 ) - Please choose one

Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.

► One, zero

► Zero, one

► One, two

► Two, one

Question No: 15 ( Marks: 1 ) - Please choose one

Page 151: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

The first parameter of overloaded stream insertion operator is _________ where second parameter is _______ http://vustudents.ning.com

► input stream, object of class

► object of class, output stream

► output stream, object of class

► object of class, input stream

Question No: 16 ( Marks: 1 ) - Please choose one

We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 17 ( Marks: 1 ) - Please choose one

Page 152: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMIf a symbolic constant has been defined, it will be an error to define it again.

► True

► False

Question No: 18 ( Marks: 1 ) - Please choose one

While calling function, the arguments are assigned to the parameters from _____________.

► left to right.

► right to left

► no specific order is followed

► none of the given options.

Question No: 19 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

Page 153: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► nested

► overloaded

► none of the given options.

Question No: 20 ( Marks: 1 ) - Please choose one

If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.

► cannot be replaced

► None of the given options

► Remain constant.

► can be changed by some operation

Question No: 21 ( Marks: 1 ) - Please choose one

Page 154: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Assignment operator is -------------------------associative. http://vustudents.ning.com

► right

► left

► binary

► unary

Question No: 22 ( Marks: 1 ) - Please choose one

If text is a pointer of class String then what is meant by the following statement?

text = new String [5];

► Creates an array of 5 string objects statically

Page 155: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Creates an array of 5 string objects dynamically

► Creates an array of pointers to string

► Creates a string Object

Question No: 23 ( Marks: 1 ) - Please choose one

The return type of the operator function for << operator is __________.

► class for which we overload operator

► reference of ostream class (ostream&)

► reference of istream class (istream&)

► void

Page 156: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 24 ( Marks: 1 ) - Please choose one

The code is written to __________ the program.

► implement

► design

► analysis

► none of the given options.

Question No: 25 ( Marks: 1 ) - Please choose one

Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the memory. http://vustudents.ning.com

Page 157: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► System

► Programmer

► User of program

► None of given options

Question No: 26 ( Marks: 1 ) - Please choose one

Templates are not type safe.

► true

► false

Question No: 27 ( Marks: 2 )

Give the general syntax of class template.

Answer:

Syntax of class template:

Page 158: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMtemplate <class T>

class class-name()

{

definition of class

};

Question No: 28 ( Marks: 2 )

What is difference between endl and \n?

Answer:

The difference between endl and \n is that endl is use to start a new line for the next row

And \n is a new line character.

Question No: 29 ( Marks: 2 )

What is the this pointer? Give an example of its use.

Answer:

This pointer is use to points to the current object in programming.

Question No: 30 ( Marks: 2 )

Page 159: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Identify each of the following as function call, function definition and function declaration.

1. int func(int num1, int num2); Function call:

Function ; Function definition: Integer; Function declaration: Num1

and Num2

2. int func(int, int); Function call:

Function ; Function definition: Integer; Function declaration: integers

3. func(5, 6) ;

Function call:

Function ; Function definition: numbers; Function declaration: 5&6

4. int func(int num1, int num2){} Function call:

Function ; Function definition: Integer; Function declaration: Num1 and Num2 from

user

Question No: 31 ( Marks: 3 )

Consider the following code segment. What will be the output of the following code segment?

Page 160: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMclass class1{

public:

class class2{

public:

class2(){

cout << “Calling default constructor of class2\n” ;

}

};

class1(){

cout << “Calling default constructor of class1\n” ;

}

} ;

main(){

class1::class2 obj1;

class1 obj2 ;

}

Question No: 32 ( Marks: 3 )

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Answer:

No, we cannot define two functions as func(intx, inty) func(int &x, int&y) because it’s give an error function not initializing.

Page 161: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 33 ( Marks: 3 )

What happens when we use new and delete operator?

Answer:

When we use new operator to create objects the memory space is allocated for the object and then its constructor is called. Similarly, when we use delete operator with our objects, the destructor is called for the object before deallocating the storage to the object.

Question No: 34 ( Marks: 5 )

What is the difference between function overloading and operator overloading?

Answer:

Difference b/w function overloading and operator overloading is:

In function overloading, the functions have the same name but differ either by the number of arguments or the type of the arguments.

Operator overloading is to allow the same operator to be bound to more than one implementation, depending on the types of the operands.

Page 162: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 35 ( Marks: 5 )

Why the first parameter of operator function for << operator must be passed by reference?

Answer:

Operator<<'s first parameter must be an ostream passed by reference. Its second parameter, the IntList that is printed, does not have to be passed as a const-reference parameter; however it is more efficient to pass it by reference than by value (since that avoids a call to the copy constructor), and it should not be modified by operator<<, so it should be a const reference parameter

Question No: 36 ( Marks: 5 )

Read the given below code and explain what task is being performed by this function

Matrix :: Matrix ( int row , int col )

{

numRows = row ;

numCols = col ;

elements = new ( double * ) [ numRows ] ;

for ( int i = 0 ; i < numRows ; i ++ )

{

elements [ i ] = new double [ numCols ] ;

for ( int j = 0 ; j < numCols ; j ++ )

elements [ i ] [ j ] = 0.0 ;

Page 163: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM }

}

Hint : This function belong to a matrix class, having

Number of Rows = numRows

Number of Columns = numCols

Answer:

In this code the matrix function is defined, it get the number of rows from the user and create the row of matrix and then get the columns from the user and create the columns. The New is showing for creating more array space for the data which user enters. The elements [i][j] will print the data in matrix form. http://vustudents.ning.com

FINALTERM EXAMINATION

Spring 2009

CS201- Introduction to Programming (Session - 1)

BY Cute~Mahi

 

 

 

Question No: 1 ( Marks: 1 ) - Please choose one

To access the data members of structure _______ is used.

dot operator (.)►

* operator►

Page 164: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

à operator ►

None of given.►

Question No: 2 ( Marks: 1 ) - Please choose one

What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?

1► 2► 3►

4►

Question No: 3 ( Marks: 1 ) - Please choose one

If constructor contains a return statement in its body then compiler will give __________

No error►

Syntax error►

Logical error ►

Run time error►

Question No: 4 ( Marks: 1 ) - Please choose one

Page 165: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM We can use New keyword inside of Class Constructor.

►True

False►

Question No: 5 ( Marks: 1 ) - Please choose one

When an operator function is define as member function for a Unary operator then the number of argument it take is/are,

►Zero

►One

►Two

►N arguments

The declarator of Plus (+) member operator function is

►Class-Name operator + (Class-Name rhs)

►operator Class-Name + ( )

►operator Class-Name + ( rhs)

Page 166: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►Class-Name operator + ( )

Question No: 7 ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ .

►Member function

►Non-member function

►Private function

►Public function

Question No: 8 ( Marks: 1 ) - Please choose one

We can also create an array of user define data type.

►True

False►

Question No: 9 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?

►Only block of memory is deallocated for objects

►Only destructor is called for objects

►Memory is deallocated first before calling destructor

Page 167: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►Destructor is called first before deallocating memory

Question No: 10 ( Marks: 1 ) - Please choose one

Deleting an array of objects without specifying [] brackets may lead to memory leak

►True

False►

Question No: 11 ( Marks: 1 ) - Please choose one

Which of the following data type(s) can operate on modulus operator ‘%’?

float, int► float, double ►

int ►

char►

Question No: 12 ( Marks: 1 ) - Please choose one

Array is passed by value to a function by default.

True►

►False

Question No: 13 ( Marks: 1 ) - Please choose one

With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.

True►

►False

Page 168: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 14 ( Marks: 1 ) - Please choose one

What will be the correct syntax to initialize all elements of two-dimensional array to value 0?

►int arr[2][3] = {0,0} ;

►int arr[2][3] = {{0},{0}} ;

►int arr[2][3] = {0},{0} ;

►int arr[2][3] = {0} ;

Question No: 15 ( Marks: 1 ) - Please choose one

When an operator function is define as member function then operand on the left side of operator must be an object.

True►

►False

Question No: 16 ( Marks: 1 ) - Please choose one

break statement can be used outside a loop or switch statement.

True►

►False

Question No: 17 ( Marks: 1 ) - Please choose one

The keyword_______ is used to return some value from a function.

►return

►break

Page 169: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►continue

►goto

Question No: 18 ( Marks: 1 ) - Please choose one

Every data member or function inside the structure is ___ by default whereas everything declared inside a class is ____ by default.

►private, public

public, private►

►private, protected

►public, protected

Question No: 19 ( Marks: 1 ) - Please choose one

Which of the following is true for the C++ statement given below?

int &ref = val ;

►It creates a synonym for variable ‘val’

►It creates an alias for variable ‘val’

►It’s a new name for variable ‘val’

All of the given options ►

Page 170: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 20 ( Marks: 1 ) - Please choose one

If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below?

obj3 = obj1 + obj2 ;

►obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator

►obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator

►Both objects (obj1, obj2) will be passed as arguments to the + operator

►Any of the objects (obj1, obj2) can drive the + operator

Question No: 21 ( Marks: 1 ) - Please choose one

Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.

►One, zero

Zero, one►

►One, two

►Two, one

Page 171: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 22 ( Marks: 1 ) - Please choose one

The input/output streams cin and cout are ________ therefore have _______.

Structures, function► Objects, member functions► Functions, objects► None of the given options►

Question No: 23 ( Marks: 1 ) - Please choose one

If a symbolic constant has been defined, it will be an error to define it again.

True►

False►

Question No: 24 ( Marks: 1 ) - Please choose one

Every class contains _______________.

Constructor►

Destructor►

Both a constructor and a destructor►

None of the given options►

Page 172: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 25 ( Marks: 1 ) - Please choose one

new and delete keywords are _____________ in C++ language.

Built-in- Function►

Operators►

Memory Allocation Function►

None of► the given options

Question No: 26 ( Marks: 1 ) - Please choose one

Consider the following code segment.

class M {

public:

M &operator+(const M &);

...

};

p + q //code of line implies that p.operator+(q)

...

Let assume if p and q are class objects then function is implemented as _______

Page 173: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►Member function

►Non-member function

►Friend function

►None of the given options

Question No: 27 ( Marks: 1 ) - Please choose one

Assignment operator is -------------------------associative.

►right

►left

binary►

►unary

Question No: 28 ( Marks: 1 ) - Please choose one

Static variable which is defined in a function is initialized __________.

Only once during its life time ►

Every time the function call► Compile time of the program► None of the above►

Page 174: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 29 ( Marks: 1 ) - Please choose one

Wecan not define a variable of user-defined data type in the class.

►True

False►

Question No: 30 ( Marks: 1 ) - Please choose one

A constructor that will create a new object with a full copy of the other object, is copy is known as ___________

►deep copy

►shallow copy

►constructor copy

►none of the options

Question No: 31 ( Marks: 1 )

What is the use of reference data type?

A reference data type is a variable that can contain an address. The reference data types in Java are arrays, classes and interfaces. You'll hear often say that Java does not have pointers. Yet, you could consider a reference data type to be a pointer

Question No: 32 ( Marks: 1 )

What are the main types of operators in terms of number of arguments they take?

The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.

Page 175: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 33 ( Marks: 2 )

What is the this pointer? Give an example of its use

In a C++ program, if you create object A of class X, you can then obtain the address of A by using the "this" pointer. The address is available as a local variable in the non-static member functions of X, and its type is const X*. The "this" pointer works because C++ creates instances of its data members, and it keeps one copy of each member function.

Question No: 34 ( Marks: 2 )

What are manipulators? Give one example.

Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of displayed endl manipulator. This manipulator has the same functionality as the ‘\n’ newline character.

Question No: 35 ( Marks: 3 )

What will be the output of following functions if we call these functions three times?

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

}

Output will be:

111

Page 176: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Output will be:

123

Question No: 36 ( Marks: 3 )

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

malloc returns a void pointer to the allocated space or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item.

By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when malloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason.

Question No: 37 ( Marks: 3 )

If we want to send the data by reference and don’t want that original data should be affected then what can we do to prevent any change?

Page 177: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 38 ( Marks: 5 )

Write down the disadvantages of the templates.

The disadvantages of templates are:

• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 39 ( Marks: 5 )

The following code segment has errors. Locate as many as you can and explain briefly.

class Circle // no need to enter colon here , so I removed it

{

private : //colon missing

double centerX;

double centerY;

double radius;

public: //colon missing

void setCenter(double, double);

void setRadius(int);

};//semi colon missing

Question No: 40 ( Marks: 10 )

Write a program which consists of two classes, Date and Person.

Date class should contain three data members day, month, year and setter and getter function for these data members. Date class should also contain showdate() member function to display date.

Page 178: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Person class should contain three data members Name, Address, and Bday, where Name and Address are char pointer while Bday(Date of birth) is of type Date, Person class should further contain two member functions Display() and setdate().

In main program Create an object of Class person and call the member functions with it.

Please find the Answer in following Box

#include <stdio.h>

#include <iostream>

#include <cstring>

using namespace std;

class Date

{

public:

int day;

int month;

int year;

public:

Date()

{

day=0;

month=0;

year=0;

}

Page 179: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM void setDay(int);

void setMonth (int);

void setYear(int);

int getDay();

int getMonth();

int getYear();

void showDate();

};

void Date: :setDay(int d)

{

if{d<1 | | d>31)

cout<<"Invalid month Renter it";

cin>>d;

}

day=d;

}

void Date: :setMonth (int m)

{

if(m<1 | | m>12)

{

cout<<"Invalid month Renter it";

cin>>m;

}

month=m;

}

void Date: :setYear (int y)

{

Page 180: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM year=y;

int Date: :getDay()

{

return day;

}

int Date: :getMonth()

{

return month:

}

int Date: :getYear()

{

return year;

}

void Date: :showDate()

{

cout<<day<<"-"<<month<<"-"<<year<<end1;

}

Class Person

{

public:

char *Name;

char *Address

Date Bday;

public:

Student()

{

Name=new char[20];

Page 181: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Address=new char[10];

cin.getline(Name,20);

cout<<"Enter Address:";

cin.getline(Address,10);

}

void setDate()

{

cout<<"Enter Day:";

cin>>Ad_date.day;

cout<<"Enter month:";

cin>>Ad_date.month;

cout<<"Enter Year:";

cin>>Ad_date.year;

}

void Display()

{

cout<<"Name: "<<end1;

cout<<"Address: "<<Address<<end1;

cout<<"Date of Birth: ";

Ad-date.showDate();

}

};

void main()

{

Person object;

object.setDate();

object.Display();

Page 182: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM system("pause");

}

Question No: 41 ( Marks: 10 )

Write a C++ program that contains a class ‘myClass’ having two data members of type int.

The class must have

· A default constructor which must initialize all the data members to their meaningful values.

· A destructor with no implementation.

· Setter member functions to set all data members of class

· Getter member functions to get all data members of class

In main function of the program

5. Prompt the user to enter the number of objects to be created.

6. Dynamically allocate memory to objects according to the size entered by user.

7. De-allocate memory that was allocated to objects

Find solution in following box:

#include <stdio.h>

#include <iostream>

#include <cstring>

using namespace std;

Page 183: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

class myclass

{

public:

int a;

int b;

int *iptr, *sptr;

construct{int,int.int}

void seta(int);

void setb(int);

void setc(int);

int geta();

int getb();

int getc();

};

void Person: :seta(int aa)

{

a=aa;

}

Page 184: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

void Person: :setb (int bb)

{

b=bb;

}

void Person: :setc (int cc)

{

c=cc;

}

main()

{

int num;

cout<<"Enter the number of objects to be created";

cin>>num;

for (int i =1;i==num;i++)

{

Person i_

}

Page 185: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

}

CS201 – Introduction to Programmming

Solved Subjective Questions

From spring 2010 Final Term Papers

By ღ( Cuŧє~Mαħϊ )♥ ♥ ღ

Question No: 1                   ( Marks: 2 )   

Write a declaration statement for an array of 10 elements of type float. Include an initialization statement of the first four elements to 1.0, 2.0, 3.0 and 4.0.

 

Answer:

float  floatArry[10] = {1.0,2.0,3.0,4.0};

 

Question No: 2                   ( Marks: 2 )   

Write the general syntax for the declaration of pre-increment and post-increment member operator function.

Page 186: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Answer:

Classname operator ++(); ---- pre increment

Classname operator ++(int)  ---- post increment

 

 

Question No: 3                   ( Marks: 2 )   

 

Give the general syntax of class template.

 

Answer:

template

class myclass { ---} ;

 

Question No: 4                   ( Marks: 2 )   

 

What is a truth Table?

 

Answer:

There are some areas where the decision structures become very complicated. Sometimes, we find it difficult to evaluate a complicated logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax statement in any language. It becomes complicated to determine what will be evaluated in what way. We know the concept of truth table. The truth tables are very important. These are still a tool available for analyzing logical

Page 187: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMexpressions. We will read logic design in future, which is actually to do with chips and gates. How we put these things together.

Question No: 5                   ( Marks: 2 )   

 

What will be the output of following code, if user input a number 123?

 

int input ;

cin >> oct >> input;

cout << hex << input ;

 

Answer:

 

53

 

Rational: it will take 123 as octal and print it in hex form which is 53.

 

 

Question No: 6                   ( Marks: 2 )   

 

What is principle of friendship in the context of functions and classes?

 

Answer:

Page 188: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMClass can declare a friend function and someone from outside the class cannot declare itself friend of a class.

A friend function can access the private variables of class just like a member function

 

Question No: 7                     ( Marks: 2 )   

 

How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

 

Answer:

Unary operator takes only one argument like i++ or i— (Post increment or post decrement operators for integers) or ++i,--i  (Pre increment or pre decrement operators for integers) ,we can not make Unary operator as binary or binary as Unary operator.

 

Question No: 8                     ( Marks: 2 )   

 

Which arithmetic operators cannot have a floating point operand?

 

Answer:

Modulus operator:

This operator can only be used with integer operands ONLY

 

Question No: 9                     ( Marks: 2 )   

Page 189: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

What are manipulators? Give one example.

 

Answer:

The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14. This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

Question No: 10                     ( Marks: 2 )   

 

Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;

 

Answer:

int matrix [3] [3] ;

 

include

 

 

main () {

    int matrix [3][3];

    int inivalue = 0;

  

Page 190: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM    for (int a=0;a<3;a++)

    { for (int b = 0;b<3;b++)

   { matrix[a][b]= inivalue;

    cout<

}

 

Question No: 11                   ( Marks: 2 )   

 

What is the difference between switch statement and if statement.

Answer:

The “If” statement is used to select among two alternatives. It uses a Boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

 

Question No: 12                   ( Marks: 2 )      

 

How can we initialize data members of contained object at construction time?

 

Answer:

Initializer list is used to initialize the contained objects at the construction time.

 

Question No: 13                   ( Marks: 2 )   

Page 191: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Can we overload new and delete operators?

 

Answer:

Yes, it is possible to overload new and delete operators to customize memory management. These operators can be overloaded in global (non-member) scope and in class scope as member operators.

 

Question No: 14                   ( Marks: 2 )   

Suppose there is a template function ‘func’ having argument of type U and return type T. What will be the C++ syntax to call this function, passing a variable ‘x’ of type double and returning an int type?

Answer:

template

T func (T a, U b) {

  return (a

}

calling

int  i;

double x;

x = func

 

 

Page 192: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 15                   ( Marks: 2 )   

Which variable will be used in inner code block if we have the same names of variable at outer code block and inner code block?

 

Answer:

Simply: variable of the inner code is use in the inner code block.

 

Question No: 16                   ( Marks: 2 )   

What is the benefit of reference and where can we use it?

 Answer:

In references we give the memory address of the object, due to references we pass values without making the copy. Hence, when we have many values & we want efficiency we use references to avoid copy.

Question No: 17                   ( Marks: 2 )   

Page 193: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWrite the C++ code for the declaration of overloaded stream insertion and stream extraction operator for the object d of type Date.

 

Answer:

Date operator >> (date & d1){

cout<<<”-”<<<”-”<

}

 

 

 

Question No: 18                     ( Marks: 2 )   

 

What is difference between endl and \n? 

Answer:

Endl is manipulator and it inserts new line character and flushes the stream.

 

\n is control character which is used to insert line break.

 

Question No: 19                     ( Marks: 2 )   

 

What does code optimization mean?

 

Page 194: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAnswer:

It is process by which we make our code in such a way that it improves the speed of program. By use of optimization we refine program codes in such a way that it run faster and consume less memory. We do it in such a way that output quality is not compromised.

 

Question No: 20                     ( Marks: 3 )   

 

How is the following cout statement interpreted by compiler?

 

cout << a << b << c ;

 

Answer:

It will give a compiler error because a,b,c are not declared.

Question No: 21                     ( Marks: 3 )   

 

Suppose an object of class A is declared as data member of class B.

 

(i)                                     The constructor of which class will be called first? 

Answer: A

(ii)                                   The destructor of which class will be called first?

Answer: B

Page 195: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

 

Question No: 22                   ( Marks: 3 )   

What will be the output of following functions if we call these functions three times?

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

}

 

Answer:

 

1

1

1

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

Page 196: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM} 

 

Answer:

1

2

3

Question No: 23                   ( Marks: 3 )   

If is not available in the system then what does calloc/malloc and new operator return?

Answer:

calloc/malloc and new operator return returns a null pointer to indicate that no memory is available

Question No: 24                   ( Marks: 3 )   

 

What is the keyword ‘this’ and what are the uses of ‘this’ pointer?

 

Answer:

'this' is use to refer the current class member without using the name of the class.

 

Page 197: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 25                     ( Marks: 3 )   

Which one (copy constructor or assignment operator) will be called in each of the following code segment?

 

1) Matrix m1 (m2);

2) Matrix m1, m2;

m1 = m2;

3) Matrix m1 = m2;

 

Answer:

 

1) Matrix m1 (m2);   copy constructor

2) Matrix m1, m2;   

m1 = m2;               assignment operator

3) Matrix m1 = m2; assignment operator

Question No: 26                     ( Marks: 3 )   

What will be the output of following function if we call this function by passing int 5?

 

template T reciprocal(T x) {return (1/x); }

Page 198: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Answer:

 

0

The output will zero as 1/5 and its .05 but conversion to int make it zero

Above is prototype of template class so assume passing an int and returning an int

 

 

 

Question No: 27                     ( Marks: 3 )   

 

Identify the errors in the following member operator function and also correct them.

 

math * operator(math m);

math * operator (math m)

{

     math temp;

     temp.number= number * number;

     return number;

   

}

 

Page 199: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAnswer:

 

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator (math *m)

{

     math temp;

     temp = m;

     temp.number= number * number;

     return temp.number;

   

}

Question No: 28                   ( Marks: 3 )   

 

What are the limitations of the friendship relation between classes?

 

Answer:

friendship relation between classes is a one way relation that is if one class declare friend another class then the another class is the friend of first class but not the first class if the friend of another class.

 

Page 200: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Question No: 29                   ( Marks: 3 )   

 

Define static variable. Also explain life time of static variable?

 

Answer:

When you declare a static variable (native data type or object) inside a function, it is created and initialized only once during the lifetime of the program.

 

Question No: 30                     ( Marks: 5 )   

 

What is difference between Unary and binary operators and how they can be overloaded?

 

Answer:

Unary operator takes one argument.

a ++ is an example of unary operator

 

Binary take two operators

+,-,* are example of binary operators

Overloaded binary operator may return any type

 

Here is general syntax of overloading

Return-type operator symbol (parameters);

Page 201: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Operator is keyword

 

Question No: 31                     ( Marks: 5 )   

 

What steps we must follow to design good program?

 

Answer 1:

 

Hence to design a program properly, we must:

Analyze a problem statement, typically expressed as a word problem.

Express its essence, abstractly and with examples.

Formulate statements and comments in a precise language.

Evaluate and revise the activities in light of checks and tests and

Pay attention to detail.

Answer 2:

 

Details:  we must check very details of any program. It is very important aspect of any program.  We must pay complete attention to calculation.

We must give attention to logic and its flow should be smooth.

 

Page 202: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMReusable: We must write program in such a way that we can reuse them in other program. Like we define function in such a way that in future if we need any similar kind of function is requires in that case we can easily modify or reuse it.

 

Comments: we write the details of important steps in the form of comments. We should use comments in such a way if any body else wanted to reuse or debug or codes he can easily understand it.

 

 

Code readability: We should use Tab and spaces so codes are easily readable.

 

User interface: we make interface user friendly. Use polite prompts for user while take input.

Question No: 32                     ( Marks: 5 )   

 

Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.

Note: Display all data with in a particular width and the empty space should be filled with character x

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

xxxxx20000

xxxxx30000

xxxxx60000

Page 203: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Answer:

 

#include

#include

main(){

       int sal1 =1000;

       int sal2 =1500;

       int sal3 =20000;

       int sal4 =30000;

       int sal5 =60000;

            

       

cout << setfill ('x') << setw (10);

cout<< sal1<

cout << setfill ('x') << setw (10);

cout<< sal2<

cout << setfill ('x') << setw (10);

cout<< sal3<

cout << setfill ('x') << setw (10);

cout<< sal4<

cout << setfill ('x') << setw (10);

cout<< sal5<

Page 204: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMint i=0; 

cin>>i; // to stop the screen to show the output

}

 

Question No: 33                   ( Marks: 5 )   

Suppose we have the following class.

class Matrix

{

private:

int Elements[3][3];

};

 

Write the operator function of stream extraction operator (>>) for this class.

Answer:

Element operator >> (Element &element){

cout<

cout<

cout<

Page 205: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM}

Question No: 34                   ( Marks: 5 )   

What is meant by user interface and class interface in C++ ? And what role a class interfaces can play in user interface [Marks 5]

 

Answer:

 

 

 

Question No: 35                   ( Marks: 5 )   

 

Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.

 

Answer:

class frinedclass{

public:

friend int compute(exforsys e1)

};

Int compute(exforsys e1){

Page 206: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM//Friend Function Definition which has access to private datareturn int(e1.a+e2.b)-5;}

 

Question No: 36                     ( Marks: 5 )   

 

What are the advantages and disadvantages of using templates?

 

Answer:

Many things can be possible without using templates but it does offer several clear advantages not offered by any other techniques:

 

Advantages:

Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.

It helps in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

 

Disadvantages:

On the other hand, if misused

Page 207: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMTemplates can make code difficult to read and follow depending

upon coding style.

They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

 

Question No: 37                     ( Marks: 5 )   

Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

         math obj1, obj2;

         obj2= 10 + obj1  ; 

 

Answer:

#include

math

{

mth operator + (obj1,int x)

{

 

number temp;

Page 208: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMtemp=obj1.number+x;

return temp.number;

}

}

 

Question No: 38                   ( Marks: 5 )   

 

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

 

Answer:

 

#include

#include

main () {

double a = 12.12345;

double b = 13.123456;

double c = 14.1234567;

cout << setprecision (5) << a << endl;

cout << setprecision (2) << a << endl;

cout << setprecision (3) << a << endl;

}

Page 209: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Question No: 39                   ( Marks: 5 )   

 

Let we have a class,

class String

{

private:

char  buf[25];

};

Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment

 

Answer:

void String::operator = ( const String &other )

 { int length ;

 length = other.length();

delete buf;

 buf = new char [length + 1];

 strcpy( buf, other.buf ); }

 

Question No: 40                   ( Marks: 5 )   

 

Page 210: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMRead the given below code and explain what task is being performed by this function

Matrix :: Matrix ( int row , int col )

{

    numRows = row ;

    numCols = col ;

    elements = new ( double * ) [ numRows ] ;

    for ( int  i = 0 ; i < numRows ; i ++ )

{

        elements [ i ] = new double [ numCols ] ;

        for ( int j = 0 ; j < numCols ; j ++ )

                elements [ i ] [ j ] = 0.0 ;

     }

}

Hint : This function belong to a matrix class, having

Number of Rows = numRows

Number of Columns = numCols

 

Answer:

In the above mentioned code, first of all programmer call the constructor who have two parameters for the number of rows & columns in the matrix. Then this constructor also dynamically allocates the memory for the elements of the matrix & also initializes the value of the all elements of matrix with 0.0

FINALTERM EXAMINATION

Page 211: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMSpring 2010

CS201- Introduction to Programming

Ref No:

Time: 90 min

Marks: 58

Student Info

Student ID:

Center:

Exam Date:

For Teacher's Use Only

Q No. 1 2 3 4 5 6 7 8 Total

Marks

Q No. 9 10 11 12 13 14 15 16

Marks

Q No. 17 18 19 20 21 22 23 24

Marks

Q No. 25 26 27 28 29 30 31 32

Marks

Q No. 33 34 35 36

Marks

Page 212: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 1 ( Marks: 1 ) - Please choose one

*.doc is _____________ by type.

.

► Sequential File

► Random Access File

► Data File

► Record File

Question No: 2 ( Marks: 1 ) - Please choose one

Which of the following is NOT a preprocessor directive?

Page 213: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► #error

► #define

► #line

► #ndefine

Question No: 3 ( Marks: 1 ) - Please choose one

The return type of operator function must always be void.

► True

► False

Page 214: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 4 ( Marks: 1 ) - Please choose one

What does (*this) represents?

► The current function of the class

► The current pointer of the class

► The current object of the class

► A value of the data member

Question No: 5 ( Marks: 1 ) - Please choose one

The statement cin.get (); is used to,

► Read a string from keyboard

Page 215: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Read a character from keyboard

► Read a string from file

► Read a character from file

Question No: 6 ( Marks: 1 ) - Please choose one

When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor.

► True

► False

Question No: 7 ( Marks: 1 ) - Please choose one

Overloaded new operator function takes parameter of type size_t and returns

Page 216: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► void (nothing)

► void pointer

► object pointer

► int pointer

Question No: 8 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

► False

Page 217: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 9 ( Marks: 1 ) - Please choose one

C++ is a case-sensitive language

► True

► False

Question No: 10 ( Marks: 1 ) - Please choose one

To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

► include <iostream.h>

► #include <iostream.h>

Question No: 11 ( Marks: 1 ) - Please choose one

A template function must have only generic data types.

► True

► False

Page 218: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 12 ( Marks: 1 ) - Please choose one

Template class can not have static variables.

► True

► False

Question No: 13 ( Marks: 1 ) - Please choose one

What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?

► *ptr = arr ;

► ptr = arr ;

► *ptr = arr[5] ;

► ptr = arr[5] ;

Page 219: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 14 ( Marks: 1 ) - Please choose one

What will be the correct syntax to access the value of fourth element of an array using pointer ptr?

► ptr[3]

► (ptr+3)

► *(ptr+3)

► Both 1and 3

Question No: 15 ( Marks: 1 ) - Please choose one

If most significant bit of un-signed number is 1 then it represents a positive number.

► True

► False

Page 220: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 16 ( Marks: 1 ) - Please choose one

If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____.

► Address of variable, reference variable

► Reference variable, value of variable

► Reference variable, address of variable

► Address of variable, value of variable

Question No: 17 ( Marks: 1 ) - Please choose one

We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 18 ( Marks: 1 ) - Please choose one

The default value of a parameter can be provided inside the ________________

Page 221: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► function prototype

► function definition

► both function prototype or function definition

► none of the given options.

Question No: 19 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Question No: 20 ( Marks: 1 ) - Please choose one

Page 222: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What purpose do classes serve?

► Data encapsulation

► Providing a convenient way of modeling real-world objects

► Simplifying code reuse

► All of the given options

Question No: 21 ( Marks: 1 ) - Please choose one

Every class contains _______________.

► Constructor

► Destructor

Page 223: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Both a constructor and a destructor

► None of the given options

Question No: 22 ( Marks: 1 ) - Please choose one

new operator is used to allocate memory from the free store during

► Compile Time

► Run Time

► Link Time

► None of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside another class then,

Page 224: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Destructor of enclosing class will be called first

► Destructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 24 ( Marks: 1 ) - Please choose one

It is possible to define a class within another class.

► True

► False

Question No: 25 ( Marks: 1 ) - Please choose one

New and Delete are also used with ___________ and data types as well.

► Class, Objects

► Structures, Pointers

Page 225: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Both Class and structures

► None of above

Question No: 26 ( Marks: 1 ) - Please choose one

With New keyword, data types and class members are initialized with meaningful values instead of garbage.

► True

► False

Question No: 27 ( Marks: 2 )

How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

Ans: Unary operator takes only one aurgument like i++ or i— (Post increment or post decrement operators for intergers) or ++i,--i (Pre increment or pre decrement operators for intergers) ,we can not make Unary operator as binary or binary as Unary operator.

Question No: 28 ( Marks: 2 )

Which arithmetic operators cannot have a floating point operand?

Page 226: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAns:

Modulus operator

This operator can only be used with integer operands ONLY

Question No: 29 ( Marks: 2 )

What are manipulators? Give one example.

Ans:

The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14 . This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

Some manipulators are parameter less. We simply use the name of the manipulator that works. For example, we have been using endl, which is actually a manipulator, not data. When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it manipulates the output stream.

Page 227: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 30 ( Marks: 2 )

Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;

Ans:

int matrix [3] [3] ;

matrix [0] [0] = 0;

matrix [0] [1] = 0;

matrix [0] [2] = 0;

matrix [1] [0] = 0;

matrix [1] [2] = 0;

matrix [1] [2] = 0;

matrix [2] [0] = 0;

matrix [2] [1] = 0;

matrix [2] [2] = 0;

we can also do it as given below

int matrix [3][3] = { 0 }; //all elements 0 

Question No: 31 ( Marks: 3 )

Which one (copy constructor or assignment operator) will be called in each of the following code segment?

1) Matrix m1 (m2);

2) Matrix m1, m2;

m1 = m2;

3) Matrix m1 = m2;

Page 228: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Ans:

1) Matrix m1 (m2); copy constructor

2) Matrix m1, m2;

m1 = m2; assignment operator

3) Matrix m1 = m2; assignment operator

Question No: 32 ( Marks: 3 )

What will be the output of following function if we call this function by passing int 5?

template <class T>T reciprocal(T x) {return (1/x); }

Ans:

1/5

Question No: 33 ( Marks: 3 )

Identify the errors in the following member operator function and also correct them.

math * operator(math m);

math * operator (math m)

{

math temp;

Page 229: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM temp.number= number * number;

return number;

}

ANS:

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator(math *m);

math *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

Question No: 34 ( Marks: 5 )

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

Page 230: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAns:

#include <iostream>

#include <iomanip>

int main ()

{

double x1 = 12345624.72345

double x2 = 987654.12345

double x3 = 1985.23456

cout << setprecision (3) << x1<< endl;

cout << setprecision (4) << x2 << endl;

cout << setprecision (5) << x3<< endl;

return 0;

}

Question No: 35 ( Marks: 5 )

What are the advantages and disadvantages of using templates?

Ans:

Many thing can be possible without using templates but it do offer several clear advantages not offered by any other techniques:

Advanatages:

• Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

Page 231: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM• Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

• Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.

• It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

Disadvantages:

On the other hand, if misused

• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 36 ( Marks: 5 )

Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

math obj1, obj2;

obj2= 10 + obj1 ;

Ans:

#include <iostream.h>

math

{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

{

Page 232: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

}

}

……………………………………………………………………………

FINALTERM EXAMINATION

CS201- Introduction to Programming

Question No: 1 ( Marks: 1 ) - Please choose one

There are mainly -------------------- types of software

► Two

► Three

► Four

► Five

Question No: 2 ( Marks: 1 ) - Please choose one

Structures help to define program-specific ___________ .

Page 233: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► functions

► datatypes

► Arithmetic operations

► None of the given options.

Question No: 3 ( Marks: 1 ) - Please choose one

A special name which is substituted in code by its definition and as a result we get an expanded code is called,

► include

► Directive

► Macro

► Define

Page 234: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 4 ( Marks: 1 ) - Please choose one

If constructor contains a return statement in its body then compiler will give __________

► No error

► Syntax error

► Logical error

► Run time error

Question No: 5 ( Marks: 1 ) - Please choose one

eof( ), bad( ), good( ), clear( ) all are manipulators.

http://vustudents.ning.com

► True

Page 235: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► False

Question No: 6 ( Marks: 1 ) - Please choose one

If a friend function outside the class declare itself friend of the class, this will neglect concept of ___________.

► encapsulation and data accessing

► encapsulation and data hiding

► friend member functions of the class

► Interface of the class

Question No: 7 ( Marks: 1 ) - Please choose one

Which of the following is the only operator that the compiler overloads for user define data type? http://vustudents.ning.com

Page 236: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Plus (+)

► Minus (-)

► Equal (= =)

► Assignment (=)

Question No: 8 ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ .

► Member function

► Non-member function

Page 237: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Private function

► Public function

Question No: 9 ( Marks: 1 ) - Please choose one

We can also create an array of user define data type.

► True

► False

Question No: 10 ( Marks: 1 ) - Please choose one

The default scope for members of structures is public whereas the default visibility for class members is private.

► True

► False

Page 238: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 11 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

► Destructor is called first before deallocating memory

Question No: 12 ( Marks: 1 ) - Please choose one

Overloaded delete operator function takes the same parameter as an argument returned by new operator function. http://vustudents.ning.com

► True

Page 239: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► False

Question No: 13 ( Marks: 1 ) - Please choose one

In a group of nested loops, which loop is executed the most number of times?

► the outermost loop

► the innermost loop

► all loops are executed the same number of times

► cannot be determined without knowing the size of the loops

Question No: 14 ( Marks: 1 ) - Please choose one

Which of the following syntax will be used to initialize an array of size 5 of int data type to value 0?

► arr[5] = {0} ;

Page 240: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► int arr[5]= 0 ;

► int arr[5] = {0} ;

► int arr[] = 0 ;

Question No: 15 ( Marks: 1 ) - Please choose one

What will be the correct syntax of the following statement?ptr is a constant pointer to integer.

► const int *ptr ;

► const *int ptr ;

► int const *ptr ;

► int *const ptr ;

Question No: 16 ( Marks: 1 ) - Please choose one

Page 241: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

http://vustudents.ning.com

We want to access array in random order which approach is better?

► Pointers

► Array index

► Both pointers and array index are better

► None of the given options.

Question No: 17 ( Marks: 1 ) - Please choose one

What is the output of the following statement?

int i = 2.5; do { cout i * 2; } while (i > 3 && i < 10);

► 510

► 5

► 48

► error

Page 242: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 18 ( Marks: 1 ) - Please choose one

Which statement about operator overloading is false?

► New operators can never be created

► Certain overloaded operators can change the number of arguments they take.

► The precedence of an operator cannot be changed by overloading.

► Overloading cannot change how an operator works on built-in types.

Question No: 19 ( Marks: 1 ) - Please choose one

The stream insertion and stream extraction operators are already overloaded for ______.

Page 243: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► User-defined data types

► Built-in data types

► User-defined and built-in data types

► None of the given options

Question No: 20 ( Marks: 1 ) - Please choose one

The stream insertion and extraction operators are not already overloaded for _______

► Built-in data types

► User-defined data types

► Both built-in and user-defined types

► None of the given options

Question No: 21 ( Marks: 1 ) - Please choose one

Page 244: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

The programs, in which we allocate static memory, run essentially on ________

► Heap

► System Cache

► None of the given options

► Stack

Question No: 22 ( Marks: 1 ) - Please choose one

The dynamic memory allocation uses memory from the ____________.

► Stack

► Heap

► System Cache

► None of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

Page 245: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

The default value of a parameter can be provided inside the ________________

► function prototype

► function definition

► both function prototype or function definition

► none of the given options.

Question No: 24 ( Marks: 1 ) - Please choose one

Consider the following code segment

class M {

friend int operator!(const M &);

...

};

!s // code of line implies that operator!(s)

...

Let assume if s is an object of the class then function is implemented as ___________

Page 246: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Member function

► Non-member function

► Binary operator function

► None of the given options

None of the given options

Question No: 25 ( Marks: 1 ) - Please choose one

The following prototype of unary operator function indicates that it is ____________ .

Date operator++(int )

► Member functions of post increment operator

► Member functions of pre increment operator

Page 247: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Non-member functions of post increment operator

► Non-member functions of pre increment operator

Question No: 26 ( Marks: 1 ) - Please choose one

The second parameter of operator function for >> operator must always be passed

► By reference

► Function takes no argument

► By value

► None of the given options

Page 248: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 27 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside another class then, http://vustudents.ning.com

► Destructor of enclosing class will be called first

► Destructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 28 ( Marks: 1 ) - Please choose one

When ever dynamic memory allocation is made in C/C++, it is freed_____________.

► Explicitly

Page 249: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Implicitly

► Both explicitly and implicitly

► None of the given options

Question No: 29 ( Marks: 1 ) - Please choose one

The prototype of friend functions is written:

► Top of the class definition.

► As Private member functions.

► Anywhere inside the class definition

► None of the given options

Page 250: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 30 ( Marks: 1 ) - Please choose one

What should be the return type of the constructor?

► void pointer

► int

► same as object type

► constructors do not return any thing

Question No: 31 ( Marks: 1 )

Is there any type checking on the parameters of macros?

Question No: 32 ( Marks: 1 )

What does an arity of operator represent? http://vustudents.ning.com

Question No: 33 ( Marks: 2 )

Page 251: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAre the methods of a class reproduced for every object?

Question No: 34 ( Marks: 2 )

Can we overload new and delete operators?

Question No: 35 ( Marks: 3 )

What will be the output of following function if we call this function by passing int 5?

template <class T>T reciprocal(T x) {return (1/x); }

Question No: 36 ( Marks: 3 )

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

Question No: 37 ( Marks: 3 )

Page 252: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

If we want to send the data by reference and don’t want that original data should be affected then what can we do to prevent any change?

Question No: 38 ( Marks: 5 )

Why the first parameter of operator function for << operator must be passed by reference? http://vustudents.ning.com

Question No: 39 ( Marks: 5 )

What will be the output of the following program?

int addValue (int &);

main () {

int x, y;

x = 23;

cout << "Value of x before calling addValue ():\t" << x << endl;

y = addValue (x);

cout << "Value of x after calling addValue ():\t" << x <<endl;

cout << "Value of y:\t" << y;

}

Page 253: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMint addValue (int &a){

a = a + 2;

return a;

}

Question No: 40 ( Marks: 10 )

While doing dynamic memory allocation in class what are the important things that should be implemented in class.

Question No: 41 ( Marks: 10 )

What is difference between using a square(x) macro and square(x) function?

FINALTERM EXAMINATION

CS201- Introduction to Programming

Question No: 1 ( Marks: 1 ) - Please choose one

If it is required to copy an array to another array then,

► Both arrays must be of the same size and data type

► Both arrays may be of different size

► Both arrays may be of different data type

Page 254: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Both arrays may be of different size and type

Question No: 2 ( Marks: 1 ) - Please choose one

Dealing with structures and functions passing by reference is the most economical method

► True

► False

Question No: 3 ( Marks: 1 ) - Please choose one

eof( ), bad( ), good( ), clear( ) all are manipulators.

► True

► False

Question No: 4 ( Marks: 1 ) - Please choose one

Page 255: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Overloaded new operator function takes parameter of type size_t and returns

► void (nothing)

► void pointer

► object pointer

► int pointer

Question No: 5 ( Marks: 1 ) - Please choose one

When new operator is overloaded at global level then corresponding built-in new operator will not be visible to whole of the program.

► True

► False

Page 256: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 6 ( Marks: 1 ) - Please choose one

If there is more than one statement in the block of a for loop, which of the following must be placed at the beginning and the ending of the loop block?

► parentheses ( )

► braces { }

► brackets [ ]

► arrows < >

Question No: 7 ( Marks: 1 ) - Please choose one

The return type of a function that do not return any value must be ________

► float

► int

► void

► double

Page 257: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 8 ( Marks: 1 ) - Please choose one

UNIX has been developed in ________ language.

► JAVA

► B

► C

► FORTRAN

Question No: 9 ( Marks: 1 ) - Please choose one

Like member functions, ______ can also access the private data members of a class.

► Non-member functions

Page 258: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Friend functions

► Any function outside class

► None of the given options

Question No: 10 ( Marks: 1 ) - Please choose one

Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition.

► It can be declared anywhere in class as these are not affected by the public and private keywords.

Question No: 11 ( Marks: 1 ) - Please choose one

Page 259: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe operator function overloaded for an Assignment operator (=) must be

► Non-member function of class

► Member function of class

► Friend function of class

► None of the given options

Question No: 12 ( Marks: 1 ) - Please choose one

The endl and flush are _______

► Functions

► Operators

► Manipulators

Page 260: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Objects

Question No: 13 ( Marks: 1 ) - Please choose one

If a symbolic constant has been defined, it will be an error to define it again.

► True

► False

Question No: 14 ( Marks: 1 ) - Please choose one

The operator used for casting, in C, is standard ________ operator.

► none of the given options.

► cast

► cost

► const

Page 261: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 15 ( Marks: 1 ) - Please choose one

Constructors can not be overloaded like ordinary functions.

► True

► False

Question No: 16 ( Marks: 1 ) - Please choose one

Which of the following function call is correct for the function prototype?

defaultParameters ( int a, int b = 7, char z = ‘*’ );

► defaultParameters (5);

► defaultParameters (5, ‘8’);

► defaultParameters (6, ‘#’);

► defaultParameters (0, 0, ‘*’, 0);

Page 262: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 17 ( Marks: 1 ) - Please choose one

When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are.

► Zero

► One

► Two

► N arguments

Question No: 18 ( Marks: 1 ) - Please choose one

We can not define a variable of user-defined data type in the class.

Page 263: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► True

► False

Question No: 19 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside an other class then,

► Constructor of enclosing class will be called first

► Constructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 20 ( Marks: 1 ) - Please choose one

Page 264: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe appropriate data type to store the number of rows and colums of the matrix is____________.

► float

► int

► char

► none of the given options.

Question No: 21 ( Marks: 1 ) - Please choose one

Class is a user defined___________.

► data type

► memory referee

► value

► none of the given options.

Question No: 22 ( Marks: 1 ) - Please choose one

Page 265: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM A pointer variable can be,

► Decremented

► Incremented

► Multiplied

► Both Decremented and Decremented

Question No: 23 ( Marks: 1 ) - Please choose one

NULL value has been defined in ______ and _________ header files.

► strings.h and iostream.h

► ctype.h and conio.c

► conio.c and conio.h

► stdlib.h and stddef.h

Page 266: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 24 ( Marks: 1 ) - Please choose one

A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

► Write a separate class to handle each

► Use templates

► Use strings to store all types

► None of the given options

Question No: 25 ( Marks: 1 ) - Please choose one

"setprecision" manipulator will set

► The number of digits after the decimal point

► The number of digits before the decimal point

Page 267: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► The number of digits in a number

► None of the given options

Question No: 26 ( Marks: 1 ) - Please choose one

Which of the following option will be true, if we overload "-=" operator?

► only - operator needs to be overloaded

► Minus (-) and = operators need to be overloaded

► the -= operator need to be overloaded explicitly

► the - and = operators need to be overloaded implicitly

Question No: 27 ( Marks: 2 )

Suppose there is a template function ‘func’ having argument of type U and return type T. What will be the C++ syntax to call this function, passing a variable ‘x’ of type double and returning an int type?

Page 268: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 28 ( Marks: 2 )

Which variable will be used in inner code block if we have the same names of variable at outer code block and inner code block?

Question No: 29 ( Marks: 2 )

What is the benefit of reference and where can we use it?

Question No: 30 ( Marks: 2 )

Write the C++ code for the declaration of overloaded stream insertion and stream extraction operator for the object d of type Date.

Question No: 31 ( Marks: 3 )

What will be the output of following functions if we call these functions three times?

Page 269: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Question No: 32 ( Marks: 3 )

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

Question No: 33 ( Marks: 3 )

Suppose an object of class A is declared as data member of class B.

(i) The constructor of which class will be called first?

(ii) The destructor of which class will be called first?

Page 270: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 34 ( Marks: 5 )

What is difference between Unary and binary operators and how they can be overloaded?

Question No: 35 ( Marks: 5 )

Suppose we have the following class.

class Matrix

{

private:

int Elements[3][3];

};

Write the operator function of stream extraction operator (>>) for this class.

Question No: 36 ( Marks: 5 )

What is meant by user interface and class interface in C++ ? And what role a class interface can play in user interface [Marks 5]

Page 271: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMFINALTERM EXAMINATION

CS201- Introduction to Programming

Time: 120 min Marks: 75

Question No: 1 ( Marks: 1 ) - Please choose one

There are mainly -------------------- types of software

► Two ► Three

► Four

► Five

Question No: 2 ( Marks: 1 ) - Please choose one

seekg() and write() are functionally _________________ .

► Different ► Identical

► Two names of same function

► None of the above

Question No: 3 ( Marks: 1 ) - Please choose one

When a pointer is incremented, it actually jumps the number of memory addresses

► According to data type

► 1 byte exactly

► 1 bit exactly

► A pointer variable can not be incremented

Question No: 4 ( Marks: 1 ) - Please choose one

setw is a parameterized manipulator.

► True ► False

Question No: 5 ( Marks: 1 ) - Please choose one

Page 272: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

eof( ), bad( ), good( ), clear( ) all are manipulators.

► True

► FalseQuestion No: 6 ( Marks: 1 ) - Please choose one

In functions that return reference, use __________variables.

► Local

► Global

► Global or static ► None of the given option

Question No: 7 ( Marks: 1 ) - Please choose one

The declarator of Plus (+) member operator function is

► Class-Name operator + (Class-Name rhs)

► operator Class-Name + ( )

► operator Class-Name + ( rhs)

► Class-Name operator + ( )

Question No: 8 ( Marks: 1 ) - Please choose one

The compiler does not provide a copy constructor if we do not provide it.

► True ► False

Page 273: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 9 ( Marks: 1 ) - Please choose one

What is the functionality of the following syntax to delete an array of 5 objects named arr allocated using new operator?

delete arr ;

► Deletes all the objects of array

► Deletes one object of array

► Do not delete any object

► Results into syntax errorQuestion No: 10 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

► Memory is allocated first before calling constructor ► Constructor is called first before allocating memory

Question No: 11 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

► Destructor is called first before deallocating memoryQuestion No: 12 ( Marks: 1 ) - Please choose one

new and delete operators cannot be overloaded as member functions.

► True

► FalseQuestion No: 13 ( Marks: 1 ) - Please choose one

The operator function of << and >> operators are always the member function of a class.

► True

► FalseQuestion No: 14 ( Marks: 1 ) - Please choose one

Page 274: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMA

template function must have at least ---------- generic data type

► Zero

► One

► Two ► Three

Question No: 15 ( Marks: 1 ) - Please choose one

If we do not mention any return_value_type with a function, it will return an _____ value.

► int ► void

► double

► float

Such function which do not return any value are called:

► int

► void ► double

► float

Page 275: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 16 ( Marks: 1 ) - Please choose one

Suppose a program contains an array declared as int arr[100]; what will be the size of array?

► 0

► 99

► 100

► 101

Question No: 17 ( Marks: 1 ) - Please choose one

The name of an array represents address of first location of array element.

► True ► False

Question No: 18 ( Marks: 1 ) - Please choose one

Reusing the variables in program helps to save the memory

► True ► False

Question No: 19 ( Marks: 1 ) - Please choose one

Which of the following option is true about new operator to dynamically allocate memory to an object?

► The new operator determines the size of an object

► Allocates memory to object and returns pointer of valid type ► Creates an object and calls the constructor to initialize the object

► All of the given options

Question No: 20 ( Marks: 1 ) - Please choose one

new and delete are _____ whereas malloc and free are _____.

► Functions, operators

► Classes, operators

► Operators, functions ► Operators, classes

Question No: 21 ( Marks: 1 ) - Please choose one

Like member functions, ______ can also access the private data members of a class.

Page 276: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Non-member functions

► Friend functions ► Any function outside class

► None of the given options

Question No: 22 ( Marks: 1 ) - Please choose one

Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition. ► It can be declared anywhere in class as these are not affected by the public and private keywords.

Question No: 23 ( Marks: 1 ) - Please choose one

The operator function overloaded for an Assignment operator (=) must be

► Non-member function of class

► Member function of class ► Friend function of class

► None of the given options

Page 277: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 24 ( Marks: 1 ) - Please choose one

For non-member operator function, object on left side of the operator may be

► Object of operator class

► Object of different class

► Built-in data type ► All of the given options

Question No: 25 ( Marks: 1 ) - Please choose one

The operator function will be implemented as _____, if obj1 drive the - operator whereas obj2 is passed as arguments to - operator in the statement given below.

obj3 = obj1 - obj2;

► Member function

► Non-member function

► Friend function ► None of the given options

Question No: 26 ( Marks: 1 ) - Please choose one

Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?

► Class-name operator +() ;

► Class-name operator +(int) ;

► Class-name operator ++() ; ► Class-name operator ++(int) ;

Question No: 27 ( Marks: 1 ) - Please choose one

The static data members of a class are initialized _______

► at file scope ► within class definition

► within member function

► within main function

Question No: 28 ( Marks: 1 ) - Please choose one

Class is a user defined___________.

Page 278: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► data type ► memory referee

► value

► none of the given options.

Question No: 29 ( Marks: 1 ) - Please choose one

We can also define a user-defines manipulators.

► True

► FalseQuestion No: 30 ( Marks: 1 ) - Please choose one

Automatic variable are created on ________.

► Heap

► Free store

► static storage ► stack

Question No: 31 ( Marks: 1 )

How do we provide the default values of function parameters?

Answer: The default value of a parameter is provided inside the function prototype or function definition. For example, we could declare the default function arguments for a function while declaring or defining it.

Page 279: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 32 ( Marks: 1 )

Why do java consider pointer as dangerous

Answer:

The concept of pointers is very important but quite limited to C and C++. The modern languages, for example JAVA, describe pointers as dangerous. We can go anywhere in the memory and can change a value. There is another problem with pointers, which is that these could be pointing to nowhere.

Question No: 33 ( Marks: 2 )

What is memory leak?

Answer: size. When there is no memory on heap, the computer will stop running and there may be a system crash. This situation is called a memory leak.

Question No: 34 ( Marks: 2 )

What does optimization the of code means?

Answer:

Header file is a nice mechanism to put function prototypes and define constants (global constants) in a single file. That file can be included simply with a single line of code.

Question No: 35 ( Marks: 3 )

What is the difference between structure and class? Answer:In structures, some data variables are gathered, grouped and named as a single entity. Class and structure are very closely related. In classes, we group some data variables and functions. These functions normally manipulate these variables. Before going ahead, it is better to understand what a class is: “A class includes both data members as well as functions to manipulate that data”

Question No: 36 ( Marks: 3 )

See the following code segment.

template <class T>

class myclass {

private:

T x;

public:

Page 280: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmyclass (T a) {

x = a;

}

};

Write the main function which creates two objects of class for int and double data types.

Question No: 37 ( Marks: 3 )

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Solution:

No, it is impossible to define two functions as in the main function the way to call both functions is same. How does the compiler know that which functions is being called? There is no way for the compiler to find out. Therefore there is an ambiguity and that is not allowed. The only thing to realize is the side effect. Side effects are critical to take care of whenever you are doing call by reference.

Question No: 38 ( Marks: 5 )

Write a program using getline() member function to inputs a string up to delimiter character comma (,) and then display the string on the screen.

Question No: 39 ( Marks: 5 )

Do you think that friend functions violate encapsulation? Justify your answer.

Answer:The friend functions of a class have access to the private data members of class. Despite being a good thing, there is possibility of vulnerability. We are opening our thoughts, inside view for somebody else. Without having 100% trust, it will be risky to make our thoughts and feelings public. We want that our private data is accessible to someone outside, not public for everybody. Otherwise, the data encapsulation and data-hiding concept will be violated. We keep the data members private and declare some specific functions that are not member of the class but friend of the class. As friends, they have access to the inside data structure of the class despite not being members.

Question No: 40 (Marks: 10 )

Page 281: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Write a simple program using the get() member function of cin object reading a text of 30 characters from the keyboard, store them in an array and then using put() member function of cout object to display them on the screen.

Question No: 41 (Marks: 10 )

Write a small program which defines two user-defined manipulators named octal and hexadecimal. These manipulators should display the decimal numbers into octal and hexadecimal.

In the main function, input a decimal number from the user and then display this decimal number into octal and hexadecimal using user-define manipulators named octal and hexadecimal.

Marks: 58Question No: 1 ( Marks: 1 ) - Please choose one*.doc is _____________ by type.. ► Sequential File ► Random Access File ► Data File ► Record FileQuestion No: 2 ( Marks: 1 ) - Please choose oneWhich of the following is NOT a preprocessor directive? ► #error ► #define ► #line ► #ndefineQuestion No: 3 ( Marks: 1 ) - Please choose oneThe return type of operator function must always be void. ► True ► FalseThe syntax of the prototype of the overloaded operator function is: return-type operator operator-symbol (parameter-list);Question No: 4 ( Marks: 1 ) - Please choose oneWhat does (*this) represents? ► The current function of the class ► The current pointer of the class ► The current object of the class ► A value of the data memberWhenever an object calls a member function, the function implicitly gets a pointer from the calling object. That pointer is known as this pointer. ‘this’ is a key word. We cannot use it as a variable name. ‘this’ pointer is present in the function, referring to the calling object. For example, if we have to refer a member, let’s say buf, of our String class, we can write it simply as: buf ; Question No: 5 ( Marks: 1 ) - Please choose oneThe statement cin.get (); is used to, ► Read a string from keyboard ► Read a character from keyboard ► Read a string from file ► Read a character from fileQuestion No: 6 ( Marks: 1 ) - Please choose one When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor. ► True

Page 282: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► FalseQuestion No: 7 ( Marks: 1 ) - Please choose one Overloaded new operator function takes parameter of type size_t and returns ► void (nothing) ► void pointer ► object pointer ► int pointerQuestion No: 8 ( Marks: 1 ) - Please choose oneThe second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators. ► True ► FalseThe second parameter to operator << is an object of the class that we are overloading the operator for. Similar is the case for operator >>. Question No: 9 ( Marks: 1 ) - Please choose one C++ is a case-sensitive language ► True ► FalseQuestion No: 10 ( Marks: 1 ) - Please choose one To include code from the library in the program, such as iostream, a directive would be called up using this command. ► #include “iostream.h” ► include ► include ► #include Question No: 11 ( Marks: 1 ) - Please choose one A template function must have only generic data types. ► True ► FalseIts not compulsory, only min we have one generic data type but we can have native data type as well. Question No: 12 ( Marks: 1 ) - Please choose one Template class can not have static variables. ► True ► FalseQuestion No: 13 ( Marks: 1 ) - Please choose oneWhat will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr? ► *ptr = arr ; ► ptr = arr ; ► *ptr = arr[5] ; ► ptr = arr[5] ; Question No: 14 ( Marks: 1 ) - Please choose oneWhat will be the correct syntax to access the value of fourth element of an array using pointer ptr? ► ptr[3] ► (ptr+3) ► *(ptr+3) ► Both 1and 3try this demo program to confirm result I wrote for you.2 option will print the reference rest 1,3 are righ options#include #include // #include  main(){int myarr [4]= {0,1,2,3};int *ptr ;ptr = myarr;cout<cout<<*(ptr+3);cout<<(ptr+3);int i = 0;cin>> i;} Question No: 15 ( Marks: 1 ) - Please choose one If most significant bit of un-signed number is 1 then it represents a positive number.

Page 283: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► True ► FalseThe most significant bit is used as a sign bit. If this bit is zero, the number is considered positive. However, if it is 1, the number will be considered negative.Question No: 16 ( Marks: 1 ) - Please choose one If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____. ► Address of variable, reference variable ► Reference variable, value of variable ► Reference variable, address of variable ► Address of variable, value of variablewe see a data type followed by & sign, it’s a reference. And when the & sign is being used in the code with a variable name then it is the address of the variableQuestion No: 17 ( Marks: 1 ) - Please choose one We can also do conditional compilation with preprocessor directives. ► True ► FalseQuestion No: 18 ( Marks: 1 ) - Please choose one The default value of a parameter can be provided inside the ________________ ► function prototype ► function definition ► both function prototype or function definition ► none of the given options.The default value of a parameter is provided inside the function prototype or function definition.Question No: 19 ( Marks: 1 ) - Please choose one Classes defined inside other classes are called ________ classes ► looped ► nested ► overloaded ► none of the given options.Question No: 20 ( Marks: 1 ) - Please choose one What purpose do classes serve? ► Data encapsulation ► Providing a convenient way of modeling real-world objects ► Simplifying code reuse ► All of the given optionsQuestion No: 21 ( Marks: 1 ) - Please choose onevuzs Every class contains _______________. ► Constructor ► Destructor ► Both a constructor and a destructor ► None of the given optionsQuestion No: 22 ( Marks: 1 ) - Please choose one new operator is used to allocate memory from the free store during ► Compile Time ► Run Time ► Link Time ► None of the given optionsQuestion No: 23 ( Marks: 1 ) - Please choose one When an object of a class is defined inside another class then, ► Destructor of enclosing class will be called first ► Destructor of inner object will be called first ► Constructor and Destructor will be called simultaneously ► None of the given optionsQuestion No: 24 ( Marks: 1 ) - Please choose one It is possible to define a class within another class. ► True ► FalseQuestion No: 25 ( Marks: 1 ) - Please choose one New and Delete are also used with ___________ and data types as well. ► Class, Objects ► Structures, Pointers

Page 284: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Both Class and structures ► None of abovewe prefer to use new and delete operators as they are designed to work with classes and objectsQuestion No: 26 ( Marks: 1 ) - Please choose one With New keyword, data types and class members are initialized with meaningful values instead of garbage. http://vuzs.net/ ► True ► FalseQuestion No: 27 ( Marks: 2 ) How many arguments a Unary Operator take? Can we make a binary operator as unary operator? Ans: Unary operator takes only one aurgument like i++ or i— (Post increment or post decrement operators for intergers) or ++i,--i (Pre increment or pre decrement operators for intergers) ,we can not make Unary operator as binary or binary as Unary operator.Question No: 28 ( Marks: 2 ) Which arithmetic operators cannot have a floating point operand?Ans:Modulus operator This operator can only be used with integer operands ONLYQuestion No: 29 ( Marks: 2 ) What are manipulators? Give one example.Ans:The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14 . This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed. Some manipulators are parameter less. We simply use the name of the manipulator that works. For example, we have been using endl, which is actually a manipulator, not data. When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it manipulates the output stream.Question No: 30 ( Marks: 2 ) Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;Ans:int matrix [3] [3] ;matrix [0] [0] = 0;matrix [0] [1] = 0;matrix [0] [2] = 0;matrix [1] [0] = 0;matrix [1] [2] = 0;matrix [1] [2] = 0;matrix [2] [0] = 0;matrix [2] [1] = 0;matrix [2] [2] = 0;we can also do it as given belowint matrix [3][3] = { 0 }; //all elements 0 Question No: 31 ( Marks: 3 ) Which one (copy constructor or assignment operator) will be called in each of the following code segment?1) Matrix m1 (m2);2) Matrix m1, m2;m1 = m2;3) Matrix m1 = m2;Ans:1) Matrix m1 (m2); copy constructor2) Matrix m1, m2; m1 = m2; assignment operator3) Matrix m1 = m2; assignment operatorQuestion No: 32 ( Marks: 3 ) What will be the output of following function if we call this function by passing int 5?template T reciprocal(T x) {return (1/x); }Ans:1/5Question No: 33 ( Marks: 3 ) Identify the errors in the following member operator function and also correct them.math * operator(math m);math * operator (math m){ math temp; temp.number= number * number; return number;

Page 285: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM }ANS:The errors are in the arguments of the member operation function and also in the body of operator member function.Correct function should bemath *operator(math *m);math *operator (math *m){ math temp; temp = m; temp.number= number * number; return temp.number; }Question No: 34 ( Marks: 5 ) Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.Ans:#include #include int main () { double x1 = 12345624.72345double x2 = 987654.12345double x3 = 1985.23456 cout << setprecision (3) << x1<< endl; cout << setprecision (4) << x2 << endl;cout << setprecision (5) << x3<< endl; return 0;}Question No: 35 ( Marks: 5 ) What are the advantages and disadvantages of using templates?Ans:Many thing can be possible without using templates but it do offer several clear advantages not offered by any other techniques:Advanatages: • Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.• Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur. • Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.• It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead. Disadvantages:On the other hand, if misused • Templates can make code difficult to read and follow depending upon coding style.• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files. • Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.Question No: 36 ( Marks: 5 ) Suppose a program has a math class having only one data member number.Write the declaration and definition of operator function to overload + operator for the statements of main function. math obj1, obj2; obj2= 10 + obj1 ; Ans:#include math{mth operator + (obj1,obj2)mth operator + (obj1,obj2){ mth operator + (obj1,obj2)

Page 286: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmth operator + (obj1,obj2)}}…………………………………………………………………………… 

Marks: 58 Question No: 1      ( Marks: 1 ) - Please choose one In if structure the block of statements is executed only,       ► When the condition is false       ► When it contain arithmetic operators       ► When it contain logical operators       ► When the condition is true Question No: 2      ( Marks: 1 ) - Please choose oneHeader file: fstream.h includes the definition of the stream classes __________.       ► ifstream, fstream, cout       ► ifstream, fstream, ofstream       ► fstream, cin, cout       ► None of the aboveQuestion No: 3      ( Marks: 1 ) - Please choose one To access the data members of structure _______ is used.       ► dot operator (.)       ► * operator       ►  operatorà        ► None of given. Question No: 4      ( Marks: 1 ) - Please choose one eof( ), bad( ), good( ), clear( ) all are manipulators.       ► True        ► False Question No: 5      ( Marks: 1 ) - Please choose one Which kind of functions can access private member variables of a class?       ► Friend functions of the class        ► Private member functions of the class        ► Public member functions of the class        ► Friend, private and public functions Question No: 6      ( Marks: 1 ) - Please choose oneThe return type of operator function must always be void.       ► True       ► False Question No: 7      ( Marks: 1 ) - Please choose oneFriend function of a class is ______________ .       ► Member function       ► Non-member function                     ► Private function       ► Public functionQuestion No: 8      ( Marks: 1 ) - Please choose oneFunction implementation of friend function must be defined outside the class.       ► True       ► False (any where in the class)Question No: 9      ( Marks: 1 ) - Please choose one The normal source of cin object is,       ► File       ► Disk       ► Keyboard       ► RAMQuestion No: 10      ( Marks: 1 ) - Please choose oneWhich of the following is correct way to initialize a variable x of int type with value 10?

Page 287: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► int x ; x = 10;       ► int x = 10;       ► int x, x = 10;       ► x = 10; http://groups.google.com/group/vuZs/Question No: 11      ( Marks: 1 ) - Please choose one Consider the following code segment. What will be the output of the following program?int func(int) ; int num = 10 ;int main(){int num ;num = 5 ;cout << num ;cout << func(num) ;}int func(int x){return num ;}        ► 5, 5         ► 10, 5       ► 5, 10         ► 10, 10Question No: 12      ( Marks: 1 ) - Please choose one With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.       ► True        ► FalseQuestion No: 13      ( Marks: 1 ) - Please choose oneWhat will be the correct syntax to declare two-dimensional array of float data type?       ► float arr{2}{2} ;       ► float arr[2][2] ;       ► float arr[2,2] ;       ► float[2][2] arr ;   Question No: 14      ( Marks: 1 ) - Please choose oneThe first parameter of operator function for << operator,       ► Must be passed by value       ► Must be passed by reference       ► Can be passed by value or reference       ► Must be object of class Question No: 15      ( Marks: 1 ) - Please choose oneHeap is constantly changing in size.       ► True       ► FalseQuestion No: 16      ( Marks: 1 ) - Please choose oneWhile calling function, the arguments are assigned to the parameters from        ► left to right.       ► right to left        ► no specific order is followed       ► none of the given options.Question No: 17      ( Marks: 1 ) - Please choose oneClasses defined inside other classes are called ________ classes       ► looped       ► nested       ► overloaded       ► none of the given options.Question No: 18      ( Marks: 1 ) - Please choose oneIf we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________

Page 288: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► can not be replace       ► None of the given options       ► Remain constant.        ► can be changed by some operation Question No: 19      ( Marks: 1 ) - Please choose one Which value is returned by the destructor of a class?       ► A pointer to the class.       ► An object of the class.       ► A status code determining whether the class was destructed correctl       ► Destructors do not return a value.Question No: 20      ( Marks: 1 ) - Please choose one Every class contains _______________.       ► Constructor       ► Destructor       ► Both a constructor and a destructor       ► None of the given optionsQuestion No: 21      ( Marks: 1 ) - Please choose oneA template function must have        ► One or more than one arguments        ► Only one argument       ► Zero argument       ► None of the given options Question No: 22      ( Marks: 1 ) - Please choose one Structured Query Language is used for ______________       ► Databases Management       ► Networks       ► Writing Operating System       ► none of the given optionsQuestion No: 23      ( Marks: 1 ) - Please choose one When a call to a user-defined function finishes, the variable defined inside the function is still in existence.       ► True       ► FalseQuestion No: 24      ( Marks: 1 ) - Please choose one The precedence of an operator can be changed through operator overloading.       ► True       ► FalseQuestion No: 25      ( Marks: 1 ) - Please choose oneA Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________       ► Write a separate class to handle each        ► Use templates       ► Use strings to store all types       ► None of the given optionsA Matrix can be composed of ints, floats or doubles as their elements. Instead of handling these data types separately, we can write Matrix class as a template class and write code once for all native data types.Write Matrix class as a template class and write code once for all native data typesQuestion No: 26      ( Marks: 1 ) - Please choose one"delete" operator is used to return memory to free store, which is allocated by the "new" operator.        ► True       ► FalseQuestion No: 27      ( Marks: 2 )What is the difference between switch statement and if statement.Question No: 28      ( Marks: 2 ) How can we initialize data members of contained object at construction time?

Page 289: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 29      ( Marks: 2 ) How the data members of a class are initialized with meaningful values?Question No: 30      ( Marks: 2 )Can we overload new and delete operators? Question No: 31      ( Marks: 3 )What will be the output of following functions if we call these functions three times?1)void func1(){int x = 0;x++;cout << x << endl;}2) void func2(){static int x = 0 ;x++;cout << x << endl ;}   Question No: 32      ( Marks: 3 ) What is the keyword ‘this’ and what are the uses of ‘this’ pointer? Question No: 33      ( Marks: 3 )Suppose an object of class A is declared as data member of class B.(i) The constructor of which class will be called first?(ii) The destructor of which class will be called first?Question No: 34      ( Marks: 5 )Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.Question No: 35      ( Marks: 5 )Write down the disadvantages of the templates.Question No: 36      ( Marks: 5 ) http://vuzs.net/Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.Note: Display all data with in a particular width and the empty space should be filled with character xOutput should be displayed as given below:xxxxxx1000xxxxxx1500xxxxx20000xxxxx30000xxxxx60000

 

Operator overloading can be performed through__________________.        ► Classes        ► Functions

 

       ► Operators       ► Reference

Question No: 2      ( Marks: 1 ) - Please choose oneWhen a value is referred by a normal variable then it is known as,       ► Direct Reference

Page 290: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM         ► Indirect Reference       ► Partial Reference       ► Proper ReferenceWhen a value is referred by a normal variable is known as direct referenceQuestion No: 3      ( Marks: 1 ) - Please choose oneWhich of the following function is used to increase the size of already allocated memory chunk?         ► malloc

 

       ► calloc       ► realloc       ► freeQuestion No: 4      ( Marks: 1 ) - Please choose oneWhich of the following is NOT a preprocessor directive?       ► #error       ► #define       ► #line

       ► #ndefine

 

list of preprocessors• #include • #include “filename” • #define • #undef • #ifdef • #ifndef • #if • #else • #elif • #endif • #error • #line • #pragma • #assert

Question No: 5      ( Marks: 1 ) - Please choose oneThe stream objects cin and cout are included in which header file?         ► iostream.h       ► fstream.h       ► istream.h       ► ostream.h Question No: 6      ( Marks: 1 ) - Please choose oneOverloaded delete operator function takes the same parameter as an argument returned by new operator function.        ► True       ► False  The same pointer that is returned by the new operator, is passed as an argument to the delete operator. These rules apply to both, if operators (new and delete) are overloaded as member or non-member operators (as global operators).

 

Question No: 7      ( Marks: 1 ) - Please choose oneWhen an array of object is created dynamically then there is no way to provide parameterized constructors for array of objects.         ► True

 

       ► Falseif we are allocating an array of objects, there is no way to pass arguments to objects’ constructors. Therefore it is required that the objects that are stored in such an array have a no-argument constructor.

Page 291: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 8      ( Marks: 1 ) - Please choose oneC is widely known as development language of _______ operating system.       ► Linux       ► Windows         ► Unix  ► Mac OSIn the start C became widely known as the development language of the UNIX operating system, and the UNIX operating system was written by using this C language. The C language is so powerful that the compiler of C and other various operating systems are written in C. Question No: 9      ( Marks: 1 ) - Please choose oneComputer can understand only machine language code.        ► True

 

       ► FalseQuestion No: 10      ( Marks: 1 ) - Please choose oneWe can not define a function as a friend of a Template class.       ► True         ► False

 

Class templates can have friends. A class or class template, function, or function template can be a friend to a template class. Friends can also be specializations of a class template or function template, but not partial specializations.

Question No: 11      ( Marks: 1 ) - Please choose oneWhat will be the value of ‘a’ and ‘b’ after executing the following statements?a = 3;b = a++;       ► 3, 4       ► 4, 4       ► 3, 3       ► 4, 3

Question No: 12      ( Marks: 1 ) - Please choose oneConsider the following code segment. What will be the output of following code?int addValue (int *a){int b = (*a) + 2;return b ;}   main () {int x =6 ;cout <<  x << “,” ;cout << addValue(&x) << “,” ;cout <<  x ;}

       ► 6,8,6

Page 292: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

       ► 6,6,8       ► 6,8,8       ► 6,6,6

Question No: 13      ( Marks: 1 ) - Please choose one _______ is used to trace the logic of the program and correct the logical errors.       ► Compiler       ► Editor       ► Linker        ► Debugger

Question No: 14      ( Marks: 1 ) - Please choose onenew and delete are _____ whereas malloc and free are _____.       ► Functions, operators       ► Classes, operators       ► Operators, functions       ► Operators, classesHence, we can call new and delete operators, P# 342we have allocated a memory space for our use by malloc function. P# 285

Question No: 15      ( Marks: 1 ) - Please choose oneLike member functions, ______ can also access the private data members of a class.       ► Non-member functions       ► Friend functions       ► Any function outside class       ► None of the given optionsQuestion No: 16      ( Marks: 1 ) - Please choose oneWhich situation would require the use of a non-member overloaded operator?       ► The overloaded operator is an Assignment operator.

       ► The left most operand is an object of a class.

 

       ► The left operand is built-in data type.       ► The operator returns a reference.

When an operator function is implemented as a non-member function, the left-most operand may be an object of the operator’s class, an object of a different class, or a built-in typeQuestion No: 17      ( Marks: 1 ) - Please choose oneThe stream insertion and stream extraction operators are already overloaded for ______.       ► User-defined data types       ► Built-in data types       ► User-defined and built-in data types       ► None of the given optionsQuestion No: 18      ( Marks: 1 ) - Please choose oneIf we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.       ► can not be replaced       ► None of the given options

Page 293: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Remain constant.        ► can be changed by some operation

Question No: 19      ( Marks: 1 ) - Please choose one vuzsAssignment operator is -------------------------associative.       ► right       ► left       ► binary       ► unaryYou can assign values to several variables in a single statement. For example, the following code sets the contents of apples and oranges to the same value:apples = oranges = 10;The assignment operator is right associative, so this statement executes by first storing the value 10 in oranges and then storing the value in oranges in apples, so it is effectivelyapples = (oranges = 10);

Question No: 20      ( Marks: 1 ) - Please choose oneWhen ever dynamic memory allocation is made in C/C++, it is freed_____________.       ► Explicitly       ► Implicitly       ► Both explicitly and implicitly       ► None of the given optionsQuestion No: 21      ( Marks: 1 ) - Please choose oneThe appropriate data type to store the number of rows and colums of the matrix is____________.       ► float       ► int       ► char       ► none of the given options.

Question No: 22      ( Marks: 1 ) - Please choose oneWhich of the following function do NOT initialize the chunk of memory to all zero?       ► calloc() function         ► Both malloc() and calloc()        ► None of the above         ► malloc() function 

The malloc function differs from calloc in the way that the space allocated by malloc is not initialized and contains any values initially.Question No: 23      ( Marks: 1 ) - Please choose oneThe function free() returns back the allocated memory got thorough calloc and malloc to _____ .        ► stack       ► heap       ► stack and heap        ► None of the given optionsQuestion No: 24      ( Marks: 1 ) - Please choose onewidth() is member function of _____________        ► cin object       ► cout object       ► Both cin and cout object        ► None of the given option  Question No: 25      ( Marks: 1 ) - Please choose one

Page 294: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Templates are not type safe.       ► true        ► false

 

Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.Question No: 26      ( Marks: 1 ) - Please choose oneA Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________       ► Write a separate class to handle each        ► Use templates       ► Use strings to store all types       ► None of the given optionsA Matrix can be composed of ints, floats or doubles as their elements. Instead of handling these data types separately, we can write Matrix class as a template class and write code once for all native data types. While writing this template class, the better approach to write will be, to go with a simple data type (e.g. double) first to write a Matrix class and then extend it to a template class later.

Question No: 27      ( Marks: 2 )Give the general syntax of class template. template class myclass { ---} ;Question No: 28      ( Marks: 2 )What is a truth Table?There are some areas where the decision structures become very complicated. Sometimes, we find it difficult to evaluate a complicated logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax statement in any language. It becomes complicated to determine what will be evaluated in what way. We know the concept of truth table. The truth tables are very important. These are still a tool available for analyzing logical expressions. We will read logic design in future, which is actually to do with chips and gates. How we put these things together. Question No: 29      ( Marks: 2 )

 

What will be the output of following code, if user input a number 123? int input ;cin >> oct >> input;cout << hex << input ; 53Rational: it will take 123 as octal and print it in hex form which is 53. Question No: 30      ( Marks: 2 )What is principle of friendship in the context of functions and classes? Class can declare a friend function and someone from outside the class cannot declare itself friend of a class.A friend function can access the private variables of class just like a member function Question No: 31      ( Marks: 3 )What are the limitations of the friendship relation between classes?

Page 295: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMClass can declare a friend class from inside and someone from outside the class cannot declare itself friend of a class. Question No: 32      ( Marks: 3 )Suppose an object of class A is declared as data member of class B.(i) The constructor of which class will be called first? a(ii) The destructor of which class will be called first?b Question No: 33      ( Marks: 3 )Define static variable. Also explain life time of static variable?When you declare a static variable (native data type or object) inside a function, it is created and initialized only once during the lifetime of the program Question No: 34      ( Marks: 5 ) Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.#include#include main () {double a = 12.12345;double b = 13.123456;double c = 14.1234567;cout << setprecision (5) << a << endl;cout << setprecision (2) << a << endl;cout << setprecision (3) << a << endl;}Question No: 35      ( Marks: 5 )Let we have a class,class String {private:char  buf[25];};Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment Answer: void String::operator = ( const String &other ) { int length ; length = other.length(); delete buf; buf = new char [length + 1]; strcpy( buf, other.buf ); } Question No: 36      ( Marks: 5 )Read the given below code and explain what task is being performed by this function Matrix :: Matrix ( int row , int col ) {    numRows = row ;    numCols = col ;    elements = new ( double * ) [ numRows ] ;    for ( int  i = 0 ; i < numRows ; i ++ ){        elements [ i ] = new double [ numCols ] ;        for ( int j = 0 ; j < numCols ; j ++ )                elements [ i ] [ j ] = 0.0 ;      }

Page 296: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COM}Hint : This function belong to a matrix class, having Number of Rows = numRowsNumber of Columns = numCols

F INA L T ERM EXA M INA T ION

C S 201- I n t r od u ction to Pr og r a m m i n g

Question No: 1 ( Marks: 1 ) - Please choose one

When we define an array of objects then,

Destructor will call once for whole array

Destructor will call for each object of the array

Destructor will never call

Depends on the size of array

Question No: 2 ( Marks: 1 ) - Please choose one

We can also create an array of user define data type

True

False

Question No: 3 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when allocating memory using new operator?

Only block of memory is allocated for objects

Only constructor is called for objects

Page 297: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Memory is allocated first before calling constructor

Constructor is called first before allocating memory

Question No: 4 ( Marks: 1 ) - Please choose one

We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally

True

False

http://vustudents.ning.com

Question No: 5 ( Marks: 1 ) - Please choose one

The declarator of Plus (+) member operator function is

Class-Name operator + (Class-Name rhs)

Operator Class-Name + ( )

Operator Class-Name + ( rhs)

Class-Name operator + ( )

Question No: 6 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators

True

False

Question No: 7 ( Marks: 1 ) - Please choose one

Which of the following is correct way to initialize a variable x of int type with

Page 298: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COM

value 10?

int x ; x = 10 ;

int x = 10 ;

int x, x = 10;

x = 10 ;

Question No: 8 ( Marks: 1 ) - Please choose one

Default mechanism of function calling in case of array is and in case of variable is _

Call by value, call by reference

Call by referene, call by reference

Call by reference, call by value

Call by value, call by value

Question No: 9 ( Marks: 1 ) - Please choose one

What does STL stand for?

Source template library

Standard template library

Stream template library

Standard temporary library

Question No: 10 ( Marks: 1 ) - Please choose one

Skill(s) that is/are needed by programmers

Paying attention to detail

Think about the reusability

Page 299: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Think about user interface

All of the given options

Question No: 11 ( Marks: 1 ) - Please choose one

For which array, the size of the array should be one more than the number of elements in an array?

int

double

float

char

Question No: 12 ( Marks: 1 ) - Please choose one

new and delete are whereas malloc and free are

Functions, operators

Classes, operators

Operators, functions

Operators, classes

http://vustudents.ning.com

Question No: 13 ( Marks: 1 ) - Please choose one

The prototype of friend functions must be written the class and its definition

Page 300: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COM

must be written

inside, inside the class

inside, outside the class

outside, inside the class

outside, outside the class

Question No: 14 ( Marks: 1 ) - Please choose one

Friend function of a class are of a class.

Non-member functions

Friend functions

Any function outside class

None of the given options

http://vustudents.ning.com

Question No: 15 ( Marks: 1 ) - Please choose one

If overloaded plus operator is implemented as non-member function then which

of the following statement will be true for the statement given below?

obj3 = obj1 + obj2 ;

obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator

obj1 will drive the + operator whereas obj2 will be passed as an argument

to + operator

Both objects (obj1, obj2) will be passed as arguments to the + operator

Any of the objects (obj1, obj2) can drive the + operator

Question No: 16 ( Marks: 1 ) - Please choose one

Page 301: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWhich one of the following is the declaration of overloaded pre-increment operator

implemented as member function?

Class-name operator +() ;

Class-name operator +(int) ;

Class-name operator ++() ;

Class-name operator ++(int) ;

Question No: 17 ( Marks: 1 ) - Please choose one

For cin, the source is normally a and destination can be

File, native data type

Disk, user-define type

Keyboard, variable

File, user-define type

http://vustudents.ning.com

Question No: 18 ( Marks: 1 ) - Please choose one

We can do condition compilation with pre processor directives.

True

False

Question No: 19 ( Marks: 1 ) - Please choose one

Page 302: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COM

The programs, in which we allocate static memory, run essentially on

Heap

System Cache

None of the given options

Stack

Question No: 20 ( Marks: 1 ) - Please choose one

A template function must have at least ---------- or more arguments http://vustudents.ning.com

Zero

One

Two

Three

Question No: 21 ( Marks: 1 ) - Please choose one

The default value of a parameter can be provided inside the

function prototype

function definition

both function prototype or function definition

none of the given options

Question No: 22 ( Marks: 1 ) - Please choose one

While calling function, the arguments are assigned to the parameters from

left to right

right to left

Page 303: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM no specific order is followed

none of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

When an operator function is defined as member function for a binary Plus (+)

operator then the number of argument it take is/are http://vustudents.ning.com

Zero

One

Two

N arguments

Question No: 24 ( Marks: 1 ) - Please choose one

new operator allocates memory from free store and return

A pointer

A reference

An integer

A float

Question No: 25 ( Marks: 1 ) - Please choose one

With user-defined data type variables (Objects), self assignment can produce

Page 304: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COM

Syntax error

Logical error

Link error

Non of the given options

Question No: 26 ( Marks: 1 ) – Write Simple Program

Assignment operator is used to initialize a newly declared object from existing object

True

False http://vustudents.ning.com

Question No: 27 ( Marks: 1 ) – Briefly define/Justify

When an object of a class is defined inside an other class then,

Constructor of enclosing class will be called first

Constructor of inner object will be called first

Constructor and Destructor will be called simultaneously

None of the given options

Question No: 28 ( Marks: 1 ) – Brief answer required

In the member initializer list, the data members are initialized,

From left to right

From right to left

In the order in which they are defined within class

None of the given options

Page 305: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 29 ( Marks: 1) - Brief answer required

"new" and "delete" keywords are in C++ language

Built-in- Function

Operators

Memory Allocation Function

None of the given options

Question No: 30 ( Marks: 2 ) - Brief answer required

What are the two types of conversion for user-defined data types?

Question No: 31 ( Marks: 2 ) - Brief answer required

Give the general syntax of class template.

Question No: 32 ( Marks: 2 ) - Brief answer required

What is a constructor in class? http://vustudents.ning.com

Question No: 33 ( Marks: 2 ) - Brief answer required

Is there a way to increase the size of already allocated memory chunk ? Can the same chunk be increased or not?

Page 306: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

r

WWW.VIRTUALINSPIRE.COM

Question No: 34 ( Marks: 3 ) - Write Program

What is the difference between structure and class?.

Question No: 35 ( Marks: 3 ) - Write Program

How many arguments does binary member operator function and binary non- member operator function take?

Question No: 36 ( Marks: 2 ) - Write Program

Find the error in the given code for ( int i=0; i<numRows; i++)

{

for(int j=0; j<numCols; j++)

{

elements[i , j] = m.elements[i][j];

}

}

http://vustudents.ning.com

Question No: 37 ( Marks: 5 ) - Write Program

Page 307: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWrite the C++ syntax for making a class friend of other class

Question No: 38 ( Marks: 5 ) - Write Program

What is a template function? Give the general syntax of writing a template function

Question No: 39 ( Marks: 10 ) - Write Program

What is Standard Template Library (STL) also describe its advantages?

Question No: 40 ( Marks: 10 ) - Write Program

Write a program which contains a class studentThe class should contain two char pointer variables Name, and departmentThe class should further contain constructors, overload the stream insertion operator (<<) for this class In main function create two objects and display these objects

#include<iostream>

#include<conio>

#include <cstdlib> using

Page 308: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMnamespace std; class student {

private:

char name[30] ;

char department[30] ;

public:

FINALTERM EXAMINATION

CS201- Introduction to Programming

Question No: 1 ( Marks: 1 ) - Please choose one

Page 309: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMIn C/C++ if we define an array of size eight (8) i.e. int Arr [8]; then the last element of this array will be stored at,

► Arr[0]

► Arr[8]

Arr[7]►

► Arr[-1]

Question No: 2 ( Marks: 1 ) - Please choose one

When an array is passed to a function then default way of passing this array is,

► By data

By reference►

By value►

► By data type

Question No: 3 ( Marks: 1 ) - Please choose one

Page 310: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Array is a data structure which store

Memory addresses►

► Variables

► Data Type

► Data

Question No: 4 ( Marks: 1 ) - Please choose one

We can also create an array of user define data type.

True►

► False

Question No: 5 ( Marks: 1 ) - Please choose one

When we define an array of objects then,

Page 311: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Destructor will call once for whole array

Destructor will call for each object of the array►

► Destructor will never call

► Depends on the size of array

Question No: 6 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

Memory is allocated first before calling constructor►

Page 312: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Constructor is called first before allocating memory

Question No: 7 ( Marks: 1 ) - Please choose one

We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally.

True►

False►

Question No: 8 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

True►

Page 313: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► False

Question No: 9 ( Marks: 1 ) - Please choose one

Which of the following is correct way to initialize a variable x of int type with value 10?

► int x ; x = 10 ;

int x = 10 ;►

► int x, x = 10;

► x = 10 ;

Question No: 10 ( Marks: 1 ) - Please choose one

Default mechanism of function calling in case of array is _____ and in case of variable is ___.

► Call by value, call by reference

Page 314: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Call by referene, call by reference

Call by reference, call by value►

► Call by value, call by value

Question No: 11 ( Marks: 1 ) - Please choose one

What does STL stand for?

► Source template library

Standard template library►

► Stream template library

► Standard temporary library

Page 315: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 12 ( Marks: 1 ) - Please choose one

Skill(s) that is/are needed by programmers _______________________.

► Paying attention to detail

► Think about the reusability

► Think about user interface

All of the given options ►

Question No: 13 ( Marks: 1 ) - Please choose one

For which array, the size of the array should be one more than the number of elements in an array?

► int

► double

Page 316: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► float

char►

Question No: 14 ( Marks: 1 ) - Please choose one

new and delete are _____ whereas malloc and free are _____.

► Functions, operators

► Classes, operators

Operators, functions►

Page 317: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Operators, classes

Question No: 15 ( Marks: 1 ) - Please choose one

Friend functions are _____ of a class.

► Member functions

► Public member functions

► Private member functions

Non-member functions►

Question No: 16 ( Marks: 1 ) - Please choose one

The prototype of friend functions must be written ____ the class and its definition must be written ____

Page 318: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► inside, inside the class

► inside, outside the class

outside, inside the class►

► outside, outside the class

Question No: 17 ( Marks: 1 ) - Please choose one

If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below?

obj3 = obj1 + obj2 ;

Page 319: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator►

► obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator

► Both objects (obj1, obj2) will be passed as arguments to the + operator

► Any of the objects (obj1, obj2) can drive the + operator

Question No: 18 ( Marks: 1 ) - Please choose one

Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?

► Class-name operator +() ;

► Class-name operator +(int) ;

► Class-name operator ++() ;

► Class-name operator ++(int) ;

Page 320: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 19 ( Marks: 1 ) - Please choose one

For cin, the source is normally a ________ and destination can be ______.

► File, native data type

► Disk, user-define type

Keyboard, variable►

► File, user-define type

Question No: 20 ( Marks: 1 ) - Please choose one

We can also do conditional compilation with preprocessor directives.

True►

Page 321: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► False

Question No: 21 ( Marks: 1 ) - Please choose one

The programs, in which we allocate static memory, run essentially on ________

► Heap

► System Cache

► None of the given options

Stack►

Question No: 22 ( Marks: 1 ) - Please choose one

The default value of a parameter can be provided inside the ________________

► function prototype

function definition►

Page 322: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► both function prototype or function definition

► none of the given options.

Question No: 23 ( Marks: 1 ) - Please choose one

While calling function, the arguments are assigned to the parameters from _____________.

► left to right.

► right to left

► no specific order is followed

► none of the given options.

Question No: 24 ( Marks: 1 ) - Please choose one

When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are.

Page 323: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Zero

► One

► Two

N arguments►

Question No: 25 ( Marks: 1 ) - Please choose one

With user-defined data type variables (Objects), self assignment can produce __________.

► Syntax error

Logical error►

► Link error

► Non of the given options

Page 324: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 26 ( Marks: 1 ) - Please choose one

Assignment operator is used to initialize a newly declared object from existing object.

True►

► False

Question No: 27 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside an other class then,

Constructor of enclosing class will be called first►

Constructor of inner object will be called first►

Page 325: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 28 ( Marks: 1 ) - Please choose one

In the member initializer list, the data members are initialized,

► From left to right

► From right to left

In the order in which they are defined within class►

► None of the given options

Question No: 29 ( Marks: 1 ) - Please choose one

Page 326: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMnew operator allocates memory from free store and return _____________.

A pointer►

► A reference

► An integer

► A float

Question No: 30 ( Marks: 1 ) - Please choose one

"new" and "delete" keywords are _____________ in C++ language.

► Built-in- Function

► Operators

Page 327: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Memory Allocation Function►

► None of the given options

Question No: 31 ( Marks: 1 )

What is a constructor in class?

Ans:

Class constructor, is the method specific to a type as a whole that run after the type is loaded and before any of the type's members are accessed. Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention. Normally, class constructors are never called from the IL code. If a type has a class constructor, this constructor is executed automatically after the type is loaded. However, a class constructor, like any other static method, can be called explicitly. As a result of such a call, the global fields of the type are reset to their initial values. Calling class constructor explicitly does not lead to type reloading.

Question No: 32 ( Marks: 1 )

What are the two types of conversion for user-defined data types?

Types of conversion

Page 328: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe possible types of conversion are:

Standard conversion:

This applies to:

Single-byte character sets (SBCS) Graphic or double-byte character sets (DBCS) Mixed character sets (containing SBCS and DBCS data) Multi-byte character sets (MBCS) By default, to binary data in INTEL format.

No conversion:

This applies to:

Character data encoded as UCS-2 or UTF-8 By default, to binary data in System Packed decimal data.

Question No: 33 ( Marks: 2 )

Is there a way to increase the size of already allocated memory chunk ? Can the same chunk be increased or not?

Ans:

Dynamic memory allocation often makes up a large part of program execution time. Different variants of the best-fit allocator are implemented and their space and time costs measured and compared.

Page 329: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Reallocating an already allocated chunk uses the realloc() library function call.

Void realloc(void*ptr,size_t size)

Question No: 34 ( Marks: 2 )

Give the general syntax of class template.

Ans:”

Tthe general syntax of class template is.

template <class myType>

myType GetMax (myType a, myType b) {

return (a>b?a:b);};

Page 330: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 35 ( Marks: 3 )

What is the difference between structure and class?

Ans:

1.The difference between a class and a structure is that, by default, all of the members of a class are private and, by default, all of the members of a structure are public.

2. 'this' pointer will work only in class.

3. Structure are value type where as class are reference type.

Question No: 36 ( Marks: 3 )

How many arguments does binary member operator function and binary non-member operator function take?

Ans:

Page 331: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMA binary operator shall be implemented either by a non-static member function with one parameter or by a non-member function with two parameters.

Question No: 37 ( Marks: 3 )

Find the error in the given code

for ( int i=0; i<numRows; i++)

{

for(int j=0; j<numCols; j++)

{

elements[i , j] = m.elements[i][j];

}

}

Ans:”

It is multiplication of two multiplication so here I m presenting a sample code to multiply two matrix a , b and the result will be stored in matrix C

#include<iostream.h>void main(){ int a[3][3] , b[3][3] , c[3][3];

Page 332: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM int i , j , k; cout<<"Enter Matrix A"; for( i = 0 ; i < 3 ; i++) for( j = 0 ; j < 3 ; j++) cin>>a[i][j]; cout<<"Enter Matrix B"; for( i = 0 ; i < 3 ; i++) for( j = 0 ; j < 3 ; j++) cin>>b[i][j]; for( i = 0 ; i < 3 ; i++) for( j = 0 ; j < 3 ; j++) { c[i][j] = 0; for( k = 0 ;k < 3 ; k++) c[i][j] += a[i][k]*b[k][j]; } cout<<"The resultant matrix is "; for( i = 0 ; i < 3 ; i++) { for( j = 0 ; j < 3 ; j++) cout<<a[i][j]<<" "; cout<<endl; }}

Question No: 38 ( Marks: 5 )

Write the C++ syntax for making a class friend of other class.

Ans:

Ans:

Page 333: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMFriend class is one which has been declared so (as a friend) inside other class to make it access the private members of the class which has extended it's friendship.

For Example, class A { private: ....... public: ..............

friend class B;};

class B { ....... .............. };

As in the above code snippet, class A has extended it's friendship to class B by declaring B as it's friend inside it's area.

Since the Class B has became a friend of A, B can directly access all the private members of A. But the reverse is not possible.

Question No: 39 ( Marks: 5 )

What is a template function? Give the general syntax of writing a template function.

Page 334: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAns:

Function Templates

To perform identical operations for each type of data compactly and conveniently, use function templates. You can write a single function template definition. Based on the argument types provided in calls to the function, the compiler automatically instantiates separate object code functions to handle each type of call appropriately. The STL algorithms are implemented as function templates.

Syntax:

Template<classT>

CLASS class-name()

{

//Definition of class

};

Class name<T>::function name (argument list)

{

//Function body

};

Question No: 40 ( Marks: 10 )

Page 335: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWrite a program which contains a class student. The class should contain two char pointer variables Name, and department. The class should further contain constructors, overload the stream insertion operator (<<) for this class.

In main function create two objects and display these objects.

#include<iostream>

#include<conio>

#include <cstdlib>

using namespace std;

class student {

private:

char name[30] ;

char department[30] ;

public:

Question No: 41 ( Marks: 10 )

What is Standard Template Library (STL) also describe its advantages?

Page 336: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Ans:

The C++ STL (Standard Template Library) is a generic collection of class templates and algorithms that allow programmers to easily implement standard data structures like queues, lists

and stacks.

STL Containers

The C++ STL provides two kinds of containers:

Sequence Containerso C++ Vectorso C++ Listso C++ Double-Ended Queues

Associative Containerso C++ Mapso C++ Multimapso C++ Setso C++ Multisets

In addition, the C++ standard library provides several non-STL containers.They can be considered containers, but don't meet all the requirements of STL containers.

Container Adapterso C++ Stackso C++ Queueso C++ Priority Queues

Otherso C++ Bitsetso C++ Valarrays

The idea behind the C++ STL is that the hard part of using complex data structures has already been completed. If a programmer would like to use a stack of integers, all one has to do is use this code:

stack<int> myStack;

With minimal effort, one can now push and pop integers onto this stack. Through the magic of C++ Templates, one could specify any data type, not just integers. The STL Stack class will provide generic functionality of a stack, regardless of the data in the stack.

edit 1-239 1266836067

Page 337: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Advantages of (STL)

Templates are easier to write then writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

Templates can be easier to understand, since they can provide a straightforward way of abstraction type information.

Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

Templates help in utilizing compiler optimizations to the extreme.

FINALTERM EXAMINATION CS201- Introduction to Programming (Session - 1)

Question No: 1 ( Marks: 1 ) - Please choose one To

access the data members of structure _______ is used. ► dot operator (.)

► * operator

► à operator

► None of given.

Question No: 2 ( Marks: 1 ) - Please choose one

What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?

► 1

Page 338: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► 2 ► 3 ► 4 Question No: 3 ( Marks: 1 ) - Please choose one

If constructor contains a return statement in its body then compiler will give __________ ► No error

► Syntax error

► Logical error

► Run time error

Question No: 4 ( Marks: 1 ) - Please choose one

We can use New keyword inside of Class Constructor.

►True►False

Question No: 5 ( Marks: 1 ) - Please choose one

When an operator function is define as member function for a Unary operator then the number of argument it take is/are,

►Zero

Page 339: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

►One

►Two

►N arguments

Question No: 6 ( Marks: 1 ) - Please choose one

The declarator of Plus (+) member operator function is

►Class-Name operator + (Class-Name rhs)

►operator Class-Name + ( )

►operator Class-Name + ( rhs)

►Class-Name operator + ( ) Question No: 7 ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ .

►Member function

Page 340: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►Non-member function

►Private function

►Public function Question No: 8 ( Marks: 1 ) - Please choose one

We can also create an array of user define data type.

►True►False

Question No: 9 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?►Only block of memory is deallocated for objects

►Only destructor is called for objects

►Memory is deallocated first before calling destructor

►Destructor is called first before deallocating memory

Question No: 10 ( Marks: 1 ) - Please choose one

Deleting an array of objects without specifying [] brackets may lead to memory leak►True

Page 341: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►False

Question No: 11 ( Marks: 1 ) - Please choose one

Which of the following data type(s) can operate on modulus operator ‘%’? ► float, int ► float, double ► int

► char

Question No: 12 ( Marks: 1 ) - Please choose one

Array is passed by value to a function by default.►True►False Question No: 13 ( Marks: 1 ) - Please choose one

With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.

►True

►False Question No: 14 ( Marks: 1 ) - Please choose one

What will be the correct syntax to initialize all elements of two-dimensional array to value 0?

Page 342: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM►int arr[2][3] = {0,0} ;

►int arr[2][3] = {{0},{0}} ;

►int arr[2][3] = {0},{0} ;

►int arr[2][3] = {0} ; Question No: 15 ( Marks: 1 ) - Please choose one

When an operator function is define as member function then operand on the left side of operator must be an object.►True►False Question No: 16 ( Marks: 1 ) - Please choose one

break statement can be used outside a loop or switch statement.

►True

►False Question No: 17 ( Marks: 1 ) - Please choose one

The keyword_______ is used to return some value from a function.

►return►break►continue►goto

Page 343: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 18 ( Marks: 1 ) - Please choose one

Every data member or function inside the structure is ___ by default whereas everything declared inside a class is ____ by default.

►private, public►public, private►private, protected►public, protected Question No: 19 ( Marks: 1 ) - Please choose one

Which of the following is true for the C++ statement given below? int &ref = val ;

►It creates a synonym for variable ‘val’►It creates an alias for variable ‘val’►It’s a new name for variable ‘val’►All of the given options

Question No: 20 ( Marks: 1 ) - Please choose one

If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below?

Page 344: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMobj3 = obj1 + obj2 ;

►obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator►obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator►Both objects (obj1, obj2) will be passed as arguments to the + operator

►Any of the objects (obj1, obj2) can drive the + operator Question No: 21 ( Marks: 1 ) - Please choose one

Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.

►One, zero►Zero, one

►One, two►Two, one Question No: 22 ( Marks: 1 ) - Please choose one

Page 345: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM The

input/output streams cin and cout are ________ therefore have _______.

► Structures, function ► Objects, member functions ► Functions, objects ► None of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

If a symbolic constant has been defined, it will be an error to define it again. ► True ► False Question No: 24 ( Marks: 1 ) - Please choose one

Every class contains _______________.

► Constructor

► Destructor

► Both a constructor and a destructor

► None of the given options

Page 346: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 25 ( Marks: 1 ) - Please choose one

new and delete keywords are _____________ in C++ language.

► Built-in- Function

► Operators

► Memory Allocation Function

► None of the given options

Question No: 26 ( Marks: 1 ) - Please choose one

Consider the following code segment.

class M {public: M &operator+(const M &);...};

p + q //code of line implies that p.operator+(q)...

Let assume if p and q are class objects then function is implemented as _______

Page 347: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

►Member function

►Non-member function

►Friend function

►None of the given options Question No: 27 ( Marks: 1 ) - Please choose one

Assignment operator is -------------------------associative.

►right

►left

►binary

►unary

Question No: 28 ( Marks: 1 ) - Please choose one

Static variable which is defined in a function is initialized __________. ► Only once during its life time ► Every time the function call ► Compile time of the program ► None of the above

Page 348: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 29 ( Marks: 1 ) - Please choose one

We can not define a variable of user-defined data type in the class.

►True►False

Question No: 30 ( Marks: 1 ) - Please choose one

A constructor that will create a new object with a full copy of the other object, is copy is known as ___________►deep copy

►shallow copy►constructor copy►none of the options Question No: 31 ( Marks: 1 )

What is the use of reference data type?

A reference data type is a variable that can contain an address. The reference data types in Java are arrays, classes and interfaces. You'll hear often say that Java does not have pointers. Yet, you could consider a reference data type to be a pointer Question No: 32 ( Marks: 1 )

What are the main types of operators in terms of number of arguments they take?

Page 349: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.

Question No: 33 ( Marks: 2 )

What is the this pointer? Give an example of its useIn a C++ program, if you create object A of class X, you can then obtain the address of A by using the "this" pointer. The address is available as a local variable in the non-static member functions of X, and its type is const X*. The "this" pointer works because C++ creates instances of its data members, and it keeps one copy of each member function.

Question No: 34 ( Marks: 2 )

What are manipulators? Give one example.

Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of displayed endl manipulator. This manipulator has the same functionality as the ‘\n’ newline character.

Question No: 35 ( Marks: 3 )

What will be the output of following functions if we call these functions three times?

1)void func1(){int x = 0;x++;cout << x << endl;}

Page 350: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMOutput will be:111

2) void func2(){static int x = 0 ;x++;cout << x << endl ;}

Output will be:123

Question No: 36 ( Marks: 3 )

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

malloc returns a void pointer to the allocated space or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item.

By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when malloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason.

Page 351: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 37 ( Marks: 3 )

If we want to send the data by reference and don’t want that original data should be affected then what can we do to prevent any change?

Question No: 38 ( Marks: 5 )

Write down the disadvantages of the templates.

The disadvantages of templates are:• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 39 ( Marks: 5 )

The following code segment has errors. Locate as many as you can and explain briefly.

class Circle // no need to enter colon here , so I removed it{private : //colon missingdouble centerX;double centerY;double radius;public: //colon missingvoid setCenter(double, double);void setRadius(int); };//semi colon missing

Page 352: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 40 ( Marks: 10 )

Write a program which consists of two classes, Date and Person. Date class should contain three data members day, month, year and setter and getter function for these data members. Date class should also contain showdate() member function to display date.

Person class should contain three data members Name, Address, and Bday, where Name and Address are char pointer while Bday(Date of birth) is of type Date, Person class should further contain two member functions Display() and setdate().

In main program Create an object of Class person and call the member functions with it.

Please find the Answer in following Box

#include <stdio.h>#include <iostream>#include <cstring>using namespace std;

class Date{public:

int day;int month;int year;

public:Date()

{day=0;month=0;year=0;

}

void setDay(int);void setMonth (int);void setYear(int);

Page 353: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMint getDay();int getMonth();int getYear();

void showDate();};void Date: :setDay(int d){

if{d<1 | | d>31)cout<<"Invalid month Renter it";cin>>d;

}day=d;

}

void Date: :setMonth (int m){

if(m<1 | | m>12){

cout<<"Invalid month Renter it";cin>>m;

}month=m;

}void Date: :setYear (int y)

{year=y;

int Date: :getDay(){

return day;}

int Date: :getMonth(){

return month:}

int Date: :getYear(){

return year;}

void Date: :showDate(){

cout<<day<<"-"<<month<<"-"<<year<<end1;}Class Person{public:

char *Name; char *Address

Date Bday;

public:Student(){

Page 354: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMName=new char[20];Address=new char[10];cin.getline(Name,20);cout<<"Enter Address:";cin.getline(Address,10);}

void setDate(){

cout<<"Enter Day:";cin>>Ad_date.day;cout<<"Enter month:";cin>>Ad_date.month;cout<<"Enter Year:";cin>>Ad_date.year;}

void Display(){cout<<"Name: "<<end1;cout<<"Address: "<<Address<<end1;cout<<"Date of Birth: ";Ad-date.showDate();}

};void main(){

Person object;object.setDate();

object.Display();

system("pause");}

Question No: 41 ( Marks: 10 )

Write a C++ program that contains a class ‘myClass’ having two data members of type int.The class must have

· A default constructor which must initialize all the data members to their meaningful values.

· A destructor with no implementation.

Page 355: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM· Setter member functions to set all data members of class· Getter member functions to get all data members of class

In main function of the program

5. Prompt the user to enter the number of objects to be created.6. Dynamically allocate memory to objects according to the size entered by

user.7. De-allocate memory that was allocated to objects

Find solution in following box:

#include <stdio.h>#include <iostream>#include <cstring>using namespace std;

class myclass

{

public:int a;int b;int *iptr, *sptr;

construct{int,int.int}

void seta(int);void setb(int);void setc(int);

int geta();int getb();int getc();

};

void Person: :seta(int aa){

a=aa;}

void Person: :setb (int bb){

Page 356: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMb=bb;

}void Person: :setc (int cc)

{c=cc;

}

main()

{

int num;

cout<<"Enter the number of objects to be created";

cin>>num;

for (int i =1;i==num;i++){

Person i_

}

}

FINALTERM  EXAMINATIONCS201- Introduction to Programming

Time: 90 minMarks: 58

Question No: 1      ( Marks: 1 ) - Please choose one

&& is -------------------- operator.

       ► An arithmetic

Page 357: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Logical

       ► Relational

       ► Unary

 

Question No: 2      ( Marks: 1 ) - Please choose one

Which of the following function(s) is/are included in ctype.h header file?

       ► isdigit(int c)

       ► isxdigit(int c )

       ► tolower(int c)

► All of the above

 

Question No: 3      ( Marks: 1 ) - Please choose one

Disks are _______________ devices having access time of _________ milliseconds.

       ► Electro-physical, 6

       ► Electro-Mechanical, 4

       ► Electro-physical, 5

► Electro-Mechanical, 7

Question No: 4      ( Marks: 1 ) - Please choose one

Page 358: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

All preprocessor directives are started with the symbol______.

       ► *

       ► ++

       ► @

► #

 

Question No: 5      ( Marks: 1 ) - Please choose one

When we use manipulators in our program then which header file should be included?

       ► iostream.h

       ► stdlib.h

       ► stdio.h

► iomanip.h

 

Question No: 6      ( Marks: 1 ) - Please choose one

setprecision is a parameter less manipulator.

       ► True

► False

Page 359: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 7      ( Marks: 1 ) - Please choose one

Which of the following is NOT a preprocessor directive?

       ► #error

       ► #define

       ► #line

► #ndefine

 

Question No: 8      ( Marks: 1 ) - Please choose one

We can do arithmetic with references like pointers.

       ► True

► False

 

Question No: 9      ( Marks: 1 ) - Please choose one

What does (*this) represents?

       ► The current function of the class

       ► The current pointer of the class

► The current object of the class

Page 360: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► A value of the data member

Question No: 10      ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ .

       ► Member function

► Non-member function

       ► Private function

       ► Public function

 

Question No: 11      ( Marks: 1 ) - Please choose one

A function can declare itself a friend of a class.

       ► True

► False

 

Question No: 12      ( Marks: 1 ) - Please choose one

Overloaded delete operator function takes the same parameter as an argument returned by new operator function.

► True

       ► False

Page 361: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 13      ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

       ► False

 

Question No: 14      ( Marks: 1 ) - Please choose one

A template function must have at least ---------- generic data type

       ► Zero

► One

       ► Two

       ► Three

 

Question No: 15      ( Marks: 1 ) - Please choose one

A template function must have only generic data types.

       ► True

► False

Question No: 16      ( Marks: 1 ) - Please choose one

Page 362: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

We can not make a member function of a class as template function.

       ► True

► False

 

Question No: 17      ( Marks: 1 ) - Please choose one

Which character is inserted at the end of string to indicate the end of string?

       ► new line

       ► tab

► null

       ► carriage return

 

Question No: 18      ( Marks: 1 ) - Please choose one

Assignment operator is used for ___________.

       ► calculation

       ► reading

► assigning value to variables

       ► None of the given options.

Page 363: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 19      ( Marks: 1 ) - Please choose one vuzs

The object _______________may be used both for file input and file output

► fstream,

       ► ifstream,

       ► ofstream,

       ► none of the given options.

 

Question No: 20      ( Marks: 1 ) - Please choose one

Which of the following function calling mechanism is true for the function prototype given below?

float func(float &);

       ► Call by value

       ► Call by reference using pointer

► Call by reference using reference variable

       ► None of the given options

 

Question No: 21      ( Marks: 1 ) - Please choose one

The programs, in which we allocate static memory, run essentially on ________

Page 364: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► Heap

       ► System Cache

       ► None of the given options

► Stack

 

Question No: 22      ( Marks: 1 ) - Please choose one

Overloaded delete operator function takes parameter of void pointer and returns ________.

       ► void

       ► void pointer

       ► pointer to an object

       ► pointer of type int

 

Question No: 23      ( Marks: 1 ) - Please choose one

What should be the return type of the constructor?

       ► void pointer

       ► int

       ► same as object type

► constructors do not return any thing

Page 365: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 

Question No: 24      ( Marks: 1 ) - Please choose one

It is a way of reusing the code when we contain objects of our already written classes into a new class,

► True

       ► False

 

Question No: 25      ( Marks: 1 ) - Please choose one vuzs

Templates are not type safe.

       ► true

► false

 

Question No: 26      ( Marks: 1 ) - Please choose one

The functions used for dynamic memory allocation return pointer of type ______

  

       ► int

       ► float

► void

Page 366: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► double

 

Question No: 27      ( Marks: 2 )

Write a declaration statement for an array of 10 elements of type float. Include an initialization statement of the first four elements to 1.0, 2.0, 3.0 and 4.0.

Answer:

float  floatArry[10] = {1.0,2.0,3.0,4.0};

Question No: 28      ( Marks: 2 )

Write the general syntax for the declaration of pre-increment and post-increment member operator function.

Classname operator ++(); ---- pre increment

Classname operator ++(int)  ---- post increment

Question No: 29      ( Marks: 2 )

What is difference between endl and \n? 

Question No: 30      ( Marks: 2 )

What does code optimization mean?

Question No: 31      ( Marks: 3 )

Page 367: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

How is the following cout statement interpreted by compiler?szuv

cout << a << b << c ;

Question No: 32      ( Marks: 3 )

Suppose an object of class A is declared as data member of class B.

(i) The constructor of which class will be called first?  Answer : A

(ii) The destructor of which class will be called first?  Answer : B

Question No: 33      ( Marks: 3 )

Define static variable. Also explain life time of static variable?

Question No: 34      ( Marks: 5 )

What is difference between Unary and binary operators and how they can be overloaded?

Question No: 35      ( Marks: 5 )

What steps we must follow to design good program?

 Question No: 36      ( Marks: 5 ) 

 Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.

Page 368: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMNote: Display all data with in a particular width and the empty space should be filled with character x

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

xxxxx20000

xxxxx30000

xxxxx60000

FINALTERM EXAMINATION

CS201- Introduction to Programming

Question No: 1 ( Marks: 1 ) - Please choose one

Pointer is a variable which store,

► Data

► Memory Address

► Data Type

► Values

Page 369: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 2 ( Marks: 1 ) - Please choose one

All preprocessor directives are started with the symbol______.

► *

► ++

► @

► #

Question No: 3 ( Marks: 1 ) - Please choose one

Within the statement obj1=obj2; obj1 will call the assignment operator function and obj2 will be passed as an argument to function.

► True

► False

Question No: 4 ( Marks: 1 ) - Please choose one

Page 370: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

► Destructor is called first before deallocating memory

Question No: 5 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

► False

Page 371: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 6 ( Marks: 1 ) - Please choose one

To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

► include <iostream.h>

► #include <iostream.h>

Question No: 7 ( Marks: 1 ) - Please choose one

The number 544.53 must be stored in _____ data type.

► int

► short

► float

Page 372: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► char

Question No: 8 ( Marks: 1 ) - Please choose one

A template function can have different type of arguments.

► True

► False

Question No: 9 ( Marks: 1 ) - Please choose one

For which values of the integer _value will the following code becomes an infinite loop?

int number=1;

while (true) {

cout << number;

if (number == 3) break;

number += integer_value; }

Page 373: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► any number other than 1 or 2

► only 0

► only 1

► only 2

Question No: 10 ( Marks: 1 ) - Please choose one

Template class cannot have static variables.

► True

► False

Question No: 11 ( Marks: 1 ) - Please choose one

Which of the following is used with bit manipulation?

► Signed integer

► Un-signed integer

Page 374: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Signed double

► Un-signed double

Question No: 12 ( Marks: 1 ) - Please choose one

Structure is a collection of ______________ under a single name.

► Only Functions

► Only Variables

► Both Functions and Variables

► None of the given options

Question No: 13 ( Marks: 1 ) - Please choose one

Page 375: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

► new int(10) ;

► new int[10] ;

► int new(10) ;

► int new[10];

Question No: 14 ( Marks: 1 ) - Please choose one

Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.

Page 376: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► One, zero

► Zero, one

► One, two

► Two, one

Question No: 15 ( Marks: 1 ) - Please choose one

The first parameter of overloaded stream insertion operator is _________ where second parameter is _______

► input stream, object of class

► object of class, output stream

► output stream, object of class

► object of class, input stream

Question No: 16 ( Marks: 1 ) - Please choose one

Page 377: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 17 ( Marks: 1 ) - Please choose one

If a symbolic constant has been defined, it will be an error to define it again.

► True

► False

Question No: 18 ( Marks: 1 ) - Please choose one

While calling function, the arguments are assigned to the parameters from _____________.

► left to right.

► right to left

Page 378: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► no specific order is followed

► none of the given options.

Question No: 19 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Question No: 20 ( Marks: 1 ) - Please choose one

If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.

► cannot be replaced

Page 379: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► None of the given options

► Remain constant.

► can be changed by some operation

Question No: 21 ( Marks: 1 ) - Please choose one

Assignment operator is -------------------------associative.

► right

► left

► binary

► unary

Page 380: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 22 ( Marks: 1 ) - Please choose one

If text is a pointer of class String then what is meant by the following statement?

text = new String [5];

► Creates an array of 5 string objects statically

► Creates an array of 5 string objects dynamically

► Creates an array of pointers to string

► Creates a string Object

Question No: 23 ( Marks: 1 ) - Please choose one

Page 381: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

The return type of the operator function for << operator is __________.

► class for which we overload operator

► reference of ostream class (ostream&)

► reference of istream class (istream&)

► void

Question No: 24 ( Marks: 1 ) - Please choose one

The code is written to __________ the program.

► implement

► design

► analysis

► none of the given options.

Page 382: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 25 ( Marks: 1 ) - Please choose one

Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the memory.

► System

► Programmer

► User of program

► None of given options

Question No: 26 ( Marks: 1 ) - Please choose one

Templates are not type safe.

► true

Page 383: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► false

Question No: 27 ( Marks: 2 )

Give the general syntax of class template.

Answer:

Syntax of class template:

template <class T>

class class-name()

{

definition of class

};

Question No: 28 ( Marks: 2 )

What is difference between endl and \n?

Answer:

The difference between endl and \n is that endl is use to start a new line for the next row

And \n is a new line character.

Page 384: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 29 ( Marks: 2 )

What is the this pointer? Give an example of its use.

Answer:

This pointer is use to points to the current object in programming.

Question No: 30 ( Marks: 2 )

Identify each of the following as function call, function definition and function declaration.

5. int func(int num1, int num2); Function call:

Function ; Function definition: Integer; Function declaration: Num1

and Num2

6. int func(int, int); Function call:

Function ; Function definition: Integer; Function declaration: integers

7. func(5, 6) ;

Function call:

Function ; Function definition: numbers; Function declaration: 5&6

Page 385: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

8. int func(int num1, int num2){} Function call:

Function ; Function definition: Integer; Function declaration: Num1 and Num2 from

user

Question No: 31 ( Marks: 3 )

Consider the following code segment. What will be the output of the following code segment?

class class1{

public:

class class2{

public:

class2(){

cout << “Calling default constructor of class2\n” ;

}

};

class1(){

cout << “Calling default constructor of class1\n” ;

}

} ;

main(){

class1::class2 obj1;

class1 obj2 ;

}

Page 386: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 32 ( Marks: 3 )

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Answer:

No, we cannot define two functions as func(intx, inty) func(int &x, int&y) because it’s give an error function not initializing.

Question No: 33 ( Marks: 3 )

What happens when we use new and delete operator?

Answer:

When we use new operator to create objects the memory space is allocated for the object and then its constructor is called. Similarly, when we use delete operator with our objects, the destructor is called for the object before deallocating the storage to the object.

Question No: 34 ( Marks: 5 )

Page 387: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What is the difference between function overloading and operator overloading?

Answer:

Difference b/w function overloading and operator overloading is:

In function overloading, the functions have the same name but differ either by the number of arguments or the type of the arguments.

Operator overloading is to allow the same operator to be bound to more than one implementation, depending on the types of the operands.

Question No: 35 ( Marks: 5 )

Why the first parameter of operator function for << operator must be passed by reference?

Answer:

Operator<<'s first parameter must be an ostream passed by reference. Its second parameter, the IntList that is printed, does not have to be passed as a const-reference parameter; however it is more efficient to pass it by reference than by value (since that avoids a call to the copy constructor), and it should not be modified by operator<<, so it should be a const reference parameter

Question No: 36 ( Marks: 5 )

Read the given below code and explain what task is being performed by this function

Page 388: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Matrix :: Matrix ( int row , int col )

{

numRows = row ;

numCols = col ;

elements = new ( double * ) [ numRows ] ;

for ( int i = 0 ; i < numRows ; i ++ )

{

elements [ i ] = new double [ numCols ] ;

for ( int j = 0 ; j < numCols ; j ++ )

elements [ i ] [ j ] = 0.0 ;

}

}

Hint : This function belong to a matrix class, having

Number of Rows = numRows

Number of Columns = numCols

Answer:

In this code the matrix function is defined, it get the number of rows from the user and create the row of matrix and then get the columns from the user and create the columns. The New is showing for creating more array space for the data which user enters. The elements [i][j] will print the data in matrix form.

Mc100401285 Moaaz Siddiq

Cs201-finalterm papers

July 2011

Paper Detail

Page 389: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMTotal 80 Marks

40 Mcqs = 40 Marks

subjective=40 Marks

4 Question = 2Marks

4 Question =3 Marks

4 Question = 4 Marks

Q.41

What does it happen if an object id assign to another object without overloading an assignment operator.2marks

Q.42

Describe the first parameter to stream insurtion<<and stream extraction>> operator function? 2Marks

Q.43

Write the meaning of given line code where m is object of class matrix

If (&m! = this)

Q.44

Why always array start with index 0(zero) ? 2Marks

Q.45

What will be the output of following function if we call this function by passing int 5? 3 marks

template T reciprocal(T x) {return (1/x); }

Q.46

Page 390: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMIdentify all the given functions as member function of input stream or output stream

Get, unget, and put, getline, Peek, Putline, 3Marks

Q.47

Write the general syntax for unary member operator 3Marks

Q.48

Write the output of this code 3Marks

Main()

{

int x,y,z;

x=5;

y=3+x++;

Q.49

What are the advantages and disadvantages of using templates? 5Marks

Q.50

Find the errors in given code and correct them; also write the output of the code-5 Marks

Q.51

Describe the effect of an overflow condition if it occurs within a program 5 Marks

Q.52

Make a program instruction was given in the code 5Marks

FINALTERM  EXAMINATIONSpring 2010

Page 391: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMCS201- Introduction to ProgrammingShared by ZeehanChecked by zubair hussain (MCQs)vuZs TeamMarks: 58Question No: 1 ( Marks: 1 ) - Please choose one*.doc is _____________ by type.. ► Sequential File ► Random Access File ► Data File ► Record FileQuestion No: 2 ( Marks: 1 ) - Please choose oneWhich of the following is NOT a preprocessor directive? ► #error ► #define ► #line ► #ndefinehttp://vuzs.net/Question No: 3 ( Marks: 1 ) - Please choose oneThe return type of operator function must always be void. ► True ► FalseThe syntax of the prototype of the overloaded operator function is: return-type operator operator-symbol (parameter-list);Question No: 4 ( Marks: 1 ) - Please choose oneWhat does (*this) represents? ► The current function of the class ► The current pointer of the class ► The current object of the class ► A value of the data memberWhenever an object calls a member function, the function implicitly gets a pointer from the calling object. That pointer is known as this paointer. ‘this’ is a key word. We cannot use it as a variable name. ‘this’ pointer is present in the function, referring to the calling object. For example, if we have to refer a member, let’s say buf, of our String class, we can write it simply as: buf ; Question No: 5 ( Marks: 1 ) - Please choose oneThe statement cin.get (); is used to, ► Read a string from keyboard ► Read a character from keyboard ► Read a string from file ► Read a character from filehttp://vuzs.net/Question No: 6 ( Marks: 1 ) - Please choose one

Page 392: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor. ► True ► FalseQuestion No: 7 ( Marks: 1 ) - Please choose one Overloaded new operator function takes parameter of type size_t and returns ► void (nothing) ► void pointer ► object pointer ► int pointerQuestion No: 8 ( Marks: 1 ) - Please choose oneThe second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators. ► True ► FalseThe second parameter to operator << is an object of the class that we are overloading the operator for. Similar is the case for operator >>. Question No: 9 ( Marks: 1 ) - Please choose one C++ is a case-sensitive language ► True ► FalseQuestion No: 10 ( Marks: 1 ) - Please choose one To include code from the library in the program, such as iostream, a directive would be called up using this command. ► #include “iostream.h” ► include ► include ► #include Question No: 11 ( Marks: 1 ) - Please choose one A template function must have only generic data types. ► True ► FalseIts not compulsory, only min we have one generic data type but we can have native data type as well. Question No: 12 ( Marks: 1 ) - Please choose one Template class can not have static variables. ► True ► FalseQuestion No: 13 ( Marks: 1 ) - Please choose oneWhat will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr? ► *ptr = arr ;

Page 393: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► ptr = arr ; ► *ptr = arr[5] ; ► ptr = arr[5] ; Question No: 14 ( Marks: 1 ) - Please choose oneWhat will be the correct syntax to access the value of fourth element of an array using pointer ptr? ► ptr[3] ► (ptr+3) ► *(ptr+3) ► Both 1and 3try this demo program to confirm result I wrote for you.2 option will print the reference rest 1,3 are righ options http://vuzs.net/#include #include // #include  main(){int myarr [4]= {0,1,2,3};int *ptr ;ptr = myarr;cout<cout<<*(ptr+3);cout<<(ptr+3);int i = 0;cin>> i;} Question No: 15 ( Marks: 1 ) - Please choose one If most significant bit of un-signed number is 1 then it represents a positive number. ► True ► FalseThe most significant bit is used as a sign bit. If this bit is zero, the number is considered positive. However, if it is 1, the number will be considered negative.Question No: 16 ( Marks: 1 ) - Please choose one If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____. ► Address of variable, reference variable ► Reference variable, value of variable ► Reference variable, address of variable ► Address of variable, value of variable

Page 394: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMwe see a data type followed by & sign, it’s a reference. And when the & sign is being used in the code with a variable name then it is the address of the variableQuestion No: 17 ( Marks: 1 ) - Please choose one We can also do conditional compilation with preprocessor directives. ► True ► FalseQuestion No: 18 ( Marks: 1 ) - Please choose one The default value of a parameter can be provided inside the ________________ ► function prototype ► function definition ► both function prototype or function definition ► none of the given options.The default value of a parameter is provided inside the function prototype or function definition.Question No: 19 ( Marks: 1 ) - Please choose one Classes defined inside other classes are called ________ classes ► looped ► nested ► overloaded ► none of the given options.Question No: 20 ( Marks: 1 ) - Please choose one What purpose do classes serve? ► Data encapsulation ► Providing a convenient way of modeling real-world objects ► Simplifying code reuse ► All of the given optionsQuestion No: 21 ( Marks: 1 ) - Please choose onevuzs Every class contains _______________. ► Constructor ► Destructor ► Both a constructor and a destructor ► None of the given optionsQuestion No: 22 ( Marks: 1 ) - Please choose one new operator is used to allocate memory from the free store during ► Compile Time ► Run Time ► Link Time ► None of the given optionsQuestion No: 23 ( Marks: 1 ) - Please choose one When an object of a class is defined inside another class then, ► Destructor of enclosing class will be called first ► Destructor of inner object will be called first ► Constructor and Destructor will be called simultaneously

Page 395: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► None of the given optionsQuestion No: 24 ( Marks: 1 ) - Please choose one It is possible to define a class within another class. ► True ► FalseQuestion No: 25 ( Marks: 1 ) - Please choose one New and Delete are also used with ___________ and data types as well. ► Class, Objects ► Structures, Pointers ► Both Class and structures ► None of abovewe prefer to use new and delete operators as they are designed to work with classes and objectsQuestion No: 26 ( Marks: 1 ) - Please choose one With New keyword, data types and class members are initialized with meaningful values instead of garbage. http://vuzs.net/ ► True ► FalseQuestion No: 27 ( Marks: 2 ) How many arguments a Unary Operator take? Can we make a binary operator as unary operator? Ans: Unary operator takes only one aurgument like i++ or i— (Post increment or post decrement operators for intergers) or ++i,--i (Pre increment or pre decrement operators for intergers) ,we can not make Unary operator as binary or binary as Unary operator.Question No: 28 ( Marks: 2 ) Which arithmetic operators cannot have a floating point operand?Ans:Modulus operator This operator can only be used with integer operands ONLYQuestion No: 29 ( Marks: 2 ) What are manipulators? Give one example.Ans:The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14 . This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed. Some manipulators are parameter less. We simply use the name of the manipulator that works. For example, we have been using endl, which is actually a manipulator, not data. When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it manipulates the output stream.

Page 396: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 30 ( Marks: 2 ) Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;Ans:int matrix [3] [3] ;matrix [0] [0] = 0;matrix [0] [1] = 0;matrix [0] [2] = 0;matrix [1] [0] = 0;matrix [1] [2] = 0;matrix [1] [2] = 0;matrix [2] [0] = 0;matrix [2] [1] = 0;matrix [2] [2] = 0;we can also do it as given belowint matrix [3][3] = { 0 }; //all elements 0 Question No: 31 ( Marks: 3 ) Which one (copy constructor or assignment operator) will be called in each of the following code segment?1) Matrix m1 (m2);2) Matrix m1, m2;m1 = m2;3) Matrix m1 = m2;Ans:1) Matrix m1 (m2); copy constructor2) Matrix m1, m2; m1 = m2; assignment operator3) Matrix m1 = m2; assignment operatorQuestion No: 32 ( Marks: 3 ) What will be the output of following function if we call this function by passing int 5?template T reciprocal(T x) {return (1/x); }Ans:1/5Question No: 33 ( Marks: 3 ) Identify the errors in the following member operator function and also correct them.math * operator(math m);math * operator (math m){ math temp; temp.number= number * number; return number; }

Page 397: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMANS:The errors are in the arguments of the member operation function and also in the body of operator member function.Correct function should bemath *operator(math *m);math *operator (math *m){ math temp; temp = m; temp.number= number * number; return temp.number; }http://vuzs.net/Question No: 34 ( Marks: 5 ) Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.Ans:#include #include int main () { double x1 = 12345624.72345double x2 = 987654.12345double x3 = 1985.23456 cout << setprecision (3) << x1<< endl; cout << setprecision (4) << x2 << endl;cout << setprecision (5) << x3<< endl; return 0;}Question No: 35 ( Marks: 5 ) What are the advantages and disadvantages of using templates?Ans:Many thing can be possible without using templates but it do offer several clear advantages not offered by any other techniques:Advanatages: • Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.• Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

Page 398: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM • Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.• It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead. Disadvantages:On the other hand, if misused • Templates can make code difficult to read and follow depending upon coding style.• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files. • Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.Question No: 36 ( Marks: 5 ) Suppose a program has a math class having only one data member number.Write the declaration and definition of operator function to overload + operator for the statements of main function. math obj1, obj2; obj2= 10 + obj1 ; Ans:#include math{mth operator + (obj1,obj2)mth operator + (obj1,obj2){ mth operator + (obj1,obj2)mth operator + (obj1,obj2)}}…………………………………………………………………………… FINALTERM  EXAMINATION Spring 2010CS201- Introduction to Programming Solved by Farhat Qudsia & Zuabir HussainvuZs Teamwwww.vuzs.net

Marks: 58 Question No: 1      ( Marks: 1 ) - Please choose one In if structure the block of statements is executed only,       ► When the condition is false

Page 399: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► When it contain arithmetic operators       ► When it contain logical operators       ► When the condition is true Question No: 2      ( Marks: 1 ) - Please choose oneHeader file: fstream.h includes the definition of the stream classes __________.       ► ifstream, fstream, cout       ► ifstream, fstream, ofstream       ► fstream, cin, cout       ► None of the aboveQuestion No: 3      ( Marks: 1 ) - Please choose one To access the data members of structure _______ is used.       ► dot operator (.)       ► * operator       ►  operatorà        ► None of given. Question No: 4      ( Marks: 1 ) - Please choose one eof( ), bad( ), good( ), clear( ) all are manipulators.       ► True        ► False Question No: 5      ( Marks: 1 ) - Please choose one Which kind of functions can access private member variables of a class?       ► Friend functions of the class        ► Private member functions of the class        ► Public member functions of the class        ► Friend, private and public functions Question No: 6      ( Marks: 1 ) - Please choose oneThe return type of operator function must always be void.       ► True       ► False Question No: 7      ( Marks: 1 ) - Please choose oneFriend function of a class is ______________ .       ► Member function       ► Non-member function                     ► Private function       ► Public functionhttp://vuzs.net/Question No: 8      ( Marks: 1 ) - Please choose oneFunction implementation of friend function must be defined outside the class.       ► True       ► False (any where in the class)Question No: 9      ( Marks: 1 ) - Please choose one The normal source of cin object is,       ► File       ► Disk       ► Keyboard       ► RAMQuestion No: 10      ( Marks: 1 ) - Please choose oneWhich of the following is correct way to initialize a variable x of int type with value 10?       ► int x ; x = 10;       ► int x = 10;       ► int x, x = 10;       ► x = 10; http://groups.google.com/group/vuZs/Question No: 11      ( Marks: 1 ) - Please choose one Consider the following code segment. What will be the output of the following program?int func(int) ; int num = 10 ;int main(){

Page 400: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMint num ;num = 5 ;cout << num ;cout << func(num) ;}int func(int x){return num ;}        ► 5, 5         ► 10, 5       ► 5, 10         ► 10, 10Question No: 12      ( Marks: 1 ) - Please choose one With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.       ► True        ► FalseQuestion No: 13      ( Marks: 1 ) - Please choose oneWhat will be the correct syntax to declare two-dimensional array of float data type?       ► float arr{2}{2} ;       ► float arr[2][2] ;       ► float arr[2,2] ;       ► float[2][2] arr ;   Question No: 14      ( Marks: 1 ) - Please choose oneThe first parameter of operator function for << operator,       ► Must be passed by value       ► Must be passed by reference       ► Can be passed by value or reference       ► Must be object of class Question No: 15      ( Marks: 1 ) - Please choose oneHeap is constantly changing in size.       ► True       ► Falsehttp://vuzs.net/Question No: 16      ( Marks: 1 ) - Please choose oneWhile calling function, the arguments are assigned to the parameters from        ► left to right.       ► right to left        ► no specific order is followed       ► none of the given options.Question No: 17      ( Marks: 1 ) - Please choose oneClasses defined inside other classes are called ________ classes       ► looped       ► nested       ► overloaded       ► none of the given options.Question No: 18      ( Marks: 1 ) - Please choose oneIf we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________       ► can not be replace       ► None of the given options       ► Remain constant.        ► can be changed by some operation Question No: 19      ( Marks: 1 ) - Please choose one Which value is returned by the destructor of a class?       ► A pointer to the class.       ► An object of the class.

Page 401: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM       ► A status code determining whether the class was destructed correctl       ► Destructors do not return a value.Question No: 20      ( Marks: 1 ) - Please choose one Every class contains _______________.       ► Constructor       ► Destructor       ► Both a constructor and a destructor       ► None of the given optionsQuestion No: 21      ( Marks: 1 ) - Please choose oneA template function must have        ► One or more than one arguments        ► Only one argument       ► Zero argument       ► None of the given options http://vuzs.net/http://groups.google.com/group/vuZs/Question No: 22      ( Marks: 1 ) - Please choose one Structured Query Language is used for ______________       ► Databases Management       ► Networks       ► Writing Operating System       ► none of the given optionsQuestion No: 23      ( Marks: 1 ) - Please choose one When a call to a user-defined function finishes, the variable defined inside the function is still in existence.       ► True       ► FalseQuestion No: 24      ( Marks: 1 ) - Please choose one The precedence of an operator can be changed through operator overloading.       ► True       ► FalseQuestion No: 25      ( Marks: 1 ) - Please choose oneA Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________       ► Write a separate class to handle each        ► Use templates       ► Use strings to store all types       ► None of the given optionsA Matrix can be composed of ints, floats or doubles as their elements. Instead of handling these data types separately, we can write Matrix class as a template class and write code once for all native data types.Write Matrix class as a template class and write code once for all native data typesQuestion No: 26      ( Marks: 1 ) - Please choose one"delete" operator is used to return memory to free store, which is allocated by the "new" operator.        ► True       ► FalseQuestion No: 27      ( Marks: 2 )What is the difference between switch statement and if statement.Question No: 28      ( Marks: 2 ) How can we initialize data members of contained object at construction time?Question No: 29      ( Marks: 2 ) How the data members of a class are initialized with meaningful values?Question No: 30      ( Marks: 2 )Can we overload new and delete operators? Question No: 31      ( Marks: 3 )What will be the output of following functions if we call these functions three times?1)

Page 402: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMvoid func1(){int x = 0;x++;cout << x << endl;}2) void func2(){static int x = 0 ;x++;cout << x << endl ;}   Question No: 32      ( Marks: 3 ) What is the keyword ‘this’ and what are the uses of ‘this’ pointer? Question No: 33      ( Marks: 3 )Suppose an object of class A is declared as data member of class B.(i) The constructor of which class will be called first?(ii) The destructor of which class will be called first?Question No: 34      ( Marks: 5 )Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.Question No: 35      ( Marks: 5 )Write down the disadvantages of the templates.Question No: 36      ( Marks: 5 ) http://vuzs.net/Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.Note: Display all data with in a particular width and the empty space should be filled with character xOutput should be displayed as given below:xxxxxx1000xxxxxx1500xxxxx20000xxxxx30000xxxxx60000

MCQS SOLVED BY: _ALIMS_http://www.virtualinspire.com

Question: 1New operator is used to allocate memory from the free store during ______________

Compile TimeRun TimeLink TimeNone of above

Question: 2To get the memory address of a variable we use _____

&&&

Page 403: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM*|

Question: 3

If we have a program that writes the output data (numbers) to the disc, and if we collect the output Data and write it on the disc in one write operation instead of writing the numbers one by one. In the above situation the area where we will gather the number is called

BufferStreamMemoryNone of these

Question: 4

What functionality the following program is performing?

#include <iostream.h>

int main(){

const int SIZE = 80;char buffer[SIZE];cout <<" Enter a sentence : ";cin.getline(buffer, SIZE);cout<<buffer <<endl;system("pause");

}

objects are used respectively to read a sentence from the key board and then print it on the screen.Using getline functionusing character arraysDoing nothing

Question: 5

The get member function, when passed no arguments, inputs an entire stream and returns it as the value of the function call.

Page 404: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMTrueFalse

Question: 6New operator allocates memory from free store and returns ___________

VoidNULLNothing returnNone of above

Question: 7

The statement cin.get (); is used to,

Read a character from keyboardRead a an entire stringRead integer valuesRead float values

Question: 8Suppose int i = 10; then what will be the value of i after being converted in to octal value

10121416

Question: 9Which of the following is a destination of cout stream?

Monitor /screenKeyboardMemoryNone of these

Question: 10Which of the following is the correct definition of streams ?

streams are memory locationssequence of bytes are called streamssequence of variables are called streamssequence does not have any definition

Page 405: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question: 11Stream operators are heavily overloaded means , they allow to handle int and character data type only.

truefalse

Question: 12What functionality the following program is performing?

int main(){

const int SIZE = 80;char buffer[SIZE];cout <<" Enter a sentence : ";cin.getline(buffer, SIZE);cout<<buffer <<endl;system("pause");

}

read and write member functions of cin and cout objects are used respectively to read a sentence from the key board and then print it on the screen.

read and put member functions of cin and cout objects are used respectively to read a sentence from the key board and then print it on the screen.

get and write member functions of cout and cin objects are used respectively to read a sentence from the key board and then print it on the screen.

get and write member functions of cout and cin objects are used respectively to read a sentence from the key board and then print it on the screen.

Question: 13Which of the following is a source for cout stream?

monitor / screenkeyboardproccessornone of these

Page 406: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion: 14If we use cin stream to read some value and store it in some integer variable and press some alphabet key instead of numeric keys. then what will happen?

Its binary representation will be ignored and the character will be stored

Its binary representation will be ignored and the value will be stored

Its ASCII code will be stored inside the computer

Some error will occur and cin stream will detect this error.

The endl and flush are _____

FunctionsOperatorsManipulatorsObjects

What is the difference between cout and cerr ?

cout is unbuffered output and cerr is buffered output

cout is standard output and cerr is not a standard output

cout is not a standard output and cerr is standard output

cout is buffered output and cerr is unbuffered output

The operator function for << (stream insertion) >> and stream extraction must be

Member function of classNon-member function of classBoth member and non-member functionNone of the given optionsReference: (These cin and cout objects will remain intact for our overloaded insertion and extraction operators. Therefore, the overloaded operators cannot be member operators.)

Note: I took this reference

Page 407: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMThe pointer returned by the new operator points to --------------- of memory chunks allocated by the new operator

First memory address

Second memory address

Last memory addressNone of the above

When we used eof (end of file) with the file reading than which of the following is a true statement?This is way to check source of the streamThis is way to check destination of the streamThis is way to check state of the streamThis is way to check type of the stream

The stream insertion and extraction operators are not already overloaded for ____

Built-in data types

User-defined data types

Both built-in and user-defined types

None of the given options

When a variable is defined as static in a class then ___________

Separate copy of this variable is created for each objectOnly one copy is created for all objects of this classA copy of this variable is created for only static objects.None of the given options

Static variable which is defined in a function is initialized ________.Only once during its life time

Every time the function call

Compile time of the program

None of the above

Page 408: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWhich of the following concept is NOT associated with stream? NULLMY PAPER OF CS 201 (DATE 18JULY2011)TOTAL 52 QUESTIONS

12 LONG AND 40 MCQ’S

AND MOSTLY MCQ’S ARE NOT FROM PAST PAPERS, ITS FROM LAST CHAPTERS…

Q: 41: Write a program with different variables V1 and V2 and print out it (hexadecimal, decimal, octal) form use one manipulator. 5 marks ..Q:42: What is structure and union structure? 3 marks.Q:43: What is template and write general syntax of templates. 3marks..Q:44: How many types of structure? 2 marksQ:45: Write the rules for using of Dynamic memory allocations… 5marks..Page 479Q:46: Describe the first parameter to stream insertion<<and stream extraction>> operator function? 2marksQ:47: See the following code segment Template <class T>Class myclass{

Page 409: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMPrivate :Tx;Public Myclass(ta){X=a;}; Write the main function which creates two objects of class for int & double types?Q:48: If the requested memory is not available in the system then what does calloc/malloc and new operatorreturn? 3marks …

Q:49, Q:50, Q:51 and Q:52 are not in my mind, These Question are from Lecture number 40 to 45….

FINALTERM EXAMINATION

Spring 2010

CS201- Introduction to Programming

Ref No:

Time: 90 min

Marks: 58

Page 410: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMStudent Info

Student ID:

Center:

Exam Date:

For Teacher's Use Only

Q No. 1 2 3 4 5 6 7 8 Total

Marks

Q No. 9 10 11 12 13 14 15 16

Marks

Q No. 17 18 19 20 21 22 23 24

Marks

Q No. 25 26 27 28 29 30 31 32

Marks

Q No. 33 34 35 36

Marks

Page 411: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 1 ( Marks: 1 ) - Please choose one

*.doc is _____________ by type.

.

► Sequential File

► Random Access File

► Data File

► Record File

Question No: 2 ( Marks: 1 ) - Please choose one

Which of the following is NOT a preprocessor directive?

Page 412: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► #error

► #define

► #line

► #ndefine

Question No: 3 ( Marks: 1 ) - Please choose one

The return type of operator function must always be void.

► True

► False

Page 413: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 4 ( Marks: 1 ) - Please choose one

What does (*this) represents?

► The current function of the class

► The current pointer of the class

► The current object of the class

► A value of the data member

Question No: 5 ( Marks: 1 ) - Please choose one

The statement cin.get (); is used to,

► Read a string from keyboard

Page 414: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Read a character from keyboard

► Read a string from file

► Read a character from file

Question No: 6 ( Marks: 1 ) - Please choose one

When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor. http://vustudents.ning.com

► True

► False

Question No: 7 ( Marks: 1 ) - Please choose one

Overloaded new operator function takes parameter of type size_t and returns

Page 415: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► void (nothing)

► void pointer

► object pointer

► int pointer

Question No: 8 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

► False

Page 416: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 9 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

C++ is a case-sensitive language

► True

► False

Question No: 10 ( Marks: 1 ) - Please choose one

To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

► include <iostream.h>

► #include <iostream.h>

Question No: 11 ( Marks: 1 ) - Please choose one

A template function must have only generic data types.

► True

► False

Page 417: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 12 ( Marks: 1 ) - Please choose one

Template class can not have static variables.

► True

► False

Question No: 13 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?

► *ptr = arr ;

► ptr = arr ;

► *ptr = arr[5] ;

► ptr = arr[5] ;

Page 418: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 14 ( Marks: 1 ) - Please choose one

What will be the correct syntax to access the value of fourth element of an array using pointer ptr?

► ptr[3]

► (ptr+3)

► *(ptr+3)

► Both 1and 3

Question No: 15 ( Marks: 1 ) - Please choose one

If most significant bit of un-signed number is 1 then it represents a positive number.

► True

► False

Page 419: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 16 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____.

► Address of variable, reference variable

► Reference variable, value of variable

► Reference variable, address of variable

► Address of variable, value of variable

Question No: 17 ( Marks: 1 ) - Please choose one

We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 18 ( Marks: 1 ) - Please choose one

The default value of a parameter can be provided inside the ________________

Page 420: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► function prototype

► function definition

► both function prototype or function definition

► none of the given options.

Question No: 19 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Question No: 20 ( Marks: 1 ) - Please choose one

Page 421: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What purpose do classes serve?

► Data encapsulation

► Providing a convenient way of modeling real-world objects

► Simplifying code reuse

► All of the given options

Question No: 21 ( Marks: 1 ) - Please choose one

Every class contains _______________.

► Constructor

► Destructor

Page 422: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Both a constructor and a destructor

► None of the given options

Question No: 22 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

new operator is used to allocate memory from the free store during

► Compile Time

► Run Time

► Link Time

► None of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside another class then,

Page 423: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Destructor of enclosing class will be called first

► Destructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 24 ( Marks: 1 ) - Please choose one

It is possible to define a class within another class.

► True

► False

Question No: 25 ( Marks: 1 ) - Please choose one

New and Delete are also used with ___________ and data types as well.

► Class, Objects

► Structures, Pointers

Page 424: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Both Class and structures

► None of above

Question No: 26 ( Marks: 1 ) - Please choose one

With New keyword, data types and class members are initialized with meaningful values instead of garbage. http://vustudents.ning.com

► True

► False

Question No: 27 ( Marks: 2 )

How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

Ans: Unary operator takes only one aurgument like i++ or i— (Post increment or post decrement operators for intergers) or ++i,--i (Pre increment or pre decrement operators for intergers) ,we can not make Unary operator as binary or binary as Unary operator.

Question No: 28 ( Marks: 2 )

Page 425: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Which arithmetic operators cannot have a floating point operand?

Ans: http://vustudents.ning.com

Modulus operator

This operator can only be used with integer operands ONLY

Question No: 29 ( Marks: 2 )

What are manipulators? Give one example.

Ans:

The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14 . This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

Some manipulators are parameter less. We simply use the name of the manipulator that works. For example, we have been using endl, which is actually a manipulator, not data. When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it manipulates the output stream.

Page 426: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 30 ( Marks: 2 )

Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;

Ans:

int matrix [3] [3] ;

matrix [0] [0] = 0;

matrix [0] [1] = 0;

matrix [0] [2] = 0;

matrix [1] [0] = 0;

matrix [1] [2] = 0;

matrix [1] [2] = 0;

matrix [2] [0] = 0;

matrix [2] [1] = 0;

matrix [2] [2] = 0;

we can also do it as given below

int matrix [3][3] = { 0 }; //all elements 0 

Question No: 31 ( Marks: 3 )

Which one (copy constructor or assignment operator) will be called in each of the following code segment?

1) Matrix m1 (m2);

2) Matrix m1, m2;

Page 427: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMm1 = m2;

3) Matrix m1 = m2;

Ans:

1) Matrix m1 (m2); copy constructor

2) Matrix m1, m2;

m1 = m2; assignment operator

3) Matrix m1 = m2; assignment operator

Question No: 32 ( Marks: 3 )

What will be the output of following function if we call this function by passing int 5?

template <class T>T reciprocal(T x) {return (1/x); }

Ans:

1/5

Question No: 33 ( Marks: 3 )

Identify the errors in the following member operator function and also correct them. http://vustudents.ning.com

math * operator(math m);

Page 428: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmath * operator (math m)

{

math temp;

temp.number= number * number;

return number;

}

ANS:

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator(math *m);

math *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

Question No: 34 ( Marks: 5 )

Page 429: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

Ans:

#include <iostream>

#include <iomanip>

int main ()

{

double x1 = 12345624.72345

double x2 = 987654.12345

double x3 = 1985.23456

cout << setprecision (3) << x1<< endl;

cout << setprecision (4) << x2 << endl;

cout << setprecision (5) << x3<< endl;

return 0;

}

Question No: 35 ( Marks: 5 )

What are the advantages and disadvantages of using templates?

Ans:

Many thing can be possible without using templates but it do offer several clear advantages not offered by any other techniques:

Page 430: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAdvanatages:

• Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

• Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

• Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.

• It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

Disadvantages: http://vustudents.ning.com

On the other hand, if misused

• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 36 ( Marks: 5 )

Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

math obj1, obj2;

obj2= 10 + obj1 ;

Ans:

#include <iostream.h>

math

Page 431: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

}

}

……………………………………………………………………………

FINALTERM EXAMINATION

Spring 2010

CS201- Introduction to Programming

Ref No:

Time: 90 min

Marks: 58

Student Info

Student ID:

Center:

Exam Date:

For Teacher's Use Only

Q No. 1 2 3 4 5 6 7 8 Total

Marks

Q No. 9 10 11 12 13 14 15 16

Marks

Page 432: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ No. 17 18 19 20 21 22 23 24

Marks

Q No. 25 26 27 28 29 30 31 32

Marks

Q No. 33 34 35 36

Marks

Page 433: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 1 ( Marks: 1 ) - Please choose one

*.doc is _____________ by type.

.

► Sequential File

► Random Access File

► Data File

► Record File

Question No: 2 ( Marks: 1 ) - Please choose one

Which of the following is NOT a preprocessor directive?

Page 434: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► #error

► #define

► #line

► #ndefine

Question No: 3 ( Marks: 1 ) - Please choose one

The return type of operator function must always be void.

► True

► False

Page 435: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 4 ( Marks: 1 ) - Please choose one

What does (*this) represents?

► The current function of the class

► The current pointer of the class

► The current object of the class

► A value of the data member

Question No: 5 ( Marks: 1 ) - Please choose one

The statement cin.get (); is used to,

► Read a string from keyboard

Page 436: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Read a character from keyboard

► Read a string from file

► Read a character from file

Question No: 6 ( Marks: 1 ) - Please choose one

When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor. http://vustudents.ning.com

► True

► False

Question No: 7 ( Marks: 1 ) - Please choose one

Overloaded new operator function takes parameter of type size_t and returns

Page 437: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► void (nothing)

► void pointer

► object pointer

► int pointer

Question No: 8 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

► True

► False

Page 438: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 9 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

C++ is a case-sensitive language

► True

► False

Question No: 10 ( Marks: 1 ) - Please choose one

To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

► include <iostream.h>

► #include <iostream.h>

Question No: 11 ( Marks: 1 ) - Please choose one

A template function must have only generic data types.

► True

► False

Page 439: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 12 ( Marks: 1 ) - Please choose one

Template class can not have static variables.

► True

► False

Question No: 13 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?

► *ptr = arr ;

► ptr = arr ;

► *ptr = arr[5] ;

► ptr = arr[5] ;

Page 440: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 14 ( Marks: 1 ) - Please choose one

What will be the correct syntax to access the value of fourth element of an array using pointer ptr?

► ptr[3]

► (ptr+3)

► *(ptr+3)

► Both 1and 3

Question No: 15 ( Marks: 1 ) - Please choose one

If most significant bit of un-signed number is 1 then it represents a positive number.

► True

► False

Page 441: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 16 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

If there is a symbol (& sign) used with the variable name followed by data type then it refers to _____ and if & is being used with variable name then it refers to _____.

► Address of variable, reference variable

► Reference variable, value of variable

► Reference variable, address of variable

► Address of variable, value of variable

Question No: 17 ( Marks: 1 ) - Please choose one

We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 18 ( Marks: 1 ) - Please choose one

The default value of a parameter can be provided inside the ________________

Page 442: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► function prototype

► function definition

► both function prototype or function definition

► none of the given options.

Question No: 19 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Question No: 20 ( Marks: 1 ) - Please choose one

Page 443: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

What purpose do classes serve?

► Data encapsulation

► Providing a convenient way of modeling real-world objects

► Simplifying code reuse

► All of the given options

Question No: 21 ( Marks: 1 ) - Please choose one

Every class contains _______________.

► Constructor

► Destructor

Page 444: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Both a constructor and a destructor

► None of the given options

Question No: 22 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

new operator is used to allocate memory from the free store during

► Compile Time

► Run Time

► Link Time

► None of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

When an object of a class is defined inside another class then,

Page 445: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Destructor of enclosing class will be called first

► Destructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 24 ( Marks: 1 ) - Please choose one

It is possible to define a class within another class.

► True

► False

Question No: 25 ( Marks: 1 ) - Please choose one

New and Delete are also used with ___________ and data types as well.

► Class, Objects

► Structures, Pointers

Page 446: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Both Class and structures

► None of above

Question No: 26 ( Marks: 1 ) - Please choose one

With New keyword, data types and class members are initialized with meaningful values instead of garbage. http://vustudents.ning.com

► True

► False

Question No: 27 ( Marks: 2 )

How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

Ans: Unary operator takes only one aurgument like i++ or i— (Post increment or post decrement operators for intergers) or ++i,--i (Pre increment or pre decrement operators for intergers) ,we can not make Unary operator as binary or binary as Unary operator.

Question No: 28 ( Marks: 2 )

Page 447: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Which arithmetic operators cannot have a floating point operand?

Ans: http://vustudents.ning.com

Modulus operator

This operator can only be used with integer operands ONLY

Question No: 29 ( Marks: 2 )

What are manipulators? Give one example.

Ans:

The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14 . This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

Some manipulators are parameter less. We simply use the name of the manipulator that works. For example, we have been using endl, which is actually a manipulator, not data. When we write cout << endl ; a new line is output besides flushing the buffer. Actually, it manipulates the output stream.

Page 448: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 30 ( Marks: 2 )

Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;

Ans:

int matrix [3] [3] ;

matrix [0] [0] = 0;

matrix [0] [1] = 0;

matrix [0] [2] = 0;

matrix [1] [0] = 0;

matrix [1] [2] = 0;

matrix [1] [2] = 0;

matrix [2] [0] = 0;

matrix [2] [1] = 0;

matrix [2] [2] = 0;

we can also do it as given below

int matrix [3][3] = { 0 }; //all elements 0 

Question No: 31 ( Marks: 3 )

Which one (copy constructor or assignment operator) will be called in each of the following code segment?

1) Matrix m1 (m2);

2) Matrix m1, m2;

Page 449: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMm1 = m2;

3) Matrix m1 = m2;

Ans:

1) Matrix m1 (m2); copy constructor

2) Matrix m1, m2;

m1 = m2; assignment operator

3) Matrix m1 = m2; assignment operator

Question No: 32 ( Marks: 3 )

What will be the output of following function if we call this function by passing int 5?

template <class T>T reciprocal(T x) {return (1/x); }

Ans:

1/5

Question No: 33 ( Marks: 3 )

Identify the errors in the following member operator function and also correct them. http://vustudents.ning.com

math * operator(math m);

Page 450: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmath * operator (math m)

{

math temp;

temp.number= number * number;

return number;

}

ANS:

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator(math *m);

math *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

Question No: 34 ( Marks: 5 )

Page 451: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

Ans:

#include <iostream>

#include <iomanip>

int main ()

{

double x1 = 12345624.72345

double x2 = 987654.12345

double x3 = 1985.23456

cout << setprecision (3) << x1<< endl;

cout << setprecision (4) << x2 << endl;

cout << setprecision (5) << x3<< endl;

return 0;

}

Question No: 35 ( Marks: 5 )

What are the advantages and disadvantages of using templates?

Ans:

Many thing can be possible without using templates but it do offer several clear advantages not offered by any other techniques:

Page 452: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAdvanatages:

• Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

• Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

• Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.

• It help in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

Disadvantages: http://vustudents.ning.com

On the other hand, if misused

• Templates can make code difficult to read and follow depending upon coding style.

• They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.

• Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 36 ( Marks: 5 )

Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

math obj1, obj2;

obj2= 10 + obj1 ;

Ans:

#include <iostream.h>

math

Page 453: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

}

}

……………………………………………………………………………

FINALTERM EXAMINATION

Spring 2010

CS201- Introduction to Programming

Ref No: 1557656

Time: 90 min

Marks: 58

Student Info

Student ID: Dc100200028

Center: OPKST

Exam Date: 12/8/10

For Teacher's Use Only

Q No. 1 2 3 4 5 6 7 8 Total

Marks

Q No. 9 10 11 12 13 14 15 16

Marks

Page 454: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ No. 17 18 19 20 21 22 23 24

Marks

Q No. 25 26 27 28 29 30 31 32

Marks

Q No. 33 34 35 36

Marks

Page 455: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 1 ( Marks: 1 ) - Please choose one

Pointer is a variable which store,

► Data

► Memory Address

► Data Type

► Values

Question No: 2 ( Marks: 1 ) - Please choose one

All preprocessor directives are started with the symbol______.

► *

► ++

► @

► #

Page 456: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 3 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

Within the statement obj1=obj2; obj1 will call the assignment operator function and obj2 will be passed as an argument to function.

► True

► False

Question No: 4 ( Marks: 1 ) - Please choose one

What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

Page 457: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Destructor is called first before deallocating memory

Question No: 5 ( Marks: 1 ) - Please choose one

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators. http://vustudents.ning.com

► True

► False

Question No: 6 ( Marks: 1 ) - Please choose one

To include code from the library in the program, such as iostream, a directive would be called up using this command.

► #include “iostream.h”

► include <iostream.h>

Page 458: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► include <iostream.h>

► #include <iostream.h>

Question No: 7 http://vustudents.ning.com ( Marks: 1 ) - Please choose one

The number 544.53 must be stored in _____ data type.

► int

► short

► float

► char

Question No: 8 ( Marks: 1 ) - Please choose one

A template function can have different type of arguments.

► True

► False

Page 459: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 9 ( Marks: 1 ) - Please choose one

For which values of the integer _value will the following code becomes an infinite loop?

int number=1;

while (true) {

cout << number;

if (number == 3) break;

number += integer_value; }

► any number other than 1 or 2

► only 0

► only 1

► only 2

Question No: 10 ( Marks: 1 ) - Please choose one

Page 460: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMTemplate class cannot have static variables. http://vustudents.ning.com

► True

► False

Question No: 11 ( Marks: 1 ) - Please choose one

Which of the following is used with bit manipulation?

► Signed integer

► Un-signed integer

► Signed double

► Un-signed double

Question No: 12 ( Marks: 1 ) - Please choose one

Structure is a collection of ______________ under a single name.

Page 461: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Only Functions

► Only Variables

► Both Functions and Variables

► None of the given options

Question No: 13 ( Marks: 1 ) - Please choose one

Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

► new int(10) ;

► new int[10] ;

Page 462: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► int new(10) ;

► int new[10];

Question No: 14 ( Marks: 1 ) - Please choose one

Unary operator implemented as member function takes ____ arguments whereas non-member function takes _____ arguments.

► One, zero

► Zero, one

► One, two

► Two, one

Question No: 15 ( Marks: 1 ) - Please choose one

Page 463: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

The first parameter of overloaded stream insertion operator is _________ where second parameter is _______ http://vustudents.ning.com

► input stream, object of class

► object of class, output stream

► output stream, object of class

► object of class, input stream

Question No: 16 ( Marks: 1 ) - Please choose one

We can also do conditional compilation with preprocessor directives.

► True

► False

Question No: 17 ( Marks: 1 ) - Please choose one

Page 464: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMIf a symbolic constant has been defined, it will be an error to define it again.

► True

► False

Question No: 18 ( Marks: 1 ) - Please choose one

While calling function, the arguments are assigned to the parameters from _____________.

► left to right.

► right to left

► no specific order is followed

► none of the given options.

Question No: 19 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

Page 465: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► nested

► overloaded

► none of the given options.

Question No: 20 ( Marks: 1 ) - Please choose one

If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.

► cannot be replaced

► None of the given options

► Remain constant.

► can be changed by some operation

Question No: 21 ( Marks: 1 ) - Please choose one

Page 466: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Assignment operator is -------------------------associative. http://vustudents.ning.com

► right

► left

► binary

► unary

Question No: 22 ( Marks: 1 ) - Please choose one

If text is a pointer of class String then what is meant by the following statement?

text = new String [5];

► Creates an array of 5 string objects statically

Page 467: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Creates an array of 5 string objects dynamically

► Creates an array of pointers to string

► Creates a string Object

Question No: 23 ( Marks: 1 ) - Please choose one

The return type of the operator function for << operator is __________.

► class for which we overload operator

► reference of ostream class (ostream&)

► reference of istream class (istream&)

► void

Page 468: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 24 ( Marks: 1 ) - Please choose one

The code is written to __________ the program.

► implement

► design

► analysis

► none of the given options.

Question No: 25 ( Marks: 1 ) - Please choose one

Memory allocated at run time is a system resource and it is the responsibility of _____ to de-allocate the memory. http://vustudents.ning.com

Page 469: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► System

► Programmer

► User of program

► None of given options

Question No: 26 ( Marks: 1 ) - Please choose one

Templates are not type safe.

► true

► false

Question No: 27 ( Marks: 2 )

Give the general syntax of class template.

Answer:

Syntax of class template:

Page 470: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMtemplate <class T>

class class-name()

{

definition of class

};

Question No: 28 ( Marks: 2 )

What is difference between endl and \n?

Answer:

The difference between endl and \n is that endl is use to start a new line for the next row

And \n is a new line character.

Question No: 29 ( Marks: 2 )

What is the this pointer? Give an example of its use.

Answer:

This pointer is use to points to the current object in programming.

Question No: 30 ( Marks: 2 )

Page 471: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Identify each of the following as function call, function definition and function declaration.

9. int func(int num1, int num2); Function call:

Function ; Function definition: Integer; Function declaration: Num1

and Num2

10. int func(int, int); Function call:

Function ; Function definition: Integer; Function declaration: integers

11. func(5, 6) ;

Function call:

Function ; Function definition: numbers; Function declaration: 5&6

12. int func(int num1, int num2){} Function call:

Function ; Function definition: Integer; Function declaration: Num1 and Num2 from

user

Question No: 31 ( Marks: 3 )

Consider the following code segment. What will be the output of the following code segment?

Page 472: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMclass class1{

public:

class class2{

public:

class2(){

cout << “Calling default constructor of class2\n” ;

}

};

class1(){

cout << “Calling default constructor of class1\n” ;

}

} ;

main(){

class1::class2 obj1;

class1 obj2 ;

}

Question No: 32 ( Marks: 3 )

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Answer:

No, we cannot define two functions as func(intx, inty) func(int &x, int&y) because it’s give an error function not initializing.

Page 473: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 33 ( Marks: 3 )

What happens when we use new and delete operator?

Answer:

When we use new operator to create objects the memory space is allocated for the object and then its constructor is called. Similarly, when we use delete operator with our objects, the destructor is called for the object before deallocating the storage to the object.

Question No: 34 ( Marks: 5 )

What is the difference between function overloading and operator overloading?

Answer:

Difference b/w function overloading and operator overloading is:

In function overloading, the functions have the same name but differ either by the number of arguments or the type of the arguments.

Operator overloading is to allow the same operator to be bound to more than one implementation, depending on the types of the operands.

Page 474: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 35 ( Marks: 5 )

Why the first parameter of operator function for << operator must be passed by reference?

Answer:

Operator<<'s first parameter must be an ostream passed by reference. Its second parameter, the IntList that is printed, does not have to be passed as a const-reference parameter; however it is more efficient to pass it by reference than by value (since that avoids a call to the copy constructor), and it should not be modified by operator<<, so it should be a const reference parameter

Question No: 36 ( Marks: 5 )

Read the given below code and explain what task is being performed by this function

Matrix :: Matrix ( int row , int col )

{

numRows = row ;

numCols = col ;

elements = new ( double * ) [ numRows ] ;

for ( int i = 0 ; i < numRows ; i ++ )

{

elements [ i ] = new double [ numCols ] ;

for ( int j = 0 ; j < numCols ; j ++ )

elements [ i ] [ j ] = 0.0 ;

Page 475: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM }

}

Hint : This function belong to a matrix class, having

Number of Rows = numRows

Number of Columns = numCols

Answer:

In this code the matrix function is defined, it get the number of rows from the user and create the row of matrix and then get the columns from the user and create the columns. The New is showing for creating more array space for the data which user enters. The elements [i][j] will print the data in matrix form. http://vustudents.ning.com

FINALTERM EXAMINATION

Spring 2010

CS201- Introduction to Programming

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 1 ( Marks: 1 ) - Please choose one

In if structure the block of statements is executed only,

► When the condition is false

► When it contain arithmetic operators

► When it contain logical operators

Page 476: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► When the condition is true

Question No: 2 ( Marks: 1 ) - Please choose one _ALIMS_ (http://www.virtualinspire.com)

Header file: fstream.h includes the definition of the stream classes __________.

► ifstream, fstream, cout

► ifstream, fstream, ofstream

► fstream, cin, cout

► None of the above

Question No: 3 ( Marks: 1 ) - Please choose one

To access the data members of structure _______ is used.

► dot operator (.)

_ALIMS_ (http://www.virtualinspire.com)

► * operator

Page 477: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► operator

► None of given.

Question No: 4 ( Marks: 1 ) - Please choose one

eof( ), bad( ), good( ), clear( ) all are manipulators.

► True

► False

_ALIMS_ (http://www.virtualinspire.com)

Question No: 5 ( Marks: 1 ) - Please choose one

Which kind of functions can access private member variables of a class?

Page 478: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Friend functions of the class

► Private member functions of the class

► Public member functions of the class

► Friend, private and public functions

_ALIMS_ (http://www.virtualinspire.com)

Question No: 6 ( Marks: 1 ) - Please choose one

The return type of operator function must always be void.

► True

► False

Question No: 7 ( Marks: 1 ) - Please choose one

Friend function of a class is ______________ . Solved by: _ALIMS_ (http://www.virtualinspire.com)

► Member function

Page 479: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Non-member function

► Private function

► Public function

Question No: 8 ( Marks: 1 ) - Please choose one

Function implementation of friend function must be defined outside the class.

Solved by: _ALIMS_ (http://www.virtualinspire.com)

► True

► False

Question No: 9 ( Marks: 1 ) - Please choose one

The normal source of cin object is,

Page 480: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► File

► Disk

► Keyboard

► RAM

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 10 ( Marks: 1 ) - Please choose one

Which of the following is correct way to initialize a variable x of int type with value 10?

► int x ; x = 10;

► int x = 10;

► int x, x = 10;

Page 481: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► x = 10;

Question No: 11 ( Marks: 1 ) - Please choose one

Consider the following code segment. What will be the output of the following program?

int func(int) ;

int num = 10 ;

int main(){

int num ;

num = 5 ;

cout << num ;

cout << func(num) ;

}

int func(int x){

return num ;

}

► 5, 5

► 10, 5

Page 482: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► 5, 10

► 10, 10

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 12 ( Marks: 1 ) - Please choose one

With template function, the compiler automatically detects the passed data and generates a new copy of function using passed data.

► True

► False

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 13 ( Marks: 1 ) - Please choose one

What will be the correct syntax to declare two-dimensional array of float data type?

Page 483: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► float arr{2}{2} ;

► float arr[2][2] ;

► float arr[2,2] ;

► float[2][2] arr ;

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 14 ( Marks: 1 ) - Please choose one

The first parameter of operator function for << operator,

► Must be passed by value

► Must be passed by reference

Page 484: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Can be passed by value or reference

► Must be object of class

Question No: 15 ( Marks: 1 ) - Please choose one

Heap is constantly changing in size.

► True

► False

Question No: 16 ( Marks: 1 ) - Please choose one

While calling function, the arguments are assigned to the parameters from _____________.

► left to right.

► right to left

► no specific order is followed

Page 485: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► none of the given options.

Question No: 17 ( Marks: 1 ) - Please choose one

Classes defined inside other classes are called ________ classes

► looped

► nested

► overloaded

► none of the given options.

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 18 ( Marks: 1 ) - Please choose one

If we define an identifier with the statement #define PI 3.1415926 then during the execution of the program the value of PI __________.

► can not be replaced

Page 486: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► None of the given options

► Remain constant.

► can be changed by some operation

Question No: 19 ( Marks: 1 ) - Please choose one

Which value is returned by the destructor of a class?

► A pointer to the class.

► An object of the class.

► A status code determining whether the class was destructed correctly

► Destructors do not return a value.

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Page 487: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 20 ( Marks: 1 ) - Please choose one

Every class contains _______________.

► Constructor

► Destructor

► Both a constructor and a destructor

► None of the given options

Question No: 21 ( Marks: 1 ) - Please choose one

A template function must have

► One or more than one arguments

Page 488: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Only one argument

► Zero arguments

► None of the given options

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 22 ( Marks: 1 ) - Please choose one

Structured Query Language is used for ______________

► Databases Management

► Networks

► Writing Operating System

► none of the given options

Question No: 23 ( Marks: 1 ) - Please choose one

Page 489: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

When a call to a user-defined function finishes, the variable defined inside the function is still in existence.

► True

► False

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 24 ( Marks: 1 ) - Please choose one

The precedence of an operator can be changed through operator overloading.

► True

► False

Question No: 25 ( Marks: 1 ) - Please choose one

A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

► Write a separate class to handle each

Page 490: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► Use templates

► Use strings to store all types

► None of the given options

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 26 ( Marks: 1 ) - Please choose one

"delete" operator is used to return memory to free store, which is allocated by the "new" operator.

► True

► False

Question No: 27 ( Marks: 2 )

What is the difference between switch statement and if statement.

Question No: 28 ( Marks: 2 )

Page 491: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

How can we initialize data members of contained object at construction time?

Question No: 29 ( Marks: 2 )

How the data members of a class are initialized with meaningful values?

Question No: 30 ( Marks: 2 )

Can we overload new and delete operators?

Question No: 31 ( Marks: 3 )

What will be the output of following functions if we call these functions three times?

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

Page 492: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

ANSWER:

1

1

1

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 32 ( Marks: 3 )

What is the keyword ‘this’ and what are the uses of ‘this’ pointer?

Question No: 33 ( Marks: 3 )

Suppose an object of class A is declared as data member of class B.

(i) The constructor of which class will be called first?

(ii) The destructor of which class will be called first?

Page 493: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 34 ( Marks: 5 )

Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.

Question No: 35 ( Marks: 5 )

Write down the disadvantages of the templates.

Question No: 36 ( Marks: 5 )

Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.

Note: Display all data with in a particular width and the empty space should be filled with character x

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

xxxxx20000

xxxxx30000

xxxxx60000

ANSWER:

#include <iostream.h>

#include <iomanip.h>

Page 494: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMint main()

{

int salaries[4];

cout<<"\n solved by _ALIMS_ (http://www.virtualinspire.com)" <<endl;

cout<<"enter salary";

cin>>salaries[0];

cout<<"enter salary";

cin>>salaries[1];

cout<<"enter salary";

cin>>salaries[2];

cout<<"enter salary";

cin>>salaries[3];

cout<<"enter salary";

cin>>salaries[4];

cout<<"\n solved by _ALIMS_ (http://www.virtualinspire.com)" <<endl;

cout<<setfill('x') <<setw(10) <<salaries[0] <<endl;

cout<<setfill('x') <<setw(10) <<salaries[1] <<endl;

cout<<setfill('x') <<setw(10) <<salaries[2] <<endl;

cout<<setfill('x') <<setw(10) <<salaries[3] <<endl;

cout<<setfill('x') <<setw(10) <<salaries[4] <<endl;

system("pause");

}

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Logon to http://www.virtualinspire.com to

Page 495: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM Download past papers of every subject Get idea solution of assignments, gdb, projects etc. Chat with your fellows To entertainment

And many more…..

FINALTERM EXAMINATION

Spring 2010

CS201- Introduction to Programming

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: (Marks: 1) - Please choose one

If it is required to copy an array to another array then,

► Both arrays must be of the same size and data type

► Both arrays may be of different size

► Both arrays may be of different data type

► Both arrays may be of different size and type

Question No: 2(Marks: 1) - Please choose one

Page 496: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Dealing with structures and functions passing by reference is the most economical method

► True

► False

Question No: 3 ( Marks: 1 ) - Please choose one

Solved by: _ALIMS_ (http://www.virtualinspire.com)

eof( ), bad( ), good( ), clear( ) all are manipulators.

► True

► False

Question No: 4 (Marks: 1) - Please choose one

Overloaded new operator function takes parameter of type size_t and returns

► void (nothing)

Page 497: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► void pointer

► object pointer

► int pointer

Question No: 5 ( Marks: 1 ) - Please choose one

When new operator is overloaded at global level then corresponding built-in new operator will not be visible to whole of the program.

► True

► False

Question No: 6 ( Marks: 1 ) - Please choose one

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Page 498: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMIf there is more than one statement in the block of a for loop, which of the following must be placed at the beginning and the ending of the loop block?

► parentheses ( )

► braces { }

► brackets [ ]

► arrows < >

Question No: 7 ( Marks: 1 ) - Please choose one

The return type of a function that do not return any value must be ________

► float

► int

► void

► double

Question No: 8 ( Marks: 1 ) - Please choose one

Page 499: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

UNIX has been developed in ________ language.

► JAVA

► B

► C

► FORTRAN

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 9 ( Marks: 1 ) - Please choose one

Like member functions, ______ can also access the private data members of a class.

► Non-member functions

► Friend functions

► Any function outside class

Page 500: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► None of the given options

Question No: 10 ( Marks: 1 ) - Please choose one

Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition.

► It can be declared anywhere in class as these are not affected by the public and private keywords.

Question No: 11 ( Marks: 1 ) - Please choose one

The operator function overloaded for an Assignment operator (=) must be

► Non-member function of class

► Member function of class

► Friend function of class

Page 501: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► None of the given options

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 12 ( Marks: 1 ) - Please choose one

The endl and flush are _______

► Functions

► Operators

► Manipulators

► Objects

Question No: 13 ( Marks: 1 ) - Please choose one

If a symbolic constant has been defined, it will be an error to define it again.

Page 502: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► True

► False

Question No: 14 ( Marks: 1 ) - Please choose one

The operator used for casting, in C, is standard ________ operator.

► none of the given options.

► cast not sure because standard tou “static_cast” hota hai

► cost

► const

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 15 ( Marks: 1 ) - Please choose one

Constructors can not be overloaded like ordinary functions.

► True

Page 503: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM ► False

Question No: 16 ( Marks: 1 ) - Please choose one

Which of the following function call is correct for the function prototype?

defaultParameters ( int a, int b = 7, char z = ‘*’ );

► defaultParameters (5);

► defaultParameters (5, ‘8’);

► defaultParameters (6, ‘#’);

► defaultParameters (0, 0, ‘*’, 0);

Question No: 17 ( Marks: 1 ) - Please choose one

When an operator function is defined as member function for a binary Plus (+) operator then the number of argument it take is/are.

Page 504: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Solved by: _ALIMS_ (http://www.virtualinspire.com)

► Zero

► One

► Two

► N arguments

Question No: 18 ( Marks: 1 ) - Please choose one

We can not define a variable of user-defined data type in the class.

► True

► False

Question No: 19 ( Marks: 1 ) - Please choose one

Page 505: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

When an object of a class is defined inside an other class then,

Solved by: _ALIMS_ (http://www.virtualinspire.com)

► Constructor of enclosing class will be called first

► Constructor of inner object will be called first

► Constructor and Destructor will be called simultaneously

► None of the given options

Question No: 20 ( Marks: 1 ) - Please choose one

The appropriate data type to store the number of rows and colums of the matrix is____________.

Solved by: _ALIMS_ (http://www.virtualinspire.com)

► float

► int

Page 506: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► char

► none of the given options.

Question No: 21 ( Marks: 1 ) - Please choose one

Class is a user defined___________.

► data type

► memory referee

► value

► none of the given options.

Question No: 22 ( Marks: 1 ) - Please choose one

Solved by: _ALIMS_ (http://www.virtualinspire.com)

A pointer variable can be,

► Decremented

► Incremented

Page 507: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

► Multiplied

► Both Decremented and Decremented

Question No: 23 ( Marks: 1 ) - Please choose one

NULL value has been defined in ______ and _________ header files.

► strings.h and iostream.h

► ctype.h and conio.c

► conio.c and conio.h

► stdlib.h and stddef.h

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 24 ( Marks: 1 ) - Please choose one

Page 508: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

A Matrix can be composed of ints, floats or doubles as their elements. Best way is to handle this , _______________

► Write a separate class to handle each

► Use templates

► Use strings to store all types

► None of the given options

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 25 ( Marks: 1 ) - Please choose one

"setprecision" manipulator will set

► The number of digits after the decimal point

► The number of digits before the decimal point

► The number of digits in a number

► None of the given options

Page 509: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 26 ( Marks: 1 ) - Please choose one

Which of the following option will be true, if we overload "-=" operator?

Solved by: _ALIMS_ (http://www.virtualinspire.com)

► only - operator needs to be overloaded

► Minus (-) and = operators need to be overloaded

► the -= operator need to be overloaded explicitly (not sure)

► the - and = operators need to be overloaded implicitly

Question No: 27 ( Marks: 2 )

Suppose there is a template function ‘func’ having argument of type U and return type T. What will be the C++ syntax to call this function, passing a variable ‘x’ of type double and returning an int type?

template <class T>

T func(T val)

{

Page 510: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM return static_cast<int>(val);

}

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 28 ( Marks: 2 )

Which variable will be used in inner code block if we have the same names of variable at outer code block and inner code block?

Question No: 29 ( Marks: 2 )

What is the benefit of reference and where can we use it?

Question No: 30 ( Marks: 2 )

Write the C++ code for the declaration of overloaded stream insertion and stream extraction operator for the object d of type Date.

Date::Ostream & operator<<(ostream & a, Date d)

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Page 511: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 31 ( Marks: 3 )

What will be the output of following functions if we call these functions three times?

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Question No: 32 ( Marks: 3 )

Solved by: _ALIMS_ (http://www.virtualinspire.com)

If the requested memory is not available in the system then what does calloc/malloc and new operator return?

Question No: 33 ( Marks: 3 )

Page 512: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Suppose an object of class A is declared as data member of class B.

(i) The constructor of which class will be called first?

(ii) The destructor of which class will be called first?

Question No: 34 ( Marks: 5 )

What is difference between Unary and binary operators and how they can be overloaded?

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Question No: 35 ( Marks: 5 )

Suppose we have the following class.

class Matrix

{

private:

int Elements[3][3];

};

Write the operator function of stream extraction operator (>>) for this class.

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Page 513: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Question No: 36 ( Marks: 5 )

What is meant by user interface and class interface in C++ ? And what role a class interface can play in user interface [Marks 5]

Solved by: _ALIMS_ (http://www.virtualinspire.com)

Cs201 Current Final Term (20 Jul 2013 TO 31 Jul 2013) CS201 Current Final Paper..........!!!Question 1:Identify each of the following as system software and application software.LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, STUDENT INFORMATION (mark 5)Q2:Write the explanation of this program. See program from book pg no534. (mark5)Q3: Write a program which defines three variables of type double which store three different values including decimal points, using set precision manipulators to print all these values with different numbers of digits after the decimal number.(5)Q4 define static variable also explain life time of static variable? (3) Q5 what do you know about run time error? (3)Q6 what are limitation of the friendship between classes? (3)Q7: read the code and explain the functionality of this program? See page 535 for cod (3)Q8: write down the meanings of given below line of code, where m is an object of the class matrix. If (& m = this) (2)Q9: write the general syntax for the definition of the user defined function? (2)Q10: what is the source and destination of cin?(2)Q11: write the general syntax of allocation memory dynamically to an array using new operator? (2)

----------------40 mcqssubjective two questions 2,2 marks3 questions 3,3 marks program coding 3 questions 5,5 marks2 program class sa and 2 program matrix sa 5 marks walyonly one question of two marks is without program and codingand midd sa kuch nae aaya

Page 514: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM-----------------mcq's 40subjective 12Mcq's were mostlly related to classess, templates, operator overloading and functions.1.Suppose an object has a pointer type as data member. (5 marks)a) What happen when we assign that object to nother object using default assignment operator?b)What hppens if we delete one object in the above situation?2. Write a progrm whhich will open a file named "this.txt" and then using write() function, writes the string "Introduction to programming" (5)3. class matrix (5 marks){private:int Elements [3][3];};Write the operator function of insertion to this clss.4. what happens when e use new and delete operators>? (2 marks)bus ye yad hy!

-----------------CS201 Current Final Paper..........!!!

Long Question

Q1: Given is the code for a class named Coordinate.(10)

class Coordinate{

private:

int X, Y;

public:

Coordinate(){X = 0;Y = 0;}

Page 515: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMclass Coordinate{friend void Displaycord();private:

int X, Y;

public:

Coordinate(){X = 0;Y = 0;}

};

Q2: Write a program that defines a Template function named Square () which finds the square of a number and then return it. Define one variable of type int in main function and then call Square function on that. (10)

Program output should look like this:

Enter an integer value to find its Square: 6Square of integer number is: 36

Q3: Why is it necessary to initialize a static object as the time of creation and how it is initialized ?(5)

Q4:Transform the following If-else statement into switch statement.(5)

if (x == 5)...else if (x == 10 || x == 11)...else if (x ==

Page 516: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM...else if (x == 9)...else...

Q5:Read the given code and explain code functionality. (3)

Matrix :: Matrix ( const Matrix & m ){numRows = m.numRows ;numCols = m.numCols ;elements = new ( double * ) [ numRows ] ;for ( int i = 0 ; i < numRows ; i ++ ){elements [ i ] = new double [ numCols ] ;for ( int j = 0 ; j < numCols ; j ++ )elements [ i ] [ j ] = m.elements [ i ] [ j ] ;}}

Hint : This function belong to a matrix class, having Number of Rows = numRowsNumber of Columns = numCols

Q6: In the following code;(3)

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

double j;

getche();

}

Is double j; an example of global variable or local variable ? Why ?

Page 517: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ7: Identify and correct the error in the given code segment.(3)main(){int val = 30;int &ref = val;

&ref = &ref++;

}

Q8: Determine the output of the given program code.(2)

for (int I = 1; i<5; i++){if ( I == 3)continue; cout I endl ;}

Q9: What is the output of following code snippet.(2)

// class templates#include <iostream>using namespace std;

template <class T>class mypair {T a, b;public:mypair (T first, T second){a=first; b=second;}T getmax ();};

template <class T>T mypair<T>::getmax (){T retval;retval = a>b? a : b;return retval;}int main () {mypair <int> myobject (100, 75);cout myobject.getmax();

Page 518: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMsystem("pause");}

Q10: How can we declare and initialize static data member of a class?(2)

Q11: When memory is allocated dynamically using new operator within the constructor of class then what is an appropriate place to de-allocate the memory?(2)

Q11: Write down the output of the following code segment? (2)

int x[10] = {0,1, 2, 3, 4, 5, 6, 7, 8, 9};int *xptr;xptr = &x[5];cout *(xptr++) + 1;

MCQSequence of event(s) when allocating memory using new operator is ____________.

Assignment operator is ________ associative

To avoid dangling reference, always return _________.Which value is returned by the destructor of a class?The functions used for dynamic memory allocation return pointer of type ______.The function free() returns back the allocated memory got through calloc and malloc to _____ .At the time of linking, library functions are available in the ________form.Which of the following is NOT a preprocessor directive?When we define an array of objects then ___________.A constructor will create a new object with a full copy of the other object, this type of copy is known as ___________.When an object of a class is defined inside another class then ______________.Every stream has _________._________in the following options is not a method of output stream.To perform manipulation with input/output, we have to include ________ header fileA class whose object is contained as const object, must have ____________.By using objects as class members, _________ is achievedThe operator function will be implemented as _____, if both objects (obj1, obj2) are passed as arguments to - operator in the statement given below. obj3 = obj1 - obj2;

ANSI stands for _________.A template function must have at least _______________ generic data type.

Page 519: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMWhich of the following is true about operator overloading?1-The operator works with only objects of the class in which it is overloaded.2-The operator works with objects and primitive data types regardless of parameters.3-The operator works with both objects and primitive data types depending on parameters.4-The operator must have a constant and an object as parameters.Operator overloading can be performed through__________________.The pointer returned by the new operator points to ___________ of memory chunks allocated by the new operator.

The return type of the operator function for operator is __________.

What will be the output of following statement?

cout setfill(‘0’) setw(7) 128 ;

The operator used for casting in C is standard ________ operator.

Consider the following code segment.

struct intorDouble{Int ival;Double charvar;};

main(){ intorDouble VAZ;int size ;size = sizeof(VAZ);}

What will be the value of variable size if integer occupies 4 bytes and double occupies 8 bytes?

The file-open mode __________ is used to open a file for output and move to the end of the file.

In the statement int &x = y; compiler ______.Which header file must be included to use the functions tolower() and toupper()?Which one of the following functions is included in stdlib.h header file?For one byte, there are _____ combinations of values which can be stored in computer.Which of the following statement is best regarding declaration of friend function?

Page 520: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAnswer- It can be declared anywhere in class as these are not affected by the public and private keywords.The operator used for dereferencing the elements of an array using a pointer is _________.The __________ statement forces the immediate next iteration of the loop.A function is a block of statements (code) that can be __________.Which of the following values are used in C/C++ to represent true and false?Ans- 1 and 0The dynamic memory allocation uses memory from the ____________.In C/C++, null character is represented as ________________.

Another paper

what is stack un winding?

what is templet?

what is relation ship between person and teacher?? diagaram thi?

what is iterator?

what is types of iterator?

long Question horse is a good pet animal describe by oop that about horse?

give C++ code to simplyfiy that oop is case senstive

(please friends dua lazmi karna mery liay mera paper normal howa hai so ap ki dua ki b zaroort hai)

give c++ code and define a templete fuction

c++ code that eror corect karna tha .....templete ka tha??

eror handling ki type this??

aur mcqs half past say half new thay...

-----------------

My CS201 Current Paper40 mcqs …..only 20% were from past papers:Subjective Qs were almost 90% from past papers … Some Qs which I remember

Page 521: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM1) What happens when we use new and delete operator? (from past papers)2) Question: Suppose an object of class A is declared as data member of class B.(i) The constructor of which class will be called first? Answer A(ii) The destructor of which class will be called first? Answer B(from past papers)3) Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column. Note: Display all data with in a particular width and the empty space should be filled with character x Output should be displayed as given below: (from past papers)xxxxxx1000xxxxxx1500xxxxx20000xxxxx30000xxxxx600004)Overflow condition is used in repetition structure at which point it oocurs?5)Is it possible to overlad operation for primitive data types.justify yor ans6) Let we have a class,class String{private: char buf[25];}; Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment(from past paper)7)What happen when an object is assigned to another object without overloading an assignment operator?8)Write function name sum which accepts an integer as a argument in the function use a static variable to add the value passed to it as argument with each function call is the endl display the sum of value pass an argument:9) Question No: 40 ( Marks: 10 ) Write a program which consists of two classes, Date and Person. Date class should contain three data members day , month , year and setter and getter function for these data members. Date class should also contain showdate() member function to display date. Person class should contain three data members Name, Address, and Bday , where Name and Address are char pointer while Bday(Date of birth) is of type Date, Person class should further contain two member functions Display() and setdate(). In main program Create an object of Class person and call the member functions with it..(from Past Papers.

-----------------

CS201 Current Final Paper

Q1: (Marks 5) Write a program that defines a Template function named Add () which adds two

Page 522: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMnumbers returns Sum. Define two variables of int type in the main and then call Add () function on that.Output should be like this:

Adding numbers (don’t remember here what was written)Enter first number:12Enter second number:25Sum=37

Q2: (Marks 5) Find the error in the program and then remove this. (Don’t remember the coding)

Q3: (Marks 5) Given the class Data

main(){class Date{private:int day, month, year;public:day=01;month=10;year=2010}……………..Dont remember the lines……………..……………..

}

Use the dynamically allocation (don’t remember what)In the last also free memory allocation.

Q4: (Marks 3) Why goto is not recommended to use in programmes?Q5: (Marks 2) Write two types of data conversions used for used defined data types.Q6: (Marks 2) Write the following using logical operators:1. number is both multiple of 4 and 62. number is even but not 83. answer is either N or n4. don’t remember this

Page 523: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ7: (Marks 3) Write the output of the following if “int 5” is passed to the variable:Template <class T);T reciprocal (T x);{return (1/x);}(it was like something. Don’t remember exactly)

Q8: (Marks 3): How can we determine that dynamically memory allocation has been set successfully or not is C++ using the new operator?

-----------------

CS201 Today Final Paper1- Write statement for constant pointer to integer and also pointer to constant integer.2- Get input from user for 2 by 4 matrix and display it.3- For given code of class rectangle, write code for copying constructor.4- How will you identify if a declared variable if reference of address variable.Some other questions were,For given code having new operator, write delete.For given code for converting Celsius to Fahrenheit, write equivalent template.

-----------------MY Long question:Q1: What are two types of conversion for user-define datatype?Q2: What will be the output of temple if pass value 5 of integer typetemplate < class T>T reciprocal (T x ) {return 1 / x; }Q2: If a structure is passed to function what will default calling conversion (by value or by reference)Q3: Write program to print floatg point 1.234 in a 9 digit with precidiog zeroQ4: What is out put of the programChar name[10] = “Asad”;Char msg[10] = “Yaqoob”;strncat(msg,name,3);

Q5 : write simple program that use getline() of cin obj for reading string of 30 char from keyboard when press enter to output;

Page 524: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ7 : output of the programint n = -77;cou.width(6);cout right n endl;cout.width(6);cout left n endl;

Q6 : use templeate function square and find the number then return define variable type double in main function…Q8: conweight into pound forumul give using function solve quesioinQ9: write program that declar 5 int and display the output followingxxxxxx1000etcQ10: question from operator overloading date ke overloading krne the…mcqsin _______ first pointer store the address of the some other pointer which contain the address of any variablewhen call to fuction statement is encountered ____________to avoid dangling pointer always return ________________

-------------Question 1: Identify each of the following as system software and application software. LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, STUDENT INFORMATION (mark 5) Q2: Write the explanation of this program. See program from book pg no534. (mark5) Q3: Write a program which defines three variables of type double which store three different values including decimal points, using set precision manipulators to print all these values with different numbers of digits after the decimal number.(5) Q4 define static variable also explain life time of static variable? (3) Q5 what do you know about run time error? (3) Q6 what are limitation of the friendship between classes? (3) Q7: read the code and explain the functionality of this program? See page 535 for cod (3) Q8: write down the meanings of given below line of code, where m is an object of the class matrix. If (& m = this) (2) Q9: write the general syntax for the definition of the user defined function? (2) Q10: what is the source and destination of cin?(2) Q11: write the general syntax of allocation memory dynamically to an array using new operator? (2)

Page 525: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

1.what is diffrent between pointer and variable? 2.What is difference between Unary and binary operators and how they can be overloaded? 3.how many types of templates? 4.What will be the output of following function if we call this function by passing int 5? template T reciprocal(T x) {return (1/x); } 5. Identify the errors in the following member operator function and also correct them. math * operator(math m); math * operator (math m) { math temp; temp.number= number * number; return number; Answer: The errors are in the arguments of the member operation function and also in the body of operator member function. Correct function should be math *operator(math *m); math *operator (math *m) { math temp; temp = m; temp.number= number * number; return temp.number;

Q# 6 what are limitation of the friendship between classes? (marks3) Q# 7 Read the code and explain the functionality of this program? See page 535 for cod (marks3]Q# 8 write down the meanings of given below line of code, where m is an object of the class matrix. If (& m = this) (marks 2) Q# 9 write the general syntax for the definition of the user defined function? (marks2) Q# 10 what is the source and destination of cin?(marks2 Q# 11 write the general syntax of allocation memory dynamically to an array using new operator? (marks2)

Q3: Write a program which defines three variables of type double which store three different values including decimal points, using set precision manipulators to print all these values with different numbers of digits after the decimal number.(5) Q4 define static variable also explain life time of static variable? (3) Q5 what do you know about run time error? (3) Q6 what are limitation of the friendship between classes? (3) Q7: read the code and explain the functionality of this program? See page 535 for cod (3) Q8: write down the meanings of given below line of code, where m is an object of the class matrix. If (& m = this) (2) Q9: write the general syntax for the definition of the user defined function? (2)

Page 526: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ10: what is the source and destination of cin?(2) Q11: write the general syntax of allocation memory dynamically to an array using new operator? (2)

Q: define buffer?explain its usage? 5 MARKS Q: why binary search algorithm is efficient than linear search algorithm? 5 marks Q: perator function ka syntex(3 marks) Q: post increment and pre increment k syntex btana thay(2 marks) Q:what is language translator?(2 marks) Q: write somethiing something about testing in designing program? 3 MARKS Q:Read the given below code and explain what task is being performed by this function 5 MARKS

Matrix :: Matrix ( int row , int col ) { numRows = row ; numCols = col ; elements = new ( double * ) [ numRows ] ; for ( int i = 0 ; i < numRows ; i ++ ) { elements [ i ] = new double [ numCols ] ; for ( int j = 0 ; j < numCols ; j ++ ) elements [ i ] [ j ] = 0.0 ; } } Hint : This function belong to a matrix class, having Number of Rows = numRows Number of Columns = numCols Which one (copy constructor or assignment operator) will be called in each of the following code segment? 1) Matrix m1 (m2); 2) Matrix m1, m2; m1 = m2; 3) Matrix m1 = m2;

-------------

CS201 Current Final Paper........!!!Question 1:Identify each of the following as system software and application software.LINUX, DISK CLEANUP, WORD PROCESSOR, WINDOWS, STUDENT INFORMATION (mark 5)Q2:Write the explanation of this program. See program from book pg no534. (mark5)Q3: Write a program which defines three variables of type double which store three different values including decimal points, using set precision manipulators to print all these values with different numbers of digits after the decimal number.(5)Q4 define static variable also explain life time of static variable? (3)

Page 527: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ5 what do you know about run time error? (3)Q6 what are limitation of the friendship between classes? (3)Q7: read the code and explain the functionality of this program? See page 535 for cod (3)Q8: write down the meanings of given below line of code, where m is an object of the class matrix. If (& m = this) (2)Q9: write the general syntax for the definition of the user defined function? (2)Q10: what is the source and destination of cin?(2)Q11: write the general syntax of allocation memory dynamically to an array using new operator? (2)Shah Baba Jee Ka Lucky Guess of CS-201 For All for Final Term Ppr...,

Assalam-o-Alykum pyaare logo...,

jisko kch nhi ata wo sirf ye Q kr ly,InshaAllah fittest ppr ho jayga...

1) Defination of Manipulators [2 ya 3 numb]2)Defination of 'if' and 'switch' Statement [3 ya 5 marks]3)Is It possible to overload new and delete operators [2 ya 3 marks]4)Defination of 'endl' and '\n' [2 ya 3 marks]5)Defination od Code Optimization [2 ya 3 marks]6)Why 'this' is used [2 marks]7)Static veriable [3 ya 5 marks]8)Unary and Binary Operators [3 ya 5 marks]9)Advantages and Dis-advantages of templates [5 ya 10 marks]10)Defination of Function overloading and Operator overloading [5 ya 10 marks]11)Defination of memory leak [2 ya 3 marks]

ye sb Q 2 ya 3 lines k hyn...,ek baar aazmao aur kamyaab ho jao to Duwaon myn yaad rkhna n comments dy k btana k ye Q ay ya nahi..., CS201 some current Final Term Papers 2013 February 20, 20131)Write a simple program, which will get a stream of character and output the stream of characters using cin and cout.2) Find the error in following syntax,while(i<=100)sum+=i*i;. class String{char buf [25];};Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment.

Page 528: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM3).make a program using template function select two floating point number and add them with add() function.(n February 20, 2013 at 4:00pmthere are some current questions .Write a program which consists of two classes, Date and Person. Date class should contain three data members day , month , year and setter and getter function for these data members. Date class should also contain showdate() member function to display date. Person class should contain three data members Name, Address, and Bday , where Name and Address are char pointer while Bday(Date of birth) is of type Date, Person class should further contain two member functions Display() and setdate(). In main program Create an object of Class person and call the member functions with it..February 20, 2013 at 4:28pmMY CS201 Current Paper:20-2-2013Total 52 Qs40 mcqs …..only 20% were from past papers:Subjective Qs were almost 90% from past papers … Some Qs which I remember1) What happens when we use new and delete operator? (from past papers)2) Question: Suppose an object of class A is declared as data member of class B.(i) The constructor of which class will be called first? Answer A(ii) The destructor of which class will be called first? Answer B(from past papers)3) Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column. Note: Display all data with in a particular width and the empty space should be filled with character x Output should be displayed as given below: (from past papers)xxxxxx1000xxxxxx1500xxxxx20000xxxxx30000xxxxx600004)Overflow condition is used in repetition structure at which point it oocurs?5)Is it possible to overlad operation for primitive data types.justify yor ans6) Let we have a class,class String{private: char buf[25];}; Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment(from past paper)

Page 529: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM7)What happen when an object is assigned to another object without overloading an assignment operator?8)Write function name sum which accepts an integer as a argument in the function use a static variable to add the value passed to it as argument with each function call is the endl display the sum of value pass an argument:9) Question No: 40 ( Marks: 10 ) Write a program which consists of two classes, Date and Person. Date class should contain three data members day , month , year and setter and getter function for these data members. Date class should also contain showdate() member function to display date.Person class should contain three data members Name, Address, and Bday , where Name and Address are char pointer while Bday(Date of birth) is of type Date, Person class should further contain two member functions Display() and setdate().In main program Create an object of Class person and call the member functions with it..(from Past Papers.

Today Final Term Paper Fall 2012On 21 Feb 2013Total Questions: 52Total Marks: 80Total MCQs: 40 (Each of 1 Mark)Total Short Questions: 4 (Each of 2 Mark)Total Short Questions: 4 (Each of 3 Mark)Total Long Questions: 4 (Each of 5 Mark)CS201

Q1: (Marks 5) Write a program that defines a Template function named Add () which adds two numbers returns Sum. Define two variables of int type in the main and then call Add () function on that.Output should be like this:

Adding numbers (don’t remember here what was written)Enter first number:12Enter second number:25Sum=37

Q2: (Marks 5) Find the error in the program and then remove this. (Don’t remember the coding)

Q3: (Marks 5) Given the class Data

Page 530: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMmain(){class Date{private:int day, month, year;public:day=01;month=10;year=2010}……………..Dont remember the lines……………..……………..

}

Use the dynamically allocation (don’t remember what)In the last also free memory allocation.

Q4: (Marks 3) Why goto is not recommended to use in programmes?Q5: (Marks 2) Write two types of data conversions used for used defined data types.Q6: (Marks 2) Write the following using logical operators:1. number is both multiple of 4 and 62. number is even but not 83. answer is either N or n4. don’t remember this

Q7: (Marks 3) Write the output of the following if “int 5” is passed to the variable:Template <class T);T reciprocal (T x);{return (1/x);}(it was like something. Don’t remember exactly)

Q8: (Marks 3): How can we determine that dynamically memory allocation has been set successfully or not is C++ using the new operator?February 21, 2013 at 6:37pmTotal Questions: 52

Page 531: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMTotal Marks: 80Total MCQs: 40 (Each of 1 Mark)Total Short Questions: 4 (Each of 2 Mark)Total Short Questions: 4 (Each of 3 Mark)Total Long Questions: 4 (Each of 5 Mark)

Q.make a program using getline functionQ.make a program using template function select two floating point number and add them with add() function.(5)Q.make a program take salary and pay tax of 5% if salary is less than 10,000 and pay 7% if the salary is greater than 10,000 using switch function.Q.what are the function of new and delete ?Q.what are the stream insertion overloaded operators?Q Write a program which open a file name “this.txt” and then using write() function, writes the string “Introduction to Programming” at the end of the file.Q What will be the output of the program:Int x=10;Int&y=x;Y+=5;Coutx;Q A two dimensional array has 3 rows and 4 columns. Write down the syntax to initialize first element of all three rows of two dimensional array with value 2.Q How can we initialize an object which is created as constant data member in another class?Q An array delare int day[]={1,2,3,4,5,6,7}How many elements have?If the declaration ofarray is changed to int days[7]={1,2,3,4,5,6,7}How many elements have? CS201 Current pprs ( 22 feb to 6 march ) here ... !!!

Q.make a program using getline functionQ.make a program using template function select two floating point number and add them with add() function.(5)Q.make a program take salary and pay tax of 5% if salary is less than 10,000 and pay 7% if the salary is greater than 10,000 using switch function.Q.what are the function of new and delete ?Q.what are the stream insertion overloaded operators?

Q Write a program which open a file name “this.txt” and then using write() function, writes the string “Introduction to Programming” at the end of the file.

Page 532: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQ What will be the output of the program:Int x=10;Int&y=x;Y+=5;Coutx;Q A two dimensional array has 3 rows and 4 columns. Write down the syntax to initialize first element of all three rows of two dimensional array with value 2.Q How can we initialize an object which is created as constant data member in another class?Q An array delare int day[]={1,2,3,4,5,6,7}How many elements have?If the declaration ofarray is changed to int days[7]={1,2,3,4,5,6,7}How many elements have?

-------------------------------------------------------------

Q1: (Marks 5) Write a program that defines a Template function named Add () which adds two numbers returns Sum. Define two variables of int type in the main and then call Add () function on that.Output should be like this:

Adding numbers (don’t remember here what was written)Enter first number:12Enter second number:25Sum=37

Q2: (Marks 5) Find the error in the program and then remove this. (Don’t remember the coding)

Q3: (Marks 5) Given the class Data

main(){class Date{private:int day, month, year;public:day=01;month=10;year=2010}……………..Dont remember the lines……………..……………..

Page 533: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM}

Use the dynamically allocation (don’t remember what)In the last also free memory allocation.

Q4: (Marks 3) Why goto is not recommended to use in programmes?Q5: (Marks 2) Write two types of data conversions used for used defined data types.Q6: (Marks 2) Write the following using logical operators:1. number is both multiple of 4 and 62. number is even but not 83. answer is either N or n4. don’t remember this

Q7: (Marks 3) Write the output of the following if “int 5” is passed to the variable:Template <class T);T reciprocal (T x);{return (1/x);}(it was like something. Don’t remember exactly)

Q8: (Marks 3): How can we determine that dynamically memory allocation has been set successfully or not is C++ using the new operator? CS201 Introduction to Programming Lecture no.23 Discussions and Solution

Composed and solved by Z!NDAG! ,Alims,Nayab chaudhary,and WAJID MALIK E.mail: [email protected] Introduction to Programming Lecture no.23 Discussions and Solution

CS201 Introduction to ProgrammingBack to CS201 Introduction to Programming Discussions

Preprocessor Directives

Preprocessor directives, such as #define and #ifdef , are typically used to make source programs easy to change and easy to compile in different execution environments. Directives

Page 534: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMin the source file tell the preprocessor to perform specific actions.

The #include Directive

The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears.

#include "path-spec"

#include

The #define Directive

You can use the #define directive to give a meaningful name to a constant in your program. The two forms of the syntax are:

#define identifier token-stringopt

#define identifier[( identifieropt, ... , identifieropt )] token-stringopt

Macros

Macros are classified into two categories. The first type of macros can be written using

#define. The value of PI can be defined as:

#define PI 3.1415926

Here the symbol PI will be replaced with the actual value (i.e. 3.1415926) in the program.

These are simple macros like symbolic names mapped to constants.

The second type of macros takes arguments. It is also called a parameterized

macros. Consider the following:

#define square(x) x * x

Tips

• All the preprocessor directives start with the # sign

• A symbol can not be redefined without undefining it first

• The conditional compilation directives help in debugging the program

Page 535: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM• Do not declare variable names starting with underscore

• Always use parenthesis while defining macros that takes arguments

Note: these papers solved by urself .This vu groups is not responsible for any solved content http://www.virtualinspire.comcs 201 current paper share by a student

Ads not by this site

Final term paper of cs 201

My current paper of spring 2011-07-15Spring 2011Q 1Give gernal syntax of class template? (2)Q2What is this pointer? Give an example(2)Q3What is first parameter to stream insertion and stream exertion operater function?(2)Q4Why we use editors for writing programes?(2)Q5How is the following statement interpreted by the compiler?(3)abcQ6What happens when we use new and delete operators(3)Q7Given lines of code are three most important functions in matrix class described each of them in one line(3)

Page 536: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMMatrix(int=0,int=0);Matrix(const Matrix&);~Matrix();Q8Write a declaration fuction of called divide that takes arguments fo tye int and return of type float(3)Q9What is automatic veriable and why it is called automatic veriable?(5)Q10What do u mean by garbage and how it works in c++ and java(5)Q11Do u think that friend function violates the encapsulation justify(5)Q12Consider the following code a give the out put before and after the updateAge function called(5)Void updateAge(int[]);Int main (){Int age[5]=10;Cout array element before passing to functionendl;For (i=0,i<5,i++)Cout array of element i+1age[i]endl;}UpdateAge (age);Cout array element after passing to functionendl;For (i=0,i<5,i++)Cout array of element i+1age[i]endl;}

Void updateAge (int age[], int size){Age[3]=20} Today's CS201 Paper 22-07-2011

Ads not by this site

Assalam-u-AlaikumMy today's CS201 Paper 22-07-2011

Suppose there is class A having data member ‘x’, ‘y’ and an object of class B. The classB has data members ‘z’. Write the constructor of a class A that initializes the datamembers of class A and class B (using member initializing list). [2]How can we declare and initialize static data members of a class? [2]Write down the general syntax for a function that returns a reference? [2]Differentiate between single-dimensional and multi-dimensional array? [2]How many arguments does binary member operator function binary non-memberoperator function take? [3]What is an alternative of setw manipulator? [3]

Page 537: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMFind the error in the given code [3]

for (int i=0; i<numRows; i++)

{for (int j=0; j<numCols; j++)

}elements[i,j] =m.elements [i][j];

}}

Determine the output of following code segment. [3]x=5;Switch (x){case 5;cout<<Value is 5;case 4;cout<<Value is 4;break;}

What is meant by the lifetime of a variable and what is the lifetime of an automaticvariable? [5]Write a short note on files and also explain how they are handled in C++? [5]

Find the error [5]

class Data {private:const in data;public:Data( ){cout<<"Constructor Call…\n"; }

void getIncreament( ){Cout<<"Data = " <<++data;}};

void main( )

Page 538: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM{char*String ;String= New char[15];String="Welcome to VU";cout<<String;free (String);}

CS201 – Introduction to Programmming

Solved Subjective Questions

From spring 2010 Final Term Papersby

..:: ρя!ηcess M!sH! ::..

Question No: 1 ( M a r k s: 2 )

Write a declaration statement for an array of 10 elements of type float. Include an initialization statement of the first four elements to 1.0, 2.0, 3.0 and 4.0.

Answer:

float floatArry[10] = {1.0,2.0,3.0,4.0};

Question No: 2 ( M a r k s: 2 )

Write the general syntax for the declaration of pre-increment and post-increment member operator function.

Answer:

Classname operator ++(); ---- pre increment

Classname operator ++(int) ---- post increment

Question No: 3 ( M a r k s: 2 )

Give the general syntax of class template.

Page 539: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAnswer:

template

class myclass { ---} ;

Question No: 4 ( M a r k s: 2 )

What is a truth Table?

Answer:

There are some areas where the decision structures become very complicated. Sometimes, we find it difficult to evaluate a complicated logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax statement in any language. It becomes complicated to determine what will be evaluated in what way. We know the concept of truth table. The truth tables are very important. These are still a tool available for analyzing logical expressions. We will read logic design in future, which is actually to do with chips and gates. How we put these things together.

Question No: 5 ( M a r k s: 2 )

What will be the output of following code, if user input a number 123?

int input ;

cin >> oct >> input;

cout << hex << input ;

Answer:

53

Rational: it will take 123 as octal and print it in hex form which is 53.

Question No: 6 ( M a r k s: 2 )

What is principle of friendship in the context of functions and classes?

Answer:

Class can declare a friend function and someone from outside the class cannot declare itself friend of a class.

A friend function can access the private variables of class just like a member function

Page 540: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 7 ( M a r k s: 2 )

How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

Answer:

Unary operator takes only one argument like i++ or i— (Post increment or post decrement operators for integers) or ++i,--i (Pre increment or pre decrement operators for integers) ,we can not make Unary operator as binary or binary as Unary operator.

Question No: 8 ( M a r k s: 2 )

Which arithmetic operators cannot have a floating point operand?

Answer:

Modulus operator:

This operator can only be used with integer operands ONLY

Question No: 9 ( M a r k s: 2 )

What are manipulators? Give one example.

Answer:

The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14. This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

Question No: 10 ( M a r k s: 2 )

Write down piece of code that will declare a matrix of 3x3. And initialize all its locations with 0;

Answer:

int matrix [3] [3] ;

include

main () {

int matrix [3][3];

Page 541: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMint inivalue = 0;

for (int a=0;a<3;a++)

{ for (int b = 0;b<3;b++)

{ matrix[a][b]= inivalue;

cout<

}

Question No: 11 ( M a r k s: 2 )

What is the difference between switch statement and if statement.

Answer:

The “If” statement is used to select among two alternatives. It uses a Boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

Question No: 12 ( M a r k s: 2 )

How can we initialize data members of contained object at construction time?

Answer:

Initializer list is used to initialize the contained objects at the construction time.

Question No: 13 ( M a r k s: 2 )

Can we overload new and delete operators?

Answer:

Yes, it is possible to overload new and delete operators to customize memory management. These operators can be overloaded in global (non-member) scope and in class scope as member operators.

Question No: 14 ( M a r k s: 2 )

Suppose there is a template function ‘func’ having argument of type U and return type T. What will be the C++ syntax to call this function, passing a variable ‘x’ of type double and returning an int type?

Page 542: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMAnswer:

template

T func (T a, U b) {

return (a

}

calling

int i;

double x;

x = func

Question No: 15 ( M a r k s: 2 )

Which variable will be used in inner code block if we have the same names of variable at outer code block and inner code block?

Answer:

Simply: variable of the inner code is use in the inner code block.

Question No: 16 ( M a r k s: 2 )

What is the benefit of reference and where can we use it?

Answer:

In references we give the memory address of the object, due to references we pass values without making the copy. Hence, when we have many values & we want efficiency we use references to avoid copy.

Question No: 17 ( M a r k s: 2 )

Write the C++ code for the declaration of overloaded stream insertion and stream extraction operator for the object d of type Date.

Answer:

Date operator >> (date & d1){

Page 543: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMcout<<<”-”<<<”-”<

}

Question No: 18 ( M a r k s: 2 )

What is difference between endl and \n?

Answer:

Endl is manipulator and it inserts new line character and flushes the stream.

\n is control character which is used to insert line break.

Question No: 19 ( M a r k s: 2 )

What does code optimization mean?

Answer:

It is process by which we make our code in such a way that it improves the speed of program. By use of optimization we refine program codes in such a way that it run faster and consume less memory. We do it in such a way that output quality is not compromised.

Question No: 20 ( M a r k s: 3 )

How is the following cout statement interpreted by compiler?

cout << a << b << c ;

Answer:

It will give a compiler error because a,b,c are not declared.

Question No: 21 ( M a r k s: 3 )

Suppose an object of class A is declared as data member of class B.

(i) The constructor of which class will be called first?

Answer: A

(ii) The destructor of which class will be called first?

Answer: B

Page 544: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 22 ( M a r k s: 3 )

What will be the output of following functions if we call these functions three times?

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

}

Answer:

1

1

1

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Answer:

1

2

3

Question No: 23 ( M a r k s: 3 )

Page 545: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMIf is not available in the system then what does calloc/malloc and new operator return?

Answer:

calloc/malloc and new operator return returns a null pointer to indicate that no memory is available

Question No: 24 ( M a r k s: 3 )

What is the keyword ‘this’ and what are the uses of ‘this’ pointer?

Answer:

'this' is use to refer the current class member without using the name of the class.

Question No: 25 ( M a r k s: 3 )

Which one (copy constructor or assignment operator) will be called in each of the following code segment?

1) Matrix m1 (m2);

2) Matrix m1, m2;

m1 = m2;

3) Matrix m1 = m2;

Answer:

1) Matrix m1 (m2); copy constructor

2) Matrix m1, m2;

m1 = m2; assignment operator

3) Matrix m1 = m2; assignment operator

Question No: 26 ( M a r k s: 3 )

What will be the output of following function if we call this function by passing int 5?

template T reciprocal(T x) {return (1/x); }

Answer:

Page 546: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM0

The output will zero as 1/5 and its .05 but conversion to int make it zero

Above is prototype of template class so assume passing an int and returning an int

Question No: 27 ( M a r k s: 3 )

Identify the errors in the following member operator function and also correct them.

math * operator(math m);

math * operator (math m)

{

math temp;

temp.number= number * number;

return number;

}

Answer:

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

Page 547: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMQuestion No: 28 ( M a r k s: 3 )

What are the limitations of the friendship relation between classes?

Answer:

friendship relation between classes is a one way relation that is if one class declare friend another class then the another class is the friend of first class but not the first class if the friend of another class.

Question No: 29 ( M a r k s: 3 )

Define static variable. Also explain life time of static variable?

Answer:

When you declare a static variable (native data type or object) inside a function, it is created and initialized only once during the lifetime of the program.

Question No: 30 ( M a r k s: 5 )

What is difference between Unary and binary operators and how they can be overloaded?

Answer:

Unary operator takes one argument.

a ++ is an example of unary operator

Binary take two operators

+,-,* are example of binary operators

Overloaded binary operator may return any type

Here is general syntax of overloading

Return-type operator symbol (parameters);

Operator is keyword

Question No: 31 ( M a r k s: 5 )

What steps we must follow to design good program?

Answer 1:

Page 548: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMHence to design a program properly, we must:

· Analyze a problem statement, typically expressed as a word problem.

· Express its essence, abstractly and with examples.

· Formulate statements and comments in a precise language.

· Evaluate and revise the activities in light of checks and tests and

· Pay attention to detail.

Answer 2:

Details: we must check very details of any program. It is very important aspect of any program. We must pay complete attention to calculation.

We must give attention to logic and its flow should be smooth.

Reusable: We must write program in such a way that we can reuse them in other program. Like we define function in such a way that in future if we need any similar kind of function is requires in that case we can easily modify or reuse it.

Comments: we write the details of important steps in the form of comments. We should use comments in such a way if any body else wanted to reuse or debug or codes he can easily understand it.

Code readability: We should use Tab and spaces so codes are easily readable.

User interface: we make interface user friendly. Use polite prompts for user while take input.

Question No: 32 ( M a r k s: 5 )

Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.

Note: Display all data with in a particular width and the empty space should be filled with character x

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

Page 549: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMxxxxx20000

xxxxx30000

xxxxx60000

Answer:

#include

#include

main(){

int sal1 =1000;

int sal2 =1500;

int sal3 =20000;

int sal4 =30000;

int sal5 =60000;

cout << setfill ('x') << setw (10);

cout<< sal1<

cout << setfill ('x') << setw (10);

cout<< sal2<

cout << setfill ('x') << setw (10);

cout<< sal3<

cout << setfill ('x') << setw (10);

cout<< sal4<

cout << setfill ('x') << setw (10);

cout<< sal5<

int i=0;

Page 550: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMcin>>i; // to stop the screen to show the output

}

Question No: 33 ( M a r k s: 5 )

Suppose we have the following class.

class Matrix

{

private:

int Elements[3][3];

};

Write the operator function of stream extraction operator (>>) for this class.

Answer:

Element operator >> (Element &element){

cout<

cout<

cout<

}

Question No: 34 ( M a r k s: 5 )

What is meant by user interface and class interface in C++ ? And what role a class interfaces can play in user interface [Marks 5]

Answer:

Question No: 35 ( M a r k s: 5 )

Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.

Answer:

Page 551: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMclass frinedclass{

public:

friend int compute(exforsys e1)

};

Int compute(exforsys e1) { //Friend Function Definition which has access to private data return int(e1.a+e2.b)-5; }

Question No: 36 ( M a r k s: 5 )

What are the advantages and disadvantages of using templates?

Answer:

Many things can be possible without using templates but it does offer several clear advantages not offered by any other techniques:

Advantages:

· Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.

· Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.

· Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.

· It helps in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

Disadvantages:

On the other hand, if misused

· Templates can make code difficult to read and follow depending upon coding style.

· They can present seriously confusing syntactical problems esp. when the code is large and

Page 552: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMspread over several header and source files.

· Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

Question No: 37 ( M a r k s: 5 )

Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

math obj1, obj2;

obj2= 10 + obj1 ;

Answer:

#include

math

{

mth operator + (obj1,int x)

{

number temp;

temp=obj1.number+x;

return temp.number;

}

}

Question No: 38 ( M a r k s: 5 )

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

Answer:

Page 553: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM#include

#include

main () {

double a = 12.12345;

double b = 13.123456;

double c = 14.1234567;

cout << setprecision (5) << a << endl;

cout << setprecision (2) << a << endl;

cout << setprecision (3) << a << endl;

}

Question No: 39 ( M a r k s: 5 )

Let we have a class,

class String

{

private:

char buf[25];

};

Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment

Answer:

void String::operator = ( const String &other )

{ int length ;

length = other.length();

Page 554: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMdelete buf;

buf = new char [length + 1];

strcpy( buf, other.buf ); }

Question No: 40 ( M a r k s: 5 )

Read the given below code and explain what task is being performed by this function

Matrix :: Matrix ( int row , int col )

{

numRows = row ;

numCols = col ;

elements = new ( double * ) [ numRows ] ;

for ( int i = 0 ; i < numRows ; i ++ )

{

elements [ i ] = new double [ numCols ] ;

for ( int j = 0 ; j < numCols ; j ++ )

elements [ i ] [ j ] = 0.0 ;

}

}

Hint : This function belong to a matrix class, having

Number of Rows = numRows

Number of Columns = numCols

Answer:

In the above mentioned code, first of all programmer call the constructor who have two parameters for the number of rows & columns in the matrix. Then this constructor also dynamically allocates the memory for the elements of the matrix & also initializes the value of the all elements of matrix with 0.0

Page 555: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

Solution q 1:

(a)

Consider the following ambiguous CFG.

S -> S+S | S*S | number

number -> 0|1|2|3..|9

Expression= 9 + 4 + 7 * 5 * 2

Where S and number are non-terminals and the operators behave like terminals.

The above CFG creates the ambiguity as the Expression= 9 + 4 + 7 * 5 * 2 has two possibilities (9+4+7)*5*2=200 and 9 + 4 + (7*5*2)=83

Which can be expressed by the following production trees

(1) (9+4+7)*5*2=200

S

S

* S

Page 556: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM S + S S * S

S +

S 7 5 2

9

4

(2)

9 + 4 + (7*5*2)=83

S

S

+ S

S + S S * S

S S

9 4 * 2

Page 557: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

7 5

The ambiguity that has been observed in this example can be removed with the change in the CFG as

S ->(S+S) |(S*S) | number

Here it can be observed that

S ->(S+S)

->(S+(S*S))

->(9+4+(7*5*2))=83

S->(S*S)

->((S+S)*S)

->((9+4+7)*5*2)=200

(b).

Solution:

In Polish Notation, most of the S,s are eliminated. The branches are connected directly with the operators. Moreover, the operators + and * are no longer terminals as these are to be replaced by numbers.

The arithmetic expression shown by trees (1) and (2) can be calculated from the following trees using Polish Notation as under

(1)

Page 558: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

S

*

+ *

2

+ 7 5

9 4

(2)

S

+

+ *

Page 559: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM 2

9 4 *

7 5

To write the arithmetic expressions, it is required to traverse from the left side of S and going onward along the tree. The arithmetic expressions will be as under

(1) *++9 4 7*5 2=*++9 4 7 10= *+13 7 10= *20 10= 200(2) ++9 4**7 5 2= ++9 4*35 2= ++9 4 70= +13 70= 83

Q2:

Consider the language L expressed by (a+b)*a defined over Σ = {a, b}. Draw FA and construct the CFG corresponding to the language L.

Solution:

To construct the CFG corresponding to L, consider the following FA accepting L,

S- B+

ab

a

Page 560: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

CFG corresponding to the above FA may be

S-> aB|bS

B->aB|bS|ᴧ

Solution q 1:

(a)

Consider the following ambiguous CFG.

S -> S+S | S*S | number

number -> 0|1|2|3..|9

Expression= 9 + 4 + 7 * 5 * 2

Where S and number are non-terminals and the operators behave like terminals.

The above CFG creates the ambiguity as the Expression= 9 + 4 + 7 * 5 * 2 has two possibilities (9+4+7)*5*2=200 and 9 + 4 + (7*5*2)=83

Which can be expressed by the following production trees

b

Page 561: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

(2) (9+4+7)*5*2=200

S

S

* S

S + S S * S

S +

S 7 5 2

9

4

(2)

9 + 4 + (7*5*2)=83

Page 562: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

S

S

+ S

S + S S * S

S S

9 4 * 2

7 5

The ambiguity that has been observed in this example can be removed with the change in the CFG as

S ->(S+S) |(S*S) | number

Here it can be observed that

S ->(S+S)

->(S+(S*S))

->(9+4+(7*5*2))=83

S->(S*S)

->((S+S)*S)

->((9+4+7)*5*2)=200

Page 563: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM(b).

Solution:

In Polish Notation, most of the S,s are eliminated. The branches are connected directly with the operators. Moreover, the operators + and * are no longer terminals as these are to be replaced by numbers.

The arithmetic expression shown by trees (1) and (2) can be calculated from the following trees using Polish Notation as under

(1)

S

*

+ *

2

+ 7 5

Page 564: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COM

9 4

(2)

S

+

+ *

2

9 4 *

7 5

To write the arithmetic expressions, it is required to traverse from the left side of S and going onward along the tree. The arithmetic expressions will be as under

(3) *++9 4 7*5 2=*++9 4 7 10= *+13 7 10= *20 10= 200(4) ++9 4**7 5 2= ++9 4*35 2= ++9 4 70= +13 70= 83

Q2:

Consider the language L expressed by (a+b)*a defined over Σ = {a, b}. Draw FA and construct the CFG corresponding to the language L.

Page 565: api.ning.comapi.ning.com/.../Cs201total.docx · Web viewQ10: How can we declare and initialize static data member of a class?(2)Q11: When memory is allocated dynamically using new

WWW.VIRTUALINSPIRE.COMSolution:

To construct the CFG corresponding to L, consider the following FA accepting L,

CFG corresponding to the above FA may be

S-> aB|bS

B->aB|bS|ᴧ

S- B+

a

b

b

a