Docsity

CSCI 1301 Practice (Python) Exam, Exams of Computer Science

Toggle sidebar
bg1
1 / 43
CSCI 1301 Practice (Python) Exam
1.
Which of the following is the correct extension of the Python file?:
.py
2. Does python code need to be compiled or interpreted?: python code is both compiled
and
interpreted
3.
A
syntax error is a
.:
The program contains invalid
code that cannot be understood. It is found before the program is ever run by the interpreter.
4.
Which of the following is used to define a block of code in Python language?-
:
Indentation
5.
Which of the following functions is a built-in function in python?:
print( )
6.
Which of the following lines is a properly formatted comment? # This
is a comment / " " " This is a comment " " " / " " "
This is
a comment
" "
": All of
them
7.
Which of the following is a legal Python identifier?:
_oneForAll
8. Which of the following is an example of an invalid assignment or declaration
statement?: x + 1 = 3
9. Assume x=80; y=30; z=4
What is the result of x
//
y
//
z?: 0
10. Assume a=80.0; b=30.0; c=4.0
What is the result of a / b / c?:
0.67
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download CSCI 1301 Practice (Python) Exam and more Exams Computer Science in PDF only on Docsity!

CSCI 1301 Practice (Python) Exam

1. Which of the following is the correct extension of the Python file?: .py

2. Does python code need to be compiled or interpreted?: python code is both compiled and

interpreted

3. A syntax error is a .: The program contains invalid

code that cannot be understood. It is found before the program is ever run by the interpreter.

4. Which of the following is used to define a block of code in Python language?-

: Indentation

5. Which of the following functions is a built-in function in python?: print( )

6. Which of the following lines is a properly formatted comment? # This

is a comment / " " " This is a comment " " " / " " " This is a comment " " ": All of them

7. Which of the following is a legal Python identifier?: _oneForAll

8. Which of the following is an example of an invalid assignment or declaration

statement?: x + 1 = 3

9. Assume x=80; y=30; z=

What is the result of x // y // z?: 0

10. Assume a=80.0; b=30.0; c=4.

What is the result of a / b / c?: 0.

11. What value is contained in the integer variable size after the following

statements are executed? size = 18 size = size + 12 size = size * 2 size = size / 4: 15

12. For the following code, the program will print the word "Here" and then

print: print("Here ", end="") print("There "

  • "Everywhere") print("But not " + "in Texas": "There Everywhere" on the same line as "Here"

13. Which statement reads value as integer type?: x =int( input( 'Enter an integer' ) )

14. To find distance between point (x1, y1) and point (x2, y2), which expression is

correct?: distance = math.sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) )

15. Assume

age = 10 pet = "dog" pet_name = "Gerald" To generate the output "Gerald is 10 years old dog", which print statement is NOT correct?: print( f"pet_name, ' is ', age, ' years old ', pet " )

16. Which of the following is not a core data type in Python programming?: Clsss

17. What will be the output of the following Python code?

str = 'abcd'

['a', 'c', 2] ['b', 1, 3] ['b', 'c', 1]

23. What is the content of mylist in the following Python's code fragment?

mylist = [-12, -43, 55, 99, -32, 12, -2] mylist.insert(2, 67): [-12, -43, 67, 55, 99, -32, 12, - 2]

24. What output of result in following Python program?

list1 = [1,2,3,4] list2 = [2,4,5,6] result1 = list1 + list result2 = list1 * 2 print( result1 ) print( result2 ): 1 2 3 4 2 4 5 6 1 2 3 4 1 2 3 4

25. What is the output of the following python code fragment?: list

= [1, 2, 5, 7]

list2 = [4, 2, 5, 3] print([item for item in list1 if item not in list2]): [1 ,7]

26. Given the following tuple:

my_tuple = (5, 10, 15, 25, 30) Suppose you want to update the value of this tuple at 3rd index to 20. Which of the following option will you choose?: The above actions are invalid for updating tuple (Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.)

27. Want to add 20 at 3rd place in my_tuple

my_tuple = (5, 10, 15, 25, 30) And update the tuple as (5, 10, 15, 20, 25, 30) Which of the following code can help to realize the purpose?: my_tuple = (5, 10, 15, 25, 30) mylist = list(my_tuple) mylist.insert(3, 20) my_tuple = tuple(mylist)

28. Which of the following is NOT a valid way to define Python's dictionary?: dic

= {('Albert': 23), ('Mary': 23), ('Simon': 43)}

29. Suppose dictionary

d1 = {"Susan":42, "John":40, "Peter":45} to delete the entry for "john" what command do we use?: del d1["John"]

30. Suppose dictionary

d1 = {"Susan":42, "John":40, "Peter":45} To get Susan's age, what command do we use?: d1.get("Susan")

31. What is the output of print str[2:5] if str = 'Python Programming'?: tho

32. What is the output of print str[2: ] if str = 'Python Programming'?: thon

Programming

33. What is the output of print str[ : : 3 ] if str = 'Python Programming' ?: Ph oai

34. What will be the output of the following Python code?: ABCD

student= ( 'Jack', 19, 3.6) result= ", ".join( student ) print( student ): Error

47. What will be the output of the following Python code?

student= ( 'Jack', 19, 3.6) result= ", ".join( str(d) for d in student ) print( student ): Jack, 19, 3.

48. What are the outputs of print the following statements?

'xyyabcdxyy'.strip('xyy') 'xyyabcdxyy'.lstrip('xyy') 'xyyabcdxyy'.rstrip('xyy'): abcd abcdxyy xyyabcd

