Mettl Fundamental Coding Skills 3 — Questions and Answers
Question 1: Which keyword is used to exit a loop prematurely in most languages?
- exit
- return
- stop
- break (Correct answer)
Correct answer: break
The `break` statement immediately terminates the innermost enclosing loop.
Question 2: What is a recursive function?
- A function that runs in parallel
- A function that calls itself (Correct answer)
- A function with no return value
- A function that accepts functions as arguments
Correct answer: A function that calls itself
A recursive function is one that calls itself, usually with a smaller input, until reaching a base case.
Question 3: Which of the following is NOT a primitive data type in Java?
- int
- boolean
- String (Correct answer)
- double
Correct answer: String
String is an object (reference type) in Java, while int, boolean, and double are primitive types.
Question 4: What does `&&` represent in most C-style programming languages?
- Bitwise AND
- Logical AND (Correct answer)
- Bitwise OR
- Logical OR
Correct answer: Logical AND
`&&` is the logical AND operator, returning true only when both operands are true.
Question 5: Which sorting algorithm has an average time complexity of O(n log n)?
- Bubble Sort
- Insertion Sort
- Selection Sort
- Merge Sort (Correct answer)
Correct answer: Merge Sort
Merge Sort consistently achieves O(n log n) average and worst-case time complexity by dividing and merging.
Question 6: What is the purpose of a `return` statement in a function?
- To restart the function
- To skip to the next iteration
- To send a value back to the caller (Correct answer)
- To declare a variable
Correct answer: To send a value back to the caller
The `return` statement exits the function and optionally passes a value back to wherever the function was called.
Question 7: In Python, what is the result of `len('hello')`?
- 4
- 6
- 5 (Correct answer)
- 0
Correct answer: 5
The string 'hello' contains 5 characters (h, e, l, l, o), so `len('hello')` returns 5.
Which keyword is used to exit a loop prematurely in most languages?