Home Tuitions

chapter-Flow Of Control

Important MCQ-Based Questions on Informatics Practices (IP) class 11 chapter-Flow Of Control

This page consists of Important MCQ-Based Questions on Informatics Practices (IP) class 11 chapter-Flow Of Control all the questions are uploaded for practice with detailed explanations of every question. To check the solution, click on the answer. 

Find below Important MCQ-Based Questions on Informatics Practices (IP) class 11 chapter-Flow Of Control

Important Questions for Informatics Practices (IP) class 11 chapter-Flow Of Control set-1

Informatics Practices - MCQ on Flow Of Control

Class XI

Q.1 While attempt to compile and run the below code

public class Value{

boolean b;

public static void main(String argv[]){

Value mi = new Value();

}

Value(){

if(b){

System.out.println("The value of b was true");

}

else{

System.out.println("The value of b was false");

}

}

}

the error occurs is

a) compile time error variable b was not initialised.

b) compile time error the parameter to the if operator must evaluate to a Boolean.

c) compile time error, cannot simultaneously create and assign value for boolean value.

d) Compilation and run with output of false.

Answer:

(d) compilation and run with output of false.

Explanation- As the boolean b was created at the class level it did not need to be explicitly initialised and instead took the default value of a boolean which is false. An if statement must evaluate to a boolean value and thus b meets this criterion.

Q.2 The difference between looping statements and selection statement is

a) loops run until the exit condition is met.

b) loop tests for static condition one time.

c) loops are conditional statements.

d) loops grab information from fields and display it.

Answer:

(a)loops run until the exit condition is met.

Explanation- Loops run until the exit condition is met (meaning the loop variable can change in the loop to cause exit). Selection tests for an existing static condition one time. Looping statements are statements that won't stop its process unless it satisfies its condition while selection statements it will be executed if the input matches one of the selections. The selection that matches the input will be processed.

Q.3 Each pass through a loop is called a/an

a) enumeration.

b) iteration.

c) culmination.

d) pass through.

Answer:

(b)iteration.

Explanation: The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled.

Q.3 Looping process which checks the test condition at the end of the loop is

a) for loop

b) while loop

c) do-while loop

d) no looping process checks the test condition at the end

Answer:

(c)do-while loop

Explanation: do-while loop is an exit-controlled loop, i.e., it evaluates its test-expression at the bottom of the loop after executing its loop-body statements.

Q.4 A continue statement causes execution to skip to

a) the end of the program.

b) the first statement after the loop.

c) the statement following the continue statement.

d) the next iteration of the loop.

Answer:

(d)the next iteration of the loop.

Explanation: The continue statement skips the rest of the loop statements and causes the next iteration of the loop.

Q.5. In a group of nested loops, loop that is executed the most number of times is

a) the outermost loop.

b) the innermost loop.

c) all loops are executed the same number of times.

d) cannot be determined without knowing the size of the loops.

Answer:

(b)the innermost loop.

Explanation- Nested loop contains another loop in its body, the inner loop gets executed before the control goes to outer loop.

Q.6. The statement i++; is equivalent to

a) i = i + i;

b) i = i + 1;

c) i = i - 1;

d) i - - ;

Answer:

(b) i = i + 1;

Explanation- ++ is an unary increment operator which increments the value of the operand by 1. It means "use the value of i first, then increment".

Q.7. The entry control loops are

a) for and while.

b) for and while-do.

c) while and while-do.

d) for, while and while-do.

Answer:

(a) for and while.

Explanation- In an entry-control loop, the test expression is evaluated before executing the body of the loop i.e., before entering into a loop. The for and while loops are the entry-control loops.

Q.8. Expression which change the value of loop variable is known as

a) update expression.

b) true expression.

c) test expression.

d) initialization expression.

Answer:

(a) update expression.

Explanation- The update expression changes the value of loop variables. The update expression is executed; at the end of the loop after the loop body is executed.

Q.9. The exit-control loop among the following is

a) for loop.

b) while loop.

c) do-while loop.

d) exit loop.

Answer:

(c) do-while loop.

Explanation- do-while is an exit-control loop in which the test expression is evaluated before exiting from the loop.

Q.10. The looping process best used when the number of iterations are known is

a) for.

b) while.

c) do-while.

d) all looping processes require that the iterations be known.

Answer:

(a)for.

Explanation- Fixed number of iterations is decided in for loop at the beginning while initializing the for loop.

Q.11. The two types of selection statements are

a) if and else.

b) if and not.

c) if and switch.

d) goto and switch.

Answer:

(c) if and switch.

Explanation- An if statement is a selection statement that allows more than one possible flow of control. A switch statement is a selection statement that transfers control to different statements within the switch body depending on the value of the switch expression.

Q.12. In the statement: for (int k = 2, k <= 12, k++)

a) the increment should always be ++k.

b) the variable must always be the letter i when using a for loop.

c) there should be a semicolon at the end of the statement.

d) the commas should be semicolons.

Answer:

(c)there should be a semicolon at the end of the statement.

Explanation- Semicolons (;) are used to separate the control expressions.

Q.13. The iteration construct is also known as

a) closed construct.

b) open construct.

c) decision construct.

d) looping construct.

Answer:

(d) looping construct.

Explanation- The iteration construct means repetition of a set of statements depending upon a condition test. Till the condition is true, a set-of-statements are repeated again and again. This construct is also known as looping construct.

Q.14. If there is more than one statement in the block of a for loop,

a) parentheses ( )

b) French curly braces { }

c) brackets [ ]

d) arrows < >

e) must be placed at the beginning and the ending of the loop block.

Answer:

(b)French curly braces { }

Explanation- French curly braces must be placed if there are more than one statement in a loop to avoid ambiguity.

Q.15. for (num = 1; num <= 5; num++), value stored in num at the end of this looping is

a) 1

b) 4

c) 5

d) 6

Answer:

(d) Value stored in num is 6 as num gets incremented after test condition is evaluated. The incremented valued is stored.

Q.16. The syntax of switch statement is

a) switch ( expression ) switch.

b) switch ( expression ) statement switch.

c) switch ( expression ) statement.

d) switch (statement) expression.

Answer:

(c) switch ( expression ) statement.

Explanation- The switch statement executes one or more of a series of cases, based on the value of a controlling expression. The switch statement has the following syntax: switch (expression) Statement

Q.17. In while loop, the evaluation is done at the

a) bottom of the loop.

b) top of the loop.

c) middle loop.

d) top or bottom of the loop.

Answer:

(b) top of the loop.

Explanation- In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Here the evaluation is done at the top of the loop.

Q.18. The statement i--; is equivalent to

a) i=i+1.

b) i=i-1.

c) i=i+i.

d) i=i+0.

Answer:

(b) i=i-1.

Explanation- i-- is equal to i-1. -- is a decrement operator which decrements value by 1.

Q.19. Error in the code below is

for (i=0, i<10, i++)

a) instead of commas semicolon should be used.

b) there should be a semicolon at the end of the statement.

c) there is no error.

d) the increment should always be ++i.

Answer:

(a) instead of commas semicolon should be used.

Explanation- In for loop the proper syntax is for (initialization; condition; increment). So it should be for (i=0; i<10; i++).

Q.20. Elements controlling a loop are

a) Initialization, test and update expression.

b) Initialization and update expression.

c) Initialization, test, update and body of the loop.

d) Test, initialization and update expression

Answer:

(c) Initialization, test, update and body of the loop.

Explanation- A loop generally have four elements that have different purposes.

Q.21. Java has statements that perform an unconditional branch, they are

a) return, break and continue

b) goto, return

c) break and label

d) while and continue

Answer:

(a)return, break and continue

Explanation- Return, Continue and break are used to transfer program control within a function.

Q.22. A loop that never ends is

a) iteration loop.

b) selection loop.

c) finite loop.

d) infinite loop.

Answer:

(d) infinite loop.

Explanation- An infinite loop can be created by omitting the test expression which makes it endless loop.

Q.23. Labels are used with

1. Iteration statements

2. Break statements

3. If-else statements

4. Do-while statements

Answer:

(2)

Explanation- Labels are mostly used on blocks and loops. Break label causes flow of control to break out of the containing statement which must be labelled label.

Q.24. When the number of if statement is more than the number of else clauses, it is called

a) Nested ifs.

b) Dangling else.

c) If-else-if ladder.

d) Fall through.

Answer:

(b) Dangling else.

Explanation- In if-else statement the last else statement goes with the preceding if statement that does not have an else statement; arising dangling else problem.

Q.25. An operator that can be used as an alterative to if statement is

a) ==

b) >

c) :

d) ?:

Answer:

(d)?:

Explanation- ?: can be used to replace if-else statements and makes the code more simple and compact.

Q.26. The selection statements are also called as

a) Iteration statements.

b) Sequence statements.

c) Conditional statements.

d) Looping constructs.

Answer:

(c) Conditional statements.

Explanation- Selection statements test the condition and choose the set of instruction, if the condition is true then set of statements is executed otherwise they are ignored.

Q.27. If inner if enclosed within{ } then outer else goes with

a) inner else.

b) outer if.

c) outer else.

d) dangling else.

Answer:

(b) outer if.

Explanation- Using braces{ } we can override the dangling else condition and outer else will go with outer if.

Q.28. A case label contains the word case followed by

a) an integral constant expression and a colon.

b) an character constant expression and a colon.

c) an integral constant expression and a semi-colon.

d) an character constant expression and a semi-colon.

Answer:

(a) an integral constant expression and a colon.

Explanation- A case label contains the word case followed by an integral constant expression and a colon. The value of each integral constant expression must represent a different value.

Q.29. The number of ways in which flow of control in a program can be, is

a) two.

b) three.

c) four.

d) five.

Answer:

(b) three.

Explanation- There are three ways: sequentially, selectively and iteratively.

Q.30. The statement which abandons the current iteration of the loop by skipping over the rest of the statements in the loop-body, is known as

a) continue statement.

b) break statement.

c) goto statement.

d) return statement.

Answer:

(a) continue statement.

Explanation- The continue statement immediately transfers control to the evaluation of the test-expression of the loop for the next iteration of the loop.

Q.31. The loop which is contained in another loop is known as

a) do-while loop.

b) container loop.

c) eccentric loop.

d) nested loop.

Answer:

(d) nested loop.

Explanation- A nested loop is a loop within a loop, an inner loop within the body of an outer one.

Q. 32. The switch statement is also used for

a) decision making.

b) motivation.

c) planning.

d) coordinating.

Answer:

(a) decision making.

Explanation- Switch statement helps in decision making as it successively tests the value of an expression against a list of integers.

Q.33. The result of compiling and running the following code is

public static void main(string[ ]arg){

do{

system.out.print(“inside do”);

} while (false);

while (false){

system.out.print(“inside while”);

}

System.out.print(“outside”);

}

}

a) inside dooutside.

b) outside.

c) inside do.

d) compilation error.

Answer:

(d)compilation error

Explanation- compilation error because of the second loop (while statement) : unreachable code.

Q. 34. The result of compiling and running the following code is

Public static void main(string[] args){

Int j=10;

Switch(1{

Case 20:

J+=1;

Case 40:

J+=2;

Default:

J+=3;

a) compile error, can’t use constant 1 in the switch, must be final variable.

b) compile error, default must be the last statement after all the cases.

c) compile error, must have break statement within each case.

d) 17.

Answer:

(d) 17.

Explanation- It is legal to use a constant in the switch statement. It will match default, increment j by 3 and since there is no break statement, it will continue to the next statement and increment j by 4.

Q.35. The result of compiling and running the following code is

Public static void main(string[] args){

boolean flag= false;

int x=0;

do

{

X++;

}while (flag=!flag);

System.out.println(x);

}

a) 2

b) 3

c) Compilation error

d) The loop is infinite and will cause the program to break

Answer:

(a)2

Explanation- do-while loop's body executes always at least once

Q.36. If we forget to write the update expression inside the loop’s body, it will become

a) empty loop.

b) finite loop.

c) compilation error.

d) infinite loop.

Answer:

(d)infinite loop.

Explanation- The value in the loop will never terminate resulting in an infinite loop.

Q. 37. The syntax of if statement is

a) if (expression) Statement;

b) if int a; statement;

c) if(i<100 statement1; else statement 2;

d) outer if(expression) statement; else statements;

Answer:

(a) if (expression) Statement;

Explanation- An if statement tests a particular condition; if the condition is true a statement is evaluated.

Q.38. The test condition of if must be enclosed in

a) < >

b) [ ]

c) ( )

d) ;

Answer:

(c) The test condition of if must be enclosed in parenthesis( )to avoid confusion in execution of statement.

Q. 38. In a do while loop, evaluation is done at the

a) bottom of the loop.

b) top of the loop.

c) middle loop.

d) top or bottom of the loop.

Answer:

(a) bottom of the loop.

Explanation- In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Here, the evaluation is done at the bottom of the loop.

Q.39. In a for loop, evaluation is done at the

a) bottom of the loop.

b) top of the loop.

c) middle loop.

d) top or bottom of the loop.

Answer:

(b) top of the loop.

Explanation- In computer science, a for loop is a programming language statement, which allows code to be repeatedly executed at the top of the loop. A for loop is classified as an iteration statement.

Q.40. Error in the following code is

while (i<10 && (i>24

a) the logical operator && cannot be used with while statement.

b) the while loop is an exit condition loop.

c) the test condition is always false.

d) the test condition is always true.

Answer:

(c) the test condition is always false.

Explanation- Here, the condition controlling the execution of the loop is always false. It will never be true.

Q.41. All executable statements in Java are terminated by a

a) .

b) ;

c) :

d) : -

Answer:

(b) ;

Explanation- A semi-colon (;) terminates statements in Java.

Q.42. A sequence of statements enclosed by a pair of braces ( { } ) is known as

a) executable statements.

b) compound statements.

c) simple statements.

d) complex statements.

Answer:

(b) compound statements.

Explanation- Compound statements contain (groups of) other statements; they affect or control the execution of the other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line.

Q. 43. The statement that cannot exist by itself, outside of a switch statement is

a) break

b) case

c) return

d) goto

Answer:

(b) case

Explanation- A case statement cannot exist by itself, outside of a switch, while a break statement jumps the line of code outside the body of switch statement.

Q. 44. Switch statement works with data types like

a) float.

b) Byte.

c) byte, char, short and int.

d) char only

Answer:

(c) byte, char, short and int.

Explanation- The case labels of switch must be an integer byte, short, int or a char.

Q.45. The output of below loop is

int i=0;

for (i=1;i<=10;++i)

a) 123456

b) 234567

c) 123456789

d) 12345678910

Answer:

(d)

Explanation- The above loop starts with value 1 for i. It repeats as long as i<=10. In each iteration, it increments value of i by 1.

Q.46. The for loop variation includes

a) multiple initialization and update expression.

b) Multiple initialization and skipping update expression.

c) Declaration of variables outside loops.

d) Variable declared anywhere outside loop.

Answer:

(a) multiple initialization and update expression.

Explanation- A for loop may contain multiple initialization and update expression, which are separated by commas.

Q. 47. The selection construct is also known as

a) document construct.

b) sequence construct.

c) decision construct.

d) iteration construct.

Answer:

(c) decision construct.

Explanation- The selection construct is known as decision construct because it helps in making decision about which set-of-statements is to be executed.

Q. 48. The loop used when the number of execution is fixed is

a) while loop.

b) for loop.

c) Switch statement.

d) do-while loop.

Answer:

(b) for loop.

Explanation- The for loop is appropriate when we know in advance how many times the loop will be executed.

Q. 49. When a break with label is executed

a) execution resumes at the end of the labelled block.

b) Returns to the next iteration of the labelled loop.

c) It does not execute further.

d) Infinite loop runs.

Answer:

(a) execution resumes at the end of the labelled block.

Q. 50. Output of following code is

int i=0;

label:

if(i<2{

system.out.print(“Ï is” + i);

i++;

continue label;

}

a) linking error.

b) 1.

c) compilation error.

d) infinite iteration.

Answer:

(c) compilation error.

Explanation- It will produce a compilation error line 6. This is because, labelled continue can be used only for loops and inside loops only. But, here, it is not a loop, so we can’t use labelled continue for it.

Q. 51. Output of following code will be

int f =1, i=2;

while (++i<5

f*=i;

system.out.println(f);

:

a) 24

b) 12

c) 30

d) 0

Answer:

(b) 12

Explanation- The output of above code is 12, because a while statement evaluates test expression before entering into the loop.

Q. 52. The repetition of a set-of-statements depending upon a condition-test is known as

