Python Basic Interview Question
1. Is Python a compiled language or an interpreted language?
Please remember one thing, whether a language is compiled or
interpreted or both is not defined in the language standard. In other
words, it is not a properly of a programming language. Different Python
distributions (or implementations) choose to do different things (compile
or interpret or both). However the most common implementations like
CPython do both compile and interpret, but in different stages of its
execution process.
● Compilation: When you write Python code and run it, the source
code (.py files) is first compiled into an intermediate form called
bytecode (.pyc files). This bytecode is a lower-level representation
of your code, but it is still not directly machine code. It’s something
that the Python Virtual Machine (PVM) can understand and
execute.
● Interpretation: After Python code is compiled into bytecode, it is
executed by the Python Virtual Machine (PVM), which is an
interpreter. The PVM reads the bytecode and executes it
line-by-line at runtime, which is why Python is considered an
interpreted language in practice.
Some implementations, like PyPy, use Just-In-Time (JIT) compilation,
where Python code is compiled into machine code at runtime for faster
execution, blurring the lines between interpretation and compilation.
2. How can you concatenate two lists in Python?
We can concatenate two lists in Python using the +operator or the
extend() method.
1. Using the + operator:
This creates a new list by joining two lists together.
2. Using the extend() method:
This adds all the elements of the second list to the first list in-place.
3. Difference between for loop and while loop in Python
● For loop: Used when we know how many times to repeat, often
with lists, tuples, sets, or dictionaries.
● While loop: Used when we only have an end condition and don’t
know exactly how many times it will repeat.
4. How do you floor a number in Python?
To floor a number in Python, you can use the math.floor() function, which
returns the largest integer less than or equal to the given number.
floor()method in Python returns the floor of x i.e., the largest integer not
greater than x.
Also, The method ceil(x) in Pythonreturns a ceiling value of x i.e., the
smallest integer greater than or equal to x.
5. What is the difference between / and // in Python?
/ represents precise division (result is a floating point number) whereas //
represents floor division (result is an integer).
For Example:
6. Is Indentation Required in Python?
Yes, indentation is required in Python. A Python interpreter can be
informed that a group of statements belongs to a specific block of code by
using Python indentation. Indentations make the code easy to read for
developers in all programming languages but in Python, it is very
important to indent the code in a specific order.
7. Can we Pass a function as an argument in Python?
Yes, Several arguments can be passed to a function, including objects,
variables (of the same or distinct data types) and functions. Functions can
be passed as parameters to other functions because they are objects.
Higher-order functions are functions that can take other functions as
arguments.
The add function is passed as an argument to apply_func, which applies it
to 3 and 5.
8. What is a dynamically typed language?
● In a dynamically typed language, the data type of a variable is
determined at runtime, not at compile time.
● No need to declare data types manually; Python automatically
detects it based on the assigned value.
● Examples of dynamically typed languages: Python, JavaScript.
● Examples of statically typed languages: C, C++, Java.
● Dynamically typed languages are easier and faster to code.
● Statically typed languages are usually faster to execute due to type
checking at compile time.
Example:
Here, the type of x changes at runtime based on the assigned value hence
it shows dynamic nature of Python.
9. What is pass in Python?
● The pass statement is a placeholder that does nothing.
● It is used when a statement is syntactically required but no code
needs to run.
● Commonly used when defining empty functions, classes or loops
during development.
Here, fun() does nothing, but the code stays syntactically correct.
10. How are arguments passed by value or by reference in Python?
Python’s argument-passing model is neither “Pass by Value” nor “Pass by
Reference” but it is “Pass by Object Reference”.
Depending on the type of object you pass in the function, the function
behaves differently. Immutable objects show “pass by value” whereas
mutable objects show “pass by reference”.
You can check the difference between pass-by-value and
pass-by-reference in the example below:
SQL Basic Interview Question
1. What is SQL?
SQL (Structured Query Language) is a standard programming language
used to communicate with relational databases. It allows users to create,
read, update, and delete data, and provides commands to define database
schema and manage database security.
2. What is a database?
A database is an organized collection of data stored electronically,
typically structured in tables with rows and columns. It is managed by a
database management system (DBMS), which allows for efficient
storage, retrieval, and manipulation of data.
3. What are the main types of SQL commands?
SQL commands are broadly classified into:
● DDL (Data Definition Language): CREATE, ALTER, DROP,
TRUNCATE.
● DML (Data Manipulation Language): SELECT, INSERT,
UPDATE, DELETE.
● DCL (Data Control Language): GRANT, REVOKE.
● TCL (Transaction Control Language): COMMIT, ROLLBACK,
SAVEPOINT.
4. What is the difference between CHAR and VARCHAR2 data
types?
● CHAR: Fixed-length storage. If the defined length is not fully
used, it is padded with spaces.
● VARCHAR2: Variable-length storage. Only the actual data is
stored, saving space when the full length is not needed.
5. What is a primary key?
A primary key is a unique identifier for each record in a table. It ensures
that no two rows have the same value in the primary key column(s), and
it does not allow NULL values.
6. What is a foreign key?
A foreign key is a column (or set of columns) in one table that refers to
the primary key in another table. It establishes and enforces a relationship
between the two tables, ensuring data integrity.
7. What is the purpose of the DEFAULT constraint?
The DEFAULT constraint assigns a default value to a column when no
value is provided during an INSERT operation. This helps maintain
consistent data and simplifies data entry.
8. What is normalization in databases?
Normalization is the process of organizing data in a database to reduce
redundancy and improve data integrity. This involves dividing large
tables into smaller, related tables and defining relationships between them
to ensure consistency and avoid anomalies.
9. What is denormalization, and when is it used?
Denormalization is the process of combining normalized tables into larger
tables for performance reasons. It is used when complex queries and joins
slow down data retrieval, and the performance benefits outweigh the
drawbacks of redundancy.
10. What is a query in SQL?
A query is a SQL statement used to retrieve, update, or manipulate data in
a database. The most common type of query is a SELECT statement,
which fetches data from one or more tables based on specified conditions.