Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Python 2

Uploaded by

Apka Dost
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Python 2

Uploaded by

Apka Dost
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Why does the name of local variables start with an underscore discouraged?

1. To identify the variable


2. It confuses the interpreter
3. It indicates a private variable of a class
4. None of these

(c) It indicates a private variable of a class

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.

Which of the following is not a keyword in Python language?

1. val
2. raise
3. try
4. with

(a) val

Explanation: "val" is not a keyword in python language.

Which of the following statements is correct for variable names in Python language?

1. All variable names must begin with an underscore.


2. Unlimited length
3. The variable name length is a maximum of 2.
4. All of the above

(b) Unlimited length

Explanation: None

Which of the following declarations is incorrect in python language?

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

Explanation: Spaces are not allowed in variable names.

Which of the following words cannot be a variable in python language?

1. _val
2. val
3. try
4. _try_

(c) try

Explanation: "try" is a keyword.

Which of the following precedence order is correct in Python?

1. Parentheses, Exponential, Multiplication, Division, Addition, Subtraction


2. Multiplication, Division, Addition, Subtraction, Parentheses, Exponential
3. Division, Multiplication, Addition, Subtraction, Parentheses, Exponential
4. Exponential, Parentheses, Multiplication, Division, Addition, Subtraction

(a) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

Explanation: PEMDAS (similar to BODMAS).

Which one of the following has the same precedence level?

1. Division, Power, Multiplication, Addition and Subtraction


2. Division and Multiplication
3. Subtraction and Division
4. Power and Division

(b) Division and Multiplication

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.

Which of the following is correctly evaluated for this function?

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

Study the following function:

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

What will be the output of this code?

1. error
2. 21
3. 031
4. None of these

(a) error

Explanation: Syntax error, there should not be a space between + and =.

Which one of the following syntaxes is the correct syntax to read from a simple text file stored in ''d:\java.txt''?

1. Infile = open(''d:\\java.txt'', ''r'')


2. Infile = open(file=''d:\\\java.txt'', ''r'')
3. Infile = open(''d:\java.txt'',''r'')
4. Infile = open.file(''d:\\java.txt'',''r'')

(a) Infile = open(''c:\\scores.txt'', ''r'')

Explanation: None

Study the following code:

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']

Study the following function:

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

Explanation: This function prints the square of the value.

Study the following function:

1. any([5>8, 6>3, 3>1])


What will be the output of this code?

1. False
2. Ture
3. Invalid code
4. None of these

(b) True

Study the following statement:

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.

Study the following statements:

1. >>> str1 = "javat"


2. >>> str2 = ":"
3. >>> str3 = "point"
4. >>> str1[-1:]
What will be the output of this statement?

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.

Study the following code:

1. >>> print (r"\njavat\npoint")


What will be the output of this statement?

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.

Study the following statements:

1. >>> print(ord('h') - ord('z'))


What will be the output of this statement?

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.

Study the following program:

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

Study the following program:

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

(d) Invalid syntax

Explanation: Invalid syntax, because this declaration (i = 1:) is wrong.

Study the following program:

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

Study the following program:

1. i = 0
2. while i < 5:
3. print(i)
4. i += 1
5. if i == 3:
6. break
7. else:
8. print(0)

What will be the output of this statement?

1. 1 2 3
2. 0 1 2 3
3. 0 1 2
4. 3 2 1

Study the following program:

1. i = 0
2. while i < 3:
3. print(i)
4. i += 1
5. else:
6. print(0)

What will be the output of this statement?

1. 01
2. 012
3. 0120
4. 0123

Study the following program:

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

Explanation: "j" is not in "xyz".


Study the following program:

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

Study the following program:

1. d = {0: 'a', 1: 'b', 2: 'c'}


2. for i in d:
3. print(i)
What will be the output of this statement?

1. abc
2. 012
3. 0a 1b 2c
4. None of these above

Study the following program:

1. d = {0, 1, 2}
2. for x in d:
3. print(x)
What will be the output of this statement?

1. {0, 1, 2} {0, 1, 2} {0, 1, 2}


2. 012
3. Syntax_Error
4. None of these above

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

Explanation: Mango is not defined hence the name error.

Study the following program:

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

Study the following program:

1. print(print(print("javatpoint")))
What will be the output of this program?

1. javatpoint None None


2. None None javatpoint
3. None javatpoint None
4. Javatpoint

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:

1. print(True ** False / True)


What will be the output of this program?

1. True ** False / True


2. 1.0
3. 1 ** 0 / 1
4. None of the these

Answer: (b) 1.0

Explanation: Binary values

True = 1

False = 0

(1 ** 0 / 1) = (10/ 1) = 1.0

