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

Complete_Python_Questions_with_Answers

The document contains a set of 50 questions and answers related to Python and R programming languages. It covers various topics such as data types, functions, control structures, and file handling in both languages. Each question is followed by multiple-choice options and the correct answer.

Uploaded by

shubhiyadav1105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Complete_Python_Questions_with_Answers

The document contains a set of 50 questions and answers related to Python and R programming languages. It covers various topics such as data types, functions, control structures, and file handling in both languages. Each question is followed by multiple-choice options and the correct answer.

Uploaded by

shubhiyadav1105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

### Python (50 Questions with Answers)

1. What does the len() function do in Python?


a) Deletes an object
b) Returns the number of elements in an object
c) Returns the type of an object
d) None of the above
Answer: b) Returns the number of elements in an object

2. What is the output of print(type([1, 2, 3]))?


a) <class 'list'>
b) <class 'tuple'>
c) <class 'set'>
d) <class 'dict'>
Answer: a) <class 'list'>

3. What is the result of `3 ** 2` in Python?


a) 6
b) 8
c) 9
d) 12
Answer: c) 9

4. Which of the following is not a keyword in Python?


a) def
b) class
c) for
d) function
Answer: d) function

5. What does the `is` keyword check in Python?


a) Value equality
b) Reference equality
c) Both value and reference equality
d) None of the above
Answer: b) Reference equality

6. Which method is used to add an item to a list?


a) append()
b) add()
c) insert()
d) extend()
Answer: a) append()

7. What is the output of `bool([])` in Python?


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

8. What is the file extension for Python scripts?


a) .pyn
b) .py
c) .pt
d) .python
Answer: b) .py
9. Which module in Python supports regular expressions?
a) regex
b) re
c) reg
d) expression
Answer: b) re

10. Which of the following is mutable in Python?


a) List
b) Tuple
c) String
d) Integer
Answer: a) List

11. What will be the output of `print(5 // 2)`?


a) 2.5
b) 2
c) 3
d) 2.0
Answer: b) 2

12. Which statement is used to handle exceptions in Python?


a) exception
b) catch
c) try-except
d) throw
Answer: c) try-except

13. Which method is used to convert a string to lowercase?


a) lower()
b) down()
c) casefold()
d) tolower()
Answer: a) lower()

14. What is the output of `print('Hello'[-1])`?


a) H
b) o
c) e
d) l
Answer: b) o

15. What does `type(5 / 2)` return in Python?


a) int
b) float
c) str
d) None
Answer: b) float

16. What is the correct syntax to define a function in Python?


a) def function_name:
b) function function_name():
c) def function_name():
d) def function_name{}
Answer: c) def function_name():

17. What will be the output of `print("a" * 3)`?


a) aaa
b) a a a
c) Error
d) None
Answer: a) aaa

18. Which of the following is a Python tuple?


a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) None of the above
Answer: b) (1, 2, 3)

19. What is the output of `print(10 % 3)`?


a) 1
b) 2
c) 3
d) 0
Answer: a) 1

20. Which function is used to sort a list in Python?


a) sort()
b) sorted()
c) order()
d) arrange()
Answer: b) sorted()

... (Adding the remaining 30 questions for full completion)


What is the output of print(10 == 10)? a) True
b) False
c) None
d) Error
Answer: a) True

What is the purpose of the break statement in Python?


a) To terminate a loop
b) To skip an iteration in a loop
c) To exit a function
d) None of the above
Answer: a) To terminate a loop

What will be the output of print("hello".upper())?


a) hello
b) HELLO
c) Error
d) None
Answer: b) HELLO

How do you define a dictionary in Python?


a) [key: value]
b) {key: value}
c) (key: value)
d) <key: value>
Answer: b) {key: value}

What is the method used to remove an item from a list in Python?


a) remove()
b) pop()
c) delete()
d) clear()
Answer: b) pop()
How can you create an empty dictionary in Python?
a) {}
b) []
c) ()
d) None of the above
Answer: a) {}

