Saturday 12 March 2011

learning cpp: test your programming skills

learning cpp: test your programming skills: "1. Difference between Stack and Heap? Stack = region of memory for temporaries Stack pointer pushed on function entry Popped on function exi..."

When can you tell that a memory leak will occur?
A memory leak occurs when a program loses the ability to free a block of dynamically allocated memory.
Describe the main characteristics of static functions.
The main characteristics of static functions include,
Ø  It is without the a this pointer,
Ø  It can't directly access the non-static members of its class
Ø  It can't be declared const, volatile or virtual.
Ø  It doesn't need to be invoked through an object of its class, although for convenience, it may.             
Will the inline function be compiled as the inline function always? Justify.
An inline function is a request and not a command. Hence it won't be compiled as an inline function always.
Explanation:
            Inline-expansion could fail if the inline function contains loops, the address of an inline function is used, or an inline function is called in a complex expression. The rules for inlining are compiler dependent.
Define a way other than using the keyword inline to make a function inline.
The function must be defined inside the class.  

What is C++?

Released in 1985, C++ is an object-oriented programming language created by Bjarne Stroustrup. C++ maintains almost all aspects of the C language, while simplifying memory management and adding several features - including a new datatype known as a class (you will learn more about these later) - to allow object-oriented programming. C++ maintains the features of C which allowed for low-level memory access but also gives the programmer new tools to simplify memory management.

C++ used for:

C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications. It can be used to make CGI scripts or console-only DOS programs. C++ allows you to create programs to do almost anything you need to do. The creator of C++, Bjarne Stroustrup, has put together a partial list of applications written in C++.
What is the difference between realloc() and free()?

The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.
What is the difference between declaration and definition?

The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j--) //function body
cout << *;
cout << endl; }
What are the advantages of inheritance?

It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.
Does c++ support multilevel and multiple inheritance?
Yes.
What is the difference between class and structure?
Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public.
Class: Class is a successor of Structure. By default all the members inside the class are private.
What is encapsulation?
Packaging an object’s variables within its methods is called encapsulation.
What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
virtual
What do you mean by binding of data and functions?
Encapsulation.
What is the difference between an object and a class?
Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.
- A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.
- The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.
- An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.
What are virtual functions?
A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.
What do you mean by pure virtual functions?
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.
class Shape { public: virtual void draw() = 0; };
In C++, what is the difference between method overloading and method overriding?
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
What is Empty Class?
 If there are no data members in a class it is known as EMPTY CLASS.
Assignment Operator - What is the difference between a "assignment operator" and a "copy constructor"?
Answer1.
In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object. For example:
complex c1,c2;
c1=c2; //this is assignment
complex c3=c2; //copy constructor

Answer2.
A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:

There is no need to test to see if it is being initialized from itself.
There is no need to clean up (eg, delete) an existing value (there is none).
A reference to itself is not returned.
What is the need for a Virtual Destructor ?
Destructors are declared as virtual because if do not declare it as virtual the base class destructor will be called before the derived class destructor and that will lead to memory leak because derived class’s objects will not get freed.Destructors are declared virtual so as to bind objects to the methods at runtime so that appropriate destructor is called.
What is the difference between a pointer and a reference?
What is diff between malloc()/free() and new/delete?
What are the access privileges in C++? What is the default access level?
Name the operators that cannot be overloaded.
What is pure virtual function? or what is abstract class?

When can you tell that a memory leak will occur?
When are copy constructors called?

What is difference between template and macro?
What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one?
                        Quiz

1. What function initalizes variables in a class:
 Constructor  Destructor  Constitutor  A and B are correct.
2. To include code from the library in the program, such as iostream, a directive would be called up using this command.
 #include <>; with iostream.h inside the brackets  include (iostreamh)  #include <> with iostream.h inside the brackets  include #iostream,h;
3. Single line comments explaining code would be preceded like in the following example
 /**  //  *//  /*
4. Which line has all reserved words ?
 char, int, float, doubled, short, long, unsigned, signed  sizeof, const, typedef, static, voided, enum, struct, union  if, else, for, while do, switch, continue, break  defaulted, goto, return, extern, private, public, protected
5. What punctuation must each command line have at the end of the line ?
 :  ,  !  ;
6. The C++ language is
 case-sensitive.  Not case-sensitive.  It depends  None of these
7. The number 5.9875e17 must be stored in a(n):
 int  long  double  float
8. Select the correct definition for a string variable.
 string mystr;  string mystr[20];  string[20] mystr;  char mystr[20];
9. The sentence "Hello world!" uses _____ elements in a character array.
 10  11  12  13
10. When you are creating a structure, you need to use the following keyword
 structure  struct  object  record
11. Select the correct function definition (NOT prototype) from the list below.
 void intro();  double sin(double rad);  int foo(int bar; double baz)  double pow(double num, int pow);
12. Cout can print multiple values or variables in a single command using the following syntax:
 cout << "Hi" + bob + "\n";  cout << "Hi" << bob << "\n";  cout << "Hi", bob, "\n";  cout << ("Hi" & bob & "\n");
13. Write a for loop that counts from 0 to 5.
 for (c = 0; c <= 5; c++)  for (int c = 0; c <= 6; c++)  for (c = 0; c < 5; c++)  for (c = 0; c < 5; c++);
14. What does the statement #include do ?
 It defines the function iostream.h  It tells the compiler where the program begins  It defines the words TRUE and FALSE  It allows the programmer to use cout << It defines the statement return
15. Which of the following converts an integer "value" to its ASCII equivalent ?
 atoi(value)  cout << value  (char) value  char (value)
16. Indicate which data type is not part of standard C++.
 bool  int  real  double
17. Which one of the choices would produce the following output ?
 cout << "Hello" << "World";  cout << "Hello \n World";  cout << "Hello World\n";  cin >> "Hello World";
18. What is the output of the following code?
for (int i=0; i<10; i++); cout << i%2 << " "; }
 0 1 2 3 4 5 6 7 8 9  0 2 4 6 8 10 12 14 16 18 (c) 1 0 1 0 1 0 1 0 1 0  0 1 0 1 0 1 0 1 0 1
19. What is the output of the following code?
for (int a = 1; a <= 1; a++) cout << a++; cout << a;
 22  12  error  23
20. For which values of the integer _value will the following code become an infinite loop?
int number=1;
while (true) {
cout << number;
if (number == 3) break;
number += _value; }
 only 0  only 1  only 2  only 1 or 2

1 comment: