L- 5 Info

  • Uploaded by: subhash varghese
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View L- 5 Info as PDF for free.

More details

  • Words: 1,479
  • Pages: 4
Class XI – CD

-1-

L. 5

[email protected]

Assignment Answers

Ans. 1 Null statements are also called empty statements. These statements contains only a semicolon and no other statements. Such statements are useful in creating ‘time delay loops’. These loops pause the program for some time. E.g. long wait =0; while (++wait<10000) ; Ans. 2 Sequence, Selection and Iteration. Sequence means that statements are executed sequentially from top to bottom. Selection means that the execution of the statements depend on a condition test. Iteration means repetition of a set of statements depending upon a condition test. Ans. 3 Selection means that the execution of the statements depend on a condition test. These statements are also called as conditional statements or decision statements. Java provides if and switch as selection statements. In certain circumstance, ?: can also be used as an alternative to if statement. ?: operator can make the code little shorter, e.g. if(a> b){ z=20; } else{ z=10; } this code can be written with ?: as z=(a>b)? 20 : 10; But this can be complicated if we have too many nested if’s to check eg. if(a>b){ If(a>c){ m=a; } else { m=c;} } if(b>c){ m=b; } else { m=c;} } with ?: above code will look like : m=(a>b: (a>c? a :c) : (b>c? b : c)); // this is little bit confusing.. Ans. 4 Conditional operators are feasible where conditions to check are minimal. In cases where there are too many conditions and nested conditions are to be checked, they may prove complicated and confusing. Also the precedence of conditional operator is less than the other mathematical operators. Default dangling else matching can be overridden by making proper use of {} braces.

Ans. 5 Dangling else problem arises when in a nested if statements, number of ifs is more than the number of else clauses. The question then arises, with which if does the additional else clause property match up. In such case , java matches an else with the nearest if clause. Eg.

pg 236

q4 and q5

Ans. 6 Switch and if-else statements can be used to select one of several alternatives. Ans. 7 Break statement is used to avoid ‘fall-through’ problem associated with switch statement. In ‘fall- through’ the control falls to the matching case. When ‘break’ statement is encountered in switch statement, program execution jumps to the line of code following the ‘switch’ statement i.e. outside the body of the switch statement. Ans. 8 Switch can only test for equality whereas ‘if’ can evaluate a relational or logical expression. If-else can handle ranges, whereas switch cannot. Each switch case label must be a single value. Switch case label must be a constant while if-else can be used when 2 or more variables are tested. Switch statements are more efficient while testing a value against a set of constants. Ans. 9 In the absence of a break statement in switch, java will start executing the statements from matching case and keep on executing statements for the following cases as well until either a break statement is found or switch statement end is encountered. This is called fall-through. Ans. 10 No two case labels in the same switch statement can have identical values.

S.o.p – System.out.println

Class XI – CD

-2-

[email protected]

Ans. 11 The default statement gets executed when no match is found in a switch statement. Ans. 12 Iteration statements are those statements which repeats a particular set of statements a finite/infinite number of times based on a condition . E.g. for-next, while, do-while.

S.o.p – System.out.println

Class XI – CD -3Ans. 13 A loop has 4 elements namely:

[email protected]

initialization expression: this is used to assign a starting value to the loop control variable. The initialization expression(s) is executed only once, at the start of the loop. o Test expression: this expression decides whether the loop body will be executed or not. If test expression is true, the control enters the loop and executes the loop body statements otherwise the control is transferred just after the loop.  Entry controlled loop or Top-tested loop: here the test expression is evaluated first before entering the loop. Chances are that the loop body may not be executed even once. E.g. for and while loop.  Exit controlled loop or Bottom tested loop: here the test condition is evaluated at the end of the loop. This loop will be executed at least once. E.g. do-while loop. o Update expression(s): this will change the value of loop control variable. This is evaluated at the end after the loop body is executed in the absence of this expression, the loop may be executed infinitely. o Body of the loop: these contains the statements that will be executed repeatedly until the test expression evaluates to true. Ans. 14 See ans 13. Ans. 15 See ans. 13. Ans. 16 It is true that you can decrement the loop control variable. But in such a case, the start value of the loop specified as initial value must be greater than the end value specified in test expression. Ans. 17 True . Ans. 18 False. Ans. 19 for(int i=51; i<=60;i++) S.o.p(i); Ans. 20 for(int i=10;i>=1;--i) S.o.p(i); Ans. 21 In a for loop, initialization expression(s), test expression and update expression are optional, i.e. you can do away with any or all of these expressions. Even if you skip these expressions, the blank semi colons(;) must be given. If a loop does not contain any statement in its loop body, it is said to be an empty loop. An empty loop is used to run a time delay loop. E.g. for(i=1; i<500; i++); Ans. 22 A variable scope is the part of the program segment where a particular variable will be visible or say can be used. It is a block of code where a variable has been declared. It can be used only within that block and all its subsequent blocks. o

Ans. 23 In the while loop, test expression is first evaluated and then the loop body is executed if the condition is ‘true’. In case of do-while loop, the loop body is first executed and the test expression is evaluated at the end of the loop statement. Thus with do-while loop, the loop body will be executed at least once. Ans. 24 The ‘break’ statement terminates the smallest enclosing while, do-while, for and switch statement. Execution resumes at the statement immediately following the body of the terminated statement. ‘break [label]’ causes the flow of control to break out of the containing statement which must be labelled [label]. e.g outer: for(i=0; i<10; i++){ inner: for(j=0; j<5;j++){ break outer; } }

S.o.p – System.out.println

Class XI – CD

-4-

[email protected]

in the above example, the ’break outer’ statement will transfer the control after the ‘i’ loop. If it was just a ‘break’ statement, the control would have been transferred after the ‘j’ loop. Ans. 25 Labelled loops are useful as we can then use ‘break [label]’ to break out of a particular loop based on some condition. Ans. 26 do-while loop will be used as this loop will be executed at least once and the condition will be checked at the end. Type B Questions Ans. 1 The ‘dangling-else’ problem arises when in a nested if statement, number of if’s is more than the number of else clauses. The question then arises, with which if does the additional else clause property matchup. e.g. see pg 192 - 193 to override the default dangling – else matching is to place the last occurring unmatched if in a compound statement if(expr1){ if(expr2) statement 1; } else statement 2; in the above example, the else statement will go with if(expr1). Ans. 2 See pg 198. Ans. 3 switch(a){ case 0: S.o.p(“Zero”); break; case 1: S.o.p(“One”); break; case 2: S.o.p(“Two”); break; case 3: S.o.p(“Three”); break; }

Ans. 4 – do it yourself Ans. 5 Java provides a multiple branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statement associated with that constant are executed. switch(expression){ case constant 1: statement sequence 1: break; case constant 2: statement sequence 2: break; case constant 3: statement sequence 3: break; case constant n: statement sequence n: break; [ default : default statement sequence : break; } ‘default’ case of the switch need not be the last one. It can be anywhere in the switch. Also there must not be two or more identical cases. It would lead to error.

Ans. 6

See pg 203 Ans. 7 see pg 203 Ans. 8 it is a good practice to put a ‘break’ statement after the last case statement so as to avoid forgetting the ‘break’ when you add another case statement at the end of the switch.

S.o.p – System.out.println

Related Documents

L- 5 Info
June 2020 5
Info 5
July 2020 1
Pag 5 - Info 59
May 2020 6
Parcial 5 Info
April 2020 4
Info 5.docx
December 2019 3

More Documents from "Mayra Asqui"