Find the output of the following code:
int s = 0;
if ( s <= 13 ):
if ( s % 2 == 2 ):
print(“Colour code RED”)
else:
print(“Colour code BLUE”)
print(“New Stripe”)
s = s + 1;
<1>
Answers
Answer:
Subject: Informatics Practices HOLIDAYS HOMEWORK Grade XII
1. Compare the following
(1) if and a ? : Operator.
(2) while and do.. while loop
2. Given the following code fragment:
if (a==0)
System.out.print ln (“Zero”);
if (a==1)
System.out.print ln (“One”);
if (a==2)
System.out.print ln (“Two”);
if (a==3)
System.out.print ln (“Three”);
Write an alternative code that saves on number of comparisons.
3. Rewrite the following fragment using switch:
if (ch== ‘E’)
eastern++ ;
if (ch== ‘W’)
western++ ;
if (ch== ‘N’)
northern++ ;
if (ch== ‘S’)
southern++ ;
else
unknown++ ;
4. Write the syntax and purpose of a switch statement.
5. When does an if statement prove more advantageous over a switch statement?
6. Why is it suggested to put a break statement after the last case statement in a switch
even though it is not needed syntactically?
7. Rewrite the code given in question 3 using switch?
8. Rewrite the following set of if-else statements in terms of switch-case statements:
(a) char code ;
code = character.readChar();
if (code == ‘A’)
System.out.print ln (“Accountant”);
else if (code == ‘C’ ll code == ‘G’)
System.out.print ln (“Grade IV”);
else if (code == ‘F’)
System.out.print ln (“Financial Advisor”);
Given:
Co - ordinates of point A = (-1 , 7)
Co - ordinates of point B = (4, - 3)
Ratio in which P divides A and B is 2:3.
⠀⠀⠀⠀⠀⠀⠀
To find:
Co - ordinates of point P ?
⠀⠀⠀⠀⠀⠀⠀
Solution:
⠀⠀⠀⠀⠀⠀⠀
☯ Let coordinates of point P be (x,y).
⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀━━━━━━━━━━━━━━━━━━━━━━━━━
Formula that is used to find the distance between two points :
⠀⠀⠀⠀⠀⠀⠀
Distance Formula =