author | bhanu |
Thu, 11 Nov 2010 02:16:07 +0530 | |
changeset 477 | 0406115dccd1 |
parent 438 | 4523b2048663 |
permissions | -rw-r--r-- |
315
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
1 |
Objective Questions |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
2 |
------------------- |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
3 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
4 |
.. A mininum of 8 questions here (along with answers) |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
5 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
6 |
1. Given a variable ``time``, print ``Good Morning`` if it is less |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
7 |
than 12, otherwise ``Hello``. |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
8 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
9 |
Answer:: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
10 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
11 |
if time < 12: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
12 |
print "Good Morning" |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
13 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
14 |
else: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
15 |
print "Hello" |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
16 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
17 |
#. Every ``if`` block must be followed by an ``else`` block. T or F? |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
18 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
19 |
Answer: F |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
20 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
21 |
#. Every ``if/elif/else`` ladder MUST end with an ``else`` block. T/F? |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
22 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
23 |
Answer: F |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
24 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
25 |
#. An if/elif/else ladder can have any number of elif blocks. T or F? |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
26 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
27 |
Answer: T |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
28 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
29 |
#. What will be printed at the end this code block:: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
30 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
31 |
x = 20 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
32 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
33 |
if x > 10: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
34 |
print x * 100 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
35 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
36 |
Answer: IndentationError - Expected and indented block.. |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
37 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
38 |
#. What will be printed at the end this code block:: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
39 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
40 |
x = 20 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
41 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
42 |
if x > 10: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
43 |
print x * 100 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
44 |
else: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
45 |
print x |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
46 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
47 |
Answer: SyntaxError |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
48 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
49 |
#. What will be printed at the end this code block:: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
50 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
51 |
x = 20 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
52 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
53 |
if x > 10: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
54 |
print x * 100 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
55 |
else: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
56 |
print x |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
57 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
58 |
Answer: 2000 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
59 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
60 |
#. Convert the if else ladder below into a ternary conditional |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
61 |
statement:: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
62 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
63 |
x = 20 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
64 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
65 |
if x > 10: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
66 |
print x * 100 |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
67 |
else: |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
68 |
print x |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
69 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
70 |
Answer: print x * 100 if x > 10 else x |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
71 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
72 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
73 |
Larger Questions |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
74 |
---------------- |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
75 |
|
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
76 |
.. A minimum of 2 questions here (along with answers) |
7944a4504769
Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
77 |
|
438
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
78 |
1. Given a number, say, n. If it is divisible by 10, print the |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
79 |
quotient when divided by 10, otherwise if it is divisible by 5, |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
80 |
print the corresponding quotient, otherwise, print the number. |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
81 |
|
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
82 |
|
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
83 |
Answer:: |
437
0840aa06d2e6
Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
315
diff
changeset
|
84 |
|
438
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
85 |
if n%10==0: |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
86 |
print n/10 |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
87 |
elif n%5==0: |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
88 |
print n/5 |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
89 |
else: |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
90 |
print n |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
91 |
|
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
92 |
2. Given a number, say, n. Write an if block to multiply n by three |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
93 |
and add 1, if it is odd, otherwise halve it. |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
94 |
|
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
95 |
Answer:: |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
96 |
|
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
97 |
if n % 2: |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
98 |
n = n*3+1 |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
99 |
else: |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
100 |
n /= 2 |
4523b2048663
Reviewed conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
437
diff
changeset
|
101 |