What is the output of print("apple" > "banana")?


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

What will be the output of print(2 + 3 * 4)?


a) 14
b) 20
c) 26
d) 11
Answer: a) 14

What does max() do in Python?


a) Returns the largest value in a list
b) Returns the smallest value in a list
c) Adds all values in a list
d) None of the above
Answer: a) Returns the largest value in a list

What is the correct syntax to write a single-line comment in Python?


a) // comment
b) # comment
c) /* comment */
d) -- comment
Answer: b) # comment

What is the output of print("abc" * 0)?


a) ""
b) abc
c) None
d) Error
Answer: a) ""

How do you check if an element exists in a list?


a) exists() in list
b) list.contains()
c) in
d) list.includes()
Answer: c) in

Which of the following is the correct way to open a file in Python for reading?
a) open("file.txt", "r")
b) open("file.txt", "w")
c) open("file.txt", "rb")
d) open("file.txt", "rw")
Answer: a) open("file.txt", "r")

How do you handle multiple exceptions in a Python try block?


a) except (ExceptionType1, ExceptionType2):
b) except ExceptionType1, ExceptionType2:
c) except ExceptionType:
d) except multiple:
Answer: a) except (ExceptionType1, ExceptionType2):

What will be the result of print(3 < 2 and 1 < 2)?


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

Which of the following methods is used to add an element to a set?


a) add()
b) insert()
c) append()
d) push()
Answer: a) add()

Which operator is used to check if two values are not equal in Python?
a) !=
b) <>
c) =!
d) None of the above
Answer: a) !=

What will be the result of print("123" + "456")?


a) 579
b) 123456
c) Error
d) None of the above
Answer: b) 123456

What is the purpose of the continue statement in Python?


a) To stop the loop
b) To skip the current iteration and continue to the next
c) To break the loop
d) None of the above
Answer: b) To skip the current iteration and continue to the next

Which of the following is the correct way to define a class in Python?


a) class ClassName:
b) define ClassName:
c) class = ClassName
d) def ClassName:
Answer: a) class ClassName:

How do you convert a string to an integer in Python?


a) int("123")
b) str("123")
c) toint("123")
d) None of the above
Answer: a) int("123")

Which method is used to remove all elements from a list in Python?


a) clear()
b) remove()
c) delete()
d) reset()
Answer: a) clear()

What is the output of print(2 == "2")?


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

How do you create a copy of a list in Python?


a) list.copy()
b) list()
c) list[:]
d) copy.list()
Answer: c) list[:]

Which Python function is used to find the length of a string?


a) length()
b) size()
c) len()
d) count()
Answer: c) len()

What is the output of print(10 / 3)?


a) 3.33
b) 3
c) 3.0
d) 3.3333333333333335
Answer: d) 3.3333333333333335

What is the correct way to import a module in Python?


a) import module_name
b) include module_name
c) require module_name
d) None of the above
Answer: a) import module_name

What does zip() do in Python?


a) Zips files
b) Creates an iterator that aggregates elements from two or more iterables
c) Compresses data
d) None of the above
Answer: b) Creates an iterator that aggregates elements from two or more iterables

How do you start a Python interpreter in interactive mode?


a) python -i
b) python
c) python shell
d) python --interactive
Answer: b) python

What will be the output of print(bool(""))?


a) True
b) False
c) None
d) Error
Answer: b) False
## [R]

1. What is the function to create a vector in R?


a) vector()
b) c()
c) array()
d) list()
Answer: b) c()

2. Which function is used to calculate the mean of a numeric vector in R?


a) mean()
b) sum()
c) average()
d) calc()
Answer: a) mean()

3. How do you generate a sequence of numbers from 1 to 10 in R?


a) seq(1, 10)
b) 1:10
c) c(1, 10)
d) sequence(1, 10)
Answer: b) 1:10

