Hackerrank Python Basics 1 — Questions and Answers
Question 1: What is a variable in programming?
- A named storage location for data that can change during program execution (Correct answer)
- A fixed constant
- A type of function
- A programming language
Correct answer: A named storage location for data that can change during program execution
Variables are named containers that store data values in memory, and their contents can be modified during program execution.
Question 2: What is the time complexity of binary search?
- O(log n) (Correct answer)
- O(n)
- O(n²)
- O(1)
Correct answer: O(log n)
Binary search has O(log n) complexity because it halves the search space with each comparison, efficiently finding elements in sorted arrays.
Question 3: What is a list in Python?
- An ordered, mutable collection of elements (Correct answer)
- An immutable sequence
- A key-value pair
- A single value
Correct answer: An ordered, mutable collection of elements
A Python list is an ordered, mutable sequence that can hold elements of different types, defined with square brackets.
Question 4: What is a function in programming?
- A reusable block of code that performs a specific task (Correct answer)
- A type of variable
- A data format
- An error message
Correct answer: A reusable block of code that performs a specific task
Functions encapsulate a specific task into a reusable block of code that can be called with different inputs, promoting code reuse and organization.
Question 5: What is the difference between '==' and '=' in most programming languages?
- '==' compares values; '=' assigns a value (Correct answer)
- They are the same
- '=' compares; '==' assigns
- Neither is valid
Correct answer: '==' compares values; '=' assigns a value
'=' is the assignment operator (sets a variable's value), while '==' is the comparison operator (checks if two values are equal).
Question 6: What is a loop used for in programming?
- To repeat a block of code multiple times (Correct answer)
- To store data
- To define variables
- To import libraries
Correct answer: To repeat a block of code multiple times
Loops execute a block of code repeatedly until a condition is met, avoiding the need to write the same code multiple times.
What is a variable in programming?