MCQs Python Programming 60+

1. What will be the output of the following python code?

a=2

def fun(a=3):

  global a

  a+=5

fun()

print(a)

Ans: Error

2. consider the following statement and choose the correct option:

a=[10, 4, 1, 50, 8, 2]

b=sorted(a, reverse='No')

print(b)

(1,2,4,8,10,50)

(50, 10, 8, 4, 2, 1)

[10, 4, 1, 50, 8, 2)

Ans: Error

3. What will be the output of the following statements?

s1="Hello World"

s2=[s1-3:-7:-1]

Ans: roW

4. What will be the output of the following code:

If (3-4//2):
 
  print("Hello")
 
else:
 
  print("Hiii")

Ans: Hello

5. Consider the following statements and choose the correct option:

a=(10, 20, 30, 40)

a.insert(50, 3)

Ans: Error because insert operator not supported

6. What will be the output of the following python code?

Count=0
for i in range(10):

 count+=i

 if i==3:

  break

print(count)

Ans: 6

7. consider the following statements and choose the correct option:

a=[10, 20, 30, 40]

a.insert(10, 3)

Ans: 3 will be inserted at the last position.

8. Which of the following is having highest precedence?

  • **
  • ()
  • /
  • //

Ans: ()

9. Consider the following statement and choose the correct option:

s1="Hello World"

s2=s1
  • s1 is the alias of s2.
  • ld of s1 and s2 is same.
  • ld of s1 and s2 is different.
  • None of these

Ans: ld of s1 and s2 is same

10. Which of the following syntax is used to make a data member protected in a class.

  • protected
  • __variable name__

Ans: –

11. Which of the following statements come in this python code?

class Name:
  def __init__(self,x):
     Self.x=x
name1=Name(“ABC”)
name2=name1
  • It will throw the error as multiple references to the same object is not possible
  • id(name1) and id(name2) will have same value.
  • Both name1 and name2 will have reference to two different objects of class Name
  • All of the above

Ans: id(name1) and id(name2) will have same value

12. What will be the output of the following code?

count=0
 
For i in range(10):
 
   count+=i
 
   If i==3:
 
    Break
 
Print(count)
  • 6
  • 3
  • 37
  • 34

Ans: 6

13. What will be the output of the following code?

class Name:
 count = 0
 def __init__(self, x):
  self.x = x
Name.count += 1

name1 = Name("ABC")

name2 = name1

print(name1.__dict__)

Ans: {‘x’: ‘ABC’}

14. What will be the output of above code?

def __init__(self):
self.name="abc"
self.roll=1
def __init__(self, name, roll):
self.name=name
self.roll =roll
obj1=student()

Ans: It gives error because second constructor overwrites the first constructor. so we need to pass 2 arguments.

15. Which access specifier is by default in class data member and member function

Ans: public

16. What will be the output of the following code?

a = 4//2
b = 4//0
c = 4//4

print(a)
print(b)
print(c)

Ans: Zero division error

17. What will be the output of the following code?

f= open("myfile.txt", "w")
f.write("Python Programming")
f.write("Python Programming")
f.close()

f=open("myfile.txt", "r")
print(f.read())

Ans: Python ProgrammingPython Programming

18. Consider a file “MyFile. txt” saved in the local/working directory, which of the following commands can be used to open it in the writing mode?

Ans: f = open(“MyFile. txt”, “w”)

19. What will be the output of the following code?

import pickle

f = open("myfile.txt", "w")

pickle. dump("Python", f)
pickle. dump("Programming", f)
f.close()

f = open("myfile. txt", "r")
print(pickle.load(f))

Ans: none of the above

20. What will be the output of the following code?

a = 4//2

try:
 b = 4//0

except:
 print("Exception")

c=4//4

print(a)
print(b)
print(c)

Ans: Exception
     2
     Name Error

21. Choose the correct statement for the following code.

from tkinter import

top = Tk()

top.geometry("250x250") 
label= Label(top, text = "Welcome")

label.pack()
top.mainloop()

Ans: It will create a label with “Welcome” labelled on it and pack it on the window

22. What will be the output of the following code?

from tkinter import*

top = Tk()

top.geometry("250x250")
b1=Button(top,text="Button 1")
b1.grid()
b2=Button(top,text="Button 2")
b2.grid()

top.mainloop()

Ans: After the execution ,b1 will be placed above b2 on the top window

23. Which method can be used to obtain the value associated with the selected Radiobutton?

from tkinter import*

root = Tk()
root. geometry("250x250")

v=IntVar()

R1 = Radiobutton(root, text="A", variable=v, value=1)
R1. pack()

R2 = Radiobutton(root, text="B", variable=v, value=2)
R2. pack()

R3 = Radiobutton(root, text="C", variable=v, value=3)
R3. pack()

root.mainloop()

Ans: v.get()

24. What will be the output of the following code?

f = open("myfile.txt", w)
f.write("Indian")
f.write("\n")
f.write("Premier")
f.write("\n")
f.write("League")
f.close()

f = open("myfile.txt", r)
print(f.read())

f.close()

Ans: None of the above

25. In the GUI programming, origin of the coordinate system is specified by the

Ans: Top-Left corner of the window/frame

26. Choose the correct statement for the following code

from tkinter import*

top = Tk()

top.geometry("250x250")
button = Button(top, text= "OK")
button.pack()
top.mainloop()

Ans: It will create a button with “OK” labelled on it and pack it on the window specified by top

27. Consider the following code and choose the correct statement.

f = open("file1.txt", "W")
f.write("Python Programming")
f.close()

Ans: It will create a new file “file1.txt” in the working directory and write Python Programming” in it

28. What will be the output of the following code?

import pickle

f = open("myfile. txt", "wb")
pickle. dump(123.45, f)
f.close()

f = open("myfile.txt", "rb")
print(pickle.load(f))

Ans: 123.45

29. What will be the output of the following code

f=open("myfile8.txt", "w")
f.write("Pythan Programming")
f.write("\n")
f.write("Python Programming")
f.close()
f = open("myfile8.txt", "r")
print(f.read())
f.close()

Ans: Pythan Programming
     Python Programming

30. Assuming the credentials to be correct and the table students exists, choose the correct statement for the following code

import mysql.connector
mydb = mysql.connector.connect{
host = "localhost"
user="root"
password="12345"
database= "mydatabase"
}
my_cursor = mydb.cursor()

query = "INSERT INTO students(Name, RollNo, Course) VALUES(%s, %s, %s) “
values = [("ABC",121, "B Tech ")]
my_cursor.executemany(query, values)
mydb.commit()
  • It will insert ABC, 121, and B.Tech to the columns Name, RollNo, and Course respectively in the students table
  • It will give error as SQL command is not valid
  • It gives error as there is a mismatch in the datatypes
  • None of the above

Ans: It will insert ABC, 121, and B.Tech to the columns Name, RollNo, and Course respectively in the students table

31. Consider the following code and choose the correct statement. Assume the credentials to be correct.

import mysql.connector 
mydb = mysql.connector.connect( 
  host = "localhost", 
  user = "root", 
  passwd = "12345", 
  database = "myDB" 
) 
my_cursor = mydb.cursor() 
query = "SELECT * FROM students" 
my_cursor.execute(query) 
for rec in my_cursor: 
  print(rec)
  • It will print all the rows of table students in the ascending order.
  • It will print all the rows of table students in the descending order. 
  • It will print all the rows of table students in the order of their insertion.
  • It will give error as print() is not supported in mysql

Ans: (c) It will print all the rows of table students in the order of their insertion.

32. Consider the following code and choose the correct statement. Assume the credentials to be correct.

import mysql.connector 
mydb = mysql.connector.connect( 
    host = "localhost", 
    user = "root", 
    passwd = "12345", 
    database = "myDB" 
) 
 
my_cursor = mydb.cursor() 
query = "SELECT * FROM students ORDER BY Name DESC" 
my_cursor.execute(query) 
for rec in my_cursor: 
    print(rec)
  • It will print all the rows of table students in the ascending order of the Name attribute.
  • It will print all the rows of table students in the descending order of the Name attribute.
  • It will print all the rows of table students in the order of their insertion.
  • It will give error as print() is not supported in mysql.

Ans: (b) It will print all the rows of table students in the descending order of the Name attribute.

33. What SQL command will be used if you want to delete an existing table named as students?

  • DELETE TABLE students
  • DELETE * FROM students
  • DROP TABLE students
  • DROP students

Ans: (c) DROP TABLE students

34. Which of the following is the correct statement for numpy?

  • numpy stands for Numerical Python.
  • numpy stands for Numbered Python.
  • numpy stands for Numerous Python.
  • numpy is not an abbreviation

Ans: (a) numpy stands for Numerical Python.

35. Consider the following code and choose the correct statement. Assume the credentials to be correct.

import mysql. connector

mydb = mysql. connector. connect(
host = "localhost",
user = "root",
passwd = "abc@123"
)

my_cursor = mydb. cursor()
my_cursor. execute("SHOW DATABASES")
for db in my_cursor:
print(db)
  • It will show the names of all databases which have been created.
  • It will show the names of all databases which have been created along with their time of creation.
  • It will give error as SQL command is invalid.
  • None of the above

Ans: (a) It will show the names of all databases which have been created.

36. The SQL command to create a database “mydatabase” is

Ans: CREATE DATABASE mydatabase

37. What will happen after execution of following sql command:

UPDATE students SET rollno = 25 WHERE name= ‘sachin’

Ans: It will set the rollno attribute equal to 25 in all the rows where name is Sachin

38. Output:

import mysql.connector
       mydb = mysql.connector.connect(
       host = “localhost”, 
       user = “root”,
       passwd = “abc@123”,
       database = “mydatabase”
 )
my_cursor = mydb.cursor()
query = CREATE TABLE students (name varchar(40), rollno int(10), course varchar(20))
my_cursor.execute(query)
mydb.commit() 

Ans: It will give error

39. Consider the following code and choose the correct statement. Assume the credentials to b

import mysql.connector

mydb= mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "12345",
database = "myDB"
)

