CS201 Solved Final Term Paper 2009 - 1

FINAL TERM EXAMINATION
Fall 2009
CS201- Introduction to Programming

Question No: 1                                   ( M a r k s: 1 ) http://vuzs.net
There are mainly -------------------- types of software
                                     Two
                                     ► Three
                                     ► Four
                                     ► Five

Software is categorized into two main categories
System Software
Application Software
Question No: 2                                   ( M a r k s: 1 ) http://vuzs.net
Structures help to define program-specific ___________ .
                                     ► functions
                                     ► datatypes
                                     ► Arithmetic operations
                                     ► None of the given options.
In structure, we introduce a new data type. P# 228
defined a new data type. Using structures we can declare: Page # 230

Question No: 3                                   ( M a r k s: 1 ) http://vuzs.net
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
Macro is a special name, which is substituted in the code by its definition, and as a result, we get an expanded code P# 271


Question No: 4                                   ( M a r k s: 1 ) http://vuzs.net
If constructor contains a return statement in its body then compiler will give __________
                                     ► No error
                                     ► Syntax error
                                     ► Logical error
                                     ► Run time error
As the constructor does not return any thing, so it has no return type. It means that the body of the construct function cannot have any return statement. Otherwise, the compiler will give a syntax error. P # 322
Question No: 5                                   ( M a r k s: 1 ) http://vuzs.net
eof( ), bad( ), good( ), clear( ) all are manipulators.
                                     
                                 ► True

► False

Question No: 6                                   ( M a r k s: 1 ) http://vuzs.net
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
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.

Question No: 7                                   ( M a r k s: 1 ) http://vuzs.net
Which of the following is the only operator that the compiler overloads for user define data type?
                                     ► Plus (+)
                                     ► Minus (-)
                                     Equal (= =)
                                     ► Assignment (=)
It's "==", for which the compiler will automatically generate code to
compare a hashsum of the memory areas occupied by the objects in question

Question No: 8                                   ( M a r k s: 1 ) http://vuzs.net
Friend function of a class is ______________ .
                                     ► Member function
                                     Non-member function
                                     ► Private function
                                     ► Public function
A friend function of a class is a non-member function which can gain access to the private section of the class

Question No: 9                                   ( M a r k s: 1 ) http://vuzs.net
We can also create an array of user define data type.
                                     True
                                     ►False


Question No: 10                                 ( M a r k s: 1 ) http://vuzs.net
The default scope for members of structures is public whereas the default visibility for class members is private.
                                     True
                                     ►False

The default scope for members of structures is public whereas the default visibility for class members is private. P#490
Question No: 11                                 ( M a r k s: 1 ) http://vuzs.net
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
For delete operator, destructor for the object is called first and then the memory block is deallocated. P# 414
Question No: 12                                 ( M a r k s: 1 ) http://vuzs.net
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: 13                                 ( M a r k s: 1 ) http://vuzs.net
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                                 ( M a r k s: 1 ) http://vuzs.net
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                                 ( M a r k s: 1 ) http://vuzs.net
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 ;      
const Pointers

The keyword const for pointers can appear before the type, after the type, or in both places. The following are legal declarations:
const int * ptr1;       /* A pointer to a constant integer:
                             the value pointed to cannot be changed  */
int * const ptr2;       /* A constant pointer to integer:
                             the integer can be changed, but ptr2
                             cannot point to anything else           */
const int * const ptr3; /* A constant pointer to a constant integer:
                             neither the value pointed to
                             nor the pointer itself can be changed   */
Declaring an object to be const means that the this pointer is a pointer to a const object. A const this pointer can by used only with const member functions.
P#154
Question No: 16                                 ( M a r k s: 1 ) http://vuzs.net
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.

If the array is to be accessed in random order, then the pointer approach may not be better than array indexing. P# 185
Question No: 17                                 ( M a r k s: 1 ) http://vuzs.net
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

Error due to missing << after cout
Question No: 18                                 ( M a r k s: 1 ) http://vuzs.net
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.

P# 372
Question No: 19                                 ( M a r k s: 1 ) http://vuzs.net
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
C++ is able to input and output the fundamental types using the stream extraction operator >> and the stream insertion operator <.

