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

Final Exam - Part 1 (MCQ)

The document provides 25 multiple choice questions about functions from a sample exam for a problem solving and programming course. The questions cover topics like defining and calling functions, function parameters, return values, variable scope, and default arguments.

Uploaded by

Dd d
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)
54 views

Final Exam - Part 1 (MCQ)

The document provides 25 multiple choice questions about functions from a sample exam for a problem solving and programming course. The questions cover topics like defining and calling functions, function parameters, return values, variable scope, and default arguments.

Uploaded by

Dd d
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/ 37

Sample Exam Questions

Final Exam – Part 1 (MCQ)


CPIT 110 (Problem-Solving and Programming)

Version 1.2
‫تنبيه!‬
‫• ه ذ األسئلذذعباألةب ذ عيألةذذقألةينذذاألري ذ ألتئلذذاألختبيذذاأل لذذعباألس ب ذ عألس نه ذ أل‪-‬‬
‫س جزءألس ث ن أل(س ي عست)أل ميرعألس برمجاألوحلألس مشكالتأل(‪.)CPIT-110‬‬
‫• ه األسئلعباأل أل ُبع مدألةبيه أل بم س كري‪.‬‬
‫• قدأل ألتشملأله األسئلعباألجميعألس مئسليعألس ميرعيأل ال ب ع‪.‬‬
‫• ه األسئلعباألمن لباأل بمرسجعاألبعدألس ن هذ ءألمذقألمذ س كريألوتقبيذ ألس مئسلذيعأل‬
‫س ميرعيأل ال ب ع‪.‬‬
‫• حبئلألسئلعباألمررياألنه باألصفح تأله سألس مبف‪.‬‬

‫‪2‬‬
‫تنبيه!‬
‫ب عألش ملألجميعألم ألتمألدعسل هألل بي ًألمقأل‪ Chapter 1‬إلىأل‪.Chapter 6‬‬ ‫• س‬
‫ألريذ ألونرجذذئألمرسجعذذاألجميذذعألنمذ ه أل‬ ‫• هذ سألس نمذذئه ألبذذهأل لذذعباألس بذ قألس وذ د‬
‫س ب عستألس و بياأل ألبئسقألس و بيا‪.‬أل‬

‫‪3‬‬
Chapter 1, 2, 3:
Sample Exam Questions
Mid-Term Exam 1 – Part 1 (MCQ)

Chapter 4, 5:
Sample Exam Questions
Mid-Term Exam 2 – Part 1 (MCQ)
https://csu.kau.edu.sa/Pages-cpit110-test-questions.aspx

4
Chapter 6: Functions
Questions

5
Question
#1
If a function does not return a value, by default, it returns
___________.
a) None
b) Integer
c) Float
d) String

Section 6.2 Defining a Function 6


Question
#2
The header of a function consists of ____________.
a) function name
b) function name and parameter list
c) parameter list
d) return statement

Section 6.2 Defining a Function 7


Question
#3
A function _________.
a) must have at least one parameter
b) may have no parameters
c) must always have a return statement to return
a value
d) must always have a return statement to return
multiple values

Section 6.2 Defining a Function 8


Question
#4
Arguments to functions always appear within __________.
a) brackets
b) parentheses
c) curly braces
d) quotation marks

Section 6.3 Calling a Function 9


Question
#5
Does the function call in the following function cause syntax
errors?
import math
def main():
math.sin(math.pi)

main()
a) Yes
b) No

Section 6.3 Calling a Function 10


Question
#6
Each time a function is invoked, the system stores parameters
and local variables in an area of memory, known as _______,
which stores elements in last-in first-out fashion.
a) a heap
b) storage area
c) a stack
d) an array

Section 6.3 Calling a Function 11


Question
#7
Which of the following should be defined as a None function?
a) Write a function that prints integers from 1
to 100.
b) Write a function that returns a random
integer from 1 to 100.
c) Write a function that checks whether a number
is from 1 to 100.
d) Write a function that converts an uppercase
letter to lowercase.

Section 6.4 Functions With/Without Return Values 12


Question
#8
A function with no return statement returns ______.
a) void
b) 1
c) 0
d) None

Section 6.4 Functions With/Without Return Values 13


Question
#9
Consider the following incomplete code:
def f(number):
# Missing function body
print(f(5))
The missing function body should be ________.
a) return "number"
b) print(number)
c) print("number")
d) return number

Section 6.4 Functions With/Without Return Values 14


Question
#10
Given the following function header:
def f(p1, p2, p3, p4)
Which of the following is correct to invoke it?
a) f(1, 2, 3, 4)
b) f(p1 = 1, 2, 3, 4)
c) f(p1 = 1, p2 = 2, p3 = 3, 4)
d) f(p1 = 1, p2 = 2, p3 = 3, p2 = 4)

Section 6.5 Positional and Keyword Arguments 15


Question
#11
Given the following function header:
def f(p1, p2, p3, p4)
Which of the following is correct to invoke it?
a) f(1, 2, 3)
b) f(p1 = 1, 2, 3, 4)
c) f(p1 = 1, p2 = 2, p3 = 3, 4)
d) f(p1 = 1, p2 = 2, p3 = 3, p4 = 4)

Section 6.5 Positional and Keyword Arguments 16


Question
#12
Given the following function header:
def f(p1, p2, p3, p4)
Which of the following is correct to invoke it?
a) f()
b) f(p1 = 1, 2, 3, 4)
c) f(p1 = 1, p2 = 2, p3 = 3, 4)
d) f(1, 2, 3, p4 = 4)

