Problem Solving and Programming 1 Flashcards
What is a variable in programming?
A variable is a container that holds a value or data that can be changed during program execution.
What is the difference between an integer and a float?
An integer is a whole number, while a float is a number that includes a decimal point.
What is the purpose of the “if” statement in programming?
An “if” statement allows you to execute certain code only if a specified condition is true.
What is a loop in programming?
A loop is a programming construct that repeats a block of code multiple times based on a condition.
What is the difference between a “for” loop and a “while” loop?
A “for” loop repeats a block of code a set number of times, while a “while” loop repeats as long as a given condition is true.
What is an algorithm in computer science?
An algorithm is a step-by-step procedure for solving a problem or performing a task.
What is the purpose of a function in programming?
A function is a reusable block of code that performs a specific task, making the program more modular and easier to maintain.
What is a parameter in a function?
A parameter is a variable in a function definition that receives a value when the function is called.
What is a return value in a function?
A return value is the result that a function gives back after performing its task, often used to send data back to the calling code.
What is debugging?
Debugging is the process of identifying and fixing errors or bugs in the code to ensure the program runs as expected.
What is a syntax error?
A syntax error occurs when the code violates the rules of the programming language, such as missing parentheses or a misplaced semicolon.
What is a logical error in programming?
A logical error happens when the code runs without crashing but produces incorrect results due to a flaw in the logic.
What is a “while” loop used for?
A “while” loop is used to repeat a block of code as long as a specified condition remains true.
What is a list in programming?
A list is an ordered collection of items, which can be of different data types, and is often used to store multiple values.
How do you access an item in a list in Python?
You access an item in a list using its index, starting from 0.
Example: my_list[0] accesses the first item.
What is an array?
An array is a data structure that stores multiple items of the same data type in contiguous memory locations.
What is the difference between a list and an array?
A list is more flexible and can store different data types, while an array stores only the same data type and is often more efficient for large amounts of data.
What does the “continue” statement do in a loop?
The “continue” statement skips the current iteration of the loop and moves to the next one.
What is the purpose of the “break” statement in loops?
The “break” statement terminates the loop immediately and exits the loop, regardless of the loop’s condition.
What is the difference between a “global” and “local” variable?
A global variable is declared outside of a function and can be accessed anywhere in the program, while a local variable is declared inside a function and can only be accessed within that function.
What is a string in programming?
A string is a sequence of characters, such as text, enclosed in quotation marks.
How do you concatenate two strings in Python?
You concatenate strings using the + operator.
Example: “Hello” + “ “ + “World” results in “Hello World”.
What is a conditional expression?
A conditional expression is an expression that evaluates to one of two values depending on whether a condition is true or false.
What is the purpose of the “else” clause in an “if” statement?
The “else” clause specifies code to execute if the condition in the “if” statement is false.
What is the difference between “==” and “=” in programming?
”==” is a comparison operator that checks if two values are equal, while “=” is an assignment operator used to assign a value to a variable.
What is a nested loop?
A nested loop is a loop inside another loop, used for iterating through multidimensional data structures like matrices.
What is an infinite loop?
An infinite loop occurs when a loop’s condition is always true, causing it to run endlessly unless externally stopped.
What does “try” and “except” do in Python?
“try” allows you to execute code that may raise an error, while “except” lets you handle the error without stopping the program.
What is a comment in programming?
A comment is a non-executable line of code used to add notes or explanations for others or yourself.
How do you write a single-line comment in Python?
You use the # symbol.
Example: # This is a comment.
What is the difference between “==” and “is” in Python?
”==” compares values, while “is” checks if two variables point to the same object in memory.
What is a method in programming?
A method is a function that is associated with an object and performs an action on that object.
What is the purpose of the “len()” function in Python?
The “len()” function returns the number of items (length) in a list, string, or other iterable.
What is a tuple?
A tuple is an ordered, immutable collection of items, often used to store multiple values that should not change.
How is a tuple different from a list?
A tuple is immutable (cannot be modified), while a list is mutable (can be changed).
What is the difference between “and” and “or” in Boolean expressions?
“and” returns True if both conditions are true, while “or” returns True if at least one condition is true.
What is an exception in programming?
An exception is an error that occurs during program execution, which can be handled to prevent the program from crashing.