Python Interview Questions

1) What is Python? What are the benefits of using Python and its main features?
2) What is PEP 8?
3) What Are The Built-In Types Available In Python?
4) What is mutable and immutable objects in python?
5) Difference between remove, pop, del on lists?
6) What is the difference between list and tuple?
7) What are tuples?
8) What is difference between tuple and list ? Where will you use tuple and              where will you use list ?
9) What is Dynamic Typing ?
10) What are *args, **kwargs ?
11) How instance variables are different from class variables?
12) Differentiate between “*.py” file and “*.pyc” file?
13) Explain the use “with” statement in python?
14) What does the “self” keyword do?
15) What Are Different Methods To Copy an Object In Python?
16) What does the <yield> keyword do in Python?
17) What is pickling and unpickling?
18) How are arguments passed by value or by reference?
19) What is namespace in Python?
20) How you can convert a number to a string?
21) What is module and package in Python?
22) What is a pass in Python?
23) Write a function to merge the two lists into one sorted list:
       X = [2, 5, 7, 10, 11, 18]
       Y = [1, 4, 6, 12, 14, 17, 21]
24) How can we get home directory using '~' in Python?
25) Difference between range and xrange?
26) Generators in Python and its use?
27) What are Iterators ?
28) How to debug python code using PDB?
29) What Lambda and use in Python?
30) How does Python handle the memory management?
31) What is monkey patching? How can you do it in Python?
32) Why are functions considered first class objects in Python?
33) How will you reverse the list in Python?
34) Explain how can you generate random numbers in Python?
35) Explain the usage of decorators?

What is Data structure?

Data structure is a method of organizing large amount of data more efficiently so that any operation on that data becomes easy.

Data structures are divided into two types:
  • Linear Data Structures 
  • Non linear Data structures 
Linear Data Structure: In Linear data structure, data is organised in sequential order.
Ex: Arrays, Linked list, Stacks and Queues

Non Linear Data Structure: In Non Linear data structure, data is organised in random order.
Ex: Tree, Graphs

Interrupt Handling in Linux

What is Interrupt?

An interrupt (also known as an exception or trap) is an event that causes the CPU to stop executing the current program and start executing a special piece of code called an interrupt handler or interrupt service routine (ISR).

There are two different kinds of interrupts:

•Synchronous interrupt (Exception) produced by the CPU while processing instructions
•Asynchronous interrupt (Interrupt) issued by other hardware devices

Handling interrupts:
•Interrupts can occur at any time, the kernel tries to get it out of the way as soon as possible
•An interrupt can be interrupted by another interrupt
•There are regions in the kernel which must not be interrupted at all

Two different interrupt levels are defined:
•Maskable interrupts issued by I/O devices; can be in two states, masked or unmasked. Only unmasked interrupts are getting processed.
•Nonmaskable interrupts; critical malfunctions (f.e. hardware failure); always processed by the CPU.

Every hardware device has it's own Interrupt Request (IRQ) line. The IRQs are numbered starting from 0. All IRQ lines are connected to a Programmable Interrupt Controller (PIC). The PIC listens on IRQs and assigns them to the CPU. It is also possible to disable a specific IRQ line.

Top Half and Bottom Half:


One of the main problems with interrupt handling is how to perform lengthy tasks within a handler. Often a substantial amount of workmust be done in response to a device interrupt, but interrupt handlers need to finish up quickly and not keep interrupts blocked for long. These two needs (work and speed) conflict with each other,leaving the driver writer in a bit of a bind.

Linux (along with many other systems) resolves this problem by splitting the interrupt handler into two halves. The so-called top half is the routine that actually responds to the interrupt—the one you register with request_irq. The bottom half is a routine that is scheduled by the top half to be executed later, at a safer time. The big difference between the top-half handler and the bottom half is that all interrupts are enabled during execution of the bottom half—that’s why it runs at a safer time.

To implement bottom halves,two methods:
1) task lets
2) work queues

Linux Kernel Modules

Linux kernel modules are pieces of code that can be loaded and unloaded from kernel on demand.

Kernel modules offers an easy way to extend the functionality of the base kernel without having to rebuild or recompile the kernel again. Most of the drivers are implemented as a Linux kernel modules. When those drivers are not needed, we can unload only that specific driver, which will reduce the kernel image size.

Kernel modules will have extension .ko
Kernel modules will operate on kernel space.
All Drivers are modules. Not all modules are drivers.

Kernel Modules Commands:
lsmod: To see list of modules that already loaded on system
insmod: To insert modules into kernel
modinfo: To display modules information
rmmod: To remove modules from kernel

How to Write Kernel Modules:

module.c
#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>      // included for __init and __exit macros

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Name");
MODULE_DESCRIPTION("Hello World module");

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    return 0;    // Non-zero return means that the module couldn't be loaded.
}

static void __exit hello_cleanup(void)
{
    printk(KERN_INFO "Cleaning up module.\n");
}

module_init(hello_init);
module_exit(hello_cleanup);
Makefile to compile module:
obj-m += hello.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
When a module is inserted into the kernel, the module_init macro will be invoked, which will call the function hello_init. Similarly, when the module is removed with rmmod, module_exit macro will be invoked, which will call the hello_exit. Using dmesg command, we can see the output from the sample Kernel module.

printk() is used for printing kernel messages

Latest Kernel version

Current stable kernel version: 4.5.1
https://www.kernel.org/