CS-201 Introduction to Programing
Q & A
Online Test
Short Question & Answers
Q1. 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.
Q2. 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.
Q3. 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.
Q4. 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 zerolength 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.
Q5. 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?
Call by Reference shall be used in that case.
Q6. 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.
Q7. 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 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; }
Q8. Why do java consider pointer as dangerous?
JAVA, describe pointers as dangerous. if we assign a memory through a pointer where the pointer is destroyed, the memory remains allocated and is wasted. To address these things, there are only references in JAVA instead of pointers. JAVA gives the concept of garbage collection with the use of references. Due to this garbage collection, we are free from the headache of de- allocating the memory. We allocate and use the memory. When it is no longer in use, JAVA automatically deletes (frees) it through garbage collection
Q9. What is memory leak?
There is a requirement that if the constructor of a class allocates the memory, it is necessary to write a destructor of that class. We have to provide a destructor for that class, so that when that object ceases to exist, the memory allocated by the constructor, is returned to the free store. It is critically important. Otherwise, when the object is destroyed, there will be an unreferenced block of memory. It cannot be used by our program or by any other program. It's a memory leak that should be avoided.
Q10. What does optimization the of code means?
Optimization is the process of transforming a piece of code to make more efficient without changing its output or side-effects. The only difference vuzs visible to the code's user should be that it runs faster and/or consumes less memory.
Q11. 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.
Q12. Is it possible to define two functions as given below? Justify your answer.  func(int x, int y) func(int &x, int &y)
Yes, function can be defined in both given ways, as in function no.1 , function is calling the variable by its value (which is default function call method).In the second function , function call is done by using call by reference methos.
Q13. 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.
Course Instructor

Dr. Naveed A. Malik
ScD Electrucal Engineering
Massachusetts Institure of Technology, USA

C++ How to Program
by Paul J. Deitel

The C Programming Language
by Kernighan and Ritchie