Data & MLOps
Coding logic and algorithms
Learn Coding logic and algorithms: 34 practice questions and answers with an interactive quiz — from the offline AI Learning app.
Questions & Answers
Tap a question to reveal its answer. Written for self-study and interview preparation.
What is an algorithm?
An algorithm is a clear, step-by-step set of instructions for solving a problem or completing a task. Like a recipe, each step must be precise so a computer can follow it exactly and get the same result every time.
In programming, what does a 'loop' do?
A loop repeats a block of code multiple times so you don't have to write the same instructions over and over. For example, a loop can print the numbers 1 to 10 by repeating one print statement ten times.
for i in range(1, 11):
print(i)What is a 'variable' in coding?
A variable is a named container that stores a value, such as a number or a piece of text, which can change while the program runs. For example, score = 0 creates a variable called score holding the value 0.
score = 0
score = score + 10In machine learning, what is 'training data'?
Training data is the collection of examples a model learns from. The computer studies these examples to find patterns so it can make good predictions on new data it has never seen before.
What does an 'if' statement let a program do?
An if statement lets a program make a decision by checking whether a condition is true. If the condition is true, the program runs certain code; otherwise it can skip it or run different code with 'else'.
if age >= 13:
print('Teenager')
else:
print('Child')What is a 'bug' in a program?
A bug is an error or mistake in the code that makes the program behave incorrectly or stop working. Finding and fixing bugs is called debugging.
Why do we use 'pseudocode' before writing real code?
Pseudocode is a plain-language outline of the steps an algorithm will take, without worrying about exact programming rules. It helps us plan and think through the logic clearly before translating it into a real programming language.
In a list of numbers, how does a 'linear search' find a value?
A linear search checks each item in the list one by one, from start to end, until it finds the value it is looking for or reaches the end. It is simple but can be slow for very large lists.
What is the main advantage of 'binary search' over linear search?
Binary search is much faster for large sorted lists because it repeatedly cuts the search area in half instead of checking every item. However, it only works when the list is already sorted.
What is a 'function' in programming?
A function is a reusable block of code that performs a specific task and can be called by name whenever needed. Functions help keep code organized and avoid repeating the same instructions.
def greet(name):
print('Hello ' + name)
greet('Maya')In AI, what does it mean for a model to 'make a prediction'?
Making a prediction means the model uses the patterns it learned during training to give an answer or guess about new input. For example, after learning from many photos, a model might predict whether a new photo shows a cat or a dog.
What is a value that is either True or False called in coding?
A value that is only True or False is called a Boolean. Booleans are used in if statements and loops to control which code runs.
is_raining = True
if is_raining:
print('Take an umbrella')Why is the ORDER of steps important in an algorithm?
The order matters because each step often depends on the ones before it, so doing them out of order can give a wrong result. For example, you must put on socks before shoes, not the other way around.
What does the AND logical operator require to be True?
The AND operator is True only when both conditions are True at the same time. If either condition is False, the whole AND expression is False.
if age > 12 and age < 20:
print('Teenager')What does the OR logical operator require to be True?
The OR operator is True when at least one of the conditions is True. It is False only when both conditions are False.
if is_weekend or is_holiday:
print('No school today')In a dataset, what is a 'feature'?
A feature is one measurable property or column of the data that the model uses to learn, such as a person's age or height. Choosing useful features helps a model make better predictions.
What is the difference between a 'for' loop and a 'while' loop?
A for loop repeats a set number of times or over a list of items, while a while loop keeps repeating as long as a condition stays True. Use a for loop when you know how many times to repeat, and a while loop when you stop based on a condition.
# while loop
count = 0
while count < 3:
print(count)
count = count + 1What can cause an 'infinite loop'?
An infinite loop happens when a loop's stopping condition never becomes False, so the loop runs forever. This often occurs if you forget to update the variable that the condition checks.
# Bug: count never changes, loop never ends
count = 0
while count < 5:
print(count)Why should training data for an AI model be fair and varied?
If training data only shows one type of example, the model can become biased and make unfair or wrong predictions for others. Using varied, balanced data helps the AI work well and fairly for everyone.
What is a 'list' (or array) used for in programming?
A list is used to store many values together under one name, in order, so you can access them by their position. For example, a list can hold all the test scores of a class.
scores = [85, 92, 78, 90]
print(scores[0]) # prints 85In Python, what does the index 0 refer to in a list?
In Python, index 0 refers to the very first item in a list because counting starts at zero, not one. So in [10, 20, 30], the value at index 0 is 10.
colors = ['red', 'green', 'blue']
print(colors[0]) # redWhat is 'decomposition' in computational thinking?
Decomposition means breaking a big, complex problem into smaller, easier parts that you can solve one at a time. Solving each small part makes the whole problem much more manageable.
What is 'pattern recognition' in computational thinking?
Pattern recognition is finding similarities or repeating features among problems or data. Spotting patterns helps us reuse solutions and is also how AI models learn from examples.
What does 'abstraction' mean in computer science?
Abstraction means focusing on the important details of a problem while hiding the unnecessary ones. For example, when driving you use the steering wheel without needing to know how the engine works inside.
How does a simple sorting algorithm like bubble sort work?
Bubble sort compares each pair of neighboring items and swaps them if they are in the wrong order, repeating this until the whole list is sorted. Larger values gradually 'bubble' to the end of the list.
In machine learning, what is a 'label' in supervised learning?
A label is the correct answer attached to a training example, which the model tries to learn to predict. For example, a photo of a cat might have the label 'cat' so the model learns what cats look like.
What is a 'comment' in code and why is it useful?
A comment is a note in the code that the computer ignores when running the program. Comments help people understand what the code does, making it easier to read and fix later.
# This line is a comment
print('Hello') # so is thisWhat is the result of the expression 7 % 3 (the modulo operator) in Python?
The modulo operator (%) gives the remainder after division, so 7 % 3 equals 1 because 7 divided by 3 is 2 with a remainder of 1. Modulo is often used to check if a number is even or odd.
print(7 % 3) # 1
print(8 % 2) # 0 means evenWhy is it important to test a program with different inputs?
Testing with different inputs helps you find bugs and make sure the program works correctly in many situations, not just the easy ones. It is especially important to test unusual or extreme cases that might break the code.
What does it mean if one algorithm is more 'efficient' than another?
A more efficient algorithm solves the same problem using fewer steps, less time, or less memory. Efficiency matters a lot when working with large amounts of data, like millions of records.
In an AI chatbot, why might it sometimes give a wrong answer?
An AI chatbot can give wrong answers because it predicts likely responses from patterns in its training data, not from true understanding. If the data was limited, outdated, or the question is tricky, the prediction may be incorrect, so we should always check important answers.
What is a 'flowchart' used for in algorithm design?
A flowchart is a diagram that uses boxes and arrows to show the steps and decisions of an algorithm visually. It helps people plan and understand the flow of a program before coding it.
What does a 'counter' variable typically do inside a loop?
A counter variable keeps track of how many times something has happened, usually by increasing by one each time the loop runs. It is often used to control when a loop should stop.
count = 0
for item in [5, 9, 2]:
count = count + 1
print(count) # 3When teaching a computer to sort emails as 'spam' or 'not spam', what kind of machine learning task is this?
This is a classification task, where the model learns to put each input into one of a few categories or classes. The computer learns from many labeled examples of spam and non-spam emails to decide which group a new email belongs to.
Related topics in Data & MLOps
Put it into practice with AI Learning
This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.
- Free
- No account
- Works offline