Set - 1

Question 6 :

What are the advantages of inheritance?

Answer :

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.


Question 7 :

What do you mean by inline function?

Answer :

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.


Question 8 :

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

Answer :

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

 


Question 9 :

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

Answer :

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

 


Question 10 :

Write a short code using C++ to print out all odd number from 1 to 100 using a for loop 

Answer :

for( unsigned int i = 1; i < = 100; i++ )
if( i & 0x00000001 )
cout << i << \",\";