a) sequence construct.

b) decision construct.

c) iteration construct.

d) order construct.

Answer:

(c) iteration construct.

Explanation- The iteration construct means repetition of a set of statements depending upon a condition test. Till the condition is true, a set-of-statements are repeated again and again.

Q.53. The conditional operator can be used as an alternative to

a) if statement.

b) else statement.

c) if-else statement.

d) go statement.

Answer:

(c) if-else statement.

Explanation- The conditional operator (?:) takes three operands. It tests the result of the first operand and then evaluates one of the other two operands based on the result of the first. It is similar to if-else statement.

Q.54. When the execution of the statements is done sequentially, it is known as

a) sequence construct.

b) order construct.

c) inline construct.

d) straight construct.

Answer:

(a) sequence construct.

Explanation- The sequence construct means the statements are being executed sequentially. This represents a default flow of statements.

Q.55. The repetition of a set-of-statements depending upon a condition-test is known as

a) sequence construct.

b) decision construct.

c) iteration construct.

d) order construct.

Answer:

(c)

Explanation- The iteration construct means repetition of a set of statements depending upon a condition test. Till the condition is true, a set-of-statements are repeated again and again.

Q. 56. The condition on which the execution of the loop depends is known as

a) entry condition.

b) out condition.

c) exit condition.

d) control condition.

Answer:

(c) exit condition.

Explanation- The set of statements that are repeated again and again is called the body of the loop. The condition on which the execution or exit of the loop depends is called the exit condition.

Q.57. In a control structure switch-case, the statement which gets executed when there is no match, is

a) break statement.

b) continue statement.

c) switch statement.

d) default statement.

Answer:

(d) default statement.

Explanation- The switch statement body consists of a series of case labels and an optional default label. The default label can appear only once. The default statement need not come at the end; it can appear anywhere in the body of the switch statement.

Q.58. The output of the following code fragment is

for(int i = 1; i<10; i++)

a) 1 2 3 4 5 6 7 8 9.

b) 0 1 2 3 4 5 6 7 8.

c) 1 2 3 4 5 6 7 8 10.

d) 3 4 5 6 7 8 9 10.

Answer:

(a) 1 2 3 4 5 6 7 8 9.

Explanation- Here the variable is initialised from 1 and will continue up to 9 as i<10.

Q. 59. The output of the following code is

Public static void main(string xyz[])

{

int i=1;

while(i<10

{

System.out.print(i);

i=i*2;

}

}

a) 123456789

b) 1248

c) 1359

d) 24810

Answer:

(b)1248

Explanation- The above code will print the multiples of 2 as i is multiplied by 2.

Q.60. Process of repetition is called

a) Looping

b) Iteration

c) While statement

d) For statement

Answer:

(a) Looping

Explanation- Loop statements repeatedly execute the code block, testing the conditional statement each time.

Q.61. A normal exit takes place when the

a) unexpected condition occurs.

b) loop’s test condition fails.

c) loop terminates unconditionally.

d) Statement needs to be repeated executed.

Answer:

(b) loop’s test condition fails.

Explanation- A normal exit takes place when the loop’s test condition fails. In this case, control is transferred to the first executable statement following the loop statement.

Q.62. The statement which abandons the current iteration of the loop by skipping over the rest of the statements in the loop-body , is known as

a) continue statement.

b) break statement.

c) goto statement.

d) return statement.

Ans. a) continue statement.

Explanation- The continue statement immediately transfers control to the evaluation of the test-expression of the loop for the next iteration of the loop.

Q.63. The number of times the initialization expression is executed, is

a) one.

b) two.

c) three.

d) four.

Answer:

(a)

Explanation- The initialization expression gives the loop variable their first value. The initialization expression is executed only once, in the beginning of the loop.

Q.64. An expression whose truth value decides whether the loop-body is executed or not, is

a) update expression.

b) true expression.

c) test expression.

d) initialization expression.

Answer:

(c)

Explanation- The test expression is an expression whose truth-value decides whether the loop-body will be executed or not. If the test expression evaluates to true i.e., 1, the loop-body gets executed, otherwise the loop is terminated.

Q. 65. The output of the below code is

for (int i=10; i>6;i=i-2

system.out.print(i);

a) 10 8

