Skip to content

Commit 943de76

Browse files
author
Alexander Engström
committed
Added more questions
1 parent 93179bb commit 943de76

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

data/questions.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,6 +3053,64 @@
30533053
"ValueError",
30543054
"GraphError"
30553055
]
3056+
},
3057+
{
3058+
"difficulty": "expert",
3059+
"question": "What will be the output of the following Python code?",
3060+
"codeSnippet": "class Foo(str):\n def __init__(self, value):\n self.value = value\n\n def __cap__(self):\n return \"HELLO WORLD\"\n\n\nfoo = Foo(\"hello world\")\nprint(foo.capitalize())",
3061+
"explanation": "The Foo class inherits from the built-in str class. The __init__ method assigns a value to the instance variable 'value'. The __cap__ method is defined but not used or recognized as a special method in Python. When 'foo.capitalize()' is called, it utilizes the capitalize method of the str class (since Foo inherits from str) which converts the first character of the string to uppercase and the rest to lowercase. Therefore, the output will be 'Hello world'.",
3062+
"correctAnswer": "Hello world",
3063+
"incorrectAnswers": [
3064+
"HELLO WORLD",
3065+
"hello world",
3066+
"AttributeError",
3067+
"None",
3068+
"ValueError"
3069+
]
3070+
},
3071+
{
3072+
"difficulty": "expert",
3073+
"question": "What will be the output of the following Python code?",
3074+
"codeSnippet": "class String(str):\n def __init__(self, value):\n self.value = value\n\n def __add__(self, other):\n return super().__add__(other) + other\n\n\nfoo = String(\"Hello\")\nbar = String(\"World\")\n\nprint(foo + bar)",
3075+
"explanation": "The String class inherits from the built-in str class. The __init__ method initializes the instance variable 'value'. The __add__ method is overridden to concatenate the current string with 'other' using the parent class's __add__ method, then adds 'other' again. When 'foo + bar' is executed, it first concatenates 'foo' ('Hello') and 'bar' ('World') to get 'HelloWorld', and then concatenates 'World' again, resulting in 'HelloWorldWorld'.",
3076+
"correctAnswer": "HelloWorldWorld",
3077+
"incorrectAnswers": [
3078+
"HelloWorld",
3079+
"WorldHelloWorld",
3080+
"HelloWorldHello",
3081+
"AttributeError",
3082+
"RecursionError"
3083+
]
3084+
},
3085+
{
3086+
"difficulty": "expert",
3087+
"question": "What will be the output of the following Python code?",
3088+
"codeSnippet": "class String(str):\n def __init__(self, value):\n self.value = value\n\nfoo = String(\"hello\")\nbar = String(\"world\")\n\nprint(len(foo + bar))",
3089+
"explanation": "The String class inherits from the built-in str class. The __init__ method initializes the instance variable 'value', but it does not affect the string behavior inherited from str. When 'foo + bar' is executed, it performs string concatenation of 'foo' ('hello') and 'bar' ('world'), resulting in 'helloworld'. The len() function then calculates the length of 'helloworld', which is 10 characters long.",
3090+
"correctAnswer": "10",
3091+
"incorrectAnswers": [
3092+
"5",
3093+
"AttributeError",
3094+
"ValueError",
3095+
"TypeError",
3096+
"helloworld"
3097+
]
3098+
},
3099+
{
3100+
"difficulty": "expert",
3101+
"question": "What will be the output of the following Python code?",
3102+
"codeSnippet": "class String(str):\n def __init__(self, value):\n self.value = value\n\n def __add__(self, other):\n return super().__add__(self.value)\n\n\nclass WeirdString(String):\n def __add__(self, other):\n return super().__add__(other).__len__()\n\n def __mul__(self, other):\n return self.value * 2\n\n\nfoo = WeirdString(\"hi\")\nbaz = WeirdString(\"the\")\nbar = WeirdString(\"world\")\n\nprint(foo + (bar + baz))",
3103+
"explanation": "The WeirdString class, a subclass of String, overrides the __add__ and __mul__ methods. When executing 'bar + baz', it concatenates 'world' with its value 'world', resulting in 'worldworld', and returns its length, which is 10. Then, 'foo + 10' invokes the __add__ method of foo, concatenating 'hi' with its value 'hi' and returns the length of 'hihi', which is 4.",
3104+
"correctAnswer": "4",
3105+
"incorrectAnswers": ["10", "hitheworld", "hihi", "AttributeError", "None"]
3106+
},
3107+
{
3108+
"difficulty": "expert",
3109+
"question": "What will be the output of the following Python code?",
3110+
"codeSnippet": "class Integer(int):\n def __add__(self, other):\n return super().__add__(other) - 1\n\n def __sub__(self, other):\n return super().__add__(other) + 1\n\n def __str__(self):\n return f\"{super().__mul__(-1)}\"\n\n\nfoo = Integer(3)\nbar = Integer(5)\n\nprint(foo - bar)",
3111+
"explanation": "In the Integer class, which inherits from int, the __add__ method is overridden to subtract 1 from the sum, and the __sub__ method is overridden to add the numbers and then add 1. The __str__ method, though overridden, is not used in this snippet. When 'foo - bar' is executed, it invokes the __sub__ method of foo (3), which adds 3 to bar (5), resulting in 8, and then adds 1, giving a final result of 9.",
3112+
"correctAnswer": "9",
3113+
"incorrectAnswers": ["8", "-2", "2", "RecursionError", "-9"]
30563114
}
30573115
]
30583116
}

0 commit comments

Comments
 (0)