Section 6.5 Positional and Keyword Arguments 17


Question
#13
Given the following function:
def nPrint(message, n):
while n > 0:
print(message, end="")
n -= 1
What will be displayed by the call nPrint('a', 4)?
a) aaaaa
b) aaaa
c) aaa
d) invalid call
e) infinite loop

Section 6.5 Positional and Keyword Arguments 18


Question
#14
Given the following function:
def nPrint(message, n):
while n > 0:
print(message)
n -= 1
What will be displayed by the call nPrint('a', 4)?
a) aaaaa
b) aaaa
c) aaa
d) invalid call
e) infinite loop

Section 6.5 Positional and Keyword Arguments 19


Question
#15
Given the following function:
def nPrint(message, n):
while n > 0:
print(message)
n -= 1
What is k after invoking nPrint("A message", k)?
k = 2
nPrint("A message", k)
a) 0
b) 1
c) 2
d) 3
Section 6.5 Positional and Keyword Arguments 20
Question
#16
Given the following function:
def nPrint(message, n):
while n > 0:
print(message)
n -= 1
What is k after invoking nPrint("A message", k)?
k = 2
nPrint(n = k, message = "A message")
a) 0
b) 1
c) 2
d) 3
Section 6.5 Positional and Keyword Arguments 21
Question
#17
A variable defined inside a function is referred to as __________.
a) a global variable
b) a function variable
c) a block variable
d) a local variable

Section 6.9 The Scope of Variables 22


Question
#18
A variable defined outside a function is referred to as
__________.
a) a global variable
b) a function variable
c) a block variable
d) a local variable

Section 6.9 The Scope of Variables 23


Question
#19
Whenever possible, you should avoid using __________.
a) global variables
b) function parameters
c) global constants
d) local variables

Section 6.9 The Scope of Variables 24


Question
#20
What will be displayed by the following code?
x = 1
def f1():
y = x + 2
print(y, end=" ")
f1()
print(x)
a) 1 3
b) 3 1
c) The program has a runtime error because x is not
defined.
d) 1 1
e) 3 3

Section 6.9 The Scope of Variables 25


Question
#21
What will be displayed by the following code?
x = 1
def f1():
x = 3
print(x, end=" ")
f1()
print(x)
a) 1 3
b) 3 1
c) The program has a runtime error because x is not
defined.
d) 1 1
e) 3 3

Section 6.9 The Scope of Variables 26


Question
#22
What will be displayed by the following code?
x = 1
def f1():
x = x + 2
print(x, end=" ")
f1()
print(x)
a) 1 3
b) 3 1
c) The program has a runtime error because x is not
defined.
d) 1 1
e) 3 3

Section 6.9 The Scope of Variables 27


Question
#23
What will be displayed by the following code?
x = 1
def f1():
global x
x = x + 2
print(x, end=" ")
f1()
print(x)
a) 1 3
b) 3 1
c) The program has a runtime error because x is not
defined.
d) 1 1
e) 3 3

Section 6.9 The Scope of Variables 28


Question
#24
What will be displayed by the following code?
def f1(x = 1, y = 2):
x = x + y
y += 1
print(x, y)
f1()
a) 1 3
b) 3 1
c) The program has a runtime error because x and y
are not defined.
d) 1 1
e) 3 3

Section 6.10 Default Arguments 29


Question
#25
What will be displayed by the following code?
def f1(x = 1, y = 2):
x = x + y
y += 1
print(x, y)
f1(2, 1)
a) 1 3
b) 2 3
c) The program has a runtime error because x and y
are not defined.
d) 3 2
e) 3 3

Section 6.10 Default Arguments 30


Question
#26
What will be displayed by the following code?
def f1(x = 1, y = 2):
x = x + y
y += 1
print(x, y)
f1(y = 2, x = 1)
a) 1 3
b) 2 3
c) The program has a runtime error because x and y
are not defined.
d) 3 2
e) 3 3

Section 6.10 Default Arguments 31


Question
#27
Which of the following function headers is correct?
a) def f(a = 1, b):
b) def f(a = 1, b, c = 2):
c) def f(a = 1, b = 1, c = 2):
d) def f(a = 1, b = 1, c = 2, d):

Section 6.10 Default Arguments 32


Question
#28
What will be displayed by the following code?
def hello():
print("Hello")
def hello(name = "Ahmad"):
print("Hi", name)
hello()
a) Hello
b) Hi
c) Hi Ahmad
d) Hello Ahmad

Section 6.10 Default Arguments 33


Question
#29
What will be displayed by the following code?
def hello():
print("Hello")
def hello(name = "Ahmad", age):
print("Name:", name, ", Age:", age)
hello(24)
a) Name: Ahmad, Age: 24
b) Name: None, Age: 24
c) Name: , Age: 24
d) Syntax Error

Section 6.10 Default Arguments 34


Question
#30
What will be displayed by the following code?
def f1(x = 1, y = 2):
return x + y, x - y
x, y = f1(y = 2, x = 1)
print(x, y)
a) 1 3
b) 3 1
c) The program has a runtime error because the
function returns the multiple values
d) 3 -1
e) -1 3
Section 6.11 Returning Multiple Values 35
Solutions
Question # Correct Answer Question # Correct Answer
1 A 11 D
2 B 12 D
3 B 13 B
4 B 14 E
5 B 15 C
6 C 16 C
7 A 17 D
8 D 18 A
9 D 19 A
10 A 20 B

36
Solutions
Question # Correct Answer
21 B
22 C
23 E
24 E
25 D
26 E
27 C
28 C
29 D
30 D

37

You might also like