Therefore, option (b) is the correct output of this program.

Study the following program:

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)

So, the output of this program is 4.


Study the following program:

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)

So, the output of this program is the 0.


Study the following program:

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

Explanation: Precedence table in python

High *, /, // and%

Low + and -

If the operator precedence is same, the calculation starts from left to right.

Therefore, option (d) is the correct output of this program.

Study the following program:

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

(c) (2, 10, 3, 5)

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.

Study the following program:


1. print(int(6 == 6.0) * 3 + 4 % 5)
What will be the 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.

Study the following 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".

How many control statements python supports?

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. print("java", 'point', sep='2')


What will be the output of this 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.

Study the following program:

1. _ = '1 2 3 4 5 6'
2. print(_)

What will be the output of this program?

1. SyntaxError: EOL while scanning string literal


2. SyntaxError: invalid syntax
3. NameError: name '_' is not defined
4. 123456

(d) 1 2 3 4 5 6

Explanation: "_" is a valid variable name. Therefore, option (d) is the correct output of this program.

Which of the following keywords is not reversed keyword in python?

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:

o "a * 2" means, string prints 2 times.


o "a * 0" means, string is empty.
o Any string cannot be negative. Therefore, the string will not print any word.

Study the following program:

1. print(max("zoo 145 com"))


What will be the output of this program?

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

Answer: (d) NameError

Explanation: This program will print the NameError because 'x' is not defined in this code.

21) PVM is often called _________.

1. Python interpreter
2. Python compiler
3. Python volatile machine
4. Portable virtual machine

Answer: (a) Python interpreter

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.

22) Study the following program:

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.

23) Study the following program:

1. i=(12, 20, 1, 0, 25)


2. i.sort()
3. print(i)
What will be the output of this program?

1. 0 1 12 20 25
2. 1 12 20 25
3. FunctionError
4. AttributeError

Answer: (d) 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

Hide Answer Workspace


Answer: (a) def

Explanation: In the python language, the def keyword is used to define the function.

Syntax of the function declaration

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

Hide Answer Workspace


Answer: (a) Function name and Parameters

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

Hide Answer Workspace


Answer: (c) None

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

Hide Answer Workspace


Answer: (c) function_name()

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.

29) Study the following program:

1. str1="python language"
2. str1.find("p")
3. print(str1)
What will be the output of this program?

1. Print the index value of the p.


2. p
3. python language
4. AttributeError

Hide Answer Workspace


Answer: (c) python language

Explanation: In this program, it will print the value of the str1. Therefore, the option (c) is the correct output of
this program.

30) Study the following 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

Hide Answer Workspace


Answer: (b) 4

Explanation: The output of this program is 4.

31) Study the following expression:


1. str = [(1, 1), (2, 2), (3, 3)]
What type of data is in this expression?

1. String type
2. Array lists
3. List of tuples
4. str lists

Hide Answer Workspace


Answer: (c) List of tuples

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?

1. The variable_name can begin with alphabets


2. The variable_name can begin with an underscore
3. The variable_name can begin with a number
4. None of the these

Hide Answer Workspace


Answer: (c) The variable_name can begin with a number

Explanation: The variable_name can begin with alphabets or underscore but cannot begin with numbers. So,
option (c) is the correct answer.

33) Study the following program:

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

Hide Answer Workspace


Answer: (b) 102

Explanation: This loop will run the 1 to -100 (1, 0, -1,?, -100). So, option (b) is the correct answer.

34) Study the following program:

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

Hide Answer Workspace


Answer: (a) 32 0

Explanation: The output of this program is (32, 0).

35) Which of the following statements is valid for "if statement"?

1. if f >= 12:
2. if (f >= 122)
3. if (f => 1222)
4. if f >= 12222

Hide Answer Workspace


Answer: (a) if f >= 12:

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

Hide Answer Workspace


Answer: (b) try block

Explanation: The try block allows you to test the code blocks for errors in the python language.

37) Study the following program:

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

Hide Answer Workspace


Answer: (c) 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".

38) Study the following program:

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

Hide Answer Workspace


Answer: (b) 8

Explanation: In this program, the assert keyword has been used to mislead the user. Therefore, this program
will print the value of "j".

39) Study the following program:

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

Hide Answer Workspace


Answer: (c) 3
Explanation: There will be three objects created in this program. Therefore, the option (c) is the correct
answer.

40) Study the following program:

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?

1. A constructor has been given in this program


2. id_no and age are called the parameters
3. The "teac" is the reference variable for the object Teacher(5, 25)
4. None of the these

Hide Answer Workspace


Answer: (d) None of the these

Explanation: All statements are correct. So, the option (d) is the correct answer.

You might also like