0% found this document useful (0 votes)
721 views18 pages

Week 2 Graded Assignment Solution

The document contains a code snippet that evaluates conditions for three integer variables (x, y, z) input by the user and prints output depending on whether the variables are positive, negative, or zero. It then asks several multiple-choice and numeric answer type questions to test the reader's understanding of the code snippet's behavior.

Uploaded by

dunipoxi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
721 views18 pages

Week 2 Graded Assignment Solution

The document contains a code snippet that evaluates conditions for three integer variables (x, y, z) input by the user and prints output depending on whether the variables are positive, negative, or zero. It then asks several multiple-choice and numeric answer type questions to test the reader's understanding of the code snippet's behavior.

Uploaded by

dunipoxi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Week-2, Graded

This document has questions.


Common Data for questions 1, 2
Statement
A word is termed valid if the output of the following code is True when the word is given as the
input to the code. Answer questions (1) and (2) based on this code.

1 word = input()
2 valid = False
3 # both 'a' and 'z' are in lower case
4 if 'a' <= word[0] <= 'z':
5 if word[0] == word[-1]:
6 valid = True
7 print(valid)
Question-1
Statement
Select all valid words from the options given below. (MSQ)

Options
(a)

1 tacit

(b)

1 trumpet

(c)

1 ease

(d)

1 TrumpeT

(e)

1 TaciT

Answer
(a), (b), (c)

Feedback
There are two conditional statements, one within the other (nested). These conditions correspond
to the following questions:

Does the word begin with a lower case letter?


Are the first and last characters the same?

Now, here are two follow-up questions.

What happens if the order of the conditions is reversed? Will the meaning of this code-
snippet change?
Is there a way to replace the nested block with a single conditional statement?
Question-2
Statement
Going by the code given in the common data, under what conditions can a word be termed valid?

Options
(a)

A word is termed valid if it begins with a lower case letter.

(b)

A word is termed valid if its first and last characters are the same.

(c)

A word is termed valid if it begins with a lower case letter AND its first and last characters are
the same.

(d)

A word is termed valid if it begins with a lower case letter OR its first and last characters are the
same.

Answer
(c)

Feedback
We are just combining both the conditions into a single condition using the and operator. This
should be familiar to you from the CT days.
Question-3
Statement
Consider the following snippet of code. Assume that L is a positive integer that has already been
defined at this stage.

1 # L is a positive integer that has already been defined at this stage


2 word = input()
3 space = ' ' # there is a single space
4 if len(word) < L:
5 word = 'short' + space + word
6 elif L <= len(word) < 2 * L:
7 word = 'medium' + space + word
8 else:
9 word = 'long' + space + word
10 print(word)

The code is run twice, with two different input words. Each input word is a string that consists
only of letters without any spaces in them.

Output-1

1 short good

Output-2

1 long goodnessme

With just this data given to you, what is the value of L ? (NAT)

Answer
5

Feedback
If 'good' is given as input, the output is 'short good' . For this to happen, the condition
should evaluate to True . If 'goodnessme' is given as input, the output is long goodnessme . For
this to happen, the condition should evaluate to True . Putting the two of them
together, we get:

And that gives us as the answer!


Question-4
Statement
A three digit number is called a sandwich number if the sum of its first and last digit is equal to its
middle digit. Accept a three digit number as input and print sandwich if the number is a sandwich
number. Print plain if the number is not a sandwich number. Select the correct implementation
of the code that achieves this. Sample test cases follow:

Input Output

143 sandwich

118 plain

Options
(a)

1 num = int(input())
2 first, middle, last = num[0], num[1], num[2]
3 if first + last == middle:
4 print('sandwich')
5 else:
6 print('plain')

(b)

1 num = input()
2 first, middle, last = num[0], num[1], num[2]
3 if first + last == middle:
4 print('sandwich')
5 else:
6 print('plain')

(c)

1 num = input()
2 first, middle, last = int(num[0]), int(num[1]), int(num[2])
3 if first + last == middle:
4 print('sandwich')
5 else:
6 print('plain')

(d)
1 num = int(input())
2 first, middle, last = int(num[0]), int(num[1]), int(num[2])
3 if first + last == middle:
4 print('sandwich')
5 else:
6 print('plain')

Answer
(c)

Feedback
Idea

The basic idea is to accept the three digit number as a string, then convert the three characters
into integers. From here, checking for the condition should be straightforward.

Analysis of wrong options

Questions like this are a good source to develop your debugging skills. Here is a general template.
For each wrong option, first read the code carefully and see where the error lies.

First, check if the code runs without any run-time error. Testing the code with sample inputs
can be of help here. If the code is going to throw a run-time error for valid inputs, then you
know for a fact that this code-snippet can't be the correct answer.
Next, at a deeper level, you must be able to identify the exact line or lines in the code where
the mistake lies. There may be some code-snippets (option-(b) is an example) which run
without throwing run-time errors but have a logical flaw in them.
As a last resort, you can open Replit or some interpreter of choice and try to run this code.
Note that this method will not help you from the exam point-of-view.

Here is a demonstration of how this template can be used:

Option-(a)

1 num = int(input())
2 first, middle, last = num[0], num[1], num[2]
3 if first + last == middle:
4 print('sandwich')
5 else:
6 print('plain')

Why is this incorrect? In line-1, num is an integer. In line-2, in the RHS of the assignment, we are
treating num as a string. Hence, we will get a run-time error. It is also a good idea to get
familiarized with the error messages. In this case, the message is: TypeError: 'int' object is
not subscriptable .

Option-(b)
1 num = input()
2 first, middle, last = num[0], num[1], num[2]
3 if first + last == middle:
4 print('sandwich')
5 else:
6 print('plain')