b) 2 4 6 8

c) 4 6 8

d) 8 10 12

Answer:

(a) 10 8

Explanation- The above code uses for loop to print numbers which is greater than 6 and update the expression by decreasing i by 2; so that value in i first time is 10 and second time is 8.

Q. 66. The output of below code is

int i=1;

while(i<5

{

system.out.print(i);

i=i*2;

}

a) 1 2 3 4

b) 1 2 4

c) 2 4

d) 2 3 4

Answer:

(b) 1 2 4

Explanation- The above code executes while i<5 and prints number which are multiple of 2.

Q. 67. The output of code is

public static void main(string[] args)

{

int j=1, i=2;

while (++i<5

j*=i;

system.out.print(j);

}

a) 10

b) 20

c) 30

d) 12

Answer:

(d) 12

Explanation- The output is 12. The value of i will increase till 5. When the value of j is 1, i is 2 and in the loop i becomes 3 which is less than 5. So, it execute j*=i. J=1*3=3. Similarly, on next processing, i=4 which is less than 5. So j=j*I, i.e. j=3*4=12.

Q.68. The output of following code is

Public static void main(string[] args)

{

int j=1, i=2;

do { j*=i;}

while (++i<5;

system.out.print(j);

}

a) 24

b) 26

c) 30

d) 20

Answer:

(a) 24

Explanation- When the do loop executed, the initial value of i is 2 and j is 1. Once the loop is executed the value of i will be multiplied with value of j and stored in j. The loop will continue till the value of I becomes 4. Finally, the values of i will be multiplied with j and becomes 24.

Q.69. The statement used to create a loop is

a) break

b) continue

c) return

d) for

Answer:

(d) for

Explanation- For statement allow a set of statements to be repeated or looped through a fixed number of times.

Q.70. The test condition is checked prior to executing the block is done under

a) do-while loop

b) while loop

c) case

d) switch

Answer:

(b) while loop

Explanation- while statements allow a set of statements to be repeated or looped until a certain condition is met. The test condition is checked prior to executing the block.

Q.71. Label is used in statements which has

a) for statement

b) while statement

c) continue statement

d) do-while statement

Answer:

(c) continue statement

Explanation- The only statements for which label can be used are those statements that can enclose a break or continue statement.

Q.72. When a semicolon(;) is placed as the for/while terminator, then it is considered as

a) infinite loop

b) exit loop

c) finite loop

d) empty loop

Answer:

(d) empty loop

Explanation- If a semicolon is placed at the end of the for looping statement, it is considered an empty loop and the loop’s control variable is initialized condition is tested, variable’s value is updated but no other statement outside the loop is treated as a part of loop.

Q.73. Output of following code is

Public static void main(string[ ] args)

{

int i=0;

int x=0;

while(i<10

{

if((i%2==0

{

x=x+i;

system.out.print(x+” ”);

}

}

a) 0 2 6 12 20

b) 0 2 4 6 8 10

c) 2 4 8 12 24

d) 2 6 12 20 24

Answer:

(a) 0 2 6 12 20

Explanation- The above code executes while i<10 and checks the condition if i is divisible by 2 then add I in variable x which is initialized by 0.

Q.74. Output of following code is

Public static void main(string[ ] args)

{

int=0;

for(i=1;i<=20;i++)

{

System.out.print(x+” ”);

i=i+2;

}

}

a) 1 3 5 7 9 11 13

b) 1 4 7 10 13 16 19

c) 2 4 6 8 10 12 14

d) 1 2 3 4 5 6 7 8

Answer:

(b) 1 4 7 10 13 16 19

Explanation- The above code executes i while i<=20, and increment i by 2.

Q. 75. The condition on which the execution of the loop depends is known as

a) entry condition.

b) out condition.

c) exit condition.

d) control condition.

Answer:

(c) exit condition.

Explanation- The set of statements that are repeated again and again is called the body of the loop. The condition on which the execution or exit of the loop depends is called the exit condition.

Q. 76. The syntax of for loop is

a) for( initialization expression; test expression; update expression).

b) for( initialization expression: test expression: update expression).

c) for( initialization expression, test expression, update expression).

d) for( initialization expression/ test expression/ update expression).

Answer:

