Q1: What is the fundamental difference between an interpreter and a compiler?
A: An interpreter translates code line-by-line and executes it immediately, while a compiler translates the entire code into machine code before execution.Q2: What are the built-in data types in Python?
A: The built-in data types in Python include integers, floats, strings, booleans, lists, dictionaries, tuples, and sets.Q3: Explain the 'if' statement in Python.
A: The 'if' statement in Python is used for conditional execution. It executes a block of code only if a specified condition is true.Q4: What is a list in Python?
A: A list is a data structure in Python that can hold a collection of items. Lists are mutable, ordered, and can contain elements of different data types.Q5: What is a dictionary in Python?
A: A dictionary in Python is a collection of key-value pairs. It is unordered, mutable, and each key within a dictionary must be unique.Q6: Explain tuples in Python.
A: Tuples are immutable sequences in Python, typically used to store collections of heterogeneous data. They are created using parentheses and can contain elements of different data types.Q7: What are sets in Python?
A: Sets in Python are unordered collections of unique elements. They are mutable but do not allow duplicate values.Q8: What is Object-Oriented Programming (OOP)?
A: Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data in the form of attributes and code in the form of methods.Q9: Give an example of a language that uses an interpreter.
A: Python is an example of a language that uses an interpreter. The Python interpreter executes Python code directly.Q10: What is the difference between a float and an integer in Python?
A: A float is a data type that represents floating-point numbers (decimal numbers), while an integer represents whole numbers without any decimal point.Q11: What is the purpose of the 'else' statement in Python's 'if' condition?
A: The 'else' statement is used to execute a block of code when the condition specified in the 'if' statement is false.Q12: How do you access elements in a list in Python?
A: Elements in a list can be accessed using indexing. Indexing starts from 0, so the first element is at index 0, the second at index 1, and so on.Q13: Can a dictionary in Python contain duplicate values?
A: No, dictionaries in Python cannot contain duplicate keys. Each key must be unique within a dictionary.Q14: What is the main difference between a list and a tuple?
A: The main difference is that lists are mutable (can be changed), while tuples are immutable (cannot be changed after creation).Q15: How do you remove an item from a set in Python?
A: You can use the 'remove()' method to remove a specific item from a set, or 'discard()' method which won't raise an error if the item is not present.Q16: What is a class in Python?
A: A class in Python is a blueprint for creating objects. It defines the attributes and methods common to all objects of a certain kind.Q17: Explain the advantage of using a compiler over an interpreter.
A: The main advantage of using a compiler is that it translates the entire code into machine code before execution, potentially leading to faster execution compared to interpretation.Q18: How do you check the type of a variable in Python?
A: You can use the 'type()' function to check the type of a variable in Python. For example, 'type(variable_name)' returns the type of 'variable_name'.Q19: What is the purpose of the 'elif' statement in Python's 'if-elif-else' ladder?
A: The 'elif' statement is used to check multiple conditions one by one. It is executed if the previous conditions are false and its condition is true.Q20: How do you append an element to a list in Python?
A: You can use the 'append()' method to add an element to the end of a list in Python.Q21: How do you access the value associated with a key in a dictionary?
A: You can access the value associated with a key in a dictionary using square brackets with the key inside. For example, 'my_dict[key]' returns the value associated with 'key'.Q22: Can you change the elements of a tuple after it has been created?
A: No, tuples are immutable, which means you cannot change their elements after they have been created.Q23: What is the difference between 'add()' and 'update()' methods in sets?
A: The 'add()' method adds a single element to a set, while the 'update()' method adds multiple elements from another set (or any iterable) to the current set.Q24: What is inheritance in OOP?
A: Inheritance is a mechanism in OOP that allows a new class to inherit properties and behaviors (attributes and methods) from an existing class.Q25: Give an example of a language that uses a compiler.
A: C and C++ are examples of languages that use compilers. They compile the code into machine code before execution.Q26: What is the difference between 'int()' and 'float()' functions in Python?
A: 'int()' function converts a value to an integer, while 'float()' function converts a value to a floating-point number.Q27: What is the purpose of the 'for' loop in Python?
A: The 'for' loop in Python is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence.Q28: How do you remove an element from a list in Python?
A: You can remove an element from a list using methods like 'remove()', 'pop()', or 'del'. The 'remove()' method removes the first occurrence of a specified value, 'pop()' removes an element at a specific index and returns it, and 'del' removes an element at a specific index or deletes the entire list if used without an index.Q29: Can a dictionary have multiple values for the same key?
A: No, each key in a dictionary must be unique. If you try to assign a new value to an existing key, it will overwrite the previous value associated with that key.Q30: What is the syntax for creating an empty tuple in Python?
A: An empty tuple can be created using empty parentheses '()'. For example, 'my_tuple = ()'.Q31: How do you perform set intersection and set union operations in Python?
A: Set intersection can be performed using the '&' operator or 'intersection()' method, while set union can be performed using the '|' operator or 'union()' method.Q32: What is encapsulation in OOP?
A: Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class). It helps in hiding the internal state of an object and restricting direct access to it from outside the class.Q33: Explain the concept of 'just-in-time' compilation.
A: Just-in-time (JIT) compilation is a hybrid approach that combines aspects of both interpretation and compilation. It involves compiling code into machine code at runtime, just before executing it, allowing for optimizations tailored to the specific runtime environment.Q34: What is the difference between a string and a list in Python?
A: A string is a sequence of characters, while a list is a collection of items that can be of different data types. Strings are immutable, meaning they cannot be changed after creation, while lists are mutable and can be modified.Q35: How do you exit a loop prematurely in Python?
A: You can exit a loop prematurely using the 'break' statement. When the 'break' statement is encountered within a loop, the loop is terminated immediately, and control passes to the next statement after the loop.Q36: What is the difference between the 'extend()' and 'append()' methods in Python lists?
A: The 'extend()' method is used to add elements from another list to the end of the current list, effectively extending it. The 'append()' method, on the other hand, adds a single element to the end of the list.Q37: How do you check if a key exists in a dictionary?
A: You can use the 'in' keyword to check if a key exists in a dictionary. For example, 'if key in my_dict:' checks if 'key' exists in 'my_dict'.Q38: Can you concatenate two tuples in Python?
A: Yes, you can concatenate two tuples using the '+' operator. For example, 'tuple1 + tuple2' will concatenate 'tuple1' and 'tuple2' into a new tuple.Q39: What is the purpose of the 'difference()' method in sets?
A: The 'difference()' method in sets is used to get the difference between two sets. It returns a new set containing elements that are present in the first set but not in the second set.Q40: What is polymorphism in OOP?
A: Polymorphism is the ability of objects to take on different forms or behave differently based on the context in which they are used. It allows objects of different classes to be treated as objects of a common superclass, enabling code reuse and flexibility in design.
No comments:
Post a Comment