C++ Revision Tour

Question

Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable CHANGER.
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 CHANGER;
	CHANGER=random(3);
	char CITY[][25]={'DELHI','MUMBAI','KOLKATA','CHENNAI'};
	for(int I=0;I<=CHANGER;I++)
	{
		for(int J=0;J<=I;J++){
			cout<<CITY[J];
		}
		cout<<endl;
	}
}
(i) (ii)
DELHI
DELHIMUMABAI
DELHIMUMABAIKOLKATA
DELHI
DELHIMUMABAI
DELHIMUMABAIKOLKATA
DELHIMUMABAIKOLKATACHENNAI
(iii) (iv)
MUMABAI
MUMABAIKOLKATA
MUMABAIKOLKATACHENNAI
KOLKATA
KOLKATACHENNAI

Answer

(i)
DELHI
DELHIMUMBAI
DELHIMUMBAIKOLKATA

Minimum Value of CHANGER = 0
Maximum Value of CHANGER = 2

Sponsor Area

Some More Questions From C++ Revision Tour Chapter

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 integer”;
      cin>>N;
      switch(N%2)
      case 0 cout<<'Even'; Break;
      case 1 cout<<'Odd'; Break;
}

Find and write the output of the following C++ program code :

#define Big(A,B) (A>B)?A+1:B+2
void main()
{
	char W[] = 'Exam';
	int L=strlen(W);
	for(int i=0; i<L-1; i++)
		W[i] = Big(W[i],W[i+1]);
	cout<<W<<endl;
	getch();
}

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

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 R and C.
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 R=random(3),C=random(4);
	int MAT[3][3] = {{10,20,30},{20,30,40},{30,40,50}};
	for(int I=0; I<R; I++)
	{
		for(int J=0; J<C; J++)
			cout<<MAT[I][J]<<' ';
		cout<<endl;
	}
}
(i) (ii)
10,20,30
20,30,40
30,40,50
10,20,30
20,30,40
(iii) (iv)
10,20
20,30
10,20
20,30
30,40

Aditi has used a text editing software to type some text. After saving the article as WORDS.TXT, she realised that she has wrongly typed alphabet J in place of the alphabet I everywhere in the article.
Write a function definition for JTOI() in C++ that would display the corrected version of the entire content of the file WORDS.TXT with all the alphabets “J” to be displayed as an alphabet 'I' on screen.
Note: Assuming that WORD.TXT does not contain any J alphabet otherwise.
Example: If Aditi has stored the following content in the file WORDS.TXT :

'WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH THJS TO BE A SENTENCE'

The function JTOI() should display the following content:

'WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH THIS TO BE A SENTENCE'

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