C++ Revision Tour

Question

Find and write the output of the following C++ program code:
Note: Assume all required header files are already being included in the program.

void main()
{
	int A[]={10,12,15,17,20,30};
	for(int i = 0; i<6; i++)
	{
		if(A[i]%2==0)
			A[i] /= 2;
		else if(A[i]%3==0)
			A[i] /= 3;
		if(A[i]%5==0)
			A[i] /=5;
	}
	for(i = 0; i<6; i++)
		cout<<A[i]<<'#';
	getch();
}

Answer

Output: 1#6#1#17#2#3#

Sponsor Area

Some More Questions From C++ Revision Tour Chapter

Write a definition for function COUNTDEPT( ) in C++ to read each object of a binary file TEACHERS.DAT, find and display the total number of teachers in the department MATHS. Assume that the file TEACHERS.DAT is created with the help of objects of class TEACHERS, which is defined below:

class TEACHERS
{
	int TID; char DEPT[20];
public:
	void GET()
	{
		cin>>TID;gets(DEPT);
	}
	void SHOW()
	{
		cout<<TID<<':'<<DEPT<<endl;
	}
	char *RDEPT(){return DEPT;}
};

Find the output of the following C++ code considering that the binary file BOOK.DAT exists on the hard disk with a data of 200 books.

class BOOK
{
	int BID;char BName[20];
public:
	void Enter();void Display();
};

void main()
{
	fstream InFile;
	InFile.open('BOOK.DAT',ios::binary|ios::in);
	BOOK B;
	InFile.seekg(5*sizeof(B));
	InFile.read((char*)&B, sizeof(B));
	cout<<'Book Number:'<<InFile.tellg()/sizeof(B) + 1;
	InFile.seekg(0,ios::end);
	cout<<' of '<<InFile.tellg()/sizeof(B)<<endl;
	InFile.close();
}

Write the type of C++ tokens (keywords and user defined identifiers) from the following:
(i) new
(ii) While
(iii) case
(iv) Num_2

Anil typed the following C++ code and during compilation, he found three errors as follows:
(i) Function strlen should have prototype
(ii) Undefined symbol cout
(iii) Undefined symbol endl
On asking, his teacher told him to include necessary header files in the code.Write the names of the header files, which Anil needs to include, for successful compilation and execution of the following code.

{
	char Txt[] = 'Welcome';
	for(int C= 0; C<strlen(Txt); C++)
		Txt[C] = Txt[C]+1;
	cout<<Txt<<endl;
}

Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.

void main()
{
	cout<<'Enter an Alphabet:';
	cin>>CH;
	switch(CH)
	case 'A' cout<<'Ant'; Break;
	case 'B' cout<<'Bear' ; Break;
}

Find and write the output of the following C++ program code:
Note: Assume all required header files are already included in the program.

#define Diff(N1,N2) ((N1>N2)?N1-N2:N2-N1)
void main()
{
	int A,B,NUM[] = {10,23,14,54,32};
	for(int CNT =4; CNT>0; CNT--)
	{
		A=NUM[CNT];
		B=NUM[CNT-1];
		cout<<Diff(A,B)<<'#';
	}
}

Find and write the output of the following C++ program code:
Note: Assume all required header files are already being included in the program.

void main()
{
	int *Point, Score[]={100,95,150,75,65,120};
	Point = Score;
	
	for(int L = 0; L<6; L++)
	{
		if((*Point)%10==0)
			*Point /= 2;
		else
			*Point -= 2;
		if((*Point)%5==0)
			*Point /= 5;
		Point++;
	}

	for(int L = 5; L>=0; L--){
		cout<<Score[L]<<'*';
	}
}

Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum values that can be assigned to each of the variables N and M.

Note:
● Assume all the required header files are already being included in the code.
● The function random(n) generates an integer between 0 and n-1

void main()
{
	randomize();
	int N=random(3),M=random(4);
	int DOCK[3][3] = {{1,2,3},{2,3,4},{3,4,5}};
	for(int R=0; R<N; R++)
	{
		for(int C=0; C<M; C++)
			cout<<DOCK[R][C]<<' ';
		cout<<endl;
	}
}
(i)  (ii) 
1 2 3
2 3 4
3 4 5
1 2  3   
2 3  4
(iii) (iv)
1 2 
2 3
1 2 
2 3
3 4

Polina Raj has used a text editing software to type some text in an article. After saving the article as MYNOTES.TXT, she realised that she has wrongly typed alphabet K in place of alphabet C everywhere in the article.

Write a function definition for PURETEXT() in C++ that would display the corrected version of the entire article of the file MYNOTES.TXT with all the alphabets “K” to be displayed as an alphabet “C” on screen.

Note: Assuming that MYNOTES.TXT does not contain any C alphabet otherwise.
Example:

If Polina has stored the following content in the file MYNOTES.TXT:

I OWN A KUTE LITTLE KAR.
I KARE FOR IT AS MY KHILD.
The function PURETEXT() should display the following content:

I OWN A CUTE LITTLE CAR.
I CARE FOR IT AS MY CHILD.

Write a definition for function COUNTPICS ( ) in C++ to read each object of a binary file PHOTOS.DAT, find and display the total number of PHOTOS of type PORTRAIT. Assume that the file PHOTOS.DAT is created with the help of objects of class PHOTOS, which is defined below:

class PHOTOS
{
	int PCODE;
char PTYPE[20];	//Photo Type as “PORTRAIT”,”NATURE”
public:
	void ENTER()
	{
		cin>>PCODE;gets(PTYPE);
	}
	void SHOWCASE()
	{
		cout<<PCODE<<':'<<PTYPE<<endl;
	}
	char *GETPTYPE(){
		return PTYPE;
	}
};