4. What function would you use to read data from a CSV file in R?
a) load()
b) read.csv()
c) import()
d) read.table()
Answer: b) read.csv()

5. What is the function to display the structure of an R object?


a) head()
b) summary()
c) str()
d) print()
Answer: c) str()

6. Which function is used to install a package in R?


a) install()
b) library()
c) install.packages()
d) load.packages()
Answer: c) install.packages()

7. Which of the following is not a data type in R?


a) Numeric
b) Integer
c) Character
d) String
Answer: d) String

8. How do you access a specific column in a data frame df?


a) df[1]
b) df[ ,1]
c) df[1,]
d) df[1][,]
Answer: b) df[ ,1]
9. What will the following R command do: x <- c(1, 2, 3, 4)?
a) Creates a list x with values 1, 2, 3, 4.
b) Creates a vector x with values 1, 2, 3, 4.
c) Creates a dataframe x with values 1, 2, 3, 4.
d) None of the above.
Answer: b) Creates a vector x with values 1, 2, 3, 4.

10. How do you remove an object in R?


a) remove()
b) del()
c) rm()
d) delete()
Answer: c) rm()

11. Which function is used to find the maximum value in a numeric vector?
a) max()
b) min()
c) largest()
d) biggest()
Answer: a) max()

12. What does the summary() function do in R?


a) Returns the mean of a numeric vector.
b) Displays descriptive statistics for an object.
c) Prints the structure of an object.
d) Prints the first few elements of an object.
Answer: b) Displays descriptive statistics for an object.

13. Which operator is used for element-wise multiplication in R?


a) *
b) %%
c) %*%
d) ^
Answer: a) *

14. What is the result of 2 == 2 in R?


a) TRUE
b) FALSE
c) Error
d) NaN
Answer: a) TRUE

15. Which function in R is used to return the first few rows of a data frame?
a) head()
b) tail()
c) first()
d) top()
Answer: a) head()

16. How do you combine multiple vectors into a list in R?


a) combine()
b) list()
c) c()
d) bind()
Answer: b) list()

17. Which R function returns the number of rows in a data frame?


a) nrows()
b) rowCount()
c) length()
d) nrow()
Answer: d) nrow()

18. What does the ggplot() function in R do?


a) Creates a histogram.
b) Creates a scatter plot.
c) Starts the creation of a graph.
d) All of the above.
Answer: c) Starts the creation of a graph.

19. What will the command factor(c("a", "b", "a")) return in R?


a) A vector of character values.
b) A factor with levels "a" and "b".
c) An integer vector.
d) A matrix with two levels.
Answer: b) A factor with levels "a" and "b".

20. How do you get the unique elements of a vector in R?


a) unique()
b) distinct()
c) select()
d) remove()
Answer: a) unique()

21. Which function in R can be used to join two data frames by a common column?
a) merge()
b) combine()
c) concat()
d) join()
Answer: a) merge()

22. How do you add a new column to a data frame df?


a) df$new_column <- c(1, 2, 3)
b) add.column(df, c(1, 2, 3))
c) df <- cbind(df, c(1, 2, 3))
d) Both a) and c)
Answer: d) Both a) and c)

23. Which function is used to split a string into substrings in R?


a) split()
b) substring()
c) strsplit()
d) str_split()
Answer: c) strsplit()

24. How do you generate random numbers from a normal distribution in R?


a) rnorm()
b) runif()
c) rand()
d) norm()
Answer: a) rnorm()

25. What does the function table() do in R?


a) Creates a table of summary statistics.
b) Displays data in tabular format.
c) Counts the frequency of unique elements in a vector.
d) Combines multiple vectors into a table.
Answer: c) Counts the frequency of unique elements in a vector.
26. Which of the following is an incorrect way to create a vector of numbers from 1
to 5 in R?
a) c(1, 2, 3, 4, 5)
b) 1:5
c) seq(1, 5)
d) seq(1, 5, by=2)
Answer: d) seq(1, 5, by=2) (Generates 1 3 5, not 1 2 3 4 5)

