C++ Interview Questions and Answers

Posted on Friday, October 25, 2013 by B[H] | 0 comments

1 - 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++.

2 - How do you find out if a linked-list has an end? (i.e. the list is not a cycle) 

You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle. 

3 - 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.

4 - 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).

5 - 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; }

6 - 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.

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

Unit 5 - Compiles And Run Program

Posted on Saturday, May 8, 2010 by B[H] | 0 comments
Labels:

To run a C++ program, we must first create it, then compile it, then link it with other modules ( or other compiled programs), and finally run it. You can create a C++ program using the C++ editor. The editor is like a word processor that allows you to
type, edit and save your program. The program you create is called the source module.


After you have created the source program, you compiled it using the C++ compiler. The compiler translates your C++ instructions into a machine-readable form. The compiled program is called the object module.
Besides compiling the source module into an object module, the compiler also generates information necessary for linker. The linker links the object module generated by the compiler and any other object modules, your program may request into a final executable module.


Photobucket

Unit 5 - Repetition (or Iteration) structure

Posted on by B[H] | 0 comments
Labels:

The repetition (or iteration) structure permits a sequence of the instructions to be executed repeatedly until certain condition is reached. The repetition structure or loop in C++ comes in three forms while, do-while and for.

a) The while loop

The while loop repeats the body of the loop as long as the loop condition holds. The basic form of the while statement is as below:

while (expression) statement;



In this loop, the expression is first evaluated. If it is true (not zero), the statement (which can be a block is executed; if it is (zero), the statement is bypassed.


Photobucket

Photobucket





b) The do-while loop


The do-while loop executes a statement as long as the loop condition holds. The basic form of the do-while statement as below:

Do
statement
while (expression) ;



This do-while loop is quite similar to the while loop except that the expression is evaluated after the statement is executed. This means the statement in the do-while will be executed at least once. In the while statement, the statement will not be executed if the expression is false.

Photobucket

Photobucket



c) The for Loop

The for loop repeats the body of the loop as long as the loop condition holds. The basic form of the for loop as below:

for (initialization; condition test; incrementation)
{
statements;
}


• The initialization part typically refers to the initial value (if any) given to a loop variable or counter. But it can also include the initialization of any other variable. This initialization part is carried out just once at the beginning of the loop.
• The expression part determines whether the loop execution should be continued. If the expression is zero (false), the for loop is terminated, if it is not zero (true), the for statement is executed.
• The incrementation part typically increments (or decrements) the loop counter variable. This is done after the for statement is executed. This part, like the initialization part, can also include the incrementation of other variables.

Photobucket

The example 5.9, given is a very short program and it is easy for us to understand the for loop.
First, an integer variable is declared. Then, in the initialization part, the variable, iNum is set to 1. For the condition checking, iNum is checked to see whether it is equal to or less than 10. In each cycle of the loop, iNum is incremented by 1. Once iNum reaches 10, the loop exits. We can see that the program calculates the square of the first ten natural numbers.

Program output:

1 4 9 16 25 36 49 64 81 100

Unit 5 - Logical Structures And Basic Instructions (statement)

Posted on by B[H] | 0 comments
Labels:

C++ has a set of rich and powerful control structures (statements) that makes it a popular language. Control structure generally fall into four categories but in this unit we have to know only three of them, which are:

i. sequence structure
ii. selection structure
iii. repetition or iteration structure



a-Sequence structure

The sequence control structure is the simplest of all the structures. The program instructions are executed one by one, starting from the first instruction and ending in the last instruction as in the program segment.


Photobucket



b-Selection Structure

The selection structure allows to be executed non-sequentially. It allows the comparison of two expressions, and based on the comparison, to select a certain course of action. In C++ , there are three types of selection statements. They are:
i. if statement
ii. if-else statement
iii. Switch statement.

i. if Statement

The basic form of the if statement is:

If (expression) statement


In this form, the expression is first evaluated. If the expression evaluates to non-zero (meaning TRUE), the statement is executed; if it evaluates to zero (meaning FALSE), the statement following the if statement is executed. Again, it has one entry point and one exit point.

Photobucket

Photobucket



ii. if – else statement

The basic form of the if-else statement is :

Photobucket

In this form, the expression is first evaluated. If it evaluates to non-zero, statement_1 is executed, otherwise (i.e, if it evaluates to zero) statement_2 is executed. The execution of the statements are mutually exclusive, meaning, either statement_1 is executed or statement_2, but not both. The statement can, of course, take the form of blocks.

Photobucket

Photobucket

The if – else statement in the Example5.4 is to checks the value of iNum to be not equals to zero and also that the modulus value of the variable iNum is equal to zero. If condition is true, then the message ‘Even Number’ is printed on the screen. If condition is not true, then the message ‘Odd Number or Number is 0’ is printed on screen.



iii-Switch statement

The switch statement is the multiple branch decision statement is sometimes called the multiple-choice statement. The general form of the switch statement is :

Photobucket

The expression evaluates to an integer or character constants and statement sequence is a block of statements.

Photobucket

example:

Photobucket

The sample output of the program:

Please key in grade (A-F) : C
Minimun marks is 40

In the example above, A,B,C and D are the possible values that can be assigned to the variable cGrade.
In each case of the constants, A to D, a statement or sequence of statements would be executed.



You would have noticed that every line has statement under it called “break”. The break is the only thing that stops a case statement from continuing all the way down through each case label under it. In fact, if you do not put break in, the program will keep going down in the case statements, into other case labels, until it reaches the end of a break.
However, the default part is the codes that would be executed if there were no matching case of constant’s values.

Unit 5 - Introduction To Logical Structure

Posted on by B[H] | 0 comments
Labels:

Logical structure or program control structures refer to the order of execution of instructions in a program. So far, in all our examples, the instructions were executed sequentially one by one, from the top downwards. However, most real life problems require some kind of decision making, which involves comparing values, and based on the comparison, to take a certain course of action. Hence, C++ provides structure that will allow the non-sequential execution of program instructions. This means that an instruction or a whole block of instructions can be executed, repeated or skipped.

Unit 5 - Logical Structures And Basic Instructions

Posted on by B[H] | 0 comments
Labels:

General Objective : To Understand the logical structure types and basic instruction
refers to the order of execution of instructions in program.

Specific Objectives : At the end of the unit you will be able to :

- describe the sequence structure
- describe the selection structure
- describe the Repetition structure
- explain the program control structures
- write the simple program using program control structure

Unit 4 - The Expression

Posted on Friday, May 7, 2010 by B[H] | 0 comments
Labels:

Expression in C++ are formed by properly combining operators, variables and constants. We have already seen some examples of simple arithmetic and logical expression. As algebra, C++ expression can be complex. Parentheses can be used to force the order of evaluation. An expression may also contain spaces for readability.
Examples of expressions.

Gross_pay – deductions
(basic_pay + hours * rate) – (socso + premium + loan)
(b * b – 4 * a * c) > 0
(gender = = ‘m’) && (age > 20)
(gender = = ‘m’ || gender = = ‘f’ ) && age >= 21