OA Exams

  • web.groovymark@gmail.com
  • December 8, 2024

Question 21

What does this Python code return?

print(“Python”.find(“y”))

a) 0
b) 1
c) 2
d) -1

Correct Answer: c) 2

Explanation: The find() method returns the index of the first occurrence of the specified substring. "y" occurs at index 2 in "Python".

Question 22

What will be the output of this Python code?

x = “banana”

y = “apple”

print(x > y)

a) True
b) False
c) Error
d) None

Correct Answer: a) True

Explanation: String comparison is done lexicographically based on Unicode values. "banana" comes after "apple", so the result is True.

Question 23

What does this Python code return?

print([1, 2, 3, 4][:2])

a) [1, 2]
b) [2, 3]
c) [3, 4]
d) Error

Correct Answer: a) [1, 2]

Explanation: The slice [:2] returns elements from the start of the list up to but not including index 2, which results in [1, 2].

Question 24

What will this Python code output?

x = [1, 2, 3]

x.insert(1, “a”)

print(x)

a) [1, 2, 3]
b) [1, “a”, 2, 3]
c) [1, “a”, 3]
d) Error

Correct Answer: b) [1, "a", 2, 3]

Explanation: The insert() method inserts an element at a specified index. "a" is inserted at index 1.

Question 25

What does this Python code return?

x = [1, 2, 3]

x.clear()

print(x)

a) []
b) [1, 2, 3]
c) [None]
d) Error

Correct Answer: a) []

Explanation: The clear() method removes all elements from the list, leaving it empty.

Question 26

What is the result of this Python code?

x = [“apple”, “banana”]

x.extend([“cherry”, “date”])

print(x)

a) [“apple”, “banana”, “cherry”, “date”]
b) [“apple”, “banana”]
c) [“cherry”, “date”]
d) Error

Correct Answer: a) ["apple", "banana", "cherry", "date"]

Explanation: The extend() method adds all elements from another list to the end of the current list.

Question 27

What will this Python code output?

x = 10

if x > 5:

    print(“Greater”)

else:

    print(“Smaller”)

a) Greater
b) Smaller
c) Error
d) None

Correct Answer: a) Greater

Explanation: Since x is greater than 5, the condition evaluates to True, and the program prints "Greater".

Question 28

What does this Python code return?

print(min([3, 1, 2]))

a) 1
b) 2
c) 3
d) Error

Correct Answer: a) 1

Explanation: The min() function returns the smallest element in the list. In this case, the smallest number is 1.

Question 29

What will this Python code output?

x = [10, 20, 30, 40]

print(x[1:3])

a) [20, 30]
b) [10, 20]
c) [30, 40]
d) Error

Correct Answer: a) [20, 30]

Explanation: The slice [1:3] returns elements from index 1 to 3, but not including 3.

Question 30

What does this Python code output?

print(5 + 5)

a) 5
b) 10
c) 15
d) Error

Correct Answer: b) 10

Explanation: The + operator performs addition. 5 + 5 equals 10.

Question 31

What does this Python code return?

print(type([]))

a) <class ‘list’>
b) <class ‘tuple’>
c) <class ‘set’>
d) Error

Correct Answer: a) <class 'list'>

Explanation: An empty list is denoted by [], and its type is <class 'list'>.

Question 32

What is the result of this Python code?

print([1, 2] + [3, 4])

a) [1, 2, 3, 4]
b) [3, 4]
c) [1, 2]
d) Error

Correct Answer: a) [1, 2, 3, 4]

Explanation: The + operator concatenates two lists. [1, 2] + [3, 4] results in [1, 2, 3, 4].

Question 33

What does this Python code output?

x = (1, 2, 3)

print(len(x))

a) 1
b) 2
c) 3
d) Error

Correct Answer: c) 3

Explanation: The len() function returns the number of elements in a tuple. The tuple (1, 2, 3) has three elements.

Question 34

What will this Python code return?

x = set([1, 2, 2, 3])

print(x)

a) {1, 2, 2, 3}
b) {1, 2, 3}
c) [1, 2, 3]
d) Error

Correct Answer: b) {1, 2, 3}

Explanation: A set contains only unique elements. Duplicates are removed automatically.

Question 35

What does this Python code return?

print(“Python”.endswith(“n”))

a) True
b) False
c) Error
d) None

Correct Answer: a) True

Explanation: The endswith() method checks whether a string ends with the specified suffix. "Python".endswith("n") returns True.

Question 36

What will be the output of this Python code?

x = [1, 2, 3]

y = [1, 2, 3]

print(x is y)

a) True
b) False
c) Error
d) None

Correct Answer: b) False

Explanation: The is operator checks whether two variables point to the same object in memory. In this case, x and y refer to different list objects, so x is y returns False.

Question 37

What does this Python code return?

x = (1, 2, 3)

y = (1, 2, 3)

print(x == y)

a) True
b) False
c) Error
d) None

Correct Answer: a) True

Explanation: The == operator checks whether two variables have the same value. Since both tuples have the same values, the result is True.

Question 38

What will this Python code output?

x = {“a”: 1, “b”: 2}

print(x[“a”])

a) 1
b) 2
c) “a”
d) Error

Correct Answer: a) 1

Explanation: The value associated with the key "a" in the dictionary is 1.

Question 39

What does this Python code return?

print(len({1, 2, 3}))

a) 1
b) 2
c) 3
d) 4

Correct Answer: c) 3

Explanation: The len() function returns the number of elements in the set {1, 2, 3}, which is 3.

Question 40

What will this Python code output?

x = [1, 2, 3]

print(x[-1])

a) 1
b) 2
c) 3
d) Error

Correct Answer: c) 3

Explanation: Negative indexing in Python allows you to access elements from the end of a list. x[-1] refers to the last element, which is 3.

Complete the Captcha to view next question set.

Tags

Prev Post
WGU D522 Practice Exam Questions – Set 3 – Part 1
Next Post
WGU D522 Practice Exam Questions – Set 3 – Part 3