103
57.3k
Chapter:

210+ more mcqs Solved MCQs

in Problem Solving and Python Programming

These multiple-choice questions (MCQs) are designed to enhance your knowledge and understanding in the following areas: Computer Science Engineering (CSE) , Information Technology Engineering (IT) , Electrical Engineering , Civil Engineering , Mechanical Engineering .

Chapters

More MCQs
1.

Which of the following expressions can be used to multiply a given number ‘a’ by 4?

A. a<<2
B. a<<4
C. a>>2
D. a>>4
Answer» A. a<<2
Explanation: let us consider an example wherein a=2. the binary form of 2 is 0010. when we left shift this value by 2, we get 1000, the value of which is 8. hence if we want to multiply a given number ‘a’ by 4, we can use the expression: a<<2.
2.

R operator. Hence the output of the code shown above is: 20 10.

A. 1011011
B. 11010100
C. 11101011
D. 10110011
Answer» B. 11010100
Explanation: the binary form of -44 is 00101100. the one’s complement of this value is 11010011. on adding one to this we get: 11010100 (two’s complement).
3.

What will be the output of the following
Python code snippet?
not(10<20) and not(10>30)

A. true
B. false
C. error
D. no output
Answer» B. false
Explanation: the expression not(10<20) returns false. the expression not(10>30) returns true. the and operation between false and true returns false. hence the output is false.
4.

What will be the output of the following
Python code?
for i in range(0):
print(i)

A. 0
B. no output
C. error
D. none of the mentioned
Answer» B. no output
Explanation: range(0) is empty.
5.

What will be the output of the following
Python code?
for i in range(2.0):
print(i)

A. 0.0 1.0
B. 0 1
C. error
D. none of the mentioned
Answer» C. error
Explanation: object of type float cannot be interpreted as an integer.
6.

What will be the output of the following
Python code?
for i in range(int(2.0)):
print(i)

A. 0.0 1.0
B. 0 1
C. error
D. none of the mentioned
Answer» B. 0 1
Explanation: range(int(2.0)) is the same as range(2).
7.

What will be the output of the following
Python code snippet?
a = [0, 1, 2, 3]
for a[0] in a:
print(a[0])

A. 0 1 2 3
B. 0 1 2 2
C. 3 3 3 3
D. error
Answer» A. 0 1 2 3
Explanation: the value of a[0] changes in each iteration. since the first value that it takes is itself, there is no visible error in the current example.
8.

What will be the output of the following
Python statement?
>>>"a"+"bc"

A. a
B. bc
C. bca
D. abc
Answer» D. abc
Explanation: + operator is concatenation operator.
9.

What will be the output of the following
Python statement?
>>>"abcd"[2:]

A. a
B. ab
C. cd
D. dc
Answer» C. cd
Explanation: slice operation is performed on string.
10.

What will be the output of the following
Python code?
>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]

A. olleh
B. hello
C. h
D. o
Answer» D. o
Explanation: -1 corresponds to the last index.
11.

What arithmetic operators cannot be used with strings?

A. +
B. *
C.
D. all of the mentioned
Answer» C. –
Explanation: + is used to concatenate and * is used to multiply strings.
12.

What will be the output of the following Python code? >>>print (r"\nhello")

A. a new line and hello
B. \\nhello
C. the letter r and then hello
D. error
Answer» B. \\nhello
Explanation: when prefixed with the letter ‘r’ or ‘r’ a string literal becomes a raw string and the escape sequences such as \n are not converted.
13.

What will be the output of the following Python code? >>>print('new' 'line')

A. error
B. output equivalent to print ‘new\\nline’
C. newline
D. new line
Answer» C. newline
Explanation: string literal separated by whitespace are allowed. they are concatenated.
14.

What will be the output of the following
Python code?
1. >>>str1="helloworld"
2. >>>str1[::-1]

A. dlrowolleh
B. hello
C. world
D. helloworld
Answer» A. dlrowolleh
Explanation: execute in shell to verify.
15.

What will be the output of the following
Python code?
print(0xA + 0xB + 0xC)

A. 0xa0xb0xc
B. error
C. 0x22
D. 33
Answer» D. 33
Explanation: 0xa and 0xb and 0xc are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. there sum is 33.
16.

What will be the output of the following
Python code?
1. class father:
2. def __init__(self, param):
3. self.o1 = param
4.
5. class child(father):
6. def __init__(self, param):
7. self.o2 = param
8.
9. >>>obj = child(22)
10. >>>print "%d %d" % (obj.o1, obj.o2)