Question No: 20                                 ( M a r k s: 1 ) http://vuzs.net
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
Have a look on the stream extraction operator’s ( >> ) behavior here. The similar behavior is maintained when we overload this stream extraction operator ( >> ) or stream insertion operator ( << ). P# 444
We want to overload stream extraction ( >> ) and insertion ( << ) operators which are actually already overloaded.P#445
Arguments: Built in data types is overloaded as default hence User-defined data types is not already over loaded

Question No: 21                                 ( M a r k s: 1 ) http://vuzs.net
The programs, in which we allocate static memory, run essentially on ________
                                     ► Heap
                                     ► System Cache
                                     ► None of the given options
                                     ► Stack
The programs, in which we allocate static memory, run essentially on stack. P#280
Question No: 22                                 ( M a r k s: 1 ) http://vuzs.net
The dynamic memory allocation uses memory from the ____________.
                                     ► Stack
                                     ► Heap
                                     ► System Cache
                                     ► None of the given options
The dynamic memory allocation uses memory from the heap. P#280

Question No: 23                                 ( M a r k s: 1 ) http://vuzs.net
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. P#294
Question No: 24                                 ( M a r k s: 1 ) http://vuzs.net
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: 25                                 ( M a r k s: 1 ) http://vuzs.net
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

Overloading Unary Operators

// Preincrement operator overloaded as a member function.
Date Date::operator++()
{
   helpIncrement();
   return *this;  // value return; not a reference return
}
 
// Postincrement operator overloaded as a member function.
// Note that the dummy integer parameter does not have a
// parameter name.
Date Date::operator++(int)
{
   Date temp = *this;
   helpIncrement();
 
   // return non-incremented, saved, temporary object
   return temp;   // value return; not a reference return
}
Question No: 26                                 ( M a r k s: 1 ) http://vuzs.net
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
For operator >>, the second parameter must also be passed by reference. P#453

Question No: 27                                 ( M a r k s: 1 ) http://vuzs.net
 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
If you create an object inside a function as truck A, when the function finishes, the object A will be destroyed. Destructor for this static object will be called. To prove P# 464

Question No: 28                                 ( M a r k s: 1 ) http://vuzs.net
When ever dynamic memory allocation is made in C/C++, it is freed_____________.
                                     Explicitly
                                     ► Implicitly
                                     ► Both explicitly and implicitly
                                     ► None of the given options

You must be remembering that wherever the dynamic memory allocation is made, it has to be freed explicitly. P#527
Question No: 29                                 ( M a r k s: 1 ) http://vuzs.net
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
Friend functions are not member functions of the class. 
The class itself declares the friend functions. The prototype of friend functions is written 
in the definition of the class with the keyword ‘friend’. These functions have access to the 
private data member of the class, which means they have access to everything in the
 
class.
Question No: 30                                    ( M a r k s: 1 ) http://vuzs.net
What should be the return type of the constructor?
                                     ► void pointer
                                     ► int
                                     ► same as object type
                                     constructors do not return any thing
Constructors We have written a function named Date(int, int, int) in our class. This is in the public section of our class. It has no return type, P# 313

Question No: 31                                 ( M a r k s: 1 )
Is there any type checking on the parameters of macros?
Secondly, because the macros are replaced with preprocessors and not by compiler, therefore, they are not aware of the data types. They just replace the macro definition and there is no type checking on the parameters of the macro. Same macro can be used for multiple data types.  (Page#299)
Question No: 32                                 ( M a r k s: 1 )
What does an arity of operator represent?
Its represent number of operands.
The arity (number of operands) P# 372
Question No: 33                                 ( M a r k s: 2 )
Are the methods of a class reproduced for every object?
Every object has data of its own as every object is distinct from the other. For example, in case of the date class, there may be objects date1, date2 and date3. These are three different objects having their own value of date. Being distinct objects, they must have distinct space in memory.
Question No: 34                                 ( M a r k s: 2 )
Can we overload new and delete operators?
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: 35                                 ( 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);
}


