Python Program Asked in Interviews & Exams 2020 {With Output)

Python is a general purpose language but nowadays it is mostly used in backend web development, data analysis, Artificial intelligence, and scientific computing. Further, It is a scripting language like PHP, Perl, Ruby. It is also used for web programming using the Django framework. Python also plays a role in developing complex scientific and numeric applications and it is designed with features to facilitate data analysis and visualization. Read>

Share This 

Python Program Asked in Interviews & Exams 2018 – 2019 {With Output)

Python was developed by Guido van Rossum in late 1980. Its name came from British TV show Monty Python. The very first version of this language was python 1.0 which was released in 1991. Currently, it has 3.6.6 version released. It is OOP language which has standard programming data structures, such as list, tuple and set, as part of its built-in types.

 

Python is a scripting language which is used for web applications. Now, it is used by thousands of developer for a web app (using Django framework), testing microchips at Intel, to powering Instagram, to building video games with the PyGame library. This language is mostly asked in several Interviews and exams 2018. We have curated a list of programs frequently asked nowadays in interviews. Moreover, we have attached images of the output of programs which will help you to gain a complete insight into the programs. Also, read our previous posts in the interview series>

Java Program Asked in Interviews & Exams 2018 {With Output)

C Programs that are Asked in Interviews & Exams in 2018 {With C output}

Interview Preparation 2018 (Frequently Asked Questions to Programmer)

 

Write a Python Program to Create the Multiplication Table of a Number.

In this program, the user is asked to enter the range up to which multiplication table will be displayed. The example to generate the multiplication table of a number (entered by the user) is – Enter an integer: 2, Output: 2 * 1 = 2 2 * 2 = 4 2* 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12. Here, in the example the table of 2 has range 6.

 

Program Code

n = int(input("Input a number: "))

# use for loop to iterate 20 times
for i in range(1,18):
print(n,'x',i,'=',n*i)

Program Code with Output

 

Python

 

Write a Program to Reverse a Sentence.

This program reverses the input given by the user. First, it prints “Enter a sentence: ” to ask an input in the form of a sentence.  Then, the program will immediately reverse the function which is called as the sentence is reversed. For example, Input: I AM A GEEK, Output: GEEK A AM I.

Program Code

def reverse(word):
b=word.split()
c=b[::-1]
d=" ".join(c)
print (d)

output_word=input('Please enter a sentence: \n')
reverse(output_word)

 

Program Code with Output

 

Python

 

Ask the User to Enter a Number and Determine Whether the Number is Prime or Not.

All positive integers (or non-negative integers) which are only divisible by 1 and the number itself are called prime number. For example, 11 is a prime number because it is only divisible by 1 and 11 but, 10 is not a prime number because it is divisible by 1, 2, 5 and 10.

 

Program Code

from Tools.Scripts.treesync import raw_input

num = int(raw_input('Insert a number: '))
a = [x for x in range(2, num) if num % x == 0]

def is_prime(n):
if num > 1:
if len(a) == 0:
print('prime')
else:
print('NOT prime')
else:
print('NOT prime')

is_prime(num)

Program Code with Output

 

Python

 

Ask the User to Enter a Number and Find that the Number is even or odd.

Integers which are perfectly divisible by 2 are called even numbers and those integers which are not divisible by 2 are called odd numbers. Here, in this program, the user is asked to enter a number. Then, the program will check if the number entered by the user is an even number or an odd number.

 

Program Code

num = int(input("give me a number to check: "))
check = int(input("give me a number to divide by: "))

if num % 4 == 0:
print(num, "is a multiple of 4")
elif num % 2 == 0:
print(num, "is an even number")
else:
print(num, "is an odd number")

if num % check == 0:
print(num, "divides evenly by", check)
else:
print(num, "does not divide evenly by", check)

Program Code with Output

 

Python

 

Write a Python Program to Find Factors of Number.

This Python program allows integer value. This program will find factors of a number using ‘for Loop’. Factors are the numbers you multiply together to get another number. For example, all the factors of 12: 1, 2, 3, 4, 6 and 12. As, 2 × 6 = 12, but also 3 × 4 = 12, and of course 1 × 12 = 12. So 1, 2, 3, 4, 6 and 12 are factors of 12.

 

Program Code

def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = 60
print_factors(num)

Program Code with Output

 

Python

 

Python Program to Check if a Number is Positive or Negative.

Here, this program takes a number from the user and checks whether that number is either positive or negative or zero. If a user enters a positive number, then the program will display a positive number. However, if the user enters a negative number, the program will display a negative number as the output. If a user enters a ‘0’, then the program will display ‘Zero’ because ‘0’ is neither positive nor a negative number.

 

Program Code

number = float(input("Enter a number: "))
if number >= 0:
if number == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

 

Program Code with Output

 

Python

 

Inheritance Code in Python.