A. none none
B. none 22
C. 22 none
D. error is generated
Answer» D. error is generated
Explanation: self.o1 was never created.
17.

What will be the output of the following
Python code?
1. class tester:
2. def __init__(self, id):
3. self.id = str(id)
4. id="224"
5.
6. >>>temp = tester(12)
7. >>>print(temp.id)

A. 224
B. error
C. 12
D. none
Answer» C. 12
Explanation: id in this case will be the attribute of the class.
18.

3. What will be the output of the following
Python code?
1. >>>example = "snow world"
2. >>>print("%s" % example[4:7])

A. wo
B. world
C. sn
D. rl
Answer» A. wo
Explanation: execute in the shell and verify.
19.

What will be the output of the following
Python code?
1. >>>example = "snow world"
2. >>>example[3] = 's'
3. >>>print example

A. snow
B. snow world
C. error
D. snos world
Answer» C. error
Explanation: strings cannot be modified.
20.

>>>max("what are you")

A. error
B. u
C. t
D. y
Answer» D. y
Explanation: max returns the character with the highest ascii value.
21.

Given a string example=”hello” what is the output of example.count(‘l’)?

A. 2
B. 1
C. none
D. 0
Answer» A. 2
Explanation: l occurs twice in hello.
22.

>>>example.find("e")

A. error
B. -1
C. 1
D. 0
Answer» C. 1
Explanation: returns lowest index.
23.

>>>example.rfind("e")

A. -1
B. 4
C. 3
D. 1
Answer» B. 4
Explanation: returns highest index.
24.

>>>example[::-1].startswith("d")

A. dlrowolleh
B. true
C. -1
D. none
Answer» B. true
Explanation: starts with checks if the given string starts with the parameter that is passed.
25.

To concatenate two strings to a third what statements are applicable?

A. s3 = s1 . s2
B. s3 = s1.add(s2)
C. s3 = s1.    add    (s2)
D. s3 = s1 * s2
Answer» C. s3 = s1.    add    (s2)
Explanation:     add     is another method that can be used for concatenation.
26.

>>>chr(ord('A'))

A. a
B. b
C. a
D. error
Answer» A. a
Explanation: execute in shell to verify.
27.

>>>print(chr(ord('b')+1))

A. a
B. b
C. c
D. a
Answer» C. c
Explanation: execute in the shell to verify.
28.

Which of the following statement prints hello\example\test.txt?

A. print(“hello\\example\\test.txt”)
B. print(“hello\\example\\test.txt”)
C. print(“hello\\”example\\”test.txt”)
D. print(“hello”\\example”\\test.txt”)
Answer» B. print(“hello\\example\\test.txt”)
Explanation: \is used to indicate that the next
29.

Suppose s is “\t\tWorld\n”, what is s.strip()?

A. \\t\\tworld\\n
B. \\t\\tworld\\n
C. \\t\\tworld\\n
D. world
Answer» D. world
Explanation: execute help(string.strip) to find details.
30.

The format function, when applied on a string returns                        

A. error
B. int
C. bool
D. str
Answer» D. str
Explanation: format function returns a string.
31.

+2+3?

A. hello123
B. hello
C. error
D. hello6
Answer» C. error
Explanation: cannot concatenate str and int objects.
32.

>>>print("A", end = ' ')

A. dcba
B. a, b, c, d
C. d c b a
D. d, c, b, a will be displayed on four lines
Answer» C. d c b a
Explanation: execute in the shell.
33.

What will be displayed by print(ord(‘b’) – ord(‘a’))?

A. 0
B. 1
C. -1
D. 2
Answer» B. 1
Explanation: ascii value of b is one more than a. hence the output of this code is 98-97, which is equal to 1.
34.

Say s=”hello” what will be the return value of type(s)?

A. int
B. bool
C. str
D. string
Answer» C. str
Explanation: str is used to represent strings in python.
35.

What is “Hello”.replace(“l”, “e”)?

A. heeeo
B. heelo
C. heleo
D. none
Answer» A. heeeo
Explanation: execute in shell to verify.
36.

To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed)?

A. s[]
B. s.getitem(3)
C. s.    getitem    (3)
D. s.getitem(3)
Answer» C. s.    getitem    (3)
Explanation:     getitem(..) can be used to get character at index specified as parameter.
37.