Why is this incorrect? If you observe closely, this code will run without any run-time errors for
valid inputs. But, there is a flaw. In line-2, first , middle and last are strings and not integers!
As a result, in line-3, first + last will actually be a concatenation of two strings and not the
addition of two integers! You can print first + last and see what you get.

Why is option-(d) wrong? This will be your homework.


Common Data for questions 5, 6, 7 and 8
Statement
Consider the following snippet of code:

1 x = int(input())
2 y = int(input())
3 z = int(input())
4
5 # Block-1 Start
6 if x > 0 or y > 0 or z > 0:
7 if (x > 0 and y > 0) or (y > 0 and z > 0) or (z > 0 and x > 0):
8 if x > 0 and y > 0 and z > 0:
9 print('P3')
10 else:
11 print('P2')
12 else:
13 print('P1')
14 # Block-1 End
15
16 # Block-2 Start
17 if x < 0 or y < 0 or z < 0:
18 if (x < 0 and y < 0) or (y < 0 and z < 0) or (z < 0 and x < 0):
19 if x < 0 and y < 0 and z < 0:
20 print('N3')
21 else:
22 print('N2')
23 else:
24 print('N1')
25 # Block-2 End
Question-5
Statement
What will be the value of X in the output for the given input? NAT

Input

1 -1
2 4
3 1

Output

1 PX
2 NY

Answer
2

Feedback
P in the output stands for positive, while N stands for negative. With this information, can you
figure out what PX and NY stand for? Some more hints in the form of questions follow:

What kind of inputs can clear (evaluate to True ) the if-statement in line-6 of the code?
Once you are able to answer the previous question, repeat the same process for line-7 and
line-8. What kind of inputs can clear all three if-blocks in lines 6, 7 and 8?

You have to go through a similar exercise for all other if-blocks.


Question-6
Statement
What will be the value of Y in the output for the given input? NAT

Input

1 -1
2 4
3 1

Output

1 PX
2 NY

Answer
1

Feedback
P in the output stands for positive, while N stands for negative. With this information, can you
figure out what PX and NY stand for?
Question-7
Statement
When does the code given above print no value?

Options
(a)

When any two among x , y and z are equal

(b)

When all the values of x , y and z are equal

(c)

When any one among x , y and z is zero

(d)

When all the values of x , y and z are zeros

Answer
(d)

Feedback
When all variables set to zero, the outermost if block evaluates False in both Block-1 and
Block-2.
Question-8
Statement
Is the following statement True or False?

For any combination of inputs x , y and z at least one else statement will be executed.

Options
(a)

True

(b)

False

Answer
(b)

Feedback
In the following situations, none of the else statements will be executed.

All variables are zero


All variables are positive
All variables are negative
Question-9
Statement
Select the correct implementation of a piece of code that accepts a sentence as input and prints
the number of words in it. A sentence is just a sequence of words with a space between
consecutive words. You can assume that the sentence will not have any other punctuation marks.
You can also assume that the input string will have at least one word. Sample test cases follow:

Input Output

this is a sentence 4

this sentence is not true 5

Options
(a)

1 sentence = input()
2 space = ' ' # there is a single space between the quotes
3 num_words = sentence.count(space)
4 print(num_words)

(b)

1 sentence = input()
2 space = ' ' # there is a single space between the quotes
3 num_words = sentence.count(space) + 1
4 print(num_words)

(c)

1 sentence = int(input())
2 space = ' ' # there is a single space between the quotes
3 num_words = sentence.count(space)
4 print(num_words)

(d)

1 sentence = input()
2 num_words = len(sentence)
3 print(num_words)

Answer
(b)
Feedback
The basic idea is this: the number of words is equal to one more than the number of spaces. It is
up to you to figure out why each of the other options is incorrect.
Question-10
Statement
Consider the following snippet of code.

1 dept = input()
2 course = input()
3 prefix = input()
4 name = input()
5 roll_no = input()
6 name = prefix + " " + name.upper() # one space
7 lib_id = dept[0] + course[0] + roll_no
8 print("Student record:")
9 indent = ' ' # four spaces
10 print(indent + "Dept:", dept)
11 print(indent + "Name:", name)
12 print(indent + "Roll No:", roll_no)
13 print(indent + "Library Card No:", lib_id)

What should be the input to this code to get the following output?

Output

1 Student record:
2 Dept: ABC
3 Name: MR FNAME LNAME
4 Roll No: 999
5 Library Card No: AX999

Options
(a)

1 ABD
2 XYZ
3 MR
4 FNAME LNAME
5 999

(b)

1 ABC
2 XYZ
3 MRS
4 FNAME LNAME
5 999
(c)

1 ABC
2 XYZ
3 MR
4 fname lname
5 999

(d)

1 ABC
2 MR
3 XYZ
4 FNAME LNAME
5 999

Answer
(c)

Feedback
This question demands a patient perusal of the code!
Question-11
Statement
Consider the following snippet of code:

1 word = input()
2 match = False
3 if word.count('(') == word.count(')'):
4 if word.count('[') == word.count(']'):
5 if word.count('{') == word.count('}'):
6 match = True
7 if match:
8 print('PERFECT!')
9 else:
10 print('IMPERFECT!')

Select all possible inputs for which this code prints PERFECT! as output. (MSQ)

Options
(a)

1 (a{b[c]})

(b)

1 abcd

(c)

1 )(][}{

(d)

1 a(db]

Answer
(a), (b), (c)

Feedback
The idea being expressed should be quite clear: for every type of parenthesis, are there an equal
number opening and closing parentheses? The important point to note here is
word.count(char) will return zero if char is not found in word . So, option (b) is also correct!

You might also like