Infosys
Interview Questions and Answers
Q1. what is list comprehension? write some sample code? what is use of it?
List comprehension is a concise way to create lists in Python by iterating over an existing list or iterable.
List comprehension is more concise and readable than traditional loops.
It can be used to filter elements, perform operations on elements, or create new lists based on existing ones.
Example: squares = [x**2 for x in range(10)]
Example: even_numbers = [x for x in range(10) if x % 2 == 0]
Q2. what is dictionary? is this accept duplicate or not?
A dictionary in Python is a collection of key-value pairs. It does not accept duplicate keys.
A dictionary is created using curly braces {}
Keys in a dictionary must be unique, but values can be duplicated
Example: my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
Q3. what is generator? write sample code
A generator in Python is a function that returns an iterator object which can be iterated over to generate values lazily.
Generators are created using a function with 'yield' keyword instead of 'return'.
They allow for efficient memory usage as they generate values on the fly.
Generators are useful for generating large sequences of data without storing them in memory.
Example: def my_generator(): for i in range(5): yield i
Example: gen = my_generator() for value in gen: print(valu...read more
Q4. what are the main principles of oops?
Main principles of OOP include encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation: Bundling data and methods that operate on the data into a single unit (class). Example: Class with private attributes and public methods.
Inheritance: Ability to create a new class (derived class) from an existing class (base class), inheriting its attributes and methods. Example: Subclass 'Dog' inheriting from superclass 'Animal'.
Polymorphism: Ability for objects of differe...read more
Q5. write code of fibonacci series?
Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones.
Initialize variables for the first two numbers in the series (0 and 1)
Use a loop to calculate the next number by adding the previous two numbers
Continue the loop until the desired number of terms is reached
Q6. what is aws lambda function
AWS Lambda is a serverless computing service provided by Amazon Web Services.
Serverless computing service
Allows running code without provisioning or managing servers
Automatically scales based on incoming traffic
Supports multiple programming languages like Python, Node.js, Java, etc.
Pay only for the compute time consumed
Q7. what is decorator?
Decorator is a design pattern in Python that allows adding new functionality to an existing object without modifying its structure.
Decorators are functions that take another function as an argument and extend its behavior without modifying it directly.
They are commonly used to add logging, timing, caching, or authentication to functions.
Decorators use the @ symbol followed by the decorator name above the function definition.
Example: @decorator_name def function_name(): pass
Q8. difference between list and tuple
List is mutable, tuple is immutable in Python.
List can be modified after creation, tuple cannot.
List uses square brackets [], tuple uses parentheses ().
List is used for dynamic data, tuple for fixed data.
Example: list_example = [1, 2, 3], tuple_example = (4, 5, 6)
Q9. explain type of inheritance
Type of inheritance in object-oriented programming determines how a subclass can inherit attributes and methods from a superclass.
Single inheritance: a subclass inherits from only one superclass.
Multiple inheritance: a subclass inherits from multiple superclasses.
Multilevel inheritance: a subclass inherits from a superclass, which in turn inherits from another superclass.
Hierarchical inheritance: multiple subclasses inherit from a single superclass.
Hybrid inheritance: a combi...read more
Reviews
Interviews
Salaries
Users/Month