Python 2
Python 2
Explanation: Since there is no concept of private variables in Python language, the major underscore is used
to denote variables that cannot be accessed from outside the class.
1. val
2. raise
3. try
4. with
(a) val
Which of the following statements is correct for variable names in Python language?
Explanation: None
1. xyzp = 5,000,000
2. x y z p = 5000 6000 7000 8000
3. x,y,z,p = 5000, 6000, 7000, 8000
4. x_y_z_p = 5,000,000
(b) x y z p = 5000 6000 7000 8000
1. _val
2. val
3. try
4. _try_
(c) try
Explanation: None
Study the following function:
1. round(4.576)
What will be the output of this function?
1. 4
2. 5
3. 576
4. 5
(d) 5
Explanation: The round function is a built-in function in the Python language that round-off the value (like 3.85
is 4), so the output of this function will be 5.
1. pow(x,y,z)
1. (x**y) / z
2. (x / y) * z
3. (x**y) % z
4. (x / y) / z
(c) (x**y) % z
1. all([2,4,0,6])
What will be the output of this function?
1. False
2. True
3. 0
4. Invalid code
False
Explanation: If any element is zero, it returns a false value, and if all elements are non-zero, it returns a true
value. Hence, the output of this "all([2,4,0,6])" function will be false.
Study the following program:
1. x = 1
2. while True:
3. if x % 5 = = 0:
4. break
5. print(x)
6. x+=1
1. error
2. 21
3. 031
4. None of these
(a) error
Which one of the following syntaxes is the correct syntax to read from a simple text file stored in ''d:\java.txt''?
Explanation: None
1. x = ['XX', 'YY']
2. for i in a:
3. i.lower()
4. print(a)
What will be the output of this program?
1. ['XX', 'YY']
2. ['xx', 'yy']
3. [XX, yy]
4. None of these
(a) ['XX', 'YY']
1. import math
2. abs(math.sqrt(36))
What will be the output of this code?
1. Error
2. -6
3. 6
4. 6.0
(d) 6.0
1. False
2. Ture
3. Invalid code
4. None of these
(b) True
1. >>>"a"+"bc"
What will be the output of this statement?
1. a+bc
2. abc
3. a bc
4. a
(b) abc
Explanation: In Python, the "+" operator acts as a concatenation operator between two strings.
1. t
2. j
3. point
4. java
(a) t
Explanation: The correct output of this program is "t" because -1 corresponds to the last index.
1. java
point
2. java point
3. \njavat\npoint
4. Print the letter r and then javat and then point
(c) \njavat\npoint
Explanation: None
(a) 33
Explanation: A, B and C are hexadecimal integers with values 10, 11 and 12 respectively, so the sum of A, B
and C is 33.
1. 18
2. -18
3. 17
4. -17
(b) -18
Explanation: ASCII value of h is less than the z. Hence the output of this code is 104-122, which is equal to -
18.
1. x = ['xy', 'yz']
2. for i in a:
3. i.upper()
4. print(a)
Which of the following is correct output of this program?
1. ['xy', 'yz']
2. ['XY', 'YZ']
3. [None, None]
4. None of these
1. i = 1:
2. while True:
3. if i%3 == 0:
4. break
5. print(i)
Which of the following is the correct output of this program?
1. 123
2. 321
3. 12
4. Invalid syntax
1. a = 1
2. while True:
3. if a % 7 = = 0:
4. break
5. print(a)
6. a += 1
Which of the following is correct output of this program?
1. 12345
2. 123456
3. 1234567
4. Invalid syntax
1. i = 0
2. while i < 5:
3. print(i)
4. i += 1
5. if i == 3:
6. break
7. else:
8. print(0)
1. 1 2 3
2. 0 1 2 3
3. 0 1 2
4. 3 2 1
1. i = 0
2. while i < 3:
3. print(i)
4. i += 1
5. else:
6. print(0)
1. 01
2. 012
3. 0120
4. 0123
1. z = "xyz"
2. j = "j"
3. while j in z:
4. print(j, end=" ")
What will be the output of this statement?
1. xyz
2. No output
3. xyz
4. j j j j j j j..
(b) No output
1. x = 'pqrs'
2. for i in range(len(x)):
3. x[i].upper()
4. print (x)
Which of the following is the correct output of this program?
1. PQRS
2. pqrs
3. qrs
4. None of these
1. abc
2. 012
3. 0a 1b 2c
4. None of these above
1. d = {0, 1, 2}
2. for x in d:
3. print(x)
What will be the output of this statement?
Which of the following option is not a core data type in the python language?
1. Dictionary
2. Lists
3. Class
4. All of the above
Explanation: Class is not a core data type because it is a user-defined data type.
What error will occur when you execute the following code?
1. MANGO = APPLE
1. NameError
2. SyntaxError
3. TypeError
4. ValueError
(a) NamaError
1. def example(a):
2. aa = a + '1'
3. aa = a*1
4. return a
5. >>>example("javatpoint")
What will be the output of this statement?
1. hello2hello2
2. hello2
3. Cannot perform mathematical operation on strings
4. indentationError
1. print(print(print("javatpoint")))
What will be the output of this program?
Explanation: In this program, the inner print function will run first as compared to the outer print function.
Therefore, the correct output of this program is "javatpoint None None".
Study the following program:
True = 1
False = 0
(1 ** 0 / 1) = (10/ 1) = 1.0
1. int1 = 10
2. int2 = 6
3. if int != int2:
4. int2 = ++int2
5. print(int1 - int2)
What will be the output of this program?
1. 2
2. 4
3. 6
4. None
(b) 4
Explanation: In the Python Programming Language, Increment and Decrement condition is not valid.
(y = ++y) = (y = y)
1. int1 = 10
2. int2 = 6
3. if int != int2:
4. int2 = ++int1
5. print(int1 - int2)
What will be the output of this program?
1. 2
2. 4
3. 0
4. No Output
(c) 0
Explanation: In the Python Programming Language, Increment and Decrement condition is not valid.
int1 = (10)
int2 = (6)
int2 = ++int1
int2 = 10
print(10 - 10)
1. print(6 + 5 - 4 * 3 / 2 % 1)
What will be the output of this program?
1. 7
2. 7.0
3. 15
4. 0
(d) 11.0
High *, /, // and%
Low + and -
If the operator precedence is same, the calculation starts from left to right.
1. word = "javatpoint"
2. print(*word)
What will be the output of this program?
1. javatpoint
2. javatpoint
3. *word
4. SyntaxError: invalid syntax
(b) j a v a t p o i n t
Explanation: When a user prints a string with "*", that string is printed with the space in each word. Therefore,
option (b) is the correct output of this program.
Study the following program:
1. i = 2, 10
2. j = 3, 5
3. add = i + j
4. print(add)
What will be the output of this program?
1. (5, 10)
2. 20
3. (2, 10, 3, 5)
4. SyntaxError: invalid syntax
Explanation: "i" and "j" are tuples values. The tuple values are added to the bracket. Therefore, option (c) is
the correct output of this program.
1. 22
2. 18
3. 20
4. 7
(d) 7
Explanation: In the python programming language, (int(6 == 6.0)) is a valid condition but in other languages is
not a valid condition. Therefore, option (d) is the correct output of this program.
1. i=2
2. j = 3, 5
3. add = i + j
4. print(add)
What will be the output of this program?
1. 5, 5
2. 5
3. (2 , 3 , 5)
4. TypeError
(d) TypeError
Explanation: In this program, "i" is the integer value, and "j" is the tuple value. The integer and tuple values
cannot be added in the python programming language. Therefore, this program will print the "Typeerror".
1. Four
2. Five
3. Three
4. None of the these
(c) Three
Explanation: In the Python Programming Language, there are three types of control statements.
1. Break
2. Continue
3. Pass statements
Which of the following arithmetic operators cannot be used with strings in python?
1. +
2. *
3. -
4. All of the mentioned
(c) -
Explanation: In python, only (+) and (*), two arithmetic operators are used with string. Therefore, option (c) is
the correct answer.
Study the following program:
1. javapoint2
2. japoint
3. java2point
4. javapoin2
(c) java2point
Explanation: The "sep" means separator that is used to add a separator between the two strings. Therefore,
option (c) is the correct output of this program.
1. _ = '1 2 3 4 5 6'
2. print(_)
(d) 1 2 3 4 5 6
Explanation: "_" is a valid variable name. Therefore, option (d) is the correct output of this program.
1. None
2. class
3. goto
4. and
(c) goto
Explanation: "and", "class", and "None" are reversed keywords in python. So, option (c) is the correct answer.
Study the following program:
1. a = '1 2'
2. print(a * 2)
3. print(a * 0)
4. print(a * -2)
What will be the output of this program?
1. 1212
2. 24
3. 0
4. -1 -2 -1 -2
Answer: (a) 1 2 1 2
Explanation:
1. 145
2. 122
3. a
4. z
Answer: (d) z
Explanation: The ASCII value of the a-z lies in the range 97 - 122. So, the maximum value of the string is 122
(z = 122).
Study the following program:
1. a = "123789"
2. while x in a:
3. print(x, end=" ")
What will be the output of this program?
1. iiiiii…
2. 123789
3. SyntaxError
4. NameError
Explanation: This program will print the NameError because 'x' is not defined in this code.
1. Python interpreter
2. Python compiler
3. Python volatile machine
4. Portable virtual machine
Explanation: PVM is a software that converts bytecode to machine code for a given OS. PVM is also called
Python Interpreter, and that is why Python is called Interpreted Language.
1. i = {4, 5, 6}
2. i.update({2, 3, 4})
3. print(i)
What will be the output of this program?
1. 234456
2. 23456
3. 456234
4. Error, duplicate element presents in list
5.
Answer: (b) 2 3 4 5 6
Explanation: This is a valid syntax of the update function. Therefore, the option (b) is the correct output of this
program.
1. 0 1 12 20 25
2. 1 12 20 25
3. FunctionError
4. AttributeError
Explanation: In this program, "i" is the tuple value. The tuple value cannot be sorted in python language.
Therefore, this program will print the "AttributeError".
24) Which of the following keywords is used for function declaration in Python language?
1. def
2. function_name
3. define
4. None of the these
Explanation: In the python language, the def keyword is used to define the function.
def function_name(parameters):
block of function
return expression
25) Which of the following objects are present in the function header in python?
1. Function name and Parameters
2. Only function name
3. Only parameters
4. None of the these
Explanation: Function name and Parameter are both present in the function header in python.
def function_name(parameters):
block of function
return expression
26) When a user does not use the return statement inside a function in Python, what will return the function in
that case.
1. 0
2. 1
3. None
4. No output
Explanation: When a user does not use the return statement inside a function in Python, the function will
return the "None".
27) Which one of the following is the right way to call a function?
1. call function_name()
2. function function_name()
3. function_name()
4. None of the these
Explanation: To call a function in python language, it uses the function name followed by the parentheses.
28) Suppose a user wants to print the second value of an array, which has 5 elements. What will be the syntax
of the second value of the array?
1. array[2]
2. array[1]
3. array[-1]
4. array[-2]
Hide Answer Workspace
Answer: (b) array[1]
Explanation: The index of the array starts with 0. Therefore, the option (b) is the correct answer.
1. str1="python language"
2. str1.find("p")
3. print(str1)
What will be the output of this program?
Explanation: In this program, it will print the value of the str1. Therefore, the option (c) is the correct output of
this program.
1. flag = ""
2. a=0
3. i=1
4. while(a < 3):
5. j=1
6. if flag:
7. i=j*i+5
8. else:
9. i=j*i+1
10. a=a+1
11. print(i)
What will be the output of this program?
1. 12
2. 4
3. 11
4. 16
1. String type
2. Array lists
3. List of tuples
4. str lists
Explanation: The variable str has a list of tuples attached to it. Hence it is a list of tuples. So, option (c) is the
correct answer.
32) Which of the following statements is not valid regarding the variable in python?
Explanation: The variable_name can begin with alphabets or underscore but cannot begin with numbers. So,
option (c) is the correct answer.
1. a = 2
2. while(a > -100):
3. a=a-1
4. print(a)
How many times will this program run the loop?
1. Infinite
2. 102
3. 2
4. 1
Explanation: This loop will run the 1 to -100 (1, 0, -1,?, -100). So, option (b) is the correct answer.
1. arr = [3 , 2 , 5 , 6 , 0 , 7, 9]
2. add1 = 0
3. add2 = 0
4. for elem in arr:
5. if (elem % 1 == 0):
6. add1 = add1 + elem
7. continue
8. if (elem % 3 == 0):
9. add2 = add2 + elem
10. print(add1 , end=" ")
11. print(add2)
What will be the output of this program?
1. 32 0
2. 0 32
3. 18 0
4. 0 18
1. if f >= 12:
2. if (f >= 122)
3. if (f => 1222)
4. if f >= 12222
Explanation: The "if statement" always ends with a colon (:). So, option (a) is the correct statement.
36) Which of the following blocks allows you to test the code blocks for errors?
1. except block
2. try block
3. finally block
4. None of the these
Explanation: The try block allows you to test the code blocks for errors in the python language.
1. try:
2. print(file_name)
3. except:
4. print("error comes in the line")
What will be the output of this program?
1. file_name
2. error
3. error comes in the line
4. file_name error comes in the line
Explanation: The try block will generate an error because file_name is not defined in the program. Therefore,
the output of this program will be "error comes in the line".
1. i = 10
2. j=8
3. assert i > j, 'j = i + j'
4. print(j)
What will be the output of this program?
1. 18
2. 8
3. No output
4. TypeError
Explanation: In this program, the assert keyword has been used to mislead the user. Therefore, this program
will print the value of "j".
1. class Student:
2. print("Students of Section A")
3. Student()
4. Student()
5. obj = Student()
How many objects are there for the given program?
1. 1
2. 2
3. 3
4. None of the these
1. class Teacher:
2. def __init__(name, id_no, age):
3. name.id_no = id_no
4. name.age = age
5. teac = Teacher(5, 25)
Which of the following statements is incorrect regarding this program?
Explanation: All statements are correct. So, the option (d) is the correct answer.