C++ Programming

  Home  Computer Programming  C++ Programming


“Learn C++ Programming by C++ Interview Questions and Answers”



120 C++ Programming Questions And Answers

21⟩ What is an orthogonal base class in C++?

If two base classes have no overlapping methods or data they are said to be independent of, or orthogonal to each other. Orthogonal in the sense means that two classes operate in different dimensions and do not interfere with each other in any way. The same derived class may inherit such classes with no difficulty.

 188 views

22⟩ What is a container class? What are the types of container classes in C++?

A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.

 182 views

23⟩ How do you write a function that can reverse a linked-list in C++?

void reverselist(void)

{

if(head==0)

return;

if(head-<next==0)

return;

if(head-<next==tail)

{

head-<next = 0;

tail-<next = head;

}

else

{

node* pre = head;

node* cur = head-<next;

node* curnext = cur-<next;

head-<next = 0;

cur-<next = head;

for(; curnext!=0; )

{

cur-<next = pre;

pre = cur;

cur = curnext;

curnext = curnext-<next;

}

curnext-<next = cur;

}

}

 168 views

24⟩ What is polymorphism in C++?

Polymorphism in C++ is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

 171 views

25⟩ How can you tell what shell you are running on UNIX system?

You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

 171 views

27⟩ What is Boyce Codd Normal form?

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a->b, where a and b is a subset of R, at least one of the following holds:

* a->b is a trivial functional dependency (b is a subset of a)

* a is a superkey for schema R

 182 views

31⟩ What is pure virtual function?

A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration

 188 views

33⟩ 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.

 167 views

35⟩ What is function overloading and operator overloading?

Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types.

Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).

 181 views

36⟩ What are the advantages of inheritance in C++?

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.

 166 views

37⟩ 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; }

 183 views

38⟩ How do you write a function that can reverse a linked-list?

void reverselist(void)

{

if(head==0)

return;

if(head->next==0)

return;

if(head->next==tail)

{

head->next = 0;

tail->next = head;

}

else

{

node* pre = head;

node* cur = head->next;

node* curnext = cur->next;

head->next = 0;

cur-> next = head;

for(; curnext!=0; )

{

cur->next = pre;

pre = cur;

cur = curnext;

curnext = curnext->next;

}

curnext->next = cur;

}

}

 183 views

39⟩ Write a program that ask for user input from 5 to 9 then calculate the average

#include "iostream.h"

int main() {

int MAX = 4;

int total = 0;

int average;

int numb;

for (int i=0; i<MAX; i++) {

cout <<

"Please enter your input between 5 and 9: ";

cin >> numb;

while ( numb<5 || numb>9) {

cout << "Invalid input, please re-enter: ";

cin >> numb;

}

total = total + numb;

}

average = total/MAX;

cout << "The average number is: "

<< average << "n";

return 0;

}

 153 views

40⟩ What do you mean by inline function?

The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

 165 views