my_cursor = mydb.cursor()
query = "SELECT * FROM students ORDER BY Name" my_cursor.execute(query)
for rec in my_cursor:
print(rec)
  • It will print all the rows of table students in the ascending order of the Name attribute.
  •  it will print all the rows of table students in the descending order of the Name attribute
  • It will print all the rows of table students in the order of their insertion
  •  it will give error as print() is not supported in mysql

Ans: (a) It will print all the rows of table students in the ascending order of the Name attribute.

40. Which clause/statement in SQL is used to fetch the rows from a table based on certain conditions

Ans: Both where and If can be used

41. What will be the output of following code?

import pandas as pd
Data = {“car”:[“s-cross”,”innova”,”Santro”],
“manufacturer”:[“Suzuki”,”toyota”,”Hyundai”],
“mileage”:[24,18,22] 
}
D = pd.DataFrame(data,index = [“car1”,”car2”,”car3”])
print(type(D))  

Ans: <class ‘pandas.core.frame.DataFrame’>

42. Choose the correct statement:

  • matplotlib is used to perform advanced mathematical operations in Python
  • matplotlib is used to plot graphs in Python.
  • matplotlib is used to deal with multidimensional data structures in Python.
  • matplotlib is used to deal with multidimensional arrays in Python.

Ans: (b) matplotlib is used to plot graphs in Python.