(a) for( initialization expression; test expression; update expression).

Explanation- In for loop, all its loop control elements are gathered at one place. In the syntax, semi-colons are used.

Q. 76. The output of the following program segment is

for(int = -3, sum =0; i<11; i++) sum++;

a) -3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11.

b) -2,-1,0,1,2,3,4,5,6,7,8,9,10,11.

c) -2,-1,0,1,2,3,4,5,6,7,8,9,10.

d) an error message.

Answer:

(d) an error message.

Explanation- It will produce an error message ’Declaration terminated incompletely’ during compilation as the declaration int = -3 does not declare a variable.

Q. 77. The program area inside which a variable can be accessed, is known as

a) program’s limit.

b) variable’s limit.

c) variable’s scope.

d) program’s scope.

Answer:

(c) variable’s scope.

Explanation- Rule regarding variables’ access states that, a variable can be accessed only in the block where it has been declared.

Q.78. The loop which is contained in another loop is known as

a) do-while loop.

b) container loop.

c) eccentric loop.

d) nested loop.

Answer:

(d) nested loop.

Explanation- A nested loop is a loop within a loop, an inner loop within the body of an outer one.

Q.79. The syntax of the while loop is

a) while(expression) loop-body.

b) loop body while(expression).

c) while(loop body expression).

d) loop body do-while (expression).

Answer:

(a) while(expression) loop-body.

Explanation- The syntax is while (expression) loop-body

Here the loop body may contain a single statement, a compound statement or an empty statement.

Q.80. The number of times the initialization expression is executed, is

a) one.

b) two.

c) three.

d) four.

Answer:

(a) one.

Explanation- The initialization expression gives the loop variable their first value. The initialization expression is executed only once, in the beginning of the loop.

Q.81. The loop to be repeated 200 times can be written as

a) for( int i=0;i>200;i++)

b) for(int i=0;i<200;i++)

c) while(int i=200;i<0; i++)

d) for(int i=200; i<200;i--)

Answer:

(b) for(int i=0; i<200; i++)

Explanation- for statement can be used to repeat a loop 200 times with initializing i by 0 and test expression i<200; update i++.

Q.82. Loop to print out the character set from A-Z is

a) for(ch= ‘A’; ch<= ‘Z’; ch=ch+1

b) for(ch= ‘a’; ch<= ‘z’; ch=ch+1

c) for(ch= ‘A’; ch< ‘Z’; ch++)

d) for(ch= ‘a-z’; ch<=0;ch++)

Answer:

(a) for(ch= ‘A’; ch<= ‘Z’; ch=ch+1

Explanation- The for loop will print set of character A-Z while incrementing ch after every execution.

Q.83. The output of the following program is

Public static void main(string xyz [ ])

{

for(int i=12; i>5; i-=2

{

System.out.print(i);

}

}

a) 12 11 10 9

b) 10 8 6 4

c) 12 10 8 6

d) 5 7 9 10

Answer:

(c) 12 10 8 6

Explanation- The above program takes value i=12 and then i is decremented by 2, hence the output is 12 10 8 6 until i>5.

Q. 84. The number of times the test expression is executed, is

a) one.

b) every time the loop executes.

c) three.

d) four.

Answer:

(b) every time the loop executes.

Explanation- The test expression checks the loop variable every time the expression is updated.

Q.85. The below code will give

for(i=-5;i<-8;i--) system.out.print(i+1;

a) 6

b) 7

c) 8

d) no output.

Answer:

(d) no output.

Explanation - The code will give zero output as the initialization is not done in correct manner, i.e., -5 is not taken as initialization expression.

Q. 86. The statement that can be used more efficiently in case of if-else statement for comparison of same variable is

a) while statement.

b) do-while statement.

c) switch statement.

d) return statement.

Answer:

(c) switch statement.

Explanation- The switch statement is more efficient than nested if-else statement in case of equality comparison of same variable against a set of constants.

Q.87. When a break with label is executed, then,

a) execution loop is terminated.

b) execution resumes to first line.

c) infinite looping starts.

d) execution resumes at the end of the labelled block.

Answer:

(d) execution resumes at the end of the labelled block.

Explanation- break label causes flow of control to break out of the containing statement and execution resumes at the end of the labelled block.

Q. 88. The statement that skips the current pass of a loop’s body is

a) continue statement.

b) Return statement.

c) Break statement.

d) Label statement.

Answer:

(a) continue statement.

Explanation- The continue statement ships the current pass of a loop statements and causes the next iteration of the loop.

Q.89. To execute the loop body at least once, use

a) Sequence construct

b) While loop

c) do-while loop

d) continue statement

Answer:

(c) do-while loop

Explanation- The do-while loop should be preferred when we want to execute the loop body at least once.

Q.90. In a for loop, evaluation is done at the

a) bottom of the loop.

b) top of the loop.

c) middle loop.

d) top or bottom of the loop.

Answer:

(b) top of the loop.

Explanation- In computer science, a for loop is a programming language statement, which allows code to be repeatedly executed at the top of the loop. A for loop is classified as an iteration statement.

Q. 91. The execution of statements depending upon a condition-test is

a) sequence construct.

b) iteration construct.

c) selection construct.

d) operation construct.

Answer:

(c) selection construct.

Explanation- If a condition evaluates to true, a course-of-action is followed otherwise another course of action is followed.

Q.92. Loop variable must be initialised before the loop begins in

a) while statement.

b) continue statement.

c) do-while statement.

d) break statement.

Answer:

(a) while statement.

Explanation- In a while statement, a loop control variable should be initialized before the loop begins as an uninitialised variable cannot be used in an expression.

Q.93. The output produced by following code is

int x=0;

while(1 {

System.out.print(“x +1 is” + (x+1);

}

a) 1.

b) 0.

c) compilation error.

d) X+1.

Answer:

(c) compilation error.

Explanation- It will produce a compilation error, because with while only a boolean expression can be given as test condition, i.e., while(<boolean-expression>).

Q.94. The statement that can be used with jump statement for next iteration of the specified loop can be

a) Continue.

b) Label.

c) Break.

d) Return.

Answer:

(b) Label.

Explanation- The jump statement can be labelled to break out or continue to next iteration of the specified loop.

Q.95. The condition on which the execution of the loop depends is known as

a) entry condition.

b) out condition.

c) exit condition.

d) control condition.

Answer:

(c) exit condition.

Explanation- The set of statements that are repeated again and again is called the body of the loop. The condition on which the execution or exit of the loop depends is called the exit condition.

Q.96. The two types of selection statements are

a) if and else.

b) if and not.

c) if and switch.

d) goto and switch.

Answer:

(c) if and switch.

Explanation- An if statement is a selection statement that allows more than one possible flow of control. A switch statement is a selection statement that transfers control to different statements within the switch body depending on the value of the switch expression.

Q.97. Output of the following program will be

public static void main(string[ ] args)

{

int N;

N=1;

while(N<=32 {

N=2*N;

System.out.print(N);

}

}

a) 2 4 8 16 32 64.

b) 2 4 16 32 64 128.

c) 32 16 8 4 2 1.

d) compilation error.

Answer:

(a) 2 4 8 16 32 64.

Explanation- The above program prints the multiple of 2 while the number multiplied is less than 32.

Q.98. The output of the following program segment is

public static void main(string[]args)

{

int i;

for(i=0; i<10;i++)

{

if(i==5

break;

system.out.print(i+” ”);

}

}

a) 1 2 3 4

b) 0 1 2 3 4

c) 1 2 3 4 5

d) 5 4 3 2 1

Answer:

(b) 0 1 2 3 4

Explanation- The loop breaks when i equals to 5.

Q.99. The output of the following code will be

for(int j=0;j<=10;j++);

system.out.print(++j);

a) 10

b) 9

c) 12

d) 14

Answer:

(c) 12

Explanation- The above code executes till j <=10 and increment j,i.e. 11 and will print the value of j by first incrementing it. Therefore ++j will give output as 12.

Q.100. The output of the following program will be

public static void main(string xyz[ ])

{

int c=0, i=2;

while(i<10

{

c=++i-i*2;

}

system.out.print(“c=” +c+ “i=” + i);

}

a) c=-10 i=-10

b) c=10 i=10

c) c=9 i=9

d) c=-9 i=-9

Answer:

(a)c=-10 i=-10

Explanation- The above program executes until I is less than 10 and prints the output by getting the value of variable c.