Week-2 Graded Assignment Answers
Common Data for Questions 1, 2
Question-1 (MSQ)
Statement: Select all valid words from the options given below.
Options:
- (a) tacit
- (b) trumpet
- (c) ease
- (d) TrumpeT
- (e) TaciT
Answer: (a), (b), (c)
Feedback: A word is considered valid if it begins with a lowercase letter ('a' <= word[0] <= 'z') and the first and last letters are the same.
Question-2
Statement: 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: The word is valid if both conditions are met: starts with a lowercase letter and the first and last characters are the same.
Question-3
Statement: The code snippet processes two input strings: one that outputs "short good" and another that outputs "long goodnessme". What is the value of L?
Answer: 5
Feedback: Given the outputs: "good" has a length of 4, so L > 4. "goodnessme" has a length of 10, so 2L >= 10. Therefore, L = 5.
Question-4
Statement: Which implementation checks if a three-digit number is a sandwich number?
Answer:
(c)
Feedback: The correct implementation converts the digits of the number into integers and checks if the sum of the first and last digits equals the middle digit.
Common Data for Questions 5-8
Question-5
Statement: What will be the value of X in the output for the given input?
Input:
-1
4
1
Answer: 2
Feedback: "PX" means positive, and "NY" means negative. From the input values, two conditions hold for P and N, which correspond to "P2" and "N1".
Question-6
Statement: What will be the value of Y in the output for the given input?
Answer: 1
Feedback: The output includes one "P" for positive numbers and one "N" for negative numbers.
Question-7
Statement: When does the code print no value?
Answer: (d)
Feedback: If all the values of x, y, and z are zeros, the code does not print anything.
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.
Answer: (b)
Feedback: In certain cases, such as when all values are zero, positive, or negative, none of the else statements are executed.
Question-9
Statement: Select the correct implementation to count words in a sentence.
Answer: (b)
Feedback: The correct way to count words is to count spaces and add 1, as each word is separated by a space.
Question-10
Statement: What input would give the specified output for the code?
Answer: (c)
Feedback: This question requires attention to the code that formats the output. The correct input generates the desired output.
Question-11
Statement: Select all possible inputs for which the code prints PERFECT!.
Answer: (a), (b), (c)
Feedback: The code checks if there is a matching count of opening and closing parentheses for each type ((), [], {}). If all match, it prints PERFECT!.