programming beginner 10 sample questions

Functions MCQ Practice Test

Understanding how to define functions, pass parameters, and return values.

Q1. Consider the Python function: def my_func(x, y=5, *args, **kwargs): return x + y + sum(args) + len(kwargs). What is the result of the following call: my_func(2, 10, 3, 4, z=3, a=4, b=5)?

Explanation: 2 binds to x and 10 to y positionally; the extra positional arguments 3 and 4 are collected into *args (sum 7); z, a and b are collected into **kwargs (length 3). Total: 2 + 10 + 7 + 3 = 22. The original call placed positional arguments after keyword arguments, which is a SyntaxError in Python.

Q2. Consider the Python code snippet: \npython\ndef process_data(data, func):\n return [func(x) for x in data]\n\ndata = [1, 2, 3, 4, 5]\nresult = process_data(data, lambda x: x * 2 if x % 2 == 0 else x + 1)\nprint(result)\n\nWhat will be the output of this code?

Explanation: The lambda doubles even numbers and adds 1 to odd ones: 1 becomes 2, 2 becomes 4, 3 becomes 4, 4 becomes 8, and 5 becomes 6. The output is [2, 4, 4, 8, 6].

Q3. What will be the output of the following function in JavaScript: \`function add(a, b) { if (typeof a === 'number' && typeof b === 'number') { return a + b; } else { return 'Invalid input'; } }\`

Explanation: The function checks if both inputs are numbers, and if not, it returns 'Invalid input'. If the inputs are numbers, it returns their sum. Since the function is called with two strings, it will return 'Invalid input'.

Q4. What will be the output of the following function call: `console.log(add(5, 3))` if the function is defined as `function add(x, y) { return x + y; }`?

Explanation: The function will add the two numbers and return the result, which is 8.

Q5. A function in a program is a block of code that can take arguments and return a value. Which of the following is a correct way to call a function in Python that takes two arguments, x and y?

Explanation: In Python, a function is called by specifying the function name followed by the arguments in parentheses.

Q6. Which of the following function definitions is correct in JavaScript?

Explanation: In JavaScript, function definitions do not require the use of backslashes to escape newline characters or parentheses. The correct function definition uses curly brackets to define the function body and does not escape the addition operator.

Q7. What will be the output of the function call `foo(5)` if the function `foo(x)` is defined as `function foo(x) { return x & 1; }`?

Explanation: The bitwise AND operator (&) returns 1 if both operands have at least one bit set to 1 in that position, and 0 otherwise. Since 5 is an odd number, its binary representation is 101, which has a 1 in the rightmost position. Therefore, the function will return 1.

Q8. What is the output of the function call `greet('John')` if the function `greet(name)` is defined as `def greet(name): return 'Hello ' + name + '! ' + 'You are a great person!'`?

Explanation: The function `greet(name)` returns a string that concatenates the greeting message with the input name and a fixed string. When called with the argument `'John'`, it returns the expected output.

Q9. What will be the output of the following function: `def greet(name): print("Hello " + name)`? If you call `greet("John")`, what will be printed?

Explanation: In Python, string concatenation using the `+` operator simply combines the strings. There is no special escaping or HTML entity conversion performed. Therefore, the output will be 'Hello John'.

Q10. What will be the output of the following function call: `print(sumOfDigits(123))`?

Explanation: The function `sumOfDigits` takes an integer as input, converts it to a string, iterates over each character (digit) in the string, converts each character back to an integer, and returns the sum of these integers. Therefore, `sumOfDigits(123)` will return `1 + 2 + 3 = 6`.

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
Functions MCQ Practice Test | Mind Hustle