43. Consider the following code:

import pandas as pd

data = [[“Corolla","Toyota"],["Baleno", "Suzuki"],["Creta","Hyundai"]]


----
print (df)

Which statement should be written in the blank space to get the following output?

Name Brand
Corolla Toyota
Baleno Suzuki
Creta Hyundai
  • df-data DataFrame(columns-[“Name” “Brand”))
  • df=pd.data.DataFrame(columns-(“Name”,”Brand”])
  • df =pd.DataFrame(data,columns=”Name”, “Brand”])
  • df pd DataFrame(data,columns-[Name,Brand)

Ans: (c) df =pd.DataFrame(data,columns=”Name”, “Brand”])

44. Consider the following code:

from matplotlib import pyplot as plt

x= (1, 2, 3, 4, 5)
y=(2, 5, 10, 17, 26)
plt. xlabel("x axis")
plt. ylabel("y axis")
plt.plot(x, y)
plt.show()

The graph plotted after execution will satisfy the equation


  • y=2x+1
  • y = 3x+1
  • y=x^2+1
  • y = 2x^2

Ans: (c) y=x^2+1

45. What will be the output of the following code?

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a. shape)
  • (3, 2)
  • [3, 2]
  • (2, 3)
  • [2,3]

Ans: (c) (2,3)

