0% found this document useful (0 votes)
14 views35 pages

Chapter 6 - MCQ

The document contains sample multiple-choice questions for Chapter 6 of the CPIT 110 course on Problem-Solving and Programming, focusing on functions. It includes various questions related to function definitions, parameters, return values, and variable scope, with solutions provided at the end. The questions serve as a study aid but do not encompass all topics for the exam.

Uploaded by

saletemu43
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)
14 views35 pages

Chapter 6 - MCQ

The document contains sample multiple-choice questions for Chapter 6 of the CPIT 110 course on Problem-Solving and Programming, focusing on functions. It includes various questions related to function definitions, parameters, return values, and variable scope, with solutions provided at the end. The questions serve as a study aid but do not encompass all topics for the exam.

Uploaded by

saletemu43
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/ 35

Sample Exam Questions

Chapter 6 (MCQ)
CPIT 110 (Problem -Solving and Programming)

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

‫‪2‬‬
Chapter 6: Functions
Questions

3
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 4


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 5


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 6


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

Section 6.3 Calling a Function 7


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 8


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 9


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 10


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 11


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 12


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 13


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 14


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 15


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 16


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 17


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 18
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 19
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 20


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 21


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 22


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 23


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 24


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 25


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 26


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 27


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 28


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 29


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 30


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 31


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 32


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 33
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

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

35

You might also like