To return the length of string s what command do we execute?

A. s.    len    ()
B. len(s)
C. size(s)
D. s.size()
Answer» A. s.    len    ()
Explanation: execute in shell to verify.
38.

If a class defines the     str    (self) method, for an object obj for the class, you can use which command to invoke the     str      method.

A. obj.    str    ()
B. str(obj)
C. print obj
D. all of the mentioned
Answer» D. all of the mentioned
Explanation: execute in shell to verify.
39.

To check whether string s1 contains another string s2, use                  

A. s1.    contains    (s2)
B. s2 in s1
C. s1.contains(s2)
D. si.in(s2)
Answer» A. s1.    contains    (s2)
Explanation: s2 in s1 works in the same way as calling the special function     contains     .
40.

Suppose i is 5 and j is 4, i + j is same as

A. i.    add(j)
B. i.    add    (j)
C. i.    add(j)
D. i.    add(j)
Answer» B. i.    add    (j)
Explanation: execute in shell to verify.
41.

What function do you use to read a string?

A. input(“enter a string”)
B. eval(input(“enter a string”))
C. enter(“enter a string”)
D. eval(enter(“enter a string”))
Answer» A. input(“enter a string”)
Explanation: execute in shell to verify.
42.

What is the default value of encoding in encode()?

A. ascii
B. qwerty
C. utf-8
D. utf-16
Answer» C. utf-8
Explanation: the default value of encoding is utf-8.
43.

Which of the following functions is a built- in function in python?

A. seed()
B. sqrt()
C. factorial()
D. print()
Answer» D. print()
Explanation: the function seed is a function which is present in the random module. the functions sqrt and factorial are a part of the math module. the print function is a built-in function which prints a value directly to the system output.
44.

What is the output of the function complex()?

A. 0j
B. 0+0j
C. 0
D. error
Answer» A. 0j
Explanation: the complex function returns 0j if both of the arguments are omitted, that is, if the function is in the form of complex() or complex(0), then the output will be 0j.
45.

The function divmod(a,b), where both ‘a’ and ‘b’ are integers is evaluated as:

A. (a%b, a//b)
B. (a//b, a%b)
C. (a//b, a*b)
D. (a/b, a%b)
Answer» B. (a//b, a%b)
Explanation: the function divmod(a,b) is evaluated as a//b, a%b, if both ‘a’ and ‘b’ are integers.
46.

The function complex(‘2-3j’) is valid but the function complex(‘2 – 3j’) is invalid.

A. true
B. false
Answer» A. true
Explanation: when converting from a string, the string must not contain any blank spaces around the + or – operator. hence the function complex(‘2 – 3j’) will result in an error.
47.

Which of the following functions does not necessarily accept only iterables as arguments?

A. enumerate()
B. all()
C. chr()
D. max()
Answer» C. chr()
Explanation: the functions enumerate(), all() and max() accept iterables as arguments whereas the function chr() throws an error on receiving an iterable as an argument. also note that the function chr() accepts only integer values.
48.

Which of the following functions accepts only integers as arguments?

A. ord()
B. min()
C. chr()
D. any()
Answer» C. chr()
Explanation: the function chr() accepts only integers as arguments. the function ord() accepts only strings. the functions min() and max() can accept floating point as well as integer arguments.
49.

Suppose there is a list such that: l=[2,3,4]. If we want to print this list in reverse order, which of the following methods should be used?

A. reverse(l)
B. list(reverse[(l)])
C. reversed(l)
D. list(reversed(l))
Answer» D. list(reversed(l))
Explanation: the built-in function reversed() can be used to reverse the elements of a list. this function accepts only an iterable as an argument. to print the output in the form of a list, we use: list(reversed(l)). the output will be: [4,3,2].
50.

Which of the following functions will not result in an error when no arguments are passed to it?

A. min()
B. divmod()
C. all()
D. float()
Answer» D. float()
Explanation: the built-in functions min(), max(), divmod(), ord(), any(), all() etc throw an error when no arguments are passed to them. however there are some built-in functions like float(), complex() etc which do not throw an error when no arguments are passed to them. the output of float() is 0.0.

Done Studing? Take A Test.

Great job completing your study session! Now it's time to put your knowledge to the test. Challenge yourself, see how much you've learned, and identify areas for improvement. Don’t worry, this is all part of the journey to mastery. Ready for the next step? Take a quiz to solidify what you've just studied.