A. Iteration statement.
B. switch statement.
C. do while statement.
D. If statement.
?: is conditional operator. It can be used as follows: expression1 ?expressiion 2: expression3;
A. block statement.
B. set statement.
C. group statement.
D. collective statement.
Compound statement is a group of statements enclosed by a pair of braces ( { } ).
A. int sum=0;
B. sum=(sum+i);
C. while(sum<10000);
D. i=i+1;
The invalid statement is Sum=0 It should be int sum = 0;
A. do terminating with semicolon.
B. there should be space between do;
C. braces in 2nd statement.
D. ; should be removed and in place of{ } should have ( ).
Correct code is:
do
while(value >=9)
A. library function main.
B. break.
C. continue.
D. exit function.
exit( ) function is a standard library function that helps us break out of a program.
A. if statements.
B. goto statements.
C. for statement.
D. do while statement.
goto is a jump statement. The taget destination of a goto statement is marked by label.
A. assignment statements.
B. jump statements.
C. block statements.
D. break statement.
C++ has four statements that cause unconditionally branch: return, goto, break, and continue.
A. If statement.
B. switch construct.
C. goto statement.
D. jump statement.
The target destination of a goto statement is marked by label.The target goto and label must appear in the same function.
A. testing truth value.
B. comparing two variables.
C. comparison with a literal.
D. none of the above.
Since ’y’ is character literal and we are comparing it with ans by using comparison operator(= =).
A. Iteration statement.
B. switch statement.
C. do while statement.
D. If statement.
? is a conditional operator and checks whether condition is true or false. if condition is true following statements are executed otherwise not. thus it works similar to if statement.
A. looping construct.
B. conditional expression.
C. do while loop.
D. While loop.
Conditional expression is the basis for selection and iteration statements.
A. If statement.
B. switch construct.
C. Go to statement.
D. jump statement.
A goto statement uses label. A goto statement causes your program to unconditionally transfer control anywhere in the program or to the statement associated with the label specified on the goto statement.
A. semicolon.
B. double quotes.
C. comma.
D. single quotes.
The syntax of a for loop is: for (initialization expression; test expression; update expression) body of the loop;
A. selection statement.
B. jump statement.
C. sequence statement.
D. iteration statement..
Jump statements are statements that unconditionally transfer program control. There are following jump statements: return, goto, continue, break.
A. putting semicolon after paranthesis in for statement.
B. terminating for loop by colon.
C. just by terminating paranthesis.
D. putting a fullstop after paranthesis.
Time delay or infinite loop can be created by for(; ; )
A. iostream.h.
B. conio.h.
C. process.h.
D. ostream.h.
exit( ) function causes the program to terminate, as soon as it is encountered, no matter where it appears in the program listing.
A. while loop.
B. for loop.
C. do while loop.
D. if loop.
Continue statement forces the next iteration of the loop to take place, skipping any code in between.
A. multiple statements unit.
B. single unit.
C. selection unit.
D. two statements unit.
Any number of statements can be written inside a block and it is treated as a single unit, which may appear anywhere in the program.
A. error message.
B. square of 2 is 4.
C. square of 1 is 1.
D. square of i is i.
When loop executed once then i=1 so 1*1= 1
Now according to test condition i<2 the loop is terminated
so output is: square of 1 is 1.
A. 29
B. 30
C. 31
D. 28
Loop started at 0 and will print values for 29 iterations so the terminal value of J is = 28
A. a=8 b=6
B. a=0 b=5
C. a=6 b=4
D. a=5 b=5
Since if condition is true
so b= a++-1 = 5-1= 4(post increment of a is done after assignment)
now a become 6(as it is incremented).
A. 4
B. 2
C. 3
D. i
Since initial value of i is 2 and j is 2 so i*j = 2*2 =4
and according to test expression i<=2 the loop is terminated so output is 4.
A. initialization expression only.
B. test expression only.
C. update expression only.
D. initialization expression, test expression, and update expression.
Example of an infinite loop. for(; ;) cout<< "Ëndless for loop" << "/n"
A. empty for loop.
B. infinite for loop.
C. invalid for loop.
D. do while loop.
Example: for (j=23; ;--i) as there is no test condition so loop will executed infinite times.
A. for loop.
B. while loop.
C. do while loop.
D. do loop.
In do-while loop, the condition is checked at the end of the loop. So, the loop will be evaluated at least once, even if the condition is false.
A. before the outer loop terminates.
B. after the outer loop terminates.
C. with the outer loop termination.
D. only if the outer loop terminate.
In a nested loop, one loop is inside another loop body. Here, outer loop cannot terminate before inner loop.
A. testing truth value.
B. comparing two variables.
C. comparison with a literal.
D. assigning value y to ans.
== is a relational operator, whereas = is an assignment operator.
A. while loop.
B. do loop.
C. do while
D. test loop.
In while loop first the condition is evaluated, then loop execution takes place. If loop condition is false it will not be executed even once.
A. constant.except in a block.
B. switch statement.except in a block.
C. break statement.except in a block.
D. variable definition except in a block.
A goto statement can transfer the program conrol anywhere in the program.
A. int i;
B. i=1,
C. i=i+1
D. i>10;
In for loop every expression is separated by semicolon (;). So correct syntax of for loop is: for(i=1;i>10;i=i+1)
A. selection statements.
B. jump statements.
C. repetitive statements.
D. loop statements.
They are together referred to as selection construct. It means the execution of statement depending upon a condition test. It evaluates its truth value. It can also use a conditional statement viz., a decision making statement.
A. general statements.
B. selection Statements.
C. iteration statements.
D. jump statements.
The jump statements unconditionally transfer program control within a function.
A. Selection statements.
B. Iteration statements.
C. Conditional statements.
D. Decision making statements.
Iteration construct depends upon condition test, which can be either true or false.
A. do-while loop.
B. if-else condition.
C. while loop.
D. do statement.
In a while loop, the test expression is evaluated at the beginning of the loop. If the test expression is false, the loop body won't be executed.
A. switch statement.
B. If else.
C. current iteration.
D. current program.
continue is a jump statement,which instead of forcing termination, forces the next iteration of the loop to take place, skipping any code in between.
A. forces termination.
B. forces next iteration.
C. forces exit and continue.
D. forces next termination.
Continue statement continues executing the next code.
A. 1
B. 3
C. 2
D. 6
initial value of i= 2 and test condition evaluates only once so output is 2
A. If and If else
B. If else If ladder
C. The switch statement and nested switch
D. All of the above
All these statements provide selection control structures to execute a section of code if and only if an explicit run-time condition is met. For the if statement, if-else statement, and if -else-if ladder statement the condition is an expression which evaluates to a boolean value, that is, either true or false. For the switch statement, the selector is an expression which evaluates to an integral value. Selection statements are also referred to as conditional statements.
A. while i<10;
B. opening and terminating braces.
C. cout<<”5*”i;
D. Braces problem.
The incorrect statement is (c). It should be written as---- cout<<5*i;
A. 12345678910
B. 10
C. 11
D. 1
The semicolon is used to terminate the for loop so control will go to cout statement when the for loop will terminate.
10
8
6
Yes, it can be used as delay loop. It is doing nothing else as it has null statements.
The variations are:-
Do while loop
While loop
We can also consider- Null statement loop eg delay loop
The output of the above code segment is
1 2 3 4 5
The output of the above program segment is
10
9
8
7
1.The update expression(s) change the value(s) of loop variable(s).
2.The update expression(s) is carried out at the end of the loop, after the loop body is executed.
Nested if means the if within another if statement. This type of if statement has more than one condition dependent on each other.
if (condition)
{
if (condition)
{
---statement--;
}
}
a) A conditional operator has a more clear, concise and compact code but it is less obvious compared to if.
b) A conditional operator when used in nested forms becomes complex and difficult to understand but in case of if statement it is not so.
A nested if is the statement which contains if and else in its body. The nested if can be written in various ways.
The syntax of nested if is as follows:
if (condition)
{
if (condition)
{
--statement--;
}
else
{
--statement--;
}
}
If a semicolon is put after the test condition , the if statement ends there. The block or statement following is no more a part of if in such cases.
For example
if(a>5);
{
cout<<"Hello";
}
Now the statement cout<<"Hello"; is not a part of if condition
'if-else' is a conditional statement. In an if-else statement, if the condition is true the code associated with if is executed otherwise if the condition is false the code associated with else is executed.
if (grade=’A’)
cout<<”Grade received is A”;
Above condition test for a character literal.
It checks if the character variable stores a space or not. If it does, the number of spaces are incremented by 1.
The syntax of if statement is as follows:
if (condition)
{
--Statement;
else
{
---Statement;
Here, the statement, may consist of a Single or a Compound or Null (no statement).
Iteration construct means repetition of a set of statements depending upon a condition test.
A Block or Compound statements are written within a pair of braces { }.
Null or empty statement is used when the logic of the program does not require the statement but the Syntax of the programming language needs it.
Statements can move data, make decisions and take up repetitive actions.
i) More than one variable may be initialized outside the loop.
ii) The loop index may be initialized outside the loop
iii) Increment or Decrement to loop index outside the for statement
S1 is the statement which initializes the loop index.
R determines if the statement in the loop is to be revisited or test any condition.
R2 updates the loop index
a) if(answer="y") ---- comparing with a literal
b) If(a>b) ------ comparing variables
c) if (x) ----testing truth value of a variable
Following are the various points of differences:
i) The switch statement can only test for equality, whereas, if-else can evaluate for Relational or Logical conditions.
ii) If-else is more versatile ie it can handle ranges whereas, switch cannot.
iii)
The switch statement starts with the keyword switch followed by a variable in parenthesis.This is known as switch variable. The switch variablr can be integer or character.
switch(switch variable)
The switch block consists of as number of case blocks and each is associated with case constant.
Each of the case constant s followed by a colon.
case 1: statement;
break;
case 2: statement;
break;
.
.
so on
Compiler searches for case constant matching case variable. When a match is found the statement associated with that constant are executed.
Note: If the case block does not end with a break statement the control will be passed on to next case block.
Statements can be executed :-
i) Sequentially.
ii) Selectively.
iii) Iteratively.
#include < iostream.h >
#include < conio.h >
main()
{
int n,i;
long int fact;
cout<<"enter the no whose factorial is required ";
cin>>n;
fact=1;
i=1;
do
{
fact=fact*i
i++;
} while (i <= n );
cout<< ”factorial of “<< n<< ”is = ”<< fact << endl;
getch();
enter the no whose factorial is required 5
factorial of 5 is = 120
1.The switch statement can only test for equality. Whereas if can evaluate a Rational or logical expression ie multiple conditions.
2. The switch statement selects its branches by testing the same value of same variable (against a set of constants) whereas, if else construct lets you use a series of expressions that may involve unrelated variables and complex expressions.
3. If-else is more versatile than switch.
4. Switch statement is more efficient in terms of code.
5. If-else can handle floating point apart from handling integers and character tests, whereas a switch cannot.
A. < conio.h>.
B. < stdio.h>.
C. < stdlib.h>.
D. < iomanip.h>.
This is because the stdio.h defines the standard I/O pre-defined streams. Example :stdin, stdout, stdprn, and stderr.
A. islower().
B. tolower().
C. lower().
D. lowercase().
islower() function returns true (non-zero), if it is a lowercase alphabet.
A. islower().
B. tolower().
C. lower().
D. lowercase().
Nothing is changed, if we pass a lowercase letter or a non-alphabetic character to this function.
A. < iomanip.h>.
B. < math.h>.
C. < string.h>.
D. < ctype.h>.
The header file math.h contains all the mathematical functions including the arithmetic and trigonometric functions. With the help of these library functions, mathematical operations can be performed directly.
A. < iomanip.h>.
B. < math.h>.
C. < string.h>.
D. < ctype.h>.
The header file toupper() is a function that converts a string to uppercase. Therefore, it is included in string.h.
A. < iomanip.h>.
B. < math.h>.
C. < string.h>.
D. < ctype.h>.
This header file provides interfaces, for developing multi-threaded applications.
A. < iostream.h>.
B. < stdio.h>.
C. < string.h>.
D. < ctype.h>.
The stdio.h is a standard input output header file.
A. < stdio.h>.
B. < string.h>.
C. < iomanip.h>.
D. < math.h> .
The routines in math.h perform mathematical calculations and conversions.
A. iostream.h.`
B. string.h.
C. stdio.h.
D. iomanip.h.
It declares the C++ streams I/O manipulators and contains macros for creating parameterized manipulators.
A. istream.
B. ostream.
C. fstream.
D. byte stream.
The function endl inserts a new line character and flushes the buffer, i.e., it writes all the unwritten characters in the buffer to the output sequence.
A. classes.
B. programs.
C. objects.
D. functions.
These are the objects used to perform input and output operations on the standard input and output.
A. iostream.h.
B. sstream.h.
C. fstream.h.
D. iomanip.h.
These functions are manipulators like endl() and hex().
A. endl().
B. ends().
C. flush().
D. fixed().
The manipulator ends() is used to insert a null character (‘0’). We should not confuse it with endl, which is used to insert a new line character.
A. < iostream.h>.
B. < stdlib.h>.
C. < stdio.h>.
D. < iomanip.h>.
The function is setiosflags. Other parameterized manipulators are: resetiosflags, setbase, setfill etc.
A. < iostream.h>.
B. < iomanip.h>.
C. < stdlib.h>.
D. < stdio.h>.
These are functions for memory allocation and deallocation , and are present in standard library.
A. < iostream.h>.
B. < iomanip.h>.
C. < stdlib.h>.
D. < string.h>.
It declares several string manipulation and memory manipulation routines.
A. < string.h>.
B. < stdio.h>.
C. < iomanip.h>.
D. < fstream.h>.
The header file open() is used to open a file.
A. < stdlib.h>.
B. < string.h>.
C. < fstream.h>.
D. < stdio.h>.
It is used for random number generation.
A. any srand () or rand().
B. randoms().
C. srand().
D. rand().
The srand() function starts or speeds the random-number generator, from a point that depends on the value we pass to srand() as an argument.
A. rand(0).
B. random(10).
C. random(9).
D. random(11).
random(num) is used to generate random numbers within range 0 to num-1. For example, random (10) will generate random numbers within range 0 - 9. To generate random numbers within a specific range (L to U) though random (), we need to change the use of random() to : random (U - L + 1) + L.
A. isalpha().
B. isalnum().
C. isdigit().
D. isupper&lower&digit().
It returns false, if anything other than the digit or alphabet is passed to this function.
A. isupper().
B. toupper().
C. upper().
D. uppercase().
toupper()converts its arguments to uppercase letter if its argument is a letter.
A. < istream.h>.
B. < ostream.h>.
C. < fstream.h>.
D. < stdlib.h>.
The fstream provides an interface to read and write data from files as input/output streams.
A. header files.
B. library functions.
C. iostream.
D. main().
A header file is a file containing C++ declarations and macro definitions to be shared between several source files. They supply the definitions and declarations required to invoke system calls and libraries.
A. console I/O function.
B. < iostream.h>.
C. < stdio.h> .
D. unformatted string function.
putchar() is a standard library function which is present in the standard input output library.
A. < string.h>.
B. < iostream.h>.
C. istream.
D. ostream.
all the console I/O functions are contained in the iostream header file as it deals with all the input and output functions.
A. cout stream.
B. cin and a line of text.
C. cout stream with a line of text on the screen.
D. cin stream with a line of string on the screen.
write is a function present in the output stream. It is invoked when a line of text is written on the screen.
A. input function.
B. input line oriented function.
C. line oriented output function.
D. output function.
write is used to handle the output function when a line of text is entered.
A. < stdio.h>.
B. < conio.h>.
C. < iostream.h>.
D. < string.h>.
getchar() is a library function which is defined in standard input output library to take a single charcter as an input.
A. cin and cout.
B. cin only.
C. cout only.
D. Cin and Cout.
get() is used to input data so it is used with the cin stream object while put() is used to output a string so it is used with the cout stream object.
A. text oriented input function.
B. line oriented input function.
C. input oriented function.
D. line oriented output function.
write() is an output function used to display text from a file one line at a time.
A. write a character.
B. write string of characters at console.
C. read a character.
D. read a string.
puts() displays a string of characters on the screen which is a console.
A. fabs().
B. sqrt().
C. write( ).
D. pow().
write() is a function used to operate on strings and is unformatted as there is no control on the number of characters which would be processed.
A. standard input device.
B. standard output device.
C. standard input and standard output device.
D. memory.