Mettl Fundamental Coding Skills 4 — Questions and Answers
Question 1: What is a null pointer dereference?
- Accessing memory at address zero or an uninitialized pointer (Correct answer)
- Deleting a variable twice
- Passing too many arguments to a function
- Dividing a number by null
Correct answer: Accessing memory at address zero or an uninitialized pointer
Dereferencing a null pointer attempts to access memory at an invalid address, usually causing a runtime crash.
Question 2: Which of the following is an example of a syntax error?
- Dividing by zero at runtime
- Accessing an out-of-bounds index
- Missing a closing parenthesis in code (Correct answer)
- Infinite loop during execution
Correct answer: Missing a closing parenthesis in code
A missing closing parenthesis violates the grammar rules of the language, making it a syntax error caught at compile/parse time.
Question 3: What does the following pseudocode do? `for i from 1 to n: sum = sum + i`
- Multiplies all numbers from 1 to n
- Computes the average of 1 to n
- Computes the sum of integers from 1 to n (Correct answer)
- Finds the maximum of 1 to n
Correct answer: Computes the sum of integers from 1 to n
The loop accumulates each integer from 1 to n into `sum`, computing the total sum of that range.
Question 4: What is the difference between `==` and `===` in JavaScript?
- `==` checks value only; `===` checks value and type (Correct answer)
- `===` checks value only; `==` checks value and type
- Both are identical in behavior
- `==` is for numbers; `===` is for strings
Correct answer: `==` checks value only; `===` checks value and type
`==` performs type coercion before comparing, while `===` (strict equality) requires both value and type to match.
Question 5: Which of the following best describes a binary search algorithm?
- Searches by checking every element from left to right
- Searches by repeatedly halving the search interval on a sorted array (Correct answer)
- Searches using a hash function
- Searches by jumping ahead by fixed steps
Correct answer: Searches by repeatedly halving the search interval on a sorted array
Binary search works on sorted arrays by comparing the target to the middle element and eliminating half the remaining elements each step.
Question 6: What is an infinite loop?
- A loop that runs exactly n times
- A loop whose condition never becomes false (Correct answer)
- A loop that skips the first iteration
- A loop inside another loop
Correct answer: A loop whose condition never becomes false
An infinite loop occurs when the loop's termination condition is never met, causing it to execute indefinitely.
Question 7: In object-oriented programming, what does 'encapsulation' mean?
- Inheriting behavior from a parent class
- Bundling data and methods together and restricting direct access (Correct answer)
- Running multiple methods at the same time
- Using the same function name for different types
Correct answer: Bundling data and methods together and restricting direct access
Encapsulation bundles an object's data and the methods that operate on it, exposing only a controlled interface to the outside.
What is a null pointer dereference?