0% found this document useful (0 votes)
4 views

1- Python basics

The document contains a series of questions and answers related to Python programming concepts. It covers topics such as comments, functions, data structures, and operators, providing correct answers and explanations for each question. The format is a quiz style, aimed at testing knowledge of Python syntax and functionality.

Uploaded by

eyob53834
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)
4 views

1- Python basics

The document contains a series of questions and answers related to Python programming concepts. It covers topics such as comments, functions, data structures, and operators, providing correct answers and explanations for each question. The format is a quiz style, aimed at testing knowledge of Python syntax and functionality.

Uploaded by

eyob53834
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/ 9

.‫ ال تنسوني من صالح دعائكم وبالتوفيق‬.

‫األسئلة واألجوبة من شات جيبيتي لذلك قد توجد أخطاء‬

1. Which symbol is used for single-line comments in Python?


a) //
b) <!--
c) #
d) **

2. How do you define a function in Python?


a) func
b) define
c) def
d) function

3. What will this code output?


print(5 == 5 and 3 > 2)

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

4. What is the output of the following code?


name = "HelloWorld"

print(name[:5])

a) World
b) Hello
c) elloW
d) H

5. What will this code print?


for i in range(2, 6):

print(i)

a) 2 3 4 5
b) 2 3 4
c) 3 4 5
d) 2 3 4 5 6
6. Which function gives the length of a list?
a) size()
b) length()
c) len()
d) count()

7. What will 5 // 2 return?


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

8. How do you create an empty dictionary?


a) dict = {}
b) dict = []
c) dict = ()
d) dict = None

9. What is the output of the following?x = "AI"


print(x * 3)

a) AI3
b) AI AI AI
c) AIAIAI
d) Error

10. What will bool(0) return?


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

11. What does continue do in a loop?


a) Breaks the loop
b) Restarts the loop
c) Skips the current iteration
d) Ends the program
12. What is the correct syntax to define a tuple?
a) tuple = {1, 2}
b) tuple = [1, 2]
c) tuple = (1, 2)
d) tuple = None

13. What error will print(1 / 0) raise?


a) ValueError
b) ZeroDivisionError
c) TypeError
d) NameError

14. Which Python data structure is mutable?


a) tuple
b) list
c) str
d) int

15. How do you check the type of a variable x?


a) type(x)
b) check(x)
c) var(x)
d) typeof(x)

16. What does the pop() method do in a list?


a) Removes a random element
b) Removes the last element
c) Clears the list
d) Adds an element

17. What will this code output?


def greet():

print("Hello")

greet()

a) Hello
b) None
c) greet
d) Error
18. How do you concatenate two strings in Python?
a) +
b) concat()
c) append()
d) join()

19. Which keyword exits a loop?


a) exit
b) stop
c) break
d) end

20. What will 5 % 2 return?


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

21. How do you create an infinite loop in Python?


a) for i in infinite:
b) while True:
c) for i in range(0, inf):
d) while False:

22. Which function is used to read user input?


a) input()
b) prompt()
c) read()
d) ask()

23. How do you convert a string "123" to an integer?


a) str(123)
b) int("123")
c) float("123")
d) to_int("123")

24. What will max([3, 1, 2]) return?


a) 3
b) 1
c) 2
d) Error
25. Which of the following is a valid list?
a) list = {1, 2, 3}
b) list = [1, 2, 3]
c) list = (1, 2, 3)
d) list = None

26. What will print(3 * 2 ** 2) output?


a) 36
b) 12
c) 9
d) 3

27. Which operator is used for comparison?


a) =
b) ==
c) :=
d) &&

28. What will the code len("Python") return?


a) 6
b) 5
c) 7
d) None

29. Which keyword is used to raise exceptions?


a) raise
b) throw
c) except
d) try

30. What will list(range(3)) return?


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

31. What does the strip() method do to a string?


a) Removes all spaces
b) Removes whitespace at the ends
c) Converts to lowercase
d) Splits the string
32. Which of these is a Python keyword?
a) value
b) class
c) main
d) string

33. How do you comment multiple lines in Python?


a) #
b) """
c) //
d) --

34. What will True + False return?


a) 1
b) 0
c) True
d) Error

35. What is the result of not True?


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

36. What is the purpose of pass in Python?


a) Terminates the program
b) Placeholder with no action
c) Starts a new block
d) Raises an exception

37. What will print("A" > "B") return?


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

38. What does set([1, 1, 2, 3]) return?


a) {1, 2, 3}
b) [1, 2, 3]
c) (1, 2, 3)
d) Error
39. Which method adds elements to a list?
a) append()
b) add()
c) insert()
d) push()

40. What will type(True) return?


a) <class 'bool'>
b) <class 'int'>
c) <class 'str'>
d) None
Q# | Correct Answer | Answer Explanation
1 c) # The # symbol is used for single-line comments in Python.
2 c) def def is the keyword used to define a function in Python.
3 a) True 5 == 5 and 3 > 2 are both True. and ensures both conditions are true.
4 b) Hello [:5] slices the string from the start to index 4 (not including index 5).
5 a) 2 3 4 5 range(2, 6) generates numbers starting from 2 up to (but not including) 6.
6 c) len() len() returns the number of elements in a list, string, or iterable.
7 b) 2 // is floor division, which returns the integer part of the division.
8 a) dict = {} {} creates an empty dictionary in Python.
9 c) AIAIAI Multiplying a string ("AI" * 3) repeats it 3 times.
10 b) False bool(0) evaluates to False. Non-zero values are True.
11 c) Skips the current iteration continue skips the current loop iteration and continues.
12 c) tuple = (1, 2) Parentheses () define a tuple in Python.
13 b) ZeroDivisionError Dividing by zero raises a ZeroDivisionError.
14 b) list Lists are mutable, allowing changes to their elements.
15 a) type(x) The type() function returns the data type of a variable.
16 b) Removes the last element pop() removes the last element from a list.
17 a) Hello The function greet() prints "Hello".
18 a) + The + operator concatenates two strings.
19 c) break The break keyword exits the loop immediately.
20 c) 1 5 % 2 is the remainder of 5 divided by 2.
21 b) while True: while True: creates an infinite loop.
22 a) input() The input() function reads user input.
23 b) int("123") int() converts a string containing digits to an integer.
24 a) 3 max() returns the largest element in the list [3, 1, 2].
25 b) list = [1, 2, 3] Square brackets [] define a list.
26 b) 12 2 ** 2 is 4, and 3 * 4 equals 12.
27 b) == == is the comparison operator for equality in Python.
28 a) 6 len("Python") returns the number of characters, which is 6.
29 a) raise The raise keyword is used to raise exceptions manually.
30 b) [0, 1, 2] range(3) generates values from 0 to 2 (exclusive of 3).
b) Removes whitespace at the The strip() method removes whitespace at string
31
ends ends.
32 b) class class is a Python keyword used to define a class.
33 b) """ Triple quotes """ are used to comment multiple lines.
34 a) 1 True is treated as 1, and False as 0 in arithmetic operations.
35 a) False not True negates the value True, resulting in False.
36 b) Placeholder with no action pass acts as a no-operation placeholder.
37 b) False In lexicographical order, "A" is less than "B".
38 a) {1, 2, 3} The set() function removes duplicates and creates a set.
39 a) append() The append() method adds an element to the end of a list.
40 a) <class 'bool'> type(True) returns the <class 'bool'>.

You might also like