Kerala Plus One Computer Science Chapter Wise Questions and Answers Chapter 7 Control Statements
Very Short Answer Type Questions
(Score 1)
Question 1.
The goto statement causes control to go to
a. an operator
b. a label
c. a variable
d. a function
Answer:
b. a label
Question 2.
A break statement causes an exit
a. only from the innermost loop.
b. only from the innermost switch.
c. from all loops and switches.
d. from the innermost loop or switch.
Answer:
d. from the innermost loop or switch
Question 3.
The exit( ) function takes the control out of
a. the function it appears in.
b. the loop it appears in.
c. the block it appears in.
d. the program it appears in.
Answer:
d. the program it appears in.
Question 4.
Which one is an exit control loop?
a. for loop
b. do….while loop
c. while loop
d. all of these
Answer:
b. do….while loop
Question 5.
statement is used to transfer control back to the calling program or to come out of a function,
a. goto
b. exit
c. return
d. continue
Answer:
c. return
Question 6.
From the following which is not optional with switch statement.
a. default
b. case
c. break
d. all of the above
Answer:
a. default
Short Answer Type Questions
(Score 2)
Question 1.
What do you mean by jump statements ?
Answer:
The statements that facilitate the transfer of program control from one place to another are called jump statements.
8.
What is loop control variable ?
Answer:
Since loops work on the basis of such conditions, a variable like the counter will be used to construct a loop. This variable is generally known as loop control variable because it actually controls the execution of the loop.
Short Answer Type Questions
(Score 3)
Question 1.
Write a program to check your eligibility for admission (on the basis of mark).
Answer:
#include<iostream> using namespace std; int main() { int score ; cout << “Enter your score: cin >> score; if (score >= 500) cout << “You are Eligible”; return o; }
Question 2.
What are the are the requirements to implement a multibranching using switch statement?
Answer:
The following are the requirements to implement a multi branching using switch statement:
- Conditions involve only equality checking. In other cases, it should be converted into equality expression.
- The first operand in all the equality expressions should be the same variable or expression.
- The second operand in these expressions should be integer or character constants.
Question 3.
To find the larger number using the conditional operator.
Answer:
#include <iostream> using namespace std; int main() { int num1, num2; cout << “Enter two numbers:”; cin>> num1>> num2 ; (num1>num2)? cout<<num1<<“ is larger” : cout<<num2<<“ is larger”; return 0; }
Question 4.
To print the first 25 natural numbers.
Answer:
#include<iostream> using namespace std; int main() { int n = 1; while(n <= 25) { . cout<< n << “ + + n; } return o; }
Question 5.
Compare break and continue statements.
Answer:
break:
- Used with switch and loops.
- Brings the program control outside the switch or loop by skipping the rest of the statements within the block.
- Program control goes out of the loop even though the test expression is True.
continue:
- Used only with loops.
- Brings the program control to the beginning of the loop by skipping the rest of the statements within the block.
- Program control goes out of the loop only when the test expression becomes False.
Long Answer Type Questions
(Score 5)
Question 1.
Checks whether a given character is an alphabet or a digit.
Answer:
#include<iostream> using namespace std; int main( ) { char ch; cout << “Enter the characterfonly small letter): cin >> ch; if (ch >= ‘a’ && ch <= ‘z’) cout << “You entered an alphabet”; if (ch >= ‘o’ && ch <= ‘9’) cout << “You entered a digit\n”; return 0; }
Question 2.
Write a C++ program to check whether a given number is even or odd.
Answer:
#include <iostream> using namespace std; int main() { int num; cout << “Enter the number: ”; cin >> num; if (num%2 == o) cout << “The given number is Even”; else cout << “The given number is Odd”; return 0; }
Question 3.
Write a program to find the largest among three numbers.
Answer:
#include <iostream> using namespace std; int main() { int a, b, c; cout << “Enter three different numbers: ”; cin >> a >> b >> c ; if (a > b) { if (a > c) cout<<“The largest number is:”<< a; else * cout << “The largest number is:”<<c; } else { if (b > c) cout << “The largest number is:”<<b; else cout << “The largest number is: ”<<c; } return o; }
Question 4.
To find the grade of a student for a given score:
Answer:
#include <iostream> using namespace std; int main() { int score; cout << “Enter your score:”; cin >> score; " if (score >= 80) cout << “A Grade”; else if (score >= 60) cout << “B Grade”; else if (score >= 40) cout << “C grade”; else if (score >= 30) cout << “D grade”; else cout << “E Grade”; return 0; }
Question 5.
To check whether the given year is leap year or not.
Answer:
#include <iostream> using namespace std; void main( ) { int y ; cout <<“Enter the year (in 4-digits):”; cin >> y; if (y%1oo == o) { if (y%400 == o) cout << “Leap year\n”; else cout<< “Not a leap year\n”; } else if (y%4 == 0) cout << “Leap year\n”; else cout<< “Not a leap year\n”; return 0; }
Question 6.
To display the day of a week using switch statement.
Answer:
#include <iostream> using namespace std; int main( ) { int day ; cout << “Enter a number between 1 and 7: ”; cin >> day ; switch (day) { case 1: cout << “Sunday”; break; case 2: cout << “Monday”; break; case 3: cout << “Tuesday”; break; case 4: cout << “Wednesday”; break; case 5: cout << “Thursday”; break; case 6: cout << “Friday”; break; case 7: cout << “Saturday”; break; default: cout << “Wrong input”; } return o; }
Question 7.
To check whether the given character is a vowel or not.
Answer:
#include <iostream> using namespace std; int main() { char ch; cout<<“Enter the character to check:”; cin>>ch; switch(ch) { case ‘A’ : case ‘a’ : case ‘E’ : case ‘e’ : case T : case ‘i’ : case ‘O’ : case ‘o’ : case ‘U’ : case ‘u’ : cout<<“The given character is a vowel”; break; default : cout<<“The given character is not a vowel”; } return o; }
Question 8.
Distinguish between switch and else if ladder statements.
Answer:
Switch:
- Permits multiple branching.
- Evaluates conditions with equality operator only.
- Case constant must be an integer or a character type value.
- When no match is found, default statement is executed.
- break statement is required to exit from switch statement.
- More efficient when the same variable or expression is compared against a set of values for equality.
else if ladder:
- Permits multiple branching.
- Evaluates any relational or logical expression.
- Condition may include range of values and floating point constants.
- When no expression evaluates to True, else block is executed.
- Program control automatically goes out after the completion of a block.
- More flexible and versatile com-pared to switch.
Question 9.
To find the sum of even numbers upto 100 using while loop.
Answer:
#include<iostream> using namespace std; int main( ) { int i, sum = o; i = 2; whilef i<= 100) { sum = sum + i; i = i + 2; } cout<<“\n The sum of even numbers up to too is: ”<<sum; return 0; }
Question 10.
To find the factorial of a number using for loop.
Answer:
#include <iostream> using namespace std; int main( ) { int n, i; long fact=i; cout<<“Enter the number:”; cin> >n; for (i=t; i<=n; ++i) fact = fact * i; cout << “Factorial of ”<< n <<“ is ”<< fact; return 0; }
Question 11.
T0 find the area of a rectangle using do while loop.
Answer:
#include<iostream> using namespace std; int main() { float 1, b, area; char ch; do { cout << “Enter length and breadth:”; cin >> 1 >> b; area = 1 * b; cout << “Area = ” << area; cout << “Any more rectangle (Y/N)?”; cin >> ch; } while (ch == ‘Y’ || ch == ‘y’); return o; }
Question 12.
To display a triangle of stars,
*
* *
* * *
* * * *
* * * * *
Answer:
#include<iostream> using namespace std; int main( ) int i, j; char ch = for(i=i; i<=5; ++i) { cout<< “\n” ; for(j=i; j<=i; ++j) cout<<ch; } return 0; }
Question 13.
Write a C++ program to check whether the given number is prime or not.
Answer:
#include<iostream> #include<cstdlib> using namespace std; int main() { int i, num; cout<<“Enter the number: cin>>num; for(i=2; i<=num/2; ++i) { if(num%i == o) { cout<<“Not a Prime Number”; exit(o); } } cout<<“Prime Number”; return o; }
Question 14.
To find the roots of a quadratic equation.
Answer:
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, c, rooti, root2, d; cout<< “Enter the three coefficients:”; cin >> a >> b >> c ; if (!a) cout<<“Value of \‘a \’ should not be zero\n”<< “Aborting!!!! !\n”; else { d =b*b-4*a*c; if (d > o) { root1 = (-b + sqrt(d))/(2*a); root2 = (-b - sqrt(d))/(2*a); cout<<“Roots are REAL and UN¬EQUAL \n”; cout<<“Root1 = ”<<root1<<“\tRoot2 =”<<root2; } else if (d == o) { root1 = -b/(2*a); cout«“Roots are REAL and EQUAL\n”; cout<<“Root1 =” <<rootl; } else cout<<“Roots are COMPLEX and IMAGINARY”; } return o; }
Question 15.
To print n terms of the Fibonacci series.
Answer:
#include <iostream> using namespace std; int main() { int first=o, second=i, third, n; cout<<“\nEnter number of terms in the series: ”; cin>>n; cout<<first<<“\t”<<second; for(int i=3; i<=n; ++i) { third = first + second; cout<<“\t”<< third; first = second; second = third; } return o; }
Question 16.
To check whether the given number is palindrome or not.
Answer:
#include <iostream> using namespace std; int main() { int num, c, digit, rev=o; cout<<“Enter the number: ”; cin>>num; c=num; while(num != o) { digit = num % 10; rev = (rev * io)+ digit; num = num/io; } cout<<“The reverse of the number is” <<rev; if (rev == c) cout<<“\nThe given number is a pal indrome.”; else cout<<“\nThe given number is not a palindrome.”; return o; }
Question 17.
Read n integers and print the largest among them.
Answer:
#include <iostream> using namespace std; int main() { int num, big, c; cout<<“How many numbers to be entered?”; cin >> c; cout<<“\nEnter first number: ”; cin >> num; big = num; for(int i=2; i<=c; i++) { cout<<“\nEnter next number: ”; cin >> num; if(num > big) big = num; } cout<<“\nThe largest number is” << big; return o; }
Textual Questions & Answers
Question 1.
Write the significance of break statement in switch statement. What is the effect of absence of break in a switch statement? (2)
Answer:
When a break statement is encountered, the program control goes to the statements following the switch statement.If the break statement is absent, even after finding a match other cases fol-lowing are also checked. If no match is found, statements in the default block get executed.
Question 2.
What will the output of the foll-owing code fragment be? (1)
for(i=i; i<=iO; ++i); cout<<i+5;
Answer:
16
Question 3.
“Rewrite the following statement using while and do while loops. (2)
for (i=i; i<=io; i++)
cout<<i;
Answer:
While:-
i = 1; while (i< = 10) cout<<i; i++; }
Do while:-
i = 1; do { cout<<i; i++; } while (i<=10)
Question 4.
How many times will the following loop execute? (1)
int s=o,i=o;
while(i++<5)
s+=i;
Answer:
5 times
Question 5.
Write the name of the header file which contains the exit() function. (1)
Answer:
process.h
Question 6.
Which statement in C++ can transfer control of the program to a named label? (1)
Answer:
goto statement
Question 7.
Write the purpose of default statement in switch statement. (1)
Answer:
If no match is found, the statements in the default block get executed. The default statement is optional and if it is missing, no action takes place when all matches fail.
Question 8.
Consider two program fragments given below. (2)
// version 1 cin>>mark;
if (mark > = 90) cout<<“ A+”;
if (mark > = 80 && mark <90) cout<<“ A”;
if (mark > = 70 && mark <80) cout<<“B+”;
if (mark > = 60 && mark <70) cout<<“B”;
//version 2 cin>>mark;
if (mark>=9o) cout<<“ A+”;
else if (mark>=8o && mark <90) cout<<“ A”;
else if (mark>=70 && mark <80) cout<<“B+”;
else if (mark>=6o && mark <70) cout<<“B”;
Discuss the advantages of version 2 over version 1.
Answer:
In version 1, all the if conditions are checked. But in version 2, if the first condition is true all other conditions will not be checked.
This will reduce the execution time of version 2.
Question 9.
Briefly explain the working of a for loop along with its syntax. Give an example of for loop to support your answer. (3)
Answer:
for loop is an entry controlled loop. All the three loop elements (initialisation, test expression and update statement) are placed together in for statement. So it makes the program compact. The syntax is:
for (initialisation; test expression; up-date statement)
{ body-of-the-loop; } These three steps (test, body, update) are continued until the test
expression is evaluated to False, eg., for (n=i; n< = io; ++n) { cout << “hello”; }
Question 10.
Compare and discuss the suitability of three loops in different situations. (3)
Answer:
for loop:
- Entry controlled loop.
- Initialization along with loop definition.
- No guarantee to execute the loop body at least once.
while loop:
- Entry controlled loop.
- Initialization before loop definition.
- No guarantee to execute the loop body at least once.
do….while loop:
- Exit controlled loop.
- Initialization before loop definition.
- Will execute the loop body atleast once even though the condition is False.
Question 11.
Consider the following if else if statement. Rewrite it with switch statement. (2)
if (a==i) cout << “One”;
else if (a==o) cout << “Zero”;
else
cout << “Not a binary digit”;
Answer:
switch (a) { case 1: cout<<“One”; break; case o: cout<<“Zero”; break; default: cout<<“Not a binary digit”;
Question 12.
What is wrong with the following while statement if the value of z = 3?
while (z>=o)
sum+=z; (1)
Ans:
Endless loop as there is no updation for the variable z. The condition is always true and the execution never stop.
Question 13.
What will the output of the following code fragments be? (2)
for (outer=10; outer > 5; – -outer)
{
for (inner=i; inner<4; ++inner) cout< <outer< <“\t”< <inner <<endl;
}
Answer:
Question 14:
What will the output of the given code fragments be? Explain. (2)
for (n = 1; n <= 10; ++n)
{
for ( m=1; m < = 5 ; ++m)
num = n*m;
cout<<num <<endl;
}
Answer:
5
10
15
20
25
30
35
40
45
50
Only the outer loop statement will execute. The inner loop execute only once because of the absence of {}. The result calculated by the inner loop is stored in variable num and the value of num in the last iteration of inner loop is displayed using the cout statement.
Question 15.
Write the importance of a loop control variable. Briefly explain the different parts of a loop. (5)
Answer:
Since loops work on the basis of such conditions, a variable like the counter will he used to construct a loop. This variable is generally known as loop control variable because it actually controls the execution of the loop.
The loop has four parts:
- Initialisation: Before entering a loop, its control variable must be initialized. During initialisation, the loop control variable gets its first value. The initialisation statement is executed only once.
- Test expression: It decides whether the loop-body will be executed or not. If the test expression evaluates to True, the loop-body gets executed, otherwise it will not he executed.
- Update statement: The update statement is executed before the next iteration.
- Body of the loop: The statements that need to be executed repeatedly constitute the body of the loop.
Question 16.
What output will be produced by the following code fragment? (2)
int val, res, n=1ooo;
cin>>val;
res = n+val > 1750 ? 400 : 200;
a. If the input is 2000
b. If the input is 500
Answer:
a. 400
b. 200
Question 17.
Write a program to find the sum of digits of a number using
a. Entry controlled loop. (2)
b. Exit controlled loop. (3)
Answer:
a.
using entry controlled loop, i.e., while loop. #include<iostream> using namespace std; int main() { int val, num, sum = 0; cout << "Enter the number : cin >> val; num = val; while (num) { sum = sum + num % 10; num = num / 10; } cout << "The sum of the digits of "<< val << " is " << sum; return 0; }
b. using exit controlled loop, i.e., do… while loop.
#include<iostream> using namespace std; int main() { int val, num, sum = 0; cout << "Enter the number : "; cin >> val; num = val; do { sum = sum + num % 10; num = num / 10; } while(num!=o); cout << "The sum of the digits of "<< val << " is ” << sum; return 0; }
Question 18.
Write a program to print Armstrong numbers less than 1000. (An Armstrong number is a number which is equal to the sum of cubes of its digits,
eg., 153 = I3+53+33). (5)
Answer:
#include<iostream> using namespace std; int main() { int n, i, r, cb; for (i=i; i<=iooo; i++) { n = i; cb = o; while (n) { r = n%io; cb = cb + r * r * r; n = n/10; } if (cb==i) cout<<i<<“,”’ } return 0; }
Question 19.
Explain the different jump statements available in C++. (5)
Answer:
The statements that facilitate the transfer of program control from one place to another are called jump statements. They are:
- Return statement: It is used to trans-fer control back to the calling program or to come out of a function.
- goto statement: It can transfer the program control to anywhere in the function. The target destination of a goto statement is marked by a label, which is an identifier.
- break statement: When break statement is encountered in a program, it takes the program control outside the immediate enclosing loop (for, while, do…while) or switch statement. Execution continues from the statement immediately after the control structure.
- continue statement: It is used for skipping over a part of the code within the loop-body and forcing the next iteration. The break statement forces termination of the loop, but continue statement forces next iteration of the loop.
- exit() function: It terminates the program itself. And can be used in a program only if we include the header file process.h.
Question 20.
Write a program to produce the following output using nested loop: (3)
A
AB
ABC
ABCD
ABCDE
Answer:
#include<iostream> using namespace std; int main( ) { char ch, c; for (ch=‘A’; ch<=‘E’; ch++) { for (c=‘A’; c<=ch; C++) cout<<c<<“\t”; cout<<“\n”; } return 0; }
Question 21.
Suppose you forgot to write the word else in an if…else statement. Discuss how it will affect the output of your program? (2)
Answer:
In an if. else statement, if the test expression evaluates to True, only the statement of if is executed. If the test expression evaluates to False statement of else is executed. If we forget to write else, the code following else will always execute either the condition is true or false.
Your website is best for a student like me