Chapter 1: C++ Revision Tour Class 12 Computer Science NCERT Solutions
Chapter 1: C++ Revision Tour serves as a comprehensive refresher of C++ basics and fundamental programming constructs. It revisits the core concepts of structured and procedural programming, helping students recall key syntax, logic development strategies, and features that form the foundation for advanced topics in object-oriented programming.
This chapter lays the groundwork for writing efficient C++ programs by reinforcing control structures, data types, arrays, functions, and basic input/output operations. It is essential for building confidence and fluency in writing, reading, and debugging C++ code.
What You Will Learn in Chapter 1
This chapter helps students revise the foundational elements of the C++ programming language and prepares them to dive deeper into object-oriented concepts. From variables to functions and conditional logic to loops, it reinforces programming basics through real code examples.
Key Topics Covered
Introduction to C++ Programming
C++ as a middle-level language combining features of low-level and high-level programming.
Structure of a C++ program, header files, and syntax essentials.
Tokens in C++
Keywords: Reserved words with special meaning (e.g.,
int
,return
,if
,class
).Identifiers: Names given to variables, functions, arrays, etc.
Literals: Constants used in a program (numeric, character, string, etc.).
Operators: Arithmetic, relational, logical, bitwise, assignment, and conditional.
Punctuators: Symbols such as
;
,{}
,[]
,()
.
Data Types and Variables
Fundamental data types:
int
,float
,char
,bool
,double
.Derived and user-defined data types.
Variable declaration, initialization, and scope (local vs global).
Input and Output Operations
Using
cin
andcout
for input/output with insertion (<<
) and extraction (>>
) operators.Formatting I/O with manipulators like
setw
,setprecision
.
Control Structures
Conditional Statements:
if
,if-else
,else-if ladder
,switch
.Looping Constructs:
for
,while
,do-while
.Jump Statements:
break
,continue
,goto
,return
.
Functions
Defining and calling functions, return types, and arguments.
Function overloading, default arguments, inline functions.
Arrays and Strings
One-dimensional and two-dimensional arrays.
Basic string handling using character arrays and string functions.
Scope and Storage Classes
auto
,register
,static
,extern
– behavior and visibility of variables.Global vs local variable scope.
Preprocessor Directives
#include
,#define
,#ifdef
,#ifndef
Use of macros and conditional compilation.
</ ul>
Download Chapter 1 Solutions PDF – C++ Revision Tour
Our PDF includes:
Complete NCERT textbook solutions for all questions
Syntax explanations and code snippets
Output-based questions and their solutions
Summary charts for operators, data types, and loops
Practice programs with detailed logic and step-by-step explanation
Highlights of Our NCERT Solutions
Easy-to-understand explanations with real C++ code
Well-labeled syntax charts and programming flow
Sample programs for each key concept
Clear differentiation between similar concepts (e.g., while vs do-while)
Debugging tips and common error handling strategies
Recommended Preparation Tips
Practice writing small programs for each control structure.
Memorize C++ keywords, operators, and syntax rules.
Revise input/output formats using
cin
andcout
.Understand function behavior through examples.
Practice dry-running code to predict outputs accurately.
Additional Study Resources
Flashcards: Tokens, operators, data types, and functions
Code worksheets: Fill-in-the-blanks and logic completion tasks
Flowchart practice for conditional and looping logic
PYQs and CBSE sample papers with code-based questions
Debugging exercises and syntax correction tasks
Mastering Chapter 1 – C++ Revision Tour
Mastery of this chapter ensures a strong grasp of foundational C++ programming. It equips students with the necessary logic and structure to handle object-oriented programming and real-world problem solving.
With proper revision and consistent practice, students can confidently move on to more advanced chapters in Class 12 Computer Science, such as OOP, file handling, and data structures.
Class 12 Computer Science (C++) – Chapter 1: C++ Revision Tour
Very Short Answer Type Questions
Question 1:
Write the related library function name based upon the given information in C++:
- Get single character using keyboard. This function is available in
<stdio.h>
file: getchar() - To check whether given character is alphanumeric or not. This function is available in
<ctype.h>
file: isalnum()
Question 2:
Observe the following program very carefully and write the names of those header file(s), which are essentially needed to compile and execute the program successfully:
typedef char TEXT[80];
void main()
{
TEXT Str[] = "Peace is supreme";
int Index = 0;
while(Str[Index] != '\0')
if(isupper(Str[Index]))
Str[Index++] = '#';
else
Str[Index++] = '*';
puts(Str);
}
Answer: <ctype.h>, <stdio.h>
Question 3:
Name the header files required for the following C++ code:
void main()
{
char str[20], str1[20];
gets(str);
strcpy(str1, str);
strrev(str);
puts(str);
puts(str1);
}
Answer: <stdio.h>, <string.h>
Question 4:
Observe the following C++ code and write the name(s) of the required header file(s):
void main()
{
char CH, STR[20];
cin >> STR;
CH = toupper(STR[0]);
cout << STR << " starts with " << CH << endl;
}
Answer: <iostream.h>, <ctype.h>
Question 5:
Observe the following C++ code and write the name(s) of the required header file(s):
void main()
{
char Text[20], C;
cin >> Text;
C = tolower(Text[0]);
cout << C << " is the first char of " << Text << endl;
}
Answer: <iostream.h>, <ctype.h>
Question 6:
Name the header file(s), which are essentially required to run the following program segment:
void main()
{
char A = 'K', B;
if (islower(A))
B = toupper(A);
else
B = '*';
cout << A << " turned to " << B << endl;
}
Answer: <iostream.h>, <ctype.h>
Question 7:
Observe the following C++ code and write the name(s) of header files required to run it:
void main()
{
float Area, Side;
cin >> Area;
Side = sqrt(Area);
cout << "One side of the square: " << Side << endl;
}
Answer: <math.h>, <iostream.h>
Question 8:
Write the names of header files used:
void main()
{
int number;
cin >> number;
if (abs(number) == number)
cout << "Positive" << endl;
}
Answer: <iostream.h>, <math.h>
Question 9:
Write the names of header files, which are NOT NECESSARY to run the following program:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
void main()
{
char STR[80];
gets(STR);
puts(strrev(STR));
}
Answer: <iostream.h>, <math.h> are not required
Question 10:
Which C++ header file(s) are essentially required to be included to run the following code?
void main()
{
char TEXT[] = "Something";
cout << "Remaining SMS Chars: " << 160 - strlen(TEXT) << endl;
}
Answer: <string.h>, <iostream.h>
Question 11:
Which C++ header file(s) are essentially required to be included to run the following code?
void main()
{
char STRING[] = "SomeThing";
cout << "Balance Characters: " << 160 - strlen(STRING) << endl;
}
Answer: <iostream.h>, <string.h>
Question 12:
Ahmed has typed the following program. Identify missing header files:
void main()
{
float Radians, Value;
cin >> Radians;
Value = sin(Radians);
cout << Value << endl;
}
Answer: <iostream.h>, <math.h>
Question 13:
Which C++ header file(s) are required for the following code?
void main()
{
char *word1 = "Hello", *word2 = "Friends";
strcat(word1, word2);
cout << word1;
}
Answer: <iostream.h>, <string.h>
Short Answer Type Questions - I
Question 1: Define Macro with suitable example.
Macros are preprocessor directives created using #define
that serve as symbolic constants. They help to simplify and reduce repetitive coding.
Example:
#define max(a, b) a > b ? a : b
This defines a macro max
that takes two arguments a
and b
. It can be used like a function:
A = max(x, y);
After preprocessing, it becomes:
A = x > y ? x : y;
Question 2: Write the output of the following C++ program code:
class Calc {
char Grade;
int Bonus;
public:
Calc() { Grade = 'E'; Bonus = 0; }
void Down(int G) {
Grade -= G;
}
void Up(int G) {
Grade += G;
Bonus++;
}
void Show() {
cout << Grade << "#" << Bonus << endl;
}
};
void main() {
Calc c;
c.Down(2);
c.Show();
c.Up(7);
c.Show();
c.Down(2);
c.Show();
}
Output:
C#0
J#1
H#1
Question 3: Rewrite the program after removing syntactical errors.
Corrected Program:
#include <iostream.h>
void main() {
int A[10] = {3, 2, 5, 4, 7, 9, 10}; // array initialization with semicolon
int S = 0, p;
for(p = 0; p <= 6; p++) {
if(A[p] % 2 == 0)
S = S + A[p];
}
cout << S;
}
Question 4: Rewrite after removing all syntactical errors.
Corrected Program:
#include <iostream.h>
#define Max 70.0
void main() {
int Speed;
char Stop = 'N';
cin >> Speed;
if(Speed > Max)
Stop = 'Y';
cout << Stop << endl;
}
Question 5: Write the output of the following program:
void Position(int & C1, int C2 = 3) {
C1 += 2;
C2 += C1;
}
void main() {
int P1 = 20, P2 = 4;
Position(P1);
cout << P1 << "," << P2 << endl;
Position(P2, P1);
cout << P1 << "," << P2 << endl;
}
Output:
22,4
22,6
Question 6:
Study and find possible outputs. Also write min and max values of NUM.
void main() {
randomize();
int NUM;
NUM = random(3) + 2;
char TEXT[] = "ABCDEFGHIJK";
for(int I = 1; I <= NUM; I++) {
for(int J = NUM; J <= 7; J++)
cout << TEXT[J];
cout << endl;
}
}
Possible Outputs:
- EFGH
- CDEFGH
Minimum value of NUM: 2
Maximum value of NUM: 4
Question 7:
Rewrite the following program after removing all syntax errors:
#include <iostream.h>
#include <math.h>
#define PI 3.14
void main() {
float r, a;
cout << "enter any radius";
cin >> r;
a = PI * pow(r, 2);
cout << "Area=" << a;
}
Question 8:
Correct the following C++ code:
#include <iostream.h>
#include <stdio.h>
typedef char STR[80];
void main() {
STR Txt;
gets(Txt);
cout << Txt << endl;
}
Question 9:
Correct the following C++ code:
#include <iostream.h>
#include <stdio.h>
typedef char STRING[50];
void main() {
STRING City;
gets(City);
cout << City << endl;
}
Question 10:
Out of the following, find those identifiers which cannot be used in a C++ program:
Identifiers: Total*Tax, double, case, My Name, New switch, Column31, _Amount
Invalid Identifiers:
Total*Tax
– Contains invalid character '*'double
– Reserved keywordcase
– Reserved keywordMy Name
– Contains spaceNew switch
– Contains space and uses reserved wordswitch
Question 11:
Ronica Jose has typed the following program. Identify the required header files:
void main()
{
double X, Times, Result;
cin >> X >> Times;
Result = pow(X, Times);
cout << Result << endl;
}
Answer: <iostream.h> (for cin and cout), <math.h> (for pow)
Question 12:
Rewrite the following C++ code after correcting all syntactical errors:
#define Formula(a,b) 2*a+b
void main()
{
float X = 3.2, Y = 4.1;
float Z = Formula(X, Y);
cout << "Result=" << Z << endl;
}
Question 13:
Find and write the output of the following C++ program:
typedef char TexT[80];
void JumbleUp(TexT T)
{
int L = strlen(T);
for(int C = 0; C < L - 1; C += 2)
{
char CT = T[C];
T[C] = T[C+1];
T[C+1] = CT;
}
for(C = 1; C < L; C += 2)
if(T[C] >= 'M' && T[C] <= 'U')
T[C] = '@';
}
void main()
{
TexT Str = "HARMONIOUS";
JumbleUp(Str);
cout << Str << endl;
}
Output: AHM@N@OIS@
Question 14:
What are the possible outputs and the max/min values of PICKER in the following code?
void main()
{
randomize();
int PICKER;
PICKER = random(3);
char COLOUR[][5] = {"BLUE", "PINK", "GREEN", "RED"};
for(int I = 0; I <= PICKER; I++)
{
for(int J = 0; J <= I; J++)
cout << COLOUR[J];
cout << endl;
}
}
Minimum value of PICKER: 0
Maximum value of PICKER: 2
Question 15:
Identify the invalid identifiers:
Identifiers: _Cost
, Price*Qty
, float
, Switch
, Address one
, Delete
, Number12
, do
Invalid Identifiers:
Price*Qty
– contains invalid character *float
– reserved keywordAddress one
– contains spacedo
– reserved keyword
Question 16:
Identify missing header files in the code:
void main()
{
float A, Number, Outcome;
cin >> A >> Number;
Outcome = pow(A, Number);
cout << Outcome << endl;
}
Required Headers: <iostream.h> (for cin, cout), <math.h> (for pow)
Question 17:
Correct the following C++ code:
#define Equation(p,q) p+2*q
void main()
{
float A = 3.2, B = 4.1;
float C = Equation(A, B);
cout << "Output=" << C << endl;
}
Question 18:
Find the output of the following code:
typedef char STRING[80];
void MIXITNOW(STRING S)
{
int Size = strlen(S);
for(int I = 0; I < Size - 1; I += 2)
{
char WS = S[I];
S[I] = S[I+1];
S[I+1] = WS;
}
for(I = 1; I < Size; I += 2)
if(S[I] >= 'M' && S[I] <= 'U')
S[I] = '@';
}
void main()
{
STRING Word = "CRACKAJACK";
MIXITNOW(Word);
cout << Word << endl;
}
Output: RCCAAKAJKC
Question 19:
Find the output of the following program:
class stock
{
long int ID;
float Rate;
int Date;
public:
Stock()
{
ID = 1001; Rate = 200; Date = 1;
}
void RegCode(long int I, float R)
{
ID = I;
Rate = R;
}
void Change(int New, int DT)
{
Rate += New;
Date = DT;
}
void Show()
{
cout << "Date: " << Date << endl;
cout << ID << "#" << Rate << endl;
}
};
void main()
{
stock A, B, C;
A.RegCode(1024,150);
B.RegCode(2015,300);
B.Change(100,29);
C.Change(-20,20);
A.Show();
B.Show();
C.Show();
}
Output:
Date: 1 1024#150 Date: 29 2015#400 Date: 20 1001#180
Question 20:
Predict the possible outputs and max/min values of CHANGER:
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;
}
}
Possible Outputs:
- DELHI
- DELHIMUMBAI
- DELHIMUMBAIKOLKATA
Minimum value of CHANGER: 0
Maximum value of CHANGER: 2
Question 21:
Identify invalid C++ identifiers:
Identifiers: Days*Rent
, For
, A+Price
, Grand Total
, double
, 2Members
, .Participant1
, MyCity
Invalid Identifiers:
Days*Rent
– Contains illegal character '*'For
– Reserved keywordGrand Total
– Contains spacedouble
– Reserved keyword
Question 22:
Rewrite the following C++ code after removing any/all syntactical errors:
#define formula(a,b,c) a + 2*b + 3*c
void main()
{
int L = 1, M = 2, N = 3;
int J = formula(L, M, N);
cout << "Output=" << J << endl;
}
Corrections Explained:
- Corrected macro definition: removed space between
formula
and its parameters. - Corrected the
cout
statement to properly use<< J
instead of<cj
.
Question 23:
Find the possible outputs and the max/min values that can be assigned to P_Holder:
void main()
{
randomize();
int P_Holder;
P_Holder = random(3);
char place[][25] = {"JAIPUR", "PUNA", "KOCHI", "GOA"};
for(int P = 0; P <= P_Holder; P++)
{
for (int C = 0; C < P; C++)
cout << place[C];
cout << endl;
}
}
Possible Outputs:
- JAIPUR
- JAIPURPUNA
- JAIPURPUNAKOCHI
Minimum value of P_Holder: 0
Maximum value of P_Holder: 2
Question 24:
Rewrite the following program after removing syntactical errors:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
class product // Corrected 'Class' to 'class'
{
int product_code, qty, price;
char name[20];
public: // Added ':' after 'public'
product()
{
product_code = 0;
qty = 0;
price = 0;
strcpy(name, "NULL"); // Corrected assignment of 'name'
}
void entry()
{
cout << "\nEnter code, qty, price";
cin >> product_code >> qty >> price;
gets(name);
}
int tot_price() { return qty * price; } // Changed 'void' to 'int'
};
void main()
{
product p;
p.entry();
cout << p.tot_price(); // Corrected tot_price() call
}
Explanation of Corrections:
Class
→class
(C++ is case-sensitive)public
→public:
name = NULL;
→strcpy(name, "NULL");
void tot_price()
→int tot_price()
cout << tot_price();
→cout << p.tot_price();
Question 25:
Explain conditional operator with a suitable example.
The conditional operator (also called the ternary operator) is used to execute expressions based on a condition. It is a compact version of the if-else statement.
Syntax:
condition ? expression1 : expression2;
If the condition is true, expression1
is executed. Otherwise, expression2
is executed.
Example:
int y = 10, x;
x = (y > 10) ? 1 : 0;
cout << x;
Output: 0
Since y > 10
is false, x
is assigned 0.
Class 12 Computer Science (C++) – Topic 2: Flow Control
Short Answer Type Questions – Set 1 (2 Marks Each)
Question 1:
Find syntax error(s), if any, in the following program: (Assuming all desired header file(s) are already included)
typedef char String[80];
void main
{
String S;
int L;
for(L=0; L<26; C++)
S[L]=L+65;
cout << S << endl;
}
typedef char String[80];
void main
{
String S;
int L;
for(L=0; L<26; C++)
S[L]=L+65;
cout << S << endl;
}
Answer:
- Missing parentheses
()
aftervoid main
. - Incorrect variable
C++
in loop; it should beL++
. - Missing semicolon after
cout << S << endl
.
Question 2:
Read the following C++ code carefully and find out, which out of the given options (i) to (iv) are the expected correct outputs of it. Also, write the maximum and minimum value that can be assigned to the variable Taker used in the code:
void main()
{
int GuessMe[4] = {100, 50, 200, 20};
int Taker = random(2) + 2;
for(int Chance = 0; Chance <= Taker; Chance++)
cout << GuessMe[Chance] << "#";
}
void main()
{
int GuessMe[4] = {100, 50, 200, 20};
int Taker = random(2) + 2;
for(int Chance = 0; Chance <= Taker; Chance++)
cout << GuessMe[Chance] << "#";
}
Answer:
- Correct Output: 100#50#200#
- Maximum value of Taker: 3
- Minimum value of Taker: 2
Question 3:
Read the following C++ code carefully and find out, which out of the given options (i) to (iv) are the expected correct output(s) of it. Also, write the maximum and minimum value that can be assigned to the variable Start used in the code:
void main()
{
int Guess[4] = {200, 150, 20, 250};
int Start = random(2) + 2;
for(int C = Start; C < 4; C++)
cout << Guess[C] << "#";
}
void main()
{
int Guess[4] = {200, 150, 20, 250};
int Start = random(2) + 2;
for(int C = Start; C < 4; C++)
cout << Guess[C] << "#";
}
Answer:
- Correct Output: 20#250#
- Maximum value of Start: 3
- Minimum value of Start: 2
Question 4:
Which C++ header file(s) will be included to run/execute the following C++ code?
void main()
{
int Last = 26.5698742658;
cout << setw(5) << setprecision(9) << Last;
}
void main()
{
int Last = 26.5698742658;
cout << setw(5) << setprecision(9) << Last;
}
Answer:
- <iostream.h>
- <iomanip.h>
Question 5: Find the correct identifiers of the following, which can be used for naming variables, constants, or functions in a C++ program: For, While, INT, New, delete, IstName, Add + Subtract, namel
Answer (Valid Identifiers):
- INT
- New
- namel
Question 6: Find correct identifiers out of the following, which can be used for naming variables, constants, or functions in a C++ program: For, While, Float, new, 2ndName, A%B, Amount 2, Counter
Answer (Valid Identifiers):
- While
- Counter
Question 7:
Find out the expected correct output(s) from the options (i) to (iv) for the following C++ code. Also, find out the minimum and the maximum value that can be assigned to the variable Stop used in the code:
void main()
{
int Begin = 3, Stop;
for(int Run = 1; Run < 4; Run++)
{
Stop = random(Begin) + 6;
cout << Begin++ << Stop << "*";
}
}
void main()
{
int Begin = 3, Stop;
for(int Run = 1; Run < 4; Run++)
{
Stop = random(Begin) + 6;
cout << Begin++ << Stop << "*";
}
}
Answer:
- Correct Output: 36*46*57*
- Minimum value of Stop: 6
- Maximum value of Stop: 8
Question 8:
Find the output:
int A[2][3] = {{1, 2, 3}, {5, 6, 7}};
for(int i = 1; i < 2; i++)
for(int j = 0; j < 3; j++)
cout << A[i][j] << endl;
int A[2][3] = {{1, 2, 3}, {5, 6, 7}};
for(int i = 1; i < 2; i++)
for(int j = 0; j < 3; j++)
cout << A[i][j] << endl;
Answer:
5 6 7
Question 9:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include
typedef char Text(80);
void main()
{
Text T = "Indian";
int Count = strlen(T);
cout << T << 'has' << Count << 'characters'
<< endl;
}
#include
typedef char Text(80);
void main()
{
Text T = "Indian";
int Count = strlen(T);
cout << T << 'has' << Count << 'characters'
<< endl;
}
Corrected Code:
#include <iostream.h>
#include <string.h>
typedef char Text[80];
void main()
{
Text T = "Indian";
int Count = strlen(T);
cout << T << " has " << Count << " characters" << endl;
}
Question 10:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include
typedef char[80] String;
void main()
{
String S = "Peace";
int L = strlen(S);
cout << S << 'has' << L << 'characters' << endl;
}
#include
typedef char[80] String;
void main()
{
String S = "Peace";
int L = strlen(S);
cout << S << 'has' << L << 'characters' << endl;
}
Corrected Code:
#include <iostream.h>
#include <string.h>
typedef char String[80];
void main()
{
String S = "Peace";
int L = strlen(S);
cout << S << " has " << L << " characters" << endl;
}
Question 11:
Based on the following C++ code, find out the expected correct output(s) from the options (i) to (iv). Also, find out the minimum and maximum value that can be assigned to the variable Trick used in the code at the time when the value of count is 3:
void main()
{
char status[][10] = {"EXCEL", "GOOD", "OK"};
int Turn = 10, Trick;
for(int Count = 1; Count < 4; Count++)
{
Trick = random(Count);
cout << Turn - Trick << status[Trick] << "#";
}
}
void main()
{
char status[][10] = {"EXCEL", "GOOD", "OK"};
int Turn = 10, Trick;
for(int Count = 1; Count < 4; Count++)
{
Trick = random(Count);
cout << Turn - Trick << status[Trick] << "#";
}
}
Answer:
- Correct Output: 10EXCEL#10EXCEL#9GOOD#
- Minimum value of Trick: 0
- Maximum value of Trick: 2 (since random(Count) when Count = 3 gives values 0–2)
Question 12:
Observe the following program carefully and write the names of those header file(s) which are essentially needed to compile and execute the following program successfully:
typedef char STRING[80];
void main()
{
STRING Txt[] = "We love peace";
int count = 0;
while(Txt[count] != '\0')
{
cout << Txt[count] << '#';
count++;
}
}
typedef char STRING[80];
void main()
{
STRING Txt[] = "We love peace";
int count = 0;
while(Txt[count] != '\0')
{
cout << Txt[count] << '#';
count++;
}
}
Answer:
- <iostream.h>
- <cstring.h>
Question 13:
Which of the following options will give the correct output for the given program?
void main()
{
int Check = 10;
int val = Check++ + ++Check;
cout << val;
}
void main()
{
int Check = 10;
int val = Check++ + ++Check;
cout << val;
}
Answer: 22
Short Answer Type Questions – I
Question 1: Explain in brief the purpose of function prototype with the help of a suitable example.
A function prototype serves several purposes:
- Indicates the return type of the function
- Specifies the number of arguments
- Defines the data types of each argument
- Determines the order of arguments passed
Example:
#include <iostream.h>
int timesTwo(int num); // Function prototype
int main()
{
int number, response;
cout << "Please enter a number:"; cin >> number;
response = timesTwo(number); // Function call
cout << "The answer is " << response;
return 0;
}
// timesTwo function definition
int timesTwo(int num)
{
int answer; // Local variable
answer = 2 * num;
return answer;
}
Question 2: What is the difference between call by reference and call by value with respect to memory allocation? Give a suitable example to illustrate using C++ code.
Call by Value: Creates a copy of values. Changes in function do not affect original variables.
Call by Reference: Uses memory address. Changes inside function affect original variables.
Call by Value Example:
#include <iostream.h>
void swap(int, int); // Function prototype
int main()
{
int a = 10, b = 20;
swap(a, b); // Function call
cout << a << " " << b; // Output: 10 20
return 0;
}
void swap(int c, int d)
{
int t;
t = c;
c = d;
d = t;
}
Call by Reference Example:
#include <iostream.h>
void swap(int &, int &); // Function prototype
int main()
{
int a = 10, b = 20;
swap(a, b); // Function call
cout << a << " " << b; // Output: 20 10
return 0;
}
void swap(int &c, int &d)
{
int t;
t = c;
c = d;
d = t;
}
Question 3: What is the difference between Actual Parameter & Formal Parameter? Give a suitable example to illustrate both in a C++ code.
Parameter Type | Formal Parameter | Actual Parameter |
---|---|---|
Definition | These appear in the function definition | These appear in the function call |
Example | void set(int a, int b) |
set(10, 20) |
Example Code:
#include <iostream.h>
void set(int a, int b) // Formal Parameters
{
int c = a - b;
cout << c;
}
int main()
{
int a = 10, b = 20;
set(a, b); // Actual Parameters
return 0;
}
Question 4: Deepa has just started working as a programmer in STAR SOFTWARE company. In the company, she has got her first assignment to be done using a C++ function to find the smallest number out of a given set of numbers stored in a one-dimensional array. But she has committed some logical mistakes while writing the code and is not getting the desired result. Rewrite the correct code underlining the corrections done. Do not add any additional statements in the corrected code.
Original Code (Incorrect):
int find(int a[], int n)
{
int s = a[0];
for (int x = 1; x < n; x++)
if (a[x] > s)
a[x] = s;
return s;
}
Corrected Code:
int find(int a[], int n)
{
int s = a[0];
for (int x = 1; x < n; x++) // Correct loop index
{
if (a[x] < s) // Correct condition
s = a[x]; // Correct assignment
}
return s;
}
Question 5:
Find the correct identifiers of the following, which can be used for naming variables, constants, or functions in a C++ program:
For, While, INT, New, delete, IstName, Add + Subtract, namel
Answer (Valid Identifiers):
- INT
- New
- namel
Question 6:
Find correct identifiers out of the following, which can be used for naming variables, constants, or functions in a C++ program:
For, While, Float, new, 2ndName, A%B, Amount 2, Counter
Answer (Valid Identifiers):
- While
- Counter
Question 7:
Find out the expected correct output(s) from the options (i) to (iv) for the following C++ code. Also, find out the minimum and the maximum value that can be assigned to the variable Stop
used in the code:
void main()
{
int Begin = 3, Stop;
for(int Run = 1; Run < 4; Run++)
{
Stop = random(Begin) + 6;
cout << Begin++ << Stop << "*";
}
}
void main()
{
int Begin = 3, Stop;
for(int Run = 1; Run < 4; Run++)
{
Stop = random(Begin) + 6;
cout << Begin++ << Stop << "*";
}
}
Answer:
- Correct Output: 36*46*57*
- Minimum value of Stop: 6
- Maximum value of Stop: 8
Question 8:
Find the output:
int A[2][3] = {{1, 2, 3}, {5, 6, 7}};
for(int i = 1; i < 2; i++)
for(int j = 0; j < 3; j++)
cout << A[i][j] << endl;
int A[2][3] = {{1, 2, 3}, {5, 6, 7}};
for(int i = 1; i < 2; i++)
for(int j = 0; j < 3; j++)
cout << A[i][j] << endl;
Answer:
5 6 7
Question 9:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction:
#include
typedef char Text(80);
void main()
{
Text T = "Indian";
int Count = strlen(T);
cout << T << 'has' << Count << 'characters'
<< endl;
}
#include
typedef char Text(80);
void main()
{
Text T = "Indian";
int Count = strlen(T);
cout << T << 'has' << Count << 'characters'
<< endl;
}
Corrected Code:
#include <iostream.h>
#include <string.h>
typedef char Text[80];
void main()
{
Text T = "Indian";
int Count = strlen(T);
cout << T << " has " << Count << " characters" << endl;
}
Question 10:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction:
#include
typedef char[80] String;
void main()
{
String S = "Peace";
int L = strlen(S);
cout << S << 'has' << L << 'characters' << endl;
}
#include
typedef char[80] String;
void main()
{
String S = "Peace";
int L = strlen(S);
cout << S << 'has' << L << 'characters' << endl;
}
Corrected Code:
#include <iostream.h>
#include <string.h>
typedef char String[80];
void main()
{
String S = "Peace";
int L = strlen(S);
cout << S << " has " << L << " characters" << endl;
}
Question 11:
Study the following C++ program and select the possible output(s) from it: Find the maximum and minimum value of L.
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
randomize(); // Turbo C++ specific
char P[] = "C++PROGRAM";
long L;
for(int i = 0; P[i] != 'R'; i++)
{
L = random(sizeof(L)) + 5;
cout << P[L] << " ";
}
}
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
randomize(); // Turbo C++ specific
char P[] = "C++PROGRAM";
long L;
for(int i = 0; P[i] != 'R'; i++)
{
L = random(sizeof(L)) + 5;
cout << P[L] << " ";
}
}
Explanation: Random values of L range from 5 to 8. The loop ends when P[i] == 'R'
at index 4.
Possible Output: O R A G
Minimum value of L: 5
Maximum value of L: 8
Question 12: Write a user-defined function DIVTQ which takes an integer as a parameter and returns whether it is divisible by 13 or not. The function should return 1 if it is divisible by 13, otherwise it should return 0.
int DIVT(int x)
{
int f;
if (x % 13 == 0)
f = 1;
else
f = 0;
return f;
}
Question 13:
Find the output of the following program segment:
#include <iostream.h>
#include <ctype.h>
void Mycode(char Msg[], char CH)
{
for(int cnt = 0; Msg[cnt] != '\0'; cnt++)
{
if (Msg[cnt] >= 'B' && Msg[cnt] <= 'G')
Msg[cnt] = tolower(Msg[cnt]);
else if (Msg[cnt] == 'N' || Msg[cnt] == 'n' || Msg[cnt] == ' ')
Msg[cnt] = CH;
else if (cnt % 2 == 0)
Msg[cnt] = toupper(Msg[cnt]);
else
Msg[cnt] = Msg[cnt-1];
}
}
void main()
{
char MyText[] = "Input Raw";
Mycode(MyText, '@');
cout << "NEW TEXT: " << MyText << endl;
}
#include <iostream.h>
#include <ctype.h>
void Mycode(char Msg[], char CH)
{
for(int cnt = 0; Msg[cnt] != '\0'; cnt++)
{
if (Msg[cnt] >= 'B' && Msg[cnt] <= 'G')
Msg[cnt] = tolower(Msg[cnt]);
else if (Msg[cnt] == 'N' || Msg[cnt] == 'n' || Msg[cnt] == ' ')
Msg[cnt] = CH;
else if (cnt % 2 == 0)
Msg[cnt] = toupper(Msg[cnt]);
else
Msg[cnt] = Msg[cnt-1];
}
}
void main()
{
char MyText[] = "Input Raw";
Mycode(MyText, '@');
cout << "NEW TEXT: " << MyText << endl;
}
Correct Output: I@PPT@RRW
Question 14:
Observe the following program and find out, which output(s) out of (i) to (iv) will not be expected from the program? What will be the minimum and the maximum value assigned to the variable chance
?
#include <iostream.h>
#include <stdlib.h>
void main()
{
randomize();
int Game[] = {10, 16}, P;
int Turns = random(2) + 5; // 5 or 6
for(int T = 0; T <= 2; T++)
{
P = random(2); // 0 or 1
cout << Game[P] + Turns << "*";
}
}
#include <iostream.h>
#include <stdlib.h>
void main()
{
randomize();
int Game[] = {10, 16}, P;
int Turns = random(2) + 5; // 5 or 6
for(int T = 0; T <= 2; T++)
{
P = random(2); // 0 or 1
cout << Game[P] + Turns << "*";
}
}
Possible Values:
- Game[0] + 5 = 15
- Game[1] + 5 = 21
- Game[0] + 6 = 16
- Game[1] + 6 = 22
Expected Outputs:
- 15*22*
- 21*22*
- 16*21* ❌ (Not expected)
Minimum value of Turns (chance): 5
Maximum value of Turns (chance): 6
Question 15:
Observe the following program and find out which output(s) out of (i) to (iv) will not be expected from the program? Also, find the minimum and maximum value assigned to the variable Chance.
#include <iostream.h>
#include <stdlib.h>
void main()
{
randomize();
int Arr[] = {9, 6}, N;
int Chance = random(2) + 10;
for(int C = 0; C < 2; C++)
{
N = random(2);
cout << Arr[N] + Chance << "#";
}
}
#include <iostream.h>
#include <stdlib.h>
void main()
{
randomize();
int Arr[] = {9, 6}, N;
int Chance = random(2) + 10;
for(int C = 0; C < 2; C++)
{
N = random(2);
cout << Arr[N] + Chance << "#";
}
}
Expected values of Arr[N] + Chance:
- 9 + 10 = 19
- 6 + 10 = 16
- 9 + 11 = 20
- 6 + 11 = 17
Options:
- (i) 9#6# ❌
- (ii) 19#17# ❌
- (iii) 19#16# ✅
- (iv) 20#16# ❌
Unexpected Outputs: (i), (ii), and (iv)
Minimum Value of Chance: 10
Maximum Value of Chance: 11
Question 16:
Output of the following C++ program:
typedef char WORD[80];
void CODEWORD(WORD W)
{
int LEN = strlen(W);
for(int I = 0; I < LEN - 1; I += 2)
{
char SW = W[I];
W[I] = W[I + 1];
W[I + 1] = SW;
}
for(I = 1; I < LEN; I += 2)
if(W[I] >= 'A' && W[I] <= 'U')
W[I] = '#';
}
void main()
{
WORD Wrd = "EDITORIALS";
CODEWORD(Wrd);
cout << Wrd << endl;
}
typedef char WORD[80];
void CODEWORD(WORD W)
{
int LEN = strlen(W);
for(int I = 0; I < LEN - 1; I += 2)
{
char SW = W[I];
W[I] = W[I + 1];
W[I + 1] = SW;
}
for(I = 1; I < LEN; I += 2)
if(W[I] >= 'A' && W[I] <= 'U')
W[I] = '#';
}
void main()
{
WORD Wrd = "EDITORIALS";
CODEWORD(Wrd);
cout << Wrd << endl;
}
Output:
DETIROAISL
Question 17:
Correct the following code and rewrite it:
// Original Code
#define convert(P,Q)P+2*Q;
void main()
{
float A, B, Result;
cin >> A >> B;
result = Convert[A,B];
cout << "output" << Result << ;
endline;
}
// Original Code
#define convert(P,Q)P+2*Q;
void main()
{
float A, B, Result;
cin >> A >> B;
result = Convert[A,B];
cout << "output" << Result << ;
endline;
}
Corrected Code:
#define convert(P,Q) (P + 2 * Q)
void main()
{
float A, B, Result;
cin >> A >> B;
Result = convert(A, B);
cout << "Output" << Result << endl;
}
Class 12 Computer Science (C++) – Topic 2: Flow Control
Short Answer Type Questions – II
Question 1:
Rewrite the C++ code after removing syntactical errors:
// Original Code
#Define float MaxSpeed =60.5;
void main()
}
int MySpeed char Alert = 'N';
cin>>MySpeed;
if MySpeed>MaxSpeed
Alert = 'Y';
cout<
// Original Code
#Define float MaxSpeed =60.5;
void main()
}
int MySpeed char Alert = 'N';
cin>>MySpeed;
if MySpeed>MaxSpeed
Alert = 'Y';
cout<
Corrected Code:
#define MaxSpeed 60.5
void main()
{
int MySpeed;
char Alert = 'N';
cin >> MySpeed;
if (MySpeed > MaxSpeed)
Alert = 'Y';
cout << Alert << endl;
}
Question 2:
Output of the following C++ program:
void Location(int &X, int Y=4)
{
Y += 2;
X += Y;
}
void main()
{
int PX = 10, PY = 2;
Location(PY);
cout << PX << "," << PY << endl;
Location(PX, PY);
cout << PX << "," << PY << endl;
}
void Location(int &X, int Y=4)
{
Y += 2;
X += Y;
}
void main()
{
int PX = 10, PY = 2;
Location(PY);
cout << PX << "," << PY << endl;
Location(PX, PY);
cout << PX << "," << PY << endl;
}
Output:
10,8 20,8
Question 3:
Output of the following program:
#include <iostream.h>
void in(int x, int y, int &z)
{
x += y;
y--;
z *= (x - y);
}
void out(int z, int y, int &x)
{
x *= y;
y++;
z /= (x + y);
}
void main()
{
int a = 20, b = 30, c = 10;
out(a, c, b);
cout << a << "#" << b << "#" << c << "#" << endl;
in(b, c, a);
cout << a << "@" << b << "@" << c << "@" << endl;
out(a, b, c);
cout << a << "$" << b << "$" << c << "$" << endl;
}
#include <iostream.h>
void in(int x, int y, int &z)
{
x += y;
y--;
z *= (x - y);
}
void out(int z, int y, int &x)
{
x *= y;
y++;
z /= (x + y);
}
void main()
{
int a = 20, b = 30, c = 10;
out(a, c, b);
cout << a << "#" << b << "#" << c << "#" << endl;
in(b, c, a);
cout << a << "@" << b << "@" << c << "@" << endl;
out(a, b, c);
cout << a << "$" << b << "$" << c << "$" << endl;
}
Output:
20#300#10# 620@300@10@ 620$300$3000$
Question 4:
Output of the following program segment:
int a = 3;
void demo(int x, int y, int &z)
{
a += x + y;
z = a + y;
y += x;
cout << x << '*' << y << '*' << z << endl;
}
void main()
{
int a = 2, b = 5;
demo(::a, a, b);
demo(::a, a, b);
}
int a = 3;
void demo(int x, int y, int &z)
{
a += x + y;
z = a + y;
y += x;
cout << x << '*' << y << '*' << z << endl;
}
void main()
{
int a = 2, b = 5;
demo(::a, a, b);
demo(::a, a, b);
}
Output:
3*5*10 8*10*20
Class 12 Computer Science (C++) – Topic 4: Structures
Short Answer Type Questions – II
Question 1:
Find the output of the following program:
#include <iostream.h>
void ChangeArray(int number, int ARR[], int Size)
{
for(int L = 0; L < Size; L++)
if(L < number)
ARR[L] += L;
else
ARR[L] *= L;
}
void Show (int ARR[], int Size)
{
for(int L = 0; L < Size; L++)
(L % 2 == 0) ?
cout << ARR[L] << "#" :
cout << ARR[L] << endl;
}
void main()
{
int Array[] = {30, 20, 40, 10, 60, 50};
ChangeArray(3, Array, 6);
Show(Array, 6);
}
Output:
30#21
42#30
240#250
#include <iostream.h>
void ChangeArray(int number, int ARR[], int Size)
{
for(int L = 0; L < Size; L++)
if(L < number)
ARR[L] += L;
else
ARR[L] *= L;
}
void Show (int ARR[], int Size)
{
for(int L = 0; L < Size; L++)
(L % 2 == 0) ?
cout << ARR[L] << "#" :
cout << ARR[L] << endl;
}
void main()
{
int Array[] = {30, 20, 40, 10, 60, 50};
ChangeArray(3, Array, 6);
Show(Array, 6);
}
Question 2:
Find the output of the following program:
#include <iostream.h>
void Switchover(int A[], int N, int split)
{
for(int K = 0; K < N; K++)
if(K < split)
A[K] += K;
else
A[K] *= K;
}
void Display(int A[], int N)
{
for(int K = 0; K < N; K++)
(K % 2 == 0) ? cout << A[K] << "%" :
cout << A[K] << endl;
}
void main()
{
int H[] = {30, 40, 50, 20, 10, 5};
Switchover(H, 6, 3);
Display(H, 6);
}
Output:
30%41
52%60
40%25
#include <iostream.h>
void Switchover(int A[], int N, int split)
{
for(int K = 0; K < N; K++)
if(K < split)
A[K] += K;
else
A[K] *= K;
}
void Display(int A[], int N)
{
for(int K = 0; K < N; K++)
(K % 2 == 0) ? cout << A[K] << "%" :
cout << A[K] << endl;
}
void main()
{
int H[] = {30, 40, 50, 20, 10, 5};
Switchover(H, 6, 3);
Display(H, 6);
}
Question 3:
Find the output of the following program:
#include <iostream.h>
struct GAME
{
int Score, Bonus;
};
void Play(GAME &g, int N = 100)
{
g.Score++;
g.Bonus += N;
}
void main()
{
GAME G = {110, 50};
Play(G, 10);
cout << G.Score << ":" << G.Bonus << endl;
Play(G);
cout << G.Score << ":" << G.Bonus << endl;
Play(G, 15);
cout << G.Score << ":" << G.Bonus << endl;
}
Output:
111 : 60
112 : 160
113 : 175
#include <iostream.h>
struct GAME
{
int Score, Bonus;
};
void Play(GAME &g, int N = 100)
{
g.Score++;
g.Bonus += N;
}
void main()
{
GAME G = {110, 50};
Play(G, 10);
cout << G.Score << ":" << G.Bonus << endl;
Play(G);
cout << G.Score << ":" << G.Bonus << endl;
Play(G, 15);
cout << G.Score << ":" << G.Bonus << endl;
}
Question 4:
Rewrite the program after removing syntax errors and underline each correction:
#include <iostream.h>
struct Pixels
{
int Color, Style;
};
void ShowPoint(Pixels P)
{
cout << P.Color << P.Style << endl;
}
void main()
{
Pixels Point1 = {5, 3};
ShowPoint(Point1);
Pixels Point2 = Point1;
Point1.Color += 2;
ShowPoint(Point2);
}
#include <iostream.h>
struct Pixels
{
int Color, Style;
};
void ShowPoint(Pixels P)
{
cout << P.Color << P.Style << endl;
}
void main()
{
Pixels Point1 = {5, 3};
ShowPoint(Point1);
Pixels Point2 = Point1;
Point1.Color += 2;
ShowPoint(Point2);
}
Note: The output will be 53
twice because Point2
remains unchanged after Point1
is modified.
Question 5:
Find the output of the following program:
#include <iostream.h>
struct POINT
{
int X, Y, Z;
};
void Stepln(POINT &P, int Step = 1)
{
P.X += Step;
P.Y -= Step;
P.Z += Step;
}
void StepOut(POINT &P, int Step = 1)
{
P.X -= Step;
P.Y += Step;
P.Z -= Step;
}
void main()
{
POINT P1 = {15, 25, 5}, P2 = {10, 30, 20};
Stepln(P1);
StepOut(P2, 4);
cout << P1.X << "," << P1.Y << "," << P1.Z << endl;
cout << P2.X << "," << P2.Y << "," << P2.Z << endl;
Stepln(P2, 12);
cout << P2.X << "," << P2.Y << "," << P2.Z << endl;
}
Output:
16,24,6
6,34,16
18,22,28
#include <iostream.h>
struct POINT
{
int X, Y, Z;
};
void Stepln(POINT &P, int Step = 1)
{
P.X += Step;
P.Y -= Step;
P.Z += Step;
}
void StepOut(POINT &P, int Step = 1)
{
P.X -= Step;
P.Y += Step;
P.Z -= Step;
}
void main()
{
POINT P1 = {15, 25, 5}, P2 = {10, 30, 20};
Stepln(P1);
StepOut(P2, 4);
cout << P1.X << "," << P1.Y << "," << P1.Z << endl;
cout << P2.X << "," << P2.Y << "," << P2.Z << endl;
Stepln(P2, 12);
cout << P2.X << "," << P2.Y << "," << P2.Z << endl;
}
Question 6:
Find the output of the following program:
#include <iostream.h>
struct Score
{
int Year;
float Topper;
};
void Change(Score *S, int x = 20)
{
S->Topper = (S->Topper + 25) - x;
S->Year++;
}
void main()
{
Score Arr[] = { {2007, 100}, {2008, 95} };
Score *Point = Arr;
Change(Point, 50);
cout << Arr[0].Year << "#" << Arr[0].Topper << endl;
Change(++Point);
cout << Point->Year << "#" << Point->Topper << endl;
}
Output:
2008#75
2009#100
#include <iostream.h>
struct Score
{
int Year;
float Topper;
};
void Change(Score *S, int x = 20)
{
S->Topper = (S->Topper + 25) - x;
S->Year++;
}
void main()
{
Score Arr[] = { {2007, 100}, {2008, 95} };
Score *Point = Arr;
Change(Point, 50);
cout << Arr[0].Year << "#" << Arr[0].Topper << endl;
Change(++Point);
cout << Point->Year << "#" << Point->Topper << endl;
}
Question 7:
Find the output of the following program:
#include <iostream.h>
struct THREE_D
{
int X, Y, Z;
};
void Moveln(THREE_D &T, int Step = 1)
{
T.X += Step;
T.Y -= Step;
T.Z += Step;
}
void MoveOut(THREE_D &T, int Step = 1)
{
T.X -= Step;
T.Y += Step;
T.Z -= Step;
}
void main()
{
THREE_D T1 = {10, 20, 5}, T2 = {30, 10, 40};
Moveln(T1);
MoveOut(T2, 5);
cout << T1.X << "," << T1.Y << "," << T1.Z << endl;
cout << T2.X << "," << T2.Y << "," << T2.Z << endl;
Moveln(T2, 10);
cout << T2.X << "," << T2.Y << "," << T2.Z << endl;
}
Output:
11,19,6
25,15,35
35,5,45
#include <iostream.h>
struct THREE_D
{
int X, Y, Z;
};
void Moveln(THREE_D &T, int Step = 1)
{
T.X += Step;
T.Y -= Step;
T.Z += Step;
}
void MoveOut(THREE_D &T, int Step = 1)
{
T.X -= Step;
T.Y += Step;
T.Z -= Step;
}
void main()
{
THREE_D T1 = {10, 20, 5}, T2 = {30, 10, 40};
Moveln(T1);
MoveOut(T2, 5);
cout << T1.X << "," << T1.Y << "," << T1.Z << endl;
cout << T2.X << "," << T2.Y << "," << T2.Z << endl;
Moveln(T2, 10);
cout << T2.X << "," << T2.Y << "," << T2.Z << endl;
}