27. What function in R would you use to perform a linear regression?


a) lm()
b) linear()
c) regression()
d) fit()
Answer: a) lm()

28. What is the output of sum(c(1, 2, NA)) in R?


a) 3
b) NA
c) 1
d) 2
Answer: b) NA

29. Which of the following is used to display the last few elements of a vector in
R?
a) head()
b) tail()
c) end()
d) last()
Answer: b) tail()

30. Which operator is used to access elements of a list in R?


a) []
b) ()
c) $
d) :
Answer: c) $

31. How can you get the number of columns in a data frame?
a) ncol()
b) length()
c) count()
d) colCount()
Answer: a) ncol()

32. What does the factor() function do in R?


a) Converts an object to a factor.
b) Adds a factor to a vector.
c) Combines multiple factors into one.
d) Splits a factor into multiple levels.
Answer: a) Converts an object to a factor.

33. Which function is used to create a matrix in R?


a) matrix()
b) array()
c) table()
d) mat()
Answer: a) matrix()
34. What does the subset() function do in R?
a) Filters a data frame based on a condition.
b) Creates a submatrix.
c) Selects a part of a vector.
d) All of the above.
Answer: a) Filters a data frame based on a condition.

35. What is the default method for handling missing values in R?


a) na.omit()
b) na.exclude()
c) NA
d) R ignores missing values by default.
Answer: a) na.omit()

36. What does the apply() function do in R?


a) Applies a function over the rows or columns of a matrix or data frame.
b) Adds a function to a vector.
c) Combines multiple functions into one.
d) Applies a function over all elements in a vector.
Answer: a) Applies a function over the rows or columns of a matrix or data frame.

37. How do you check the structure of an R object?


a) summary()
b) str()
c) head()
d) check()
Answer: b) str()

38. Which function is used to calculate the standard deviation in R?


a) sd()
b) std()
c) deviation()
d) variance()
Answer: a) sd()

39. What does the corr() function do in R?


a) Computes the correlation between two variables.
b) Plots a correlation matrix.
c) Creates a correlation matrix for multiple variables.
d) Both a) and c).
Answer: d) Both a) and c).

40. What function is used to sort a vector in R?


a) sort()
b) order()
c) rank()
d) sequence()
Answer: a) sort()

41. Which function is used to find the dimensions of a matrix in R?


a) dim()
b) dimensions()
c) shape()
d) size()
Answer: a) dim()

42. How do you create a boxplot in R?


a) boxplot()
b) plot()
c) hist()
d) barplot()
Answer: a) boxplot()

43. What does the lm() function in R do?


a) Fits a linear model.
b) Computes the least squares.
c) Creates a linear regression.
d) All of the above.
Answer: d) All of the above.

44. Which of the following will return TRUE in R?


a) 1 == 2
b) 3 > 2
c) 4 < 3
d) 5 != 5
Answer: b) 3 > 2

45. What is the purpose of the subset() function in R?


a) To select a part of a vector.
b) To filter a data frame.
c) To select a part of a matrix.
d) All of the above.
Answer: b) To filter a data frame.

46. Which function returns the summary of a data frame in R?


a) summary()
b) stats()
c) info()
d) desc()
Answer: a) summary()

47. Which of the following will return the last 6 rows of a data frame?
a) head(df)
b) tail(df)
c) last(df)
d) end(df)
Answer: b) tail(df)

48. What is the function for calculating the quantiles of a numeric vector in R?
a) quantile()
b) percentile()
c) quant()
d) range()
Answer: a) quantile()

49. How can you install the 'ggplot2' package in R?


a) install.package('ggplot2')
b) install('ggplot2')
c) install.packages('ggplot2')
d) load('ggplot2')
Answer: c) install.packages('ggplot2')

50. Which of the following is used to perform a linear model fitting in R?


a) fit()
b) regression()
c) lm()
d) linear()
Answer: c) lm()

You might also like