Python also supports inheritance. In inheritance, one class can inherit attributes and behaviors methods from another class. There is parent-child relation in inheritance. A child class inherits attributes and behaviors methods of his parent class. Read, how inheritance can be explained with the help of the program.

 

Program Code

class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides
self.sides = [0 for i in range(no_of_sides)]

def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
class Triangle(Polygon):
def __init__(self):
Polygon.__init__(self,3)

def findArea(self):
a, b, c = self.sides
# calculate the semi-perimeter
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
t = Triangle()
t.inputSides()
t.dispSides()
t.findArea()

Program Code with Output

 

Python

 

Python Program to Find the Sum of Natural Numbers Using Recursion.

Positive numbers starting from 1 are known as natural numbers. For eg:- 1, 2, 3, 4, 5, 6,…. are natural numbers. The program here, take a positive integer from the user and calculates the sum up to the entered number by the user. We can find the sum of natural numbers by using a loop. Here, we solve this problem using recursion.

 

Program Code

def recur_sum(number):
if number <= 1:
return number
else:
return number + recur_sum(number-1)

num = 25
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))

Program Code with Output

 

Python

What do you Mean by Overriding Methods?

When we have a class B inherited from class A. Both have the method, their own version. B overrides in class A. So, both class A and B have a different implementation. Overriding and its explanation through a program is mostly asked in interviews. Let’s see how overriding methods can be explained through the program.

 

Program Code

class A:
def sayhello(self):
print("Hello, I'm A")

class B(A):
def sayhello(self):
print("Hel")
a=A()
b=B()
a.sayhello()

Program Code with Output

 

Python

 

Count the Even and Odd Numbers from a Series of Numbers.

There is a series of numbers, we have to write a program that finds how many odd and even numbers are there in the series. The rule is – First find out the even numbers or odd numbers in the series. For doing that, we have to use mod (%) of 2 to check which is odd or even in the given series. After that, count the numbers and print the even and odd numbers counted.

 

Program Code

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9,10,44,55,33,23,98,79,99,101)
count_odd = 0
count_even = 0
for x in numbers:
if not x % 2:
count_even+=1
else:
count_odd+=1
print("these are even numbers :",count_even)
print("these are  of odd numbers :",count_odd)

 

Program Code with Output

 

Python

 

Write a Python Program to Guess a Number Between 1 to 6.

Guess a number is a very popular game and also used in many games nowadays. In fact, guessing games are the most loved games. There is a built-in function for guess import random. Random means the program will fix the answer to a random number between our limit, in this program, 1 to 6 is the limit of guessing the numbers. When your answer matches with the answer of the computer, then it will display ‘ Great you guess the number’.

 

Program Code

import random
num,  guess_number = random.randint(1, 6), 0
while num != guess_number:
guess_number = int(input('Guess a number between 1 and 6 until you get it right : '))
print('Great you guess the number ')

 

Program Code with Output

 

Python

Write a Python Program to Check Whether an Alphabet is a Vowel or Consonant.

Vowels are:- a, e, i, o, u and the rest are consonants. Sometimes, y is considered as a vowel or sometimes consonant. So, we have to create if-else conditions accordingly. Here, in this program, we will find if the input by the user is a vowel or a consonant. However, this program is simple but sometimes many confuse with the total vowels and especially with the letter ‘y’.

 

Program Code

N = input("Input a letter of the alphabet: ")

if N in ('a', 'e', 'i', 'o', 'u'):
print("%s is a vowel." % N)
elif N == 'y':
print("Sometimes letter y stand for vowel, sometimes stand for consonant.")
else:
print("%s is a consonant." % N)

Program Code with Output

 

Python

 

 

We hope that you like the Python programs. However, we will write and post on other programming languages also. So, subscribe our site to get updates of published posts directly in your email. You can also follow us on Facebook, Twitter, LinkedIn,…etc. to get daily updates of interesting stuff and published posts. Don’t forget to like, share and comment on this post.

Please follow and like us:

10 thoughts on “Python Program Asked in Interviews & Exams 2020 {With Output)”

  1. Hold you avoid the use of words like “we, us, I” and use “you.” It keeps the article or tutorial personal. Chapters 4, 5, and 6 discuss cool things in science and Clegg gives suggestions for learning and teaching the topics.

  2. Nice blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweaks would really make my blog jump out.
    Please let me know where you got your theme.
    Kudos

  3. Wow! After all I got a weblog from where I be able to in fact take valuable facts regarding my study and knowledge.

  4. I’m in the process of starting a new blog in WordPress for Inner Clarity. Thanks for your posts.

  5. Good day! I could have sworn I’ve visited this website before but after going through many of the posts I realized it’s new to me. Nonetheless, I’m definitely happy I stumbled upon it and I’ll be bookmarking it
    and checking back often!

  6. Hi to every one, the contents existing at this website are truly remarkable for people experience, well, keep up the nice work fellows.

Comments are closed.