46. The graph plotted after execution of the following code will correspond to

from matplottib import pyplotas pli

x = np arange(0,3* np.pl, 0.1)
y np.sin(x)
pit.plot(x,y)
pit show()

the following code will correspond to
  • Sin wave
  • Straight line
  • Ellipse
  • None of the above

Ans: Sin wave

47. Choose the correct output of the following code:

import pandas as pd
D = {“a”:1,”b”:2,”c”:3}
S = pd.Series(D)
print(s[c])

Ans: None of the above

48. Output of following code:

import numpy as np
a=np.array([[1,2,3],[4,5,6]])
b=a.reshape(3,2)
print(b.size)

Ans: 6

49. What will be the output?

from matplotlib import pyplot as plt

x= [1, 2, 3, 4, 5]
y=[1,2,3,4,5]
plt. xlabel("x axis")
plt. ylabel("y axis")
plt.plot(x, y)
plt.show()

Ans: A straight line

50. Choose the correct output for the following code:

import pandas as pd
D = {“a”:1,”b”:2,”c”:3}
S = pd.Series(D)
print(S)

Ans: a    1
     b    2
     c    3
     dtype: int64

51. Consider the following code steps:

STEP 1: import pandas as pd
STEP 2 : data = {"Car": ["SCross", "Innova", "Santro"], "Manufacturer": ["Suzuki", "Toyota", "Hyundai"],"Mileage": [24, 18, 22]}
STEP 3 : d= pd.data.DataFrame(index=["Car1", "Car2","Car3"])
STEP 4 : print(d)

Which of the above code steps is incorrect?
  • Step1
  • Step2
  • Step3
  • Step4

Ans: Step3

52. How to install numpy?

Ans: Pip-install numpy

53. DataFrame is

Ans: A Two-dimensional data structure

54. Output:

import numpy as np
A=np.array([[1,2,3],[4,5,6]])
print(a.size)

Ans: 6

55. What will be the output of the following python code?

a=2
def fun(a=3):
  a=5
  print(a)
fun()

Ans: 5

56. Assuming the credentials to be correct and the table students exists, choose the correct statement for the following code

import mysql.connector

mydb = mysql.connector.connect(
  host = "localhost",
  user = "root",
  passwd = "12345",
  database = "myDB"

)

my_cursor = mydb.cursor()
q = "INSERT INTO students(Name, RollNo, Course) VALUES(%s, %s, %s)"
v = [("XYZ", 123, "B.Tech. CSE")]
my_cursor.execute(q, v)
mydb.commit()
  • It will insert XYZ, 123, and B.Tech to the columns Name, RollNo, and Course respectively in the students table
  • It will give error as SQL command is not valid
  • It gives error as there is a mismatch in the datatypes
  • None of the above

Ans: (a) It will insert XYZ, 123, and B.Tech to the columns Name, RollNo, and Course respectively in the students table

57. Which of the following SQL queries can be used to fetch the row(s) from a table students where Name is Sachin?

  • SELECT FROM students WHERE Name = ‘Sachin’
  • SELECT ALL FROM students WHERE Name = ‘Sachin’
  • SELECT * FROM students WHERE Name = ‘Sachin’
  • SELECT * FROM students WHERE Name == ‘Sachin’

Ans: (c) SELECT * FROM students WHERE Name = ‘Sachin’

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *