0% found this document useful (0 votes)
10 views6 pages

Assignment 2 Pregrad

The document consists of a series of questions related to Python programming, covering topics such as variable syntax, function definitions, data types, and built-in functions. Each question includes multiple-choice answers, testing knowledge on Python code behavior and syntax. The questions are designed to assess understanding of fundamental Python concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Assignment 2 Pregrad

The document consists of a series of questions related to Python programming, covering topics such as variable syntax, function definitions, data types, and built-in functions. Each question includes multiple-choice answers, testing knowledge on Python code behavior and syntax. The questions are designed to assess understanding of fundamental Python concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 1

1.What is the output of the following code?

x = "Hello, World!"

print(x[2:6])

a) Hello

b) llo,

c) llo

d) ll

2.What is the correct syntax for a Python variable?

a) variableName = value

b) value = variableName

c) variableName: value

d) value : variableName

3.Which keyword is used to define a function in Python?

a) func

b) define

c) function

d) def

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

x=5

y = "John"

print(x + y)

a) 5John

b) John5

c) Error: cannot concatenate 'int' and 'str' objects

d) Error: invalid syntax

5.How do you start a comment in Python?

a) //
b) /*

c) #

d) --

6.What is the correct way to create a tuple in Python?

a) (1, 2, 3)

b) {1, 2, 3}

c) [1, 2, 3]

d) 1, 2, 3

7.What does the "len()" function do?

a) Returns the length of a string

b) Returns the last element of a list

c) Returns the smallest item in a list

d) Returns the largest item in a list

8. Which of the following is used to represent a block of code in Python?

a) { }

b) ( )

c) [ ]

d) Indentation

9.What does the "remove()" function do in Python?

a) Removes the last element from a list

b) Removes the first occurrence of a specified value from a list

c) Removes all occurrences of a specified value from a list

d) Removes all elements from a list

10. What is the output of the following code snippet?

print(10 > 9)

a) 10

b) 9

c) True

d) False

12.Which data type is mutable in Python?


a) Tuple

b) String

c) Integer

d) List

13.What method is used to add an element to the end of a list in Python?

a) append()

b) extend()

c) insert()

d) add()

14.What does the following code snippet do?

x = "Hello, World!"

print(x[2:5])

a) Prints "Hello"

b) Prints "llo"

c) Prints "llo,"

d) Prints "llo, "

15.Which operator is used for exponentiation in Python?

a) ^

b) **

c) %

d) //

16.What is the correct syntax for a Python function that takes no arguments?

a) def my_function():

b) def my_function(void):

c) def my_function(void):

d) def my_function(None):

17.What method is used to remove the last item from a list in Python?

a) pop()

b) remove()

c) clear()
d) del()

18. What will be the output of the following code snippet?

x = "Hello, World!"

print(x[-5:-2])

a) "Wor"

b) "orl"

c) "rld"

d) "Worl"

19.Which keyword is used to define a function in Python?

a) function

b) define

c) def

d) fun

20. What does the following code snippet do?

x = ["apple", "banana", "cherry"]

x[1] = "kiwi"

a) Adds "kiwi" to the list

b) Removes "banana" from the list

c) Replaces "banana" with "kiwi"

d) Throws an error

21.What function is used to get the length of a list in Python?

a) length()

b) len()

c) size()

d) count()

22.What is the output of the following code snippet?

x=5

y=2

print(x / y)

a) 2.5
b) 2

c) 2.0

d) 2.2

23.Which method is used to convert a string to lowercase in Python?

a) lower()

b) toLower()

c) lowercase()

d) caseLower()

24.What does the 'in' keyword do in Python?

a) Checks if an item exists in a list

b) Checks if a string contains another string

c) Checks if a key exists in a dictionary

d) All of the above

25.What is the output of the following code snippet?

x=5

y = "2"

print(x + int(y))

a) 7

b) 52

c) Error

d) None

26. Which method is used to join elements of a list into a string in Python?

a) join()

b) concatenate()

c) merge()

d) combine()

27. What is the correct way to check if two variables are pointing to the same object in memory in
Python?

a) x is y

b) x == y
c) x.equals(y)

d) x is not y

28. What is the output of the following code snippet?

x = [1, 2, 3]

y = x.copy()

y[0] = 4

print(x)

a) [1, 2, 3]

b) [4, 2, 3]

c) [1, 4, 3]

d) [4, 4, 4]

29.Which method is used to split a string into a list of substrings in Python?

a) split()

b) explode()

c) divide()

d) slice()

30. What is the output of the following code snippet?

x = 10

y=3

print(x // y)

a) 3.3333

b) 3.0

c) 1

d) 0

You might also like