Question No: 36                                 ( M a r k s: 3 )
If the requested memory is not available in the system then what does calloc/malloc and new operator return?
what happens when either we ask for too much memory at a time of non-availability of enough memory on the heap or we ask for memory that is available on the heap , but not available as a single chunk?. In this case, the call to calloc will fail. When a call to memory allocation functions fails, it returns a NULL pointer. P#282
If the memory is not available or is fragmented (not in a sequence), malloc will return a NULL pointer. P#282
Whenever we allocate memory dynamically, it is allocated from free store. Now we will see what happens if the memory in the free store is not sufficient enough to fulfill the request. malloc() function returns NULL pointer if the memory is not enough. In C++, 0 is returned instead of NULL pointer. P#332
Question No: 37                                 ( M a r k s: 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?
Const: usage of cont can prevent any changes in the original data.
The problem with call by reference is that ‘we are letting the function to change the values at their actual storage place in the memory’. Sometimes, we want to do this according to the requirement of the logic of the program. At some other occasion, we may pass the addresses for efficiency while not affecting the values at that addresses. The use of const can be helpful in overcoming this problem.. P#154
Question No: 38                                 ( M a r k s: 5 )
Why the first parameter of operator function for << operator must be passed by reference?
first parameter is passed by reference and the compiler does not allow it to pass by value. The first object is returned back by reference by the operator function. That’s why, the compiler does not allow to pass first parameter by value.
Objects passed by value are local to the function and destroyed when function returns.
.
Question No: 39                                 ( M a r k s: 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 <
cout << "Value of y:\t" << y;
}
int addValue (int &a){
a = a + 2;
return a;
}         
Question No: 40                                 ( M a r k s: 10 )
While doing dynamic memory allocation in class what are the important things that should be implemented in class.
doing dynamic memory allocation inside the constructor of the class. You must be remembering that wherever the dynamic memory allocation is made, it has to be freed explicitly. To de-allocate the memory, we will write code inside the destructor of the class Matrix. The other consideration when we are allocating memory on free store from within constructor is that the default assignment operator will not work here. Remember, the default assignment operator makes shallow copy of the object members, therefore, we will have to write our own assignment operator ( = ) in order to make deep copy of the object data members. Remember that a copy constructor is called when a new Matrix object is initialized and constructed based on an already existent Matrix object. Therefore, we have to write our own copy constructor in order to make deep copy of the object data members. There is one very important point to mention about this class Matrix. 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. Another thing that can be templatized in the Matrix class is the Scalar number. Actually, this Scalar number can be an int, float or double; therefore, we may also use a template for this.  P# 527
Question No: 41                                 ( M a r k s: 10 )
What is difference between using a square(x) macro and square(x) function?
Whenever we call a function, a lot of work has to be done during the execution of the program. The memory in machine is used as stack for the program. The state of a program (i.e. the value of all the variables of the program), the line no which is currently executing etc is on the stack. Before calling the function, we write the arguments on the stack. In a way, we stop at the function calling point and the code jumps to the function definition code. The function picks up the values of arguments from the stack. Do some computation and return the control to the main program which starts executing next line. So there is lot of overhead in function calling. Whenever we call a function, there is some work that needed to be done. Whenever we do a function call, like if we are calling a function in a loop, this overhead is involved with every iteration. The overhead is equal number of times the loop executed. So computer time and resources are wasted. Obviously there are a number of times when we need to call functions but in this simple example of calculating square, if we use square function and the program is calling this function 1000 times, a considerable time is wasted. On the other hand, if we define square macro and use it. The code written in front of macro name is substituted at all the places in the code where we are using square macro. Therefore the code is expanded before compilation and compiler see ordinary multiplication statements. There is no function call involved, thus making the program run faster. We can write complex parameterized macros. The advantage of using macros is that there is no overhead of function calls and the program runs faster. If we are using lot of macros in our program, it is replaced by the macro definition at every place in the code making the program bloat. Therefore our source code file becomes a large file, resulting in the enlargement of the executable file too. Sometimes it is better to write functions and define things in it. For simple things like taking a square, it is nice to write macros that are only one line code substitution by the preprocessor. Take care of few things while defining macros. There is no space between the macro name and the starting parenthesis. If we put a space there, it will be considered as simple macro without parameters. We can use more than one argument in the macros using comma-separated list. The naming convention of the arguments follows the same rules as used in case of simple variable name. After writing the arguments, enclosing parenthesis is used. There is always a space before starting the definition of the macro.

Leave a Reply

Related Posts Plugin for WordPress, Blogger...