Monday, 9 September 2013

NMIMS Management Aptitude Test (NMAT) Sample Paper


LANGUAGE SKILLS

 

This is a test to see how well you ‘know’ English. Your English language ability would be tested through questions on functional grammar, vocabulary, sentence completion, synonyms, antonyms, comprehension of a passage, etc. Study and answer the sample questions given below. Please remember, in the test proper there may be questions of several other types also. 

Sunday, 8 September 2013

Unsafe to deal locate the memory using free( ) if it has been allocated using new

Why it is unsafe to deal locate the memory using free( ) if it has been allocated using new?

assert( ) macro...

assert( ) macro...

We can use a macro called assert( ) to test for conditions that should not occur in a code. This                               macro expands to an if statement. If test evaluates to 0, assert prints an error message and                               calls abort to abort the program. 

                             #include
                             #include

                             void main( )                              {
                             int i ;
                             cout << "\nEnter an integer: " ;                              cin >> i ;
                             assert ( i >= 0 ) ;                              cout << i << endl ;
                             }

Reference as a data member of a class

Can I have a reference as a data member of a class? If yes, then how do I initialise it?

Ans: 


Yes, we can have a reference as a data member of a class. A reference as a data member of a  class is initialised in the initialisation list of the constructor. This is shown in following program.
                             #include

                             class sample                              {
                             private :
                             int& i ;
                             public :
                             sample ( int& ii ) : i ( ii )
                             {                              }
                             void show( )                              {
                             cout << i << endl ;
                             }
                             } ;
                             void main( )                              {
                             int j = 10 ;                              sample s ( j ) ;                              s.show( ) ;
                             }

Here, i refers to a variable j allocated on the stack. A point to note here is that we cannot bind a reference to an object passed to the constructor as a value. If we do so, then the reference i  would refer to the function parameter (i.e. parameter ii in the constructor), which would  disappear as soon as the function returns, thereby creating a situation of dangling reference.

What is strstream

What is strstream?

Ans:
 


strstream is a type of input/output stream that works with the memory. It allows using section of the memory as a stream object. These streams provide the classes that can be used for storing the stream of bytes into memory. For example, we can store integers, floats and strings as a stream of bytes.

There are several classes that implement this in-memory formatting. The class ostrstream derived from ostream is used when output is to be sent to memory, the class istrstream derived from istream is used when input is taken from memory and strstream class derived from iostream is used for
memory objects that do both input and output.

pointer inside static member function

Can we use this pointer inside static member function?

Ans:
 


No! The this pointer cannot be used inside a static member function. This is because a static member function is never called through an object.

istream class

What is the purpose of istream class?

Ans:
The istream class performs activities specific to input. It is derived from the ios class. The most commonly used member function of this class is the overloaded >> operator which can extract values of all basic types. We can extract even a string using this operator.

What is the limitation of cin while taking input for character array

What is the limitation of cin while taking input for character array?

Ans: 


To understand this consider following statements, 

                             char str[5] ;
                             cin >> str ; 


While entering the value for str if we enter more than 5 characters then there is no provision in
 cin to check the array bounds. If the array overflows, it may be dangerous. This can be  avoided by using get( ) function. For example, consider following statement, 

                             cin.get ( str, 5 ) ;

nocreate and noreplace flag

What do the nocreate and noreplace flag ensure when they are used for opening a file?
Ans: 


nocreate and noreplace are file-opening modes. A bit in the ios class defines these modes. The flag nocreate ensures that the file must exist before opening it. On the other hand the flag noreplace ensures that while opening a file for output it does not get overwritten with new one  unless ate or app is set. 

When the app flag is set then whatever we write gets appended to the existing file. When ate flag is set we can start reading or writing at the end of existing file.

What are put and get pointers

What are put and get pointers?
Ans: These are the long integers associated with the streams. The value present in the put pointer specifies the byte number in the file from where next write would take place in the file. The get pointer specifies the byte number in the file from where the next reading should take place.

How do I get the current position of the file pointeR

How do I get the current position of the file pointer?

Ans:
 


We can get the current position of the file pointer by using the tellp( ) member function of ostream class or tellg( ) member function of istream class. These functions return (in bytes)positions of put pointer and get pointer respectively.
What is the difference between the manipulator and setf( ) function?
Ans: 


The difference between the manipulator and setf( ) function are as follows: 

The setf( ) function is used to set the flags of the ios but manipulators directly insert the formatting instructions into the stream. We can create user-defined manipulators butsetf( ) function uses data members of ios class only. The flags put on through the setf( ) function can be put off through unsetf( ) function. Such flexibility is not available with manipulators.

What are manipulators

What are manipulators? 

Ans: Manipulators are the instructions to the output stream to modify the output in various ways. The manipulators provide a clean and easy way for formatted output in comparison to the formatting flags of the ios class. When manipulators are used, the formatting instructions are inserted directly into the stream. Manipulators are of two types, those that take an   argument and those that don't.

Unitbuf flag

When should I use unitbuf flag?
Ans: The unit buffering (unitbuf) flag should be turned on when we want to ensure that each character is output as soon as it is inserted into an output stream. The same can be done using unbuffered output but unit buffering provides a better performance than the unbuffered output.

Implement assignment operator and copy constructor in this class

Write a program that implements a date class containing day, month and year as data members. Implement assignment operator and copy constructor in this class.
Ans: This is shown in following program: 

#include 

class date
{ 
private : 
int day ;
int month ;
int year ; 
public : 
date ( int d = 0, int m = 0, int y = 0 ) 
{ 
day = d ;
month = m ;
year = y ; 
}

// copy constructor
date ( date &d ) 
{ 
day = d.day ;
month = d.month ;
year = d.year ;  
}

// an overloaded assignment operator
date operator = ( date d ) 
{ 
day = d.day ;
month = d.month ;
year = d.year ; 
return d ;  
} 
void display( )
{ 
cout << day << "/" << month << "/" << year ; 
} 
} ;

void main( )
{ 
date d1 ( 25, 9, 1979 ) ;
date d2 = d1 ; 
date d3 ;
d3 = d2 ; 
d3.display( ) ; 
}
What is the purpose of ios::basefield in the following statement?
cout.setf ( ios::hex, ios::basefield ) ;  


Ans: This is an example of formatting flags that work in a group. There is a flag for each numbering system (base) like decimal, octal and hexadecimal. Collectively, these flags are   referred to as basefield and are specified by ios::basefield flag. We can have only one of these   flags on at a time. If we set the hex flag as setf ( ios::hex ) then we will set the hex bit but we           won't clear the dec bit resulting in undefined behavior. The solution is to call setf( ) as setf ( ios::hex, ios::basefield ). This call first clears all the bits and then sets the hex bit.

How do I carry out conversion of one object of user-defined type to another?

How do I carry out conversion of one object of user-defined type to another?
Ans: To perform conversion from one user-defined type to another we need to provide conversion function. Following program demonstrates how to provide such conversion function.
                           

  class circle
                        

     {
                             private :
                             int radius ;
                             public:
                             circle ( int r = 0 )
                             {  

                             radius = r ;  
                             }
                             } ;
                            class rectangle
                             {
                             private :
                             int length, breadth ;                                public :                                rectangle( int l, int b )                              {  
                             length = l ;                              breadth = b ;  
                             }
                             operator circle( )                              {  
                             return circle ( length ) ;  
                             }                                } ;                              void main( )                             {  
                             rectangle r ( 20, 10 ) ;                              circle c;                              c = r ;  
                                                    }
                             } 

Here, when the statement c = r ; is executed the compiler searches for an overloaded assignment  operator in the class circle which accepts the object of type rectangle. Since there is no such overloaded assignment operator, the conversion operator function that converts the rectangle object to the circle object is searched in the rectangle class. We have provided such a  conversion function in the rectangle class. This conversion operator function returns a circle object. By default conversion operators have the name and return type same as the object type to which it converts to. Here the type of the object is circle and hence the name of the operator function as well as the return type is circle.

overloading a binary operator

While overloading a binary operator can we provide default values?
Ans: No!. This is because even if we provide the default arguments to the parameters of the overloaded operator function we would end up using the binary operator incorrectly. This is explained  in the following example: 


                     sample operator + ( sample a, sample b = sample (2, 3.5f ) )
                             {
                             }

                             void main( )
                             {
                                    sample s1, s2, s3 ;
                                    s3 = s1 + ; // error
                             }

How do I refer to a name of class or function that is defined within a namespace?

How do I refer to a name of class or function that is defined within a namespace?

Ans:
There are two ways in which we can refer to a name of class or function that is 


defined within a namespace: Using scope resolution operator through the using keyword. 

This is shown in following example: 


                             namespace name1
                             {
                             class sample1
                             {
                             // code
                             } ;
                             }
                            namespace name2
                             {
                             class sample2
                             {
                             // code
                             } ;
                             }
                             using namespace name2 ;
                             void main( )
                             {
                             name1::sample1 s1 ;
                             sample2 s2 ;
                             } 


zero-argument manipulator that should work same as hex

How do I write my own zero-argument manipulator that should work same as hex?

Ans:
This is shown in following program.