49. Assume str = "abcdefg", to check whether string str contains "cd", use

: "cd" in str

50. What are the outputs of the following Python script? str

= "abcd1234" print( str.isalnum( ) ) print( str.isalpha( ) ) print( str.isdigit( ) ) print( str.islower( ) ) print( str.capitalize( ) ): True False False True Abcd

51. Every if statement requires an associated else statement, but not every else

statement requires an associated if statement.: False

52. Which of the following best describes this code snippet?: If the variable count is not equal

to 400, "Hello World" will be printed.

53. Which of the following expressions best represents the condition "if the grade

is between 75 and 100"?: if 75 < grade and grade < 100 :

54. In Python, a block statement is: a set of statements that are all indented to the same column.

55. In Python, the symbol "=" and the symbol "==" are used synonymously

(interchangeably).: False

56. Assume done = False; x = 10; y = 12

The expression not done or x > y is true.: True

57. Assume y = 11; str = "Goodbye"

The expression len( str ) < y is true: True

58. The

statement if a >= b : a = a+ 1 else : b = b - 1 will do the same thing as the statement if a < b : b = b- else :

else : grade = 'F': This code will work correctly only if grade < 70

61. Assuming num1 = 1; num2 = 2 what output is produced by the following code

fragment given the assumptions below? if num1 < num2 : print( 'red', end=' ') if (num1 +5) < num2 : print( 'white', end=' ' )else : print( 'blue', end=' ') print( 'yellow'): red blue yellow

62. What output is produced by the following code fragment?

limit =100; num1 = 15; num2 = 40 if limit <= 100 : if ( num1 == num2) : print( "lemon") print("orange") print( "grape"): orange grape

63. What is wrong, logically, with the following code? if

x > 10: print("Large") elif x > 6 and x <= 10: print("Medium") elif x > 3 and x <= 6:

print("Small") else : print("Very small"): There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional

64. For the following code, how to rewrite it by short hand if...else statement? if x >

y: print( x + 1 ) else : print( y - 1 ): print(x + 1) if x > y else print(y-1)

65. For the following code, how to rewrite it by short hand if...else statement? if x >

y: print( x + 1 ) else : print( y - 1 ): print(x + 1) if x > y else print(y-1)

66. Which expression checks whether the list my_list contains the value 15?: 15 in my_list

67. Which of the following for loop will cause the body of the loop to be

executed 100 times?: for x in range (0, 100)

68. What output is produced by the following code fragment? x

for i in range(1, 5): x += i print(x): 10

69. Given that str is a String, what does the following loop do? str

= 'abcdef' for x in str[ : -1 ] : print( x, end=' '): it prints str out forwards:

x *= 2 if x == 64: break print(x, end=' '): 2 4 8 16 32

75. What output is produced by the following code fragment? x

while x < 100: x *= 2 if x == 64: continue print(x, end=' '): 2 4 8 16 32 128

76. What output is produced by the following code fragment? x

while x < 100: if x == 64: continue x *= 2 print(x, end=' ': Error. Go to infinite loop

77. What output is produced by the following code fragment? x

while x < 100: x *= 2 print(x, end=' ') else: print(x): 2 4 8 16 32 64 128 128

78. What output is produced by the following code fragment? x

while x < 100: x *= 2 if x == 64: break print(x, end=' ') else: print(x): 2 4 8 16 32

79. What output is produced by the following code fragment?

str = "abcdef"

i += 1

83. Which keyword is used for function?: def

84. Which are the advantages of functions in python?: All of above

85. What are the two main types of functions in Python?: Built-in function & User defined function

86. Where is function defined in Python?: All of the mentioned (Module, Class, Another function)

87. The function must return a value: False

88. Which correctly defines two parameters x and y for a function definition: def

calc_val( ):: calc_val (x, y) :

89. Given a function definition:def calc_val(a, b, c):and given variables i, j, and k,

which are valid arguments in the call calc_val( )?: calc_val( k, i+ j, 99 )

90. A local variable is defined inside a function, while a global variable is defined

outside any function.: True

91. A function definition must be evaluated by the interpreter before the func-tion

can be called.: True

92. What will be the output of the following Python code?

def printMax(a, b):if a > b:print(a, 'is maximum')elif a == b:print(a, 'is equal to', b)else:print(b, 'is maximum') x = 3 y = 4 printMax( x+1, y): 4 is equal to 4

93. What will be the output of the following Python code? x

def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is now', x): x is 50 Changed local x to 2 x is now 50

94. What will be the output of the following Python code? x

def func( ): global x print('x is', x) x = 2 print('Changed global x to', x) func( ) print('x is now', x): x is 50 Changed global x to 2 Value of x is 2

95. What will be the output of the following Python code?

def say(message, times = 1):

def change(x = 0, y = 1): x = x + y y = y + 1 print(x, y) change(x = 0 , y = 1) change(x = 1 , y = 0) change(y = 1 , x = 0) change(y = 0 , x = 1): 1 2 1 1 1 2 1 1

99. What will be the output of the following Python code?

def power(x, y=2): result = 1 for i in range(y): result = result * x return result print( power(3) ) print( power(3, 3) ) print( power(4) )

print( power(4, 3) ): 9 27 16 64

100. What will be the output of the following Python code?

def foo( *args ): result = 0 for i in args: result += i return result print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) ): 6 15

101